StartModelTests.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections;
  3. using System.Net;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using NUnit.Framework;
  7. using Unity.Cloud.Collaborate.Models.Enums;
  8. using Unity.Cloud.Collaborate.Models.Providers;
  9. using UnityEngine.TestTools;
  10. namespace Unity.Cloud.Collaborate.Tests.Models
  11. {
  12. internal class StartModelTests
  13. {
  14. TestCollab m_Provider;
  15. AsyncToCoroutine m_Atc;
  16. [OneTimeSetUp]
  17. public void OneTimeSetup()
  18. {
  19. m_Atc = new AsyncToCoroutine();
  20. }
  21. [SetUp]
  22. public void Setup()
  23. {
  24. m_Provider = new TestCollab();
  25. }
  26. [TearDown]
  27. public void TearDown()
  28. {
  29. m_Provider = null;
  30. }
  31. [UnityTest]
  32. public IEnumerator TestWhenProjectBoundSavesAssets()
  33. {
  34. return m_Atc.Run(async () =>
  35. {
  36. var saveAssetsCallCount = 0;
  37. //ensure we return true for isProjectBound
  38. m_Provider.isProjectBoundTestImpl = () => ProjectStatus.Bound;
  39. m_Provider.saveAssetsTestImpl = () => saveAssetsCallCount++;
  40. saveAssetsCallCount.ShouldBe(0);
  41. await m_Provider.TestRequestTurnOnService();
  42. saveAssetsCallCount.ShouldBe(1, $"Expected {nameof(saveAssetsCallCount)} to be 1");
  43. });
  44. }
  45. [UnityTest]
  46. public IEnumerator TestWhenGenesisReturnsForbiddenShowsCredentialsError()
  47. {
  48. return m_Atc.Run(async () =>
  49. {
  50. var showCredentialsErrorCallCount = 0;
  51. var putAsyncCallCount = 0;
  52. //ensure we return true for isProjectBound
  53. m_Provider.isProjectBoundTestImpl = () => ProjectStatus.Bound;
  54. m_Provider.showCredentialsErrorTestImpl = () => showCredentialsErrorCallCount++;
  55. m_Provider.putAsyncTestImpl = () =>
  56. {
  57. putAsyncCallCount++;
  58. return Task.Run(() => new HttpResponseMessage(HttpStatusCode.Forbidden));
  59. };
  60. putAsyncCallCount.ShouldBe(0);
  61. showCredentialsErrorCallCount.ShouldBe(0);
  62. await m_Provider.TestRequestTurnOnService();
  63. putAsyncCallCount.ShouldBe(1, $"Expected {nameof(putAsyncCallCount)} to be 1");
  64. showCredentialsErrorCallCount.ShouldBe(1, $"Expected {nameof(showCredentialsErrorCallCount)} to be 1");
  65. });
  66. }
  67. [UnityTest]
  68. public IEnumerator TestsWhenGenesisReturnsErrorShowsGenericError()
  69. {
  70. return m_Atc.Run(async () =>
  71. {
  72. var showGeneralErrorCallCount = 0;
  73. var putAsyncCallCount = 0;
  74. //ensure we return true for isProjectBound
  75. m_Provider.isProjectBoundTestImpl = () => ProjectStatus.Bound;
  76. m_Provider.showGeneralErrorTestImpl = () =>
  77. {
  78. showGeneralErrorCallCount++;
  79. };
  80. m_Provider.putAsyncTestImpl = () =>
  81. {
  82. putAsyncCallCount++;
  83. return Task.Run(() => new HttpResponseMessage(HttpStatusCode.NotFound));
  84. };
  85. putAsyncCallCount.ShouldBe(0);
  86. showGeneralErrorCallCount.ShouldBe(0);
  87. await m_Provider.TestRequestTurnOnService();
  88. putAsyncCallCount.ShouldBe(1, $"Expected {nameof(putAsyncCallCount)} to be 1");
  89. showGeneralErrorCallCount.ShouldBe(1, $"Expected {nameof(showGeneralErrorCallCount)} to be 1");
  90. });
  91. }
  92. [UnityTest]
  93. public IEnumerator TestWhenGenesisReturnsOkCallsTurnOnCollabInternal()
  94. {
  95. return m_Atc.Run(async () =>
  96. {
  97. var putAsyncCallCount = 0;
  98. var turnOnCollabInternalCallCount = 0;
  99. // Ensure we return true for isProjectBound.
  100. m_Provider.isProjectBoundTestImpl = () => ProjectStatus.Bound;
  101. m_Provider.turnOnCollabInternalTestImpl = () => turnOnCollabInternalCallCount++;
  102. m_Provider.putAsyncTestImpl = () =>
  103. {
  104. putAsyncCallCount++;
  105. return Task.Run(() => new HttpResponseMessage(HttpStatusCode.OK));
  106. };
  107. putAsyncCallCount.ShouldBe(0);
  108. turnOnCollabInternalCallCount.ShouldBe(0);
  109. await m_Provider.TestRequestTurnOnService();
  110. putAsyncCallCount.ShouldBe(1, $"Expected {nameof(putAsyncCallCount)} to be 1");
  111. turnOnCollabInternalCallCount.ShouldBe(1, $"Expected {nameof(turnOnCollabInternalCallCount)} to be 1");
  112. });
  113. }
  114. class TestCollab : Collab
  115. {
  116. public Func<ProjectStatus> isProjectBoundTestImpl;
  117. public Action saveAssetsTestImpl;
  118. public Action turnOnCollabInternalTestImpl;
  119. public Func<Task<HttpResponseMessage>> putAsyncTestImpl;
  120. public Action showCredentialsErrorTestImpl;
  121. public Action showGeneralErrorTestImpl;
  122. public TestCollab()
  123. {
  124. isProjectBoundTestImpl = () => ProjectStatus.Unbound;
  125. saveAssetsTestImpl = () => { };
  126. turnOnCollabInternalTestImpl = () => { };
  127. showCredentialsErrorTestImpl = () => { };
  128. showGeneralErrorTestImpl = () => { };
  129. putAsyncTestImpl = () => Task.Run(() => new HttpResponseMessage());
  130. }
  131. public async Task TestRequestTurnOnService()
  132. {
  133. await RequestTurnOnServiceInternal();
  134. }
  135. protected override Task<HttpResponseMessage> PutAsync(HttpClient client, string fullUrl, StringContent content)
  136. {
  137. return putAsyncTestImpl?.Invoke();
  138. }
  139. protected override void TurnOnCollabInternal()
  140. {
  141. turnOnCollabInternalTestImpl?.Invoke();
  142. }
  143. protected override void SaveAssets()
  144. {
  145. saveAssetsTestImpl?.Invoke();
  146. }
  147. public override ProjectStatus GetProjectStatus()
  148. {
  149. return isProjectBoundTestImpl?.Invoke() ?? ProjectStatus.Unbound;
  150. }
  151. protected override void ShowCredentialsError()
  152. {
  153. showCredentialsErrorTestImpl?.Invoke();
  154. }
  155. protected override void ShowGeneralError()
  156. {
  157. showGeneralErrorTestImpl?.Invoke();
  158. }
  159. }
  160. }
  161. }