PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/Octokit.Tests.Integration/Clients/RepositoryHooksClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 487 lines | 384 code | 103 blank | 0 comment | 2 complexity | 6e48ddb2fcf669f8be202449e4f19974 MD5 | raw file
  1. using Octokit.Tests.Integration.fixtures;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Xunit;
  6. namespace Octokit.Tests.Integration.Clients
  7. {
  8. public class RepositoryHooksClientTests
  9. {
  10. [Collection(RepositoriesHooksCollection.Name)]
  11. public class TheGetAllMethod
  12. {
  13. readonly RepositoriesHooksFixture _fixture;
  14. public TheGetAllMethod(RepositoriesHooksFixture fixture)
  15. {
  16. _fixture = fixture;
  17. }
  18. [IntegrationTest]
  19. public async Task ReturnsAllHooksFromRepository()
  20. {
  21. var github = Helper.GetAuthenticatedClient();
  22. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryOwner, _fixture.RepositoryName);
  23. Assert.Equal(_fixture.ExpectedHooks.Count, hooks.Count);
  24. var actualHook = hooks[0];
  25. AssertHook(_fixture.ExpectedHook, actualHook);
  26. }
  27. [IntegrationTest]
  28. public async Task ReturnsAllHooksFromRepositoryWithRepositoryId()
  29. {
  30. var github = Helper.GetAuthenticatedClient();
  31. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryId);
  32. Assert.Equal(_fixture.ExpectedHooks.Count, hooks.Count);
  33. var actualHook = hooks[0];
  34. AssertHook(_fixture.ExpectedHook, actualHook);
  35. }
  36. [IntegrationTest]
  37. public async Task ReturnsCorrectCountOfHooksWithoutStart()
  38. {
  39. var github = Helper.GetAuthenticatedClient();
  40. var options = new ApiOptions
  41. {
  42. PageSize = 5,
  43. PageCount = 1
  44. };
  45. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryOwner, _fixture.RepositoryName, options);
  46. Assert.Equal(_fixture.ExpectedHooks.Count, hooks.Count);
  47. }
  48. [IntegrationTest]
  49. public async Task ReturnsCorrectCountOfHooksWithoutStartWithRepositoryId()
  50. {
  51. var github = Helper.GetAuthenticatedClient();
  52. var options = new ApiOptions
  53. {
  54. PageSize = 5,
  55. PageCount = 1
  56. };
  57. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryId, options);
  58. Assert.Equal(_fixture.ExpectedHooks.Count, hooks.Count);
  59. }
  60. [IntegrationTest]
  61. public async Task ReturnsCorrectCountOfHooksWithStart()
  62. {
  63. var github = Helper.GetAuthenticatedClient();
  64. var options = new ApiOptions
  65. {
  66. PageSize = 2,
  67. PageCount = 1,
  68. StartPage = 3
  69. };
  70. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryOwner, _fixture.RepositoryName, options);
  71. Assert.Equal(1, hooks.Count);
  72. }
  73. [IntegrationTest]
  74. public async Task ReturnsCorrectCountOfHooksWithStartWithRepositoryId()
  75. {
  76. var github = Helper.GetAuthenticatedClient();
  77. var options = new ApiOptions
  78. {
  79. PageSize = 2,
  80. PageCount = 1,
  81. StartPage = 3
  82. };
  83. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryId, options);
  84. Assert.Equal(1, hooks.Count);
  85. }
  86. [IntegrationTest]
  87. public async Task ReturnsDistinctResultsBasedOnStartPage()
  88. {
  89. var github = Helper.GetAuthenticatedClient();
  90. var startOptions = new ApiOptions
  91. {
  92. PageSize = 2,
  93. PageCount = 1
  94. };
  95. var firstPage = await github.Repository.Hooks.GetAll(_fixture.RepositoryOwner, _fixture.RepositoryName, startOptions);
  96. var skipStartOptions = new ApiOptions
  97. {
  98. PageSize = 2,
  99. PageCount = 1,
  100. StartPage = 2
  101. };
  102. var secondPage = await github.Repository.Hooks.GetAll(_fixture.RepositoryOwner, _fixture.RepositoryName, skipStartOptions);
  103. Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
  104. Assert.NotEqual(firstPage[1].Id, secondPage[1].Id);
  105. }
  106. [IntegrationTest]
  107. public async Task ReturnsDistinctResultsBasedOnStartPageWithRepositoryId()
  108. {
  109. var github = Helper.GetAuthenticatedClient();
  110. var startOptions = new ApiOptions
  111. {
  112. PageSize = 2,
  113. PageCount = 1
  114. };
  115. var firstPage = await github.Repository.Hooks.GetAll(_fixture.RepositoryId, startOptions);
  116. var skipStartOptions = new ApiOptions
  117. {
  118. PageSize = 2,
  119. PageCount = 1,
  120. StartPage = 2
  121. };
  122. var secondPage = await github.Repository.Hooks.GetAll(_fixture.RepositoryId, skipStartOptions);
  123. Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
  124. Assert.NotEqual(firstPage[1].Id, secondPage[1].Id);
  125. }
  126. }
  127. [Collection(RepositoriesHooksCollection.Name)]
  128. public class TheGetMethod
  129. {
  130. readonly RepositoriesHooksFixture _fixture;
  131. public TheGetMethod(RepositoriesHooksFixture fixture)
  132. {
  133. _fixture = fixture;
  134. }
  135. [IntegrationTest]
  136. public async Task GetHookByCreatedId()
  137. {
  138. var github = Helper.GetAuthenticatedClient();
  139. var actualHook = await github.Repository.Hooks.Get(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHook.Id);
  140. AssertHook(_fixture.ExpectedHook, actualHook);
  141. }
  142. [IntegrationTest]
  143. public async Task GetHookByCreatedIdWithRepositoryId()
  144. {
  145. var github = Helper.GetAuthenticatedClient();
  146. var actualHook = await github.Repository.Hooks.Get(_fixture.RepositoryId, _fixture.ExpectedHook.Id);
  147. AssertHook(_fixture.ExpectedHook, actualHook);
  148. }
  149. }
  150. public class TheCreateMethod
  151. {
  152. [IntegrationTest]
  153. public async Task CreateAWebHookForTestRepository()
  154. {
  155. var github = Helper.GetAuthenticatedClient();
  156. var repoName = Helper.MakeNameWithTimestamp("create-hooks-test");
  157. var repository = await github.Repository.Create(new NewRepository(repoName) { AutoInit = true });
  158. var url = "http://test.com/example";
  159. var contentType = WebHookContentType.Json;
  160. var secret = "53cr37";
  161. var config = new Dictionary<string, string>
  162. {
  163. { "hostname", "http://hostname.url" },
  164. { "username", "username" },
  165. { "password", "password" }
  166. };
  167. var parameters = new NewRepositoryWebHook("windowsazure", config, url)
  168. {
  169. Events = new[] { "push" },
  170. Active = false,
  171. ContentType = contentType,
  172. Secret = secret
  173. };
  174. var hook = await github.Repository.Hooks.Create(Helper.UserName, repository.Name, parameters.ToRequest());
  175. var baseHookUrl = CreateExpectedBaseHookUrl(repository.Url, hook.Id);
  176. var webHookConfig = CreateExpectedConfigDictionary(config, url, contentType, secret);
  177. Assert.Equal("windowsazure", hook.Name);
  178. Assert.Equal(new[] { "push" }.ToList(), hook.Events.ToList());
  179. Assert.Equal(baseHookUrl, hook.Url);
  180. Assert.Equal(baseHookUrl + "/test", hook.TestUrl);
  181. Assert.Equal(baseHookUrl + "/pings", hook.PingUrl);
  182. Assert.NotNull(hook.CreatedAt);
  183. Assert.NotNull(hook.UpdatedAt);
  184. Assert.Equal(webHookConfig.Keys, hook.Config.Keys);
  185. Assert.Equal(webHookConfig.Values, hook.Config.Values);
  186. Assert.Equal(false, hook.Active);
  187. }
  188. [IntegrationTest]
  189. public async Task CreateAWebHookForTestRepositoryWithRepositoryId()
  190. {
  191. var github = Helper.GetAuthenticatedClient();
  192. var repoName = Helper.MakeNameWithTimestamp("create-hooks-test");
  193. var repository = await github.Repository.Create(new NewRepository(repoName) { AutoInit = true });
  194. var url = "http://test.com/example";
  195. var contentType = WebHookContentType.Json;
  196. var secret = "53cr37";
  197. var config = new Dictionary<string, string>
  198. {
  199. { "hostname", "http://hostname.url" },
  200. { "username", "username" },
  201. { "password", "password" }
  202. };
  203. var parameters = new NewRepositoryWebHook("windowsazure", config, url)
  204. {
  205. Events = new[] { "push" },
  206. Active = false,
  207. ContentType = contentType,
  208. Secret = secret
  209. };
  210. var hook = await github.Repository.Hooks.Create(repository.Id, parameters.ToRequest());
  211. var baseHookUrl = CreateExpectedBaseHookUrl(repository.Url, hook.Id);
  212. var webHookConfig = CreateExpectedConfigDictionary(config, url, contentType, secret);
  213. Assert.Equal("windowsazure", hook.Name);
  214. Assert.Equal(new[] { "push" }.ToList(), hook.Events.ToList());
  215. Assert.Equal(baseHookUrl, hook.Url);
  216. Assert.Equal(baseHookUrl + "/test", hook.TestUrl);
  217. Assert.Equal(baseHookUrl + "/pings", hook.PingUrl);
  218. Assert.NotNull(hook.CreatedAt);
  219. Assert.NotNull(hook.UpdatedAt);
  220. Assert.Equal(webHookConfig.Keys, hook.Config.Keys);
  221. Assert.Equal(webHookConfig.Values, hook.Config.Values);
  222. Assert.Equal(false, hook.Active);
  223. }
  224. Dictionary<string, string> CreateExpectedConfigDictionary(Dictionary<string, string> config, string url, WebHookContentType contentType, string secret)
  225. {
  226. return new Dictionary<string, string>
  227. {
  228. { "url", url },
  229. { "content_type", contentType.ToString().ToLowerInvariant() },
  230. { "secret", secret },
  231. { "insecure_ssl", "False" }
  232. }.Union(config).ToDictionary(k => k.Key, v => v.Value);
  233. }
  234. string CreateExpectedBaseHookUrl(string url, int id)
  235. {
  236. return url + "/hooks/" + id;
  237. }
  238. }
  239. [Collection(RepositoriesHooksCollection.Name)]
  240. public class TheEditMethod
  241. {
  242. readonly RepositoriesHooksFixture _fixture;
  243. public TheEditMethod(RepositoriesHooksFixture fixture)
  244. {
  245. _fixture = fixture;
  246. }
  247. [IntegrationTest]
  248. public async Task EditHookWithNoNewConfigRetainsTheOldConfig()
  249. {
  250. var github = Helper.GetAuthenticatedClient();
  251. var editRepositoryHook = new EditRepositoryHook
  252. {
  253. AddEvents = new[] { "pull_request" }
  254. };
  255. var actualHook = await github.Repository.Hooks.Edit(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHooks[0].Id, editRepositoryHook);
  256. var expectedConfig = new Dictionary<string, string> { { "content_type", "json" }, { "url", "http://test.com/example" } };
  257. Assert.Equal(new[] { "deployment", "pull_request" }.ToList(), actualHook.Events.ToList());
  258. Assert.Equal(expectedConfig.Keys, actualHook.Config.Keys);
  259. Assert.Equal(expectedConfig.Values, actualHook.Config.Values);
  260. }
  261. [IntegrationTest]
  262. public async Task EditHookWithNoNewConfigRetainsTheOldConfigWithRepositoryId()
  263. {
  264. var github = Helper.GetAuthenticatedClient();
  265. var editRepositoryHook = new EditRepositoryHook
  266. {
  267. AddEvents = new[] { "pull_request" }
  268. };
  269. var actualHook = await github.Repository.Hooks.Edit(_fixture.RepositoryId, _fixture.ExpectedHooks[1].Id, editRepositoryHook);
  270. var expectedConfig = new Dictionary<string, string> { { "content_type", "json" }, { "url", "http://test.com/example" } };
  271. Assert.Equal(new[] { "push", "pull_request" }.ToList(), actualHook.Events.ToList());
  272. Assert.Equal(expectedConfig.Keys, actualHook.Config.Keys);
  273. Assert.Equal(expectedConfig.Values, actualHook.Config.Values);
  274. }
  275. [IntegrationTest]
  276. public async Task EditHookWithNewInformation()
  277. {
  278. var github = Helper.GetAuthenticatedClient();
  279. var editRepositoryHook = new EditRepositoryHook(new Dictionary<string, string> { { "project", "GEZDGORQFY2TCNZRGY2TSMBVGUYDK" } })
  280. {
  281. AddEvents = new[] { "pull_request" }
  282. };
  283. var actualHook = await github.Repository.Hooks.Edit(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHooks[2].Id, editRepositoryHook);
  284. var expectedConfig = new Dictionary<string, string> { { "project", "GEZDGORQFY2TCNZRGY2TSMBVGUYDK" } };
  285. Assert.Equal(new[] { "push", "pull_request" }.ToList(), actualHook.Events.ToList());
  286. Assert.Equal(expectedConfig.Keys, actualHook.Config.Keys);
  287. Assert.Equal(expectedConfig.Values, actualHook.Config.Values);
  288. }
  289. [IntegrationTest]
  290. public async Task EditHookWithNewInformationWithRepositoryId()
  291. {
  292. var github = Helper.GetAuthenticatedClient();
  293. var editRepositoryHook = new EditRepositoryHook(new Dictionary<string, string> { { "project", "GEZDGORQFY2TCNZRGY2TSMBVGUYDK" } })
  294. {
  295. AddEvents = new[] { "pull_request" }
  296. };
  297. var actualHook = await github.Repository.Hooks.Edit(_fixture.RepositoryId, _fixture.ExpectedHooks[3].Id, editRepositoryHook);
  298. var expectedConfig = new Dictionary<string, string> { { "project", "GEZDGORQFY2TCNZRGY2TSMBVGUYDK" } };
  299. Assert.Equal(new[] { "push", "pull_request" }.ToList(), actualHook.Events.ToList());
  300. Assert.Equal(expectedConfig.Keys, actualHook.Config.Keys);
  301. Assert.Equal(expectedConfig.Values, actualHook.Config.Values);
  302. }
  303. }
  304. [Collection(RepositoriesHooksCollection.Name)]
  305. public class TheTestMethod
  306. {
  307. readonly RepositoriesHooksFixture _fixture;
  308. public TheTestMethod(RepositoriesHooksFixture fixture)
  309. {
  310. _fixture = fixture;
  311. }
  312. [IntegrationTest]
  313. public async Task TestACreatedHook()
  314. {
  315. var github = Helper.GetAuthenticatedClient();
  316. await github.Repository.Hooks.Test(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHook.Id);
  317. }
  318. [IntegrationTest]
  319. public async Task TestACreatedHookWithRepositoryId()
  320. {
  321. var github = Helper.GetAuthenticatedClient();
  322. await github.Repository.Hooks.Test(_fixture.RepositoryId, _fixture.ExpectedHook.Id);
  323. }
  324. }
  325. [Collection(RepositoriesHooksCollection.Name)]
  326. public class ThePingMethod
  327. {
  328. readonly RepositoriesHooksFixture _fixture;
  329. public ThePingMethod(RepositoriesHooksFixture fixture)
  330. {
  331. _fixture = fixture;
  332. }
  333. [IntegrationTest]
  334. public async Task PingACreatedHook()
  335. {
  336. var github = Helper.GetAuthenticatedClient();
  337. await github.Repository.Hooks.Ping(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHook.Id);
  338. }
  339. [IntegrationTest]
  340. public async Task PingACreatedHookWithRepositoryId()
  341. {
  342. var github = Helper.GetAuthenticatedClient();
  343. await github.Repository.Hooks.Ping(_fixture.RepositoryId, _fixture.ExpectedHook.Id);
  344. }
  345. }
  346. [Collection(RepositoriesHooksCollection.Name)]
  347. public class TheDeleteMethod
  348. {
  349. readonly RepositoriesHooksFixture _fixture;
  350. public TheDeleteMethod(RepositoriesHooksFixture fixture)
  351. {
  352. _fixture = fixture;
  353. }
  354. [IntegrationTest]
  355. public async Task DeleteCreatedWebHook()
  356. {
  357. var github = Helper.GetAuthenticatedClient();
  358. await github.Repository.Hooks.Delete(_fixture.RepositoryOwner, _fixture.RepositoryName, _fixture.ExpectedHook.Id);
  359. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryOwner, _fixture.RepositoryName);
  360. Assert.Empty(hooks.Where(hook => hook.Id == _fixture.ExpectedHook.Id));
  361. }
  362. [IntegrationTest]
  363. public async Task DeleteCreatedWebHookWithRepositoryId()
  364. {
  365. var github = Helper.GetAuthenticatedClient();
  366. await github.Repository.Hooks.Delete(_fixture.RepositoryId, _fixture.ExpectedHooks[1].Id);
  367. var hooks = await github.Repository.Hooks.GetAll(_fixture.RepositoryId);
  368. Assert.Empty(hooks.Where(hook => hook.Id == _fixture.ExpectedHooks[1].Id));
  369. }
  370. }
  371. static void AssertHook(RepositoryHook expectedHook, RepositoryHook actualHook)
  372. {
  373. Assert.Equal(expectedHook.Id, actualHook.Id);
  374. Assert.Equal(expectedHook.Active, actualHook.Active);
  375. Assert.Equal(expectedHook.Config, actualHook.Config);
  376. Assert.Equal(expectedHook.CreatedAt, actualHook.CreatedAt);
  377. Assert.Equal(expectedHook.Name, actualHook.Name);
  378. Assert.Equal(expectedHook.PingUrl, actualHook.PingUrl);
  379. Assert.Equal(expectedHook.TestUrl, actualHook.TestUrl);
  380. Assert.Equal(expectedHook.UpdatedAt, actualHook.UpdatedAt);
  381. Assert.Equal(expectedHook.Url, actualHook.Url);
  382. }
  383. }
  384. }