PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests/Clients/AuthorizationsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 362 lines | 301 code | 57 blank | 4 comment | 11 complexity | d68a1d27d8bfc252e5e2b3648579d0cc MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Threading.Tasks;
  5. using NSubstitute;
  6. using Octokit.Internal;
  7. using Xunit;
  8. namespace Octokit.Tests.Clients
  9. {
  10. /// <summary>
  11. /// Client tests mostly just need to make sure they call the IApiConnection with the correct
  12. /// relative Uri. No need to fake up the response. All *those* tests are in ApiConnectionTests.cs.
  13. /// </summary>
  14. public class AuthorizationsClientTests
  15. {
  16. public class TheCtor
  17. {
  18. [Fact]
  19. public void EnsuresNonNullArguments()
  20. {
  21. Assert.Throws<ArgumentNullException>(() => new AuthorizationsClient(null));
  22. }
  23. }
  24. public class TheGetAllMethod
  25. {
  26. [Fact]
  27. public void RequestsCorrectUrl()
  28. {
  29. var client = Substitute.For<IApiConnection>();
  30. var authEndpoint = new AuthorizationsClient(client);
  31. authEndpoint.GetAll();
  32. client.Received().GetAll<Authorization>(
  33. Arg.Is<Uri>(u => u.ToString() == "authorizations"),
  34. Args.ApiOptions);
  35. }
  36. [Fact]
  37. public void RequestsCorrectUrlWithApiOptions()
  38. {
  39. var client = Substitute.For<IApiConnection>();
  40. var authEndpoint = new AuthorizationsClient(client);
  41. var options = new ApiOptions
  42. {
  43. StartPage = 1,
  44. PageSize = 1,
  45. PageCount = 1
  46. };
  47. authEndpoint.GetAll(options);
  48. client.Received().GetAll<Authorization>(
  49. Arg.Is<Uri>(u => u.ToString() == "authorizations"),
  50. options);
  51. }
  52. [Fact]
  53. public async Task EnsuresArgumentsNotNull()
  54. {
  55. var client = Substitute.For<IApiConnection>();
  56. var authEndpoint = new AuthorizationsClient(client);
  57. await Assert.ThrowsAsync<ArgumentNullException>(() => authEndpoint.GetAll(null));
  58. }
  59. }
  60. public class TheGetMethod
  61. {
  62. [Fact]
  63. public void GetsAnAuthorization()
  64. {
  65. var client = Substitute.For<IApiConnection>();
  66. var authEndpoint = new AuthorizationsClient(client);
  67. authEndpoint.Get(1);
  68. client.Received().Get<Authorization>(
  69. Arg.Is<Uri>(u => u.ToString() == "authorizations/1"),
  70. null);
  71. }
  72. }
  73. public class TheUpdateMethod
  74. {
  75. [Fact]
  76. public void SendsUpdateToCorrectUrl()
  77. {
  78. var client = Substitute.For<IApiConnection>();
  79. var authEndpoint = new AuthorizationsClient(client);
  80. authEndpoint.Update(1, new AuthorizationUpdate());
  81. client.Received().Patch<Authorization>(Arg.Is<Uri>(u => u.ToString() == "authorizations/1"),
  82. Args.AuthorizationUpdate);
  83. }
  84. }
  85. public class TheDeleteMethod
  86. {
  87. [Fact]
  88. public void DeletesCorrectUrl()
  89. {
  90. var client = Substitute.For<IApiConnection>();
  91. var authEndpoint = new AuthorizationsClient(client);
  92. authEndpoint.Delete(1);
  93. client.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "authorizations/1"));
  94. }
  95. }
  96. public class TheGetOrCreateApplicationAuthenticationMethod
  97. {
  98. [Fact]
  99. public void GetsOrCreatesAuthenticationAtCorrectUrl()
  100. {
  101. var data = new NewAuthorization();
  102. var client = Substitute.For<IApiConnection>();
  103. var authEndpoint = new AuthorizationsClient(client);
  104. authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data);
  105. client.Received().Put<ApplicationAuthorization>(Arg.Is<Uri>(u => u.ToString() == "authorizations/clients/clientId"),
  106. Args.Object);
  107. }
  108. [Fact]
  109. public void GetsOrCreatesAuthenticationAtCorrectUrlUsingTwoFactor()
  110. {
  111. var data = new NewAuthorization();
  112. var client = Substitute.For<IApiConnection>();
  113. var authEndpoint = new AuthorizationsClient(client);
  114. authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data, "two-factor");
  115. client.Received().Put<ApplicationAuthorization>(
  116. Arg.Is<Uri>(u => u.ToString() == "authorizations/clients/clientId"),
  117. Args.Object,
  118. "two-factor");
  119. }
  120. [Fact]
  121. public async Task WrapsTwoFactorFailureWithTwoFactorException()
  122. {
  123. var data = new NewAuthorization();
  124. var client = Substitute.For<IApiConnection>();
  125. client.Put<ApplicationAuthorization>(Args.Uri, Args.Object, Args.String)
  126. .ThrowsAsync<ApplicationAuthorization>(
  127. new AuthorizationException(
  128. new Response(HttpStatusCode.Unauthorized, null, new Dictionary<string, string>(), "application/json")));
  129. var authEndpoint = new AuthorizationsClient(client);
  130. await Assert.ThrowsAsync<TwoFactorChallengeFailedException>(() =>
  131. authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data, "authenticationCode"));
  132. }
  133. [Fact]
  134. public async Task UsesCallbackToRetrieveTwoFactorCode()
  135. {
  136. var twoFactorChallengeResult = new TwoFactorChallengeResult("two-factor-code");
  137. var data = new NewAuthorization { Note = "note" };
  138. var client = Substitute.For<IAuthorizationsClient>();
  139. client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any<NewAuthorization>())
  140. .Returns<Task<ApplicationAuthorization>>(_ => { throw new TwoFactorRequiredException(); });
  141. client.GetOrCreateApplicationAuthentication("clientId",
  142. "secret",
  143. Arg.Any<NewAuthorization>(),
  144. "two-factor-code")
  145. .Returns(Task.Factory.StartNew(() => new ApplicationAuthorization("xyz")));
  146. var result = await client.GetOrCreateApplicationAuthentication("clientId",
  147. "secret",
  148. data,
  149. e => Task.Factory.StartNew(() => twoFactorChallengeResult));
  150. client.Received().GetOrCreateApplicationAuthentication("clientId",
  151. "secret",
  152. Arg.Is<NewAuthorization>(u => u.Note == "note"));
  153. client.Received().GetOrCreateApplicationAuthentication("clientId",
  154. "secret",
  155. Arg.Any<NewAuthorization>(), "two-factor-code");
  156. Assert.Equal("xyz", result.Token);
  157. }
  158. [Fact]
  159. public async Task RetriesWhenResendRequested()
  160. {
  161. var challengeResults = new Queue<TwoFactorChallengeResult>(new[]
  162. {
  163. TwoFactorChallengeResult.RequestResendCode,
  164. new TwoFactorChallengeResult("two-factor-code")
  165. });
  166. var data = new NewAuthorization();
  167. var client = Substitute.For<IAuthorizationsClient>();
  168. client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any<NewAuthorization>())
  169. .Returns<Task<ApplicationAuthorization>>(_ => { throw new TwoFactorRequiredException(); });
  170. client.GetOrCreateApplicationAuthentication("clientId",
  171. "secret",
  172. Arg.Any<NewAuthorization>(),
  173. "two-factor-code")
  174. .Returns(Task.Factory.StartNew(() => new ApplicationAuthorization("OAUTHSECRET")));
  175. var result = await client.GetOrCreateApplicationAuthentication("clientId",
  176. "secret",
  177. data,
  178. e => Task.Factory.StartNew(() => challengeResults.Dequeue()));
  179. client.Received(2).GetOrCreateApplicationAuthentication("clientId",
  180. "secret",
  181. Args.NewAuthorization);
  182. client.Received().GetOrCreateApplicationAuthentication("clientId",
  183. "secret",
  184. Args.NewAuthorization,
  185. "two-factor-code");
  186. Assert.Equal("OAUTHSECRET", result.Token);
  187. }
  188. [Fact]
  189. public async Task ThrowsTwoFactorChallengeFailedExceptionWhenProvidedCodeIsIncorrect()
  190. {
  191. var challengeResults = new Queue<TwoFactorChallengeResult>(new[]
  192. {
  193. TwoFactorChallengeResult.RequestResendCode,
  194. new TwoFactorChallengeResult("wrong-code")
  195. });
  196. var data = new NewAuthorization();
  197. var client = Substitute.For<IAuthorizationsClient>();
  198. client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any<NewAuthorization>())
  199. .Returns<Task<ApplicationAuthorization>>(_ => { throw new TwoFactorRequiredException(); });
  200. client.GetOrCreateApplicationAuthentication("clientId",
  201. "secret",
  202. Arg.Any<NewAuthorization>(),
  203. "wrong-code")
  204. .Returns<Task<ApplicationAuthorization>>(_ => { throw new TwoFactorChallengeFailedException(); });
  205. var exception = await Assert.ThrowsAsync<TwoFactorChallengeFailedException>(() =>
  206. client.GetOrCreateApplicationAuthentication(
  207. "clientId",
  208. "secret",
  209. data,
  210. e => Task.Factory.StartNew(() => challengeResults.Dequeue())));
  211. Assert.NotNull(exception);
  212. client.Received().GetOrCreateApplicationAuthentication("clientId",
  213. "secret",
  214. Arg.Any<NewAuthorization>());
  215. client.Received().GetOrCreateApplicationAuthentication("clientId",
  216. "secret",
  217. Arg.Any<NewAuthorization>(),
  218. "wrong-code");
  219. }
  220. [Fact]
  221. public async Task GetsOrCreatesAuthenticationWithFingerprintAtCorrectUrl()
  222. {
  223. var data = new NewAuthorization { Fingerprint = "ha-ha-fingerprint" };
  224. var client = Substitute.For<IApiConnection>();
  225. var authEndpoint = new AuthorizationsClient(client);
  226. Uri calledUri = null;
  227. dynamic calledBody = null;
  228. client.Put<ApplicationAuthorization>(Arg.Do<Uri>(u => calledUri = u), Arg.Do<object>(body => calledBody = body));
  229. authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data);
  230. Assert.NotNull(calledUri);
  231. Assert.Equal(calledUri.ToString(), "authorizations/clients/clientId");
  232. Assert.NotNull(calledBody);
  233. Assert.Equal(calledBody.fingerprint, "ha-ha-fingerprint");
  234. }
  235. }
  236. public class TheCheckApplicationAuthenticationMethod
  237. {
  238. [Fact]
  239. public async Task ChecksApplicationAuthenticateAtCorrectUrl()
  240. {
  241. var client = Substitute.For<IApiConnection>();
  242. var authEndpoint = new AuthorizationsClient(client);
  243. authEndpoint.CheckApplicationAuthentication("clientId", "accessToken");
  244. client.Received().Get<ApplicationAuthorization>(
  245. Arg.Is<Uri>(u => u.ToString() == "applications/clientId/tokens/accessToken"),
  246. null);
  247. }
  248. [Fact]
  249. public async Task EnsuresArgumentsNotNull()
  250. {
  251. var client = Substitute.For<IApiConnection>();
  252. var authEndpoint = new AuthorizationsClient(client);
  253. await Assert.ThrowsAsync<ArgumentNullException>(() => authEndpoint.CheckApplicationAuthentication(null, "accessToken"));
  254. await Assert.ThrowsAsync<ArgumentException>(() => authEndpoint.CheckApplicationAuthentication("", "accessToken"));
  255. await Assert.ThrowsAsync<ArgumentNullException>(() => authEndpoint.CheckApplicationAuthentication("clientId", null));
  256. await Assert.ThrowsAsync<ArgumentException>(() => authEndpoint.CheckApplicationAuthentication("clientId", ""));
  257. }
  258. }
  259. public class TheResetApplicationAuthenticationMethod
  260. {
  261. [Fact]
  262. public async Task ResetsApplicationAuthenticationAtCorrectUrl()
  263. {
  264. var client = Substitute.For<IApiConnection>();
  265. var authEndpoint = new AuthorizationsClient(client);
  266. authEndpoint.ResetApplicationAuthentication("clientId", "accessToken");
  267. client.Received().Post<ApplicationAuthorization>(
  268. Arg.Is<Uri>(u => u.ToString() == "applications/clientId/tokens/accessToken"),
  269. Args.Object);
  270. }
  271. [Fact]
  272. public async Task EnsuresArgumentsNotNull()
  273. {
  274. var client = Substitute.For<IApiConnection>();
  275. var authEndpoint = new AuthorizationsClient(client);
  276. await Assert.ThrowsAsync<ArgumentNullException>(() => authEndpoint.ResetApplicationAuthentication(null, "accessToken"));
  277. await Assert.ThrowsAsync<ArgumentException>(() => authEndpoint.ResetApplicationAuthentication("", "accessToken"));
  278. await Assert.ThrowsAsync<ArgumentNullException>(() => authEndpoint.ResetApplicationAuthentication("clientId", null));
  279. await Assert.ThrowsAsync<ArgumentException>(() => authEndpoint.ResetApplicationAuthentication("clientId", ""));
  280. }
  281. }
  282. public class TheRevokeApplicationAuthenticationMethod
  283. {
  284. [Fact]
  285. public async Task RevokesApplicationAuthenticationAtCorrectUrl()
  286. {
  287. var client = Substitute.For<IApiConnection>();
  288. var authEndpoint = new AuthorizationsClient(client);
  289. authEndpoint.RevokeApplicationAuthentication("clientId", "accessToken");
  290. client.Received().Delete(
  291. Arg.Is<Uri>(u => u.ToString() == "applications/clientId/tokens/accessToken"));
  292. }
  293. [Fact]
  294. public async Task EnsuresArgumentsNotNull()
  295. {
  296. var client = Substitute.For<IApiConnection>();
  297. var authEndpoint = new AuthorizationsClient(client);
  298. await Assert.ThrowsAsync<ArgumentNullException>(() => authEndpoint.RevokeApplicationAuthentication(null, "accessToken"));
  299. await Assert.ThrowsAsync<ArgumentException>(() => authEndpoint.RevokeApplicationAuthentication("", "accessToken"));
  300. await Assert.ThrowsAsync<ArgumentNullException>(() => authEndpoint.RevokeApplicationAuthentication("clientId", null));
  301. await Assert.ThrowsAsync<ArgumentException>(() => authEndpoint.RevokeApplicationAuthentication("clientId", ""));
  302. }
  303. }
  304. }
  305. }