PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests/Clients/RepositoryHooksClientTest.cs

https://gitlab.com/Hexexpeck/GitHub-API-.NET
C# | 345 lines | 265 code | 80 blank | 0 comment | 16 complexity | c6466d1a38caf366f0236a06068e6348 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using NSubstitute;
  5. using Xunit;
  6. namespace Octokit.Tests.Clients
  7. {
  8. public class RepositoryHooksClientTests
  9. {
  10. public class TheCtor
  11. {
  12. [Fact]
  13. public void EnsuresNonNullArguments()
  14. {
  15. Assert.Throws<ArgumentNullException>(
  16. () => new RepositoryHooksClient(null));
  17. }
  18. }
  19. public class TheGetAllMethod
  20. {
  21. [Fact]
  22. public async Task RequestsCorrectUrl()
  23. {
  24. var connection = Substitute.For<IApiConnection>();
  25. var client = new RepositoryHooksClient(connection);
  26. await client.GetAll("fake", "repo");
  27. connection.Received().GetAll<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks"),
  28. Args.ApiOptions);
  29. }
  30. [Fact]
  31. public async Task RequestsCorrectUrlWithRepositoryId()
  32. {
  33. var connection = Substitute.For<IApiConnection>();
  34. var client = new RepositoryHooksClient(connection);
  35. await client.GetAll(1);
  36. connection.Received().GetAll<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks"),
  37. Args.ApiOptions);
  38. }
  39. [Fact]
  40. public async Task RequestsCorrectUrlWithApiOptions()
  41. {
  42. var connection = Substitute.For<IApiConnection>();
  43. var client = new RepositoryHooksClient(connection);
  44. var options = new ApiOptions
  45. {
  46. PageCount = 1,
  47. PageSize = 1,
  48. StartPage = 1
  49. };
  50. await client.GetAll("fake", "repo", options);
  51. connection.Received(1)
  52. .GetAll<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks"),
  53. options);
  54. }
  55. [Fact]
  56. public async Task RequestsCorrectUrlWithRepositoryIdWithApiOptions()
  57. {
  58. var connection = Substitute.For<IApiConnection>();
  59. var client = new RepositoryHooksClient(connection);
  60. var options = new ApiOptions
  61. {
  62. PageCount = 1,
  63. PageSize = 1,
  64. StartPage = 1
  65. };
  66. await client.GetAll(1, options);
  67. connection.Received(1)
  68. .GetAll<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks"),
  69. options);
  70. }
  71. [Fact]
  72. public async Task EnsuresNonNullArguments()
  73. {
  74. var client = new RepositoryHooksClient(Substitute.For<IApiConnection>());
  75. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null, "name"));
  76. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll("owner", null));
  77. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null, "name", ApiOptions.None));
  78. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll("owner", null, ApiOptions.None));
  79. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll("owner", "name", null));
  80. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(1, null));
  81. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll("", "name"));
  82. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll("owner", ""));
  83. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll("", "name", ApiOptions.None));
  84. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll("owner", "", ApiOptions.None));
  85. }
  86. }
  87. public class TheGetMethod
  88. {
  89. [Fact]
  90. public async Task RequestsCorrectUrl()
  91. {
  92. var connection = Substitute.For<IApiConnection>();
  93. var client = new RepositoryHooksClient(connection);
  94. await client.Get("fake", "repo", 12345678);
  95. connection.Received().Get<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks/12345678"));
  96. }
  97. [Fact]
  98. public async Task RequestsCorrectUrlWithRepositoryId()
  99. {
  100. var connection = Substitute.For<IApiConnection>();
  101. var client = new RepositoryHooksClient(connection);
  102. await client.Get(1, 12345678);
  103. connection.Received().Get<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks/12345678"));
  104. }
  105. [Fact]
  106. public async Task EnsuresNonNullArguments()
  107. {
  108. var client = new RepositoryHooksClient(Substitute.For<IApiConnection>());
  109. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get(null, "name", 123));
  110. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get("owner", null, 123));
  111. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("", "name", 123));
  112. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("owner", "", 123));
  113. }
  114. }
  115. public class TheCreateMethod
  116. {
  117. [Fact]
  118. public void RequestsCorrectUrl()
  119. {
  120. var connection = Substitute.For<IApiConnection>();
  121. var client = new RepositoryHooksClient(connection);
  122. var hook = new NewRepositoryHook("name", new Dictionary<string, string> { { "config", "" } });
  123. client.Create("fake", "repo", hook);
  124. connection.Received().Post<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks"), hook);
  125. }
  126. [Fact]
  127. public void RequestsCorrectUrlWithRepositoryId()
  128. {
  129. var connection = Substitute.For<IApiConnection>();
  130. var client = new RepositoryHooksClient(connection);
  131. var hook = new NewRepositoryHook("name", new Dictionary<string, string> { { "config", "" } });
  132. client.Create(1, hook);
  133. connection.Received().Post<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks"), hook);
  134. }
  135. [Fact]
  136. public async Task EnsuresNonNullArguments()
  137. {
  138. var client = new RepositoryHooksClient(Substitute.For<IApiConnection>());
  139. var config = new Dictionary<string, string> { { "config", "" } };
  140. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(null, "name", new NewRepositoryHook("name", config)));
  141. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", null, new NewRepositoryHook("name", config)));
  142. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", "name", null));
  143. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(1, null));
  144. await Assert.ThrowsAsync<ArgumentException>(() => client.Create("", "name", new NewRepositoryHook("name", config)));
  145. await Assert.ThrowsAsync<ArgumentException>(() => client.Create("owner", "", new NewRepositoryHook("name", config)));
  146. }
  147. }
  148. public class TheEditMethod
  149. {
  150. [Fact]
  151. public void RequestsCorrectUrl()
  152. {
  153. var connection = Substitute.For<IApiConnection>();
  154. var client = new RepositoryHooksClient(connection);
  155. var hook = new EditRepositoryHook();
  156. client.Edit("fake", "repo", 12345678, hook);
  157. connection.Received().Patch<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks/12345678"), hook);
  158. }
  159. [Fact]
  160. public void RequestsCorrectUrlWithRepositoryId()
  161. {
  162. var connection = Substitute.For<IApiConnection>();
  163. var client = new RepositoryHooksClient(connection);
  164. var hook = new EditRepositoryHook();
  165. client.Edit(1, 12345678, hook);
  166. connection.Received().Patch<RepositoryHook>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks/12345678"), hook);
  167. }
  168. [Fact]
  169. public async Task EnsuresNonNullArguments()
  170. {
  171. var client = new RepositoryHooksClient(Substitute.For<IApiConnection>());
  172. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit(null, "name", 12345678, new EditRepositoryHook()));
  173. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit("owner", null, 12345678, new EditRepositoryHook()));
  174. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit("owner", "name", 12345678, null));
  175. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit(1, 12345678, null));
  176. await Assert.ThrowsAsync<ArgumentException>(() => client.Edit("", "name", 12345678, new EditRepositoryHook()));
  177. await Assert.ThrowsAsync<ArgumentException>(() => client.Edit("owner", "", 12345678, new EditRepositoryHook()));
  178. }
  179. }
  180. public class TheTestMethod
  181. {
  182. [Fact]
  183. public void RequestsCorrectUrl()
  184. {
  185. var connection = Substitute.For<IApiConnection>();
  186. var client = new RepositoryHooksClient(connection);
  187. client.Test("fake", "repo", 12345678);
  188. connection.Received().Post(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks/12345678/tests"));
  189. }
  190. [Fact]
  191. public void RequestsCorrectUrlWithRepositoryId()
  192. {
  193. var connection = Substitute.For<IApiConnection>();
  194. var client = new RepositoryHooksClient(connection);
  195. client.Test(1, 12345678);
  196. connection.Received().Post(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks/12345678/tests"));
  197. }
  198. [Fact]
  199. public async Task EnsuresNonNullArguments()
  200. {
  201. var client = new RepositoryHooksClient(Substitute.For<IApiConnection>());
  202. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Test(null, "name", 12345678));
  203. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Test("owner", null, 12345678));
  204. await Assert.ThrowsAsync<ArgumentException>(() => client.Test("", "name", 12345678));
  205. await Assert.ThrowsAsync<ArgumentException>(() => client.Test("owner", "", 12345678));
  206. }
  207. }
  208. public class ThePingMethod
  209. {
  210. [Fact]
  211. public void RequestsCorrectUrl()
  212. {
  213. var connection = Substitute.For<IApiConnection>();
  214. var client = new RepositoryHooksClient(connection);
  215. client.Ping("fake", "repo", 12345678);
  216. connection.Received().Post(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks/12345678/pings"));
  217. }
  218. [Fact]
  219. public void RequestsCorrectUrlWithRepositoryId()
  220. {
  221. var connection = Substitute.For<IApiConnection>();
  222. var client = new RepositoryHooksClient(connection);
  223. client.Ping(1, 12345678);
  224. connection.Received().Post(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks/12345678/pings"));
  225. }
  226. [Fact]
  227. public async Task EnsuresNonNullArguments()
  228. {
  229. var client = new RepositoryHooksClient(Substitute.For<IApiConnection>());
  230. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Ping(null, "name", 12345678));
  231. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Ping("owner", null, 12345678));
  232. await Assert.ThrowsAsync<ArgumentException>(() => client.Ping("", "name", 12345678));
  233. await Assert.ThrowsAsync<ArgumentException>(() => client.Ping("owner", "", 12345678));
  234. }
  235. }
  236. public class TheDeleteMethod
  237. {
  238. [Fact]
  239. public void RequestsCorrectUrl()
  240. {
  241. var connection = Substitute.For<IApiConnection>();
  242. var client = new RepositoryHooksClient(connection);
  243. client.Delete("fake", "repo", 12345678);
  244. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/hooks/12345678"));
  245. }
  246. [Fact]
  247. public void RequestsCorrectUrlWithRepositoryId()
  248. {
  249. var connection = Substitute.For<IApiConnection>();
  250. var client = new RepositoryHooksClient(connection);
  251. client.Delete(1, 12345678);
  252. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "repositories/1/hooks/12345678"));
  253. }
  254. [Fact]
  255. public async Task EnsuresNonNullArguments()
  256. {
  257. var client = new RepositoryHooksClient(Substitute.For<IApiConnection>());
  258. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Delete(null, "name", 12345678));
  259. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Delete("owner", null, 12345678));
  260. await Assert.ThrowsAsync<ArgumentException>(() => client.Delete("", "name", 12345678));
  261. await Assert.ThrowsAsync<ArgumentException>(() => client.Delete("owner", "", 12345678));
  262. }
  263. }
  264. }
  265. }