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

/Octokit.Tests.Integration/Clients/AuthorizationClientTests.cs

https://gitlab.com/Hexexpeck/GitHub-API-.NET
C# | 239 lines | 179 code | 47 blank | 13 comment | 0 complexity | ed979c42981eec26920ae29776e0c56f MD5 | raw file
  1. using System.Threading.Tasks;
  2. using Xunit;
  3. namespace Octokit.Tests.Integration.Clients
  4. {
  5. public class AuthorizationClientTests
  6. {
  7. [BasicAuthenticationTest]
  8. public async Task CanCreatePersonalToken()
  9. {
  10. var github = Helper.GetBasicAuthClient();
  11. var note = Helper.MakeNameWithTimestamp("Testing authentication");
  12. var newAuthorization = new NewAuthorization(
  13. note,
  14. new[] { "user" });
  15. var created = await github.Authorization.Create(newAuthorization);
  16. Assert.False(string.IsNullOrWhiteSpace(created.Token));
  17. Assert.False(string.IsNullOrWhiteSpace(created.TokenLastEight));
  18. Assert.False(string.IsNullOrWhiteSpace(created.HashedToken));
  19. var get = await github.Authorization.Get(created.Id);
  20. Assert.Equal(created.Id, get.Id);
  21. Assert.Equal(created.Note, get.Note);
  22. }
  23. [IntegrationTest]
  24. public async Task CanGetAuthorization()
  25. {
  26. var github = Helper.GetBasicAuthClient();
  27. var authorizations = await github.Authorization.GetAll();
  28. Assert.NotEmpty(authorizations);
  29. }
  30. [IntegrationTest]
  31. public async Task CanGetAuthorizationWithApiOptions()
  32. {
  33. var github = Helper.GetBasicAuthClient();
  34. var authorizations = await github.Authorization.GetAll(ApiOptions.None);
  35. Assert.NotEmpty(authorizations);
  36. }
  37. [IntegrationTest]
  38. public async Task ReturnsNotEmptyAuthorizationsWithoutStart()
  39. {
  40. var github = Helper.GetBasicAuthClient();
  41. var options = new ApiOptions
  42. {
  43. PageSize = 5,
  44. PageCount = 1
  45. };
  46. var authorizations = await github.Authorization.GetAll(options);
  47. Assert.NotEmpty(authorizations);
  48. }
  49. [IntegrationTest]
  50. public async Task CannotCreatePersonalTokenWhenUsingOauthTokenCredentials()
  51. {
  52. var github = Helper.GetAuthenticatedClient();
  53. var note = Helper.MakeNameWithTimestamp("Testing authentication");
  54. var newAuthorization = new NewAuthorization(
  55. note,
  56. new[] { "user" });
  57. var error = Assert.ThrowsAsync<ForbiddenException>(() => github.Authorization.Create(newAuthorization));
  58. Assert.True(error.Result.Message.Contains("username and password Basic Auth"));
  59. }
  60. [BasicAuthenticationTest(Skip = "See https://github.com/octokit/octokit.net/issues/1000 for issue to investigate this further")]
  61. public async Task CanCreateAndGetAuthorizationWithoutFingerPrint()
  62. {
  63. var github = Helper.GetBasicAuthClient();
  64. var note = Helper.MakeNameWithTimestamp("Testing authentication");
  65. var newAuthorization = new NewAuthorization(
  66. note,
  67. new[] { "user" });
  68. // the first call will create the authorization
  69. var created = await github.Authorization.GetOrCreateApplicationAuthentication(
  70. Helper.ClientId,
  71. Helper.ClientSecret,
  72. newAuthorization);
  73. Assert.False(string.IsNullOrWhiteSpace(created.Token));
  74. Assert.False(string.IsNullOrWhiteSpace(created.TokenLastEight));
  75. Assert.False(string.IsNullOrWhiteSpace(created.HashedToken));
  76. // we can then query it through the regular API
  77. var get = await github.Authorization.Get(created.Id);
  78. Assert.Equal(created.Id, get.Id);
  79. Assert.Equal(created.Note, get.Note);
  80. // but the second time we call this API we get
  81. // a different set of data
  82. var getExisting = await github.Authorization.GetOrCreateApplicationAuthentication(
  83. Helper.ClientId,
  84. Helper.ClientSecret,
  85. newAuthorization);
  86. Assert.Equal(created.Id, getExisting.Id);
  87. // the token is no longer returned for subsequent calls
  88. Assert.True(string.IsNullOrWhiteSpace(getExisting.Token));
  89. // however the hashed and last 8 characters are available
  90. Assert.False(string.IsNullOrWhiteSpace(getExisting.TokenLastEight));
  91. Assert.False(string.IsNullOrWhiteSpace(getExisting.HashedToken));
  92. await github.Authorization.Delete(created.Id);
  93. }
  94. [BasicAuthenticationTest]
  95. public async Task CanCreateAndGetAuthorizationByFingerprint()
  96. {
  97. var github = Helper.GetBasicAuthClient();
  98. var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
  99. var note = Helper.MakeNameWithTimestamp("Testing authentication");
  100. var newAuthorization = new NewAuthorization(
  101. note,
  102. new[] { "user" },
  103. fingerprint);
  104. var created = await github.Authorization.GetOrCreateApplicationAuthentication(
  105. Helper.ClientId,
  106. Helper.ClientSecret,
  107. newAuthorization);
  108. Assert.NotNull(created);
  109. Assert.False(string.IsNullOrWhiteSpace(created.Token));
  110. // we can then query it through the regular API
  111. var get = await github.Authorization.Get(created.Id);
  112. Assert.Equal(created.Id, get.Id);
  113. Assert.Equal(created.Note, get.Note);
  114. // but the second time we call this API we get
  115. // a different set of data
  116. var getExisting = await github.Authorization.GetOrCreateApplicationAuthentication(
  117. Helper.ClientId,
  118. Helper.ClientSecret,
  119. newAuthorization);
  120. Assert.Equal(created.Id, getExisting.Id);
  121. // NOTE: the new API will no longer return the full
  122. // token as soon as you specify a Fingerprint
  123. Assert.True(string.IsNullOrWhiteSpace(getExisting.Token));
  124. // NOTE: however you will get these two new properties
  125. // to help identify the authorization at hand
  126. Assert.False(string.IsNullOrWhiteSpace(getExisting.TokenLastEight));
  127. Assert.False(string.IsNullOrWhiteSpace(getExisting.HashedToken));
  128. await github.Authorization.Delete(created.Id);
  129. }
  130. [BasicAuthenticationTest]
  131. public async Task CanCheckApplicationAuthentication()
  132. {
  133. var github = Helper.GetBasicAuthClient();
  134. var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
  135. var note = Helper.MakeNameWithTimestamp("Testing authentication");
  136. var newAuthorization = new NewAuthorization(
  137. note,
  138. new[] { "user" },
  139. fingerprint);
  140. var created = await github.Authorization.GetOrCreateApplicationAuthentication(
  141. Helper.ClientId,
  142. Helper.ClientSecret,
  143. newAuthorization);
  144. var applicationClient = Helper.GetAuthenticatedApplicationClient();
  145. var applicationAuthorization = await applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, created.Token);
  146. Assert.NotNull(applicationAuthorization);
  147. Assert.Equal(created.Token, applicationAuthorization.Token);
  148. await github.Authorization.Delete(created.Id);
  149. Assert.ThrowsAsync<NotFoundException>(() => github.Authorization.Get(created.Id));
  150. }
  151. [BasicAuthenticationTest]
  152. public async Task CanResetApplicationAuthentication()
  153. {
  154. var github = Helper.GetBasicAuthClient();
  155. var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
  156. var note = Helper.MakeNameWithTimestamp("Testing authentication");
  157. var newAuthorization = new NewAuthorization(
  158. note,
  159. new[] { "user" },
  160. fingerprint);
  161. var created = await github.Authorization.GetOrCreateApplicationAuthentication(
  162. Helper.ClientId,
  163. Helper.ClientSecret,
  164. newAuthorization);
  165. var applicationClient = Helper.GetAuthenticatedApplicationClient();
  166. var applicationAuthorization = await applicationClient.Authorization.ResetApplicationAuthentication(Helper.ClientId, created.Token);
  167. Assert.NotNull(applicationAuthorization);
  168. Assert.NotEqual(created.Token, applicationAuthorization.Token);
  169. await github.Authorization.Delete(created.Id);
  170. Assert.ThrowsAsync<NotFoundException>(() => github.Authorization.Get(created.Id));
  171. }
  172. [BasicAuthenticationTest]
  173. public async Task CanRevokeApplicationAuthentication()
  174. {
  175. var github = Helper.GetBasicAuthClient();
  176. var fingerprint = Helper.MakeNameWithTimestamp("authorization-testing");
  177. var note = Helper.MakeNameWithTimestamp("Testing authentication");
  178. var newAuthorization = new NewAuthorization(
  179. note,
  180. new[] { "user" },
  181. fingerprint);
  182. var created = await github.Authorization.GetOrCreateApplicationAuthentication(
  183. Helper.ClientId,
  184. Helper.ClientSecret,
  185. newAuthorization);
  186. var applicationClient = Helper.GetAuthenticatedApplicationClient();
  187. await applicationClient.Authorization.RevokeApplicationAuthentication(Helper.ClientId, created.Token);
  188. Assert.ThrowsAsync<NotFoundException>(() => applicationClient.Authorization.CheckApplicationAuthentication(Helper.ClientId, created.Token));
  189. Assert.ThrowsAsync<NotFoundException>(() => github.Authorization.Get(created.Id));
  190. }
  191. }
  192. }