PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Octokit.Tests/Clients/RepositoryCommentsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 404 lines | 321 code | 83 blank | 0 comment | 16 complexity | ae90b4e238ed6e80b98343ae2e432427 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;
  7. using Octokit.Internal;
  8. using Octokit.Tests;
  9. using Xunit;
  10. public class RepositoryCommentsClientTests
  11. {
  12. public class TheGetMethod
  13. {
  14. [Fact]
  15. public async Task RequestsCorrectUrl()
  16. {
  17. var connection = Substitute.For<IApiConnection>();
  18. var client = new RepositoryCommentsClient(connection);
  19. await client.Get("fake", "repo", 42);
  20. connection.Received().Get<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/comments/42"), Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview");
  21. }
  22. [Fact]
  23. public async Task RequestsCorrectUrlWithRepositoryId()
  24. {
  25. var connection = Substitute.For<IApiConnection>();
  26. var client = new RepositoryCommentsClient(connection);
  27. await client.Get(1, 42);
  28. connection.Received().Get<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/comments/42"), Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview");
  29. }
  30. [Fact]
  31. public async Task EnsuresNonNullArguments()
  32. {
  33. var client = new RepositoryCommentsClient(Substitute.For<IApiConnection>());
  34. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get(null, "name", 1));
  35. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get("owner", null, 1));
  36. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("", "name", 1));
  37. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("owner", "", 1));
  38. }
  39. }
  40. public class TheGetAllForRepositoryMethod
  41. {
  42. [Fact]
  43. public async Task RequestsCorrectUrl()
  44. {
  45. var connection = Substitute.For<IApiConnection>();
  46. var client = new RepositoryCommentsClient(connection);
  47. await client.GetAllForRepository("fake", "repo");
  48. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/comments"), Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview", Args.ApiOptions);
  49. }
  50. [Fact]
  51. public async Task RequestsCorrectUrlWithRepositoryId()
  52. {
  53. var connection = Substitute.For<IApiConnection>();
  54. var client = new RepositoryCommentsClient(connection);
  55. await client.GetAllForRepository(1);
  56. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/comments"), Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview", Args.ApiOptions);
  57. }
  58. [Fact]
  59. public async Task RequestsCorrectUrlWithApiOptions()
  60. {
  61. var connection = Substitute.For<IApiConnection>();
  62. var client = new RepositoryCommentsClient(connection);
  63. var options = new ApiOptions
  64. {
  65. StartPage = 1,
  66. PageCount = 1,
  67. PageSize = 1
  68. };
  69. await client.GetAllForRepository("fake", "repo", options);
  70. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/comments"), Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview", options);
  71. }
  72. [Fact]
  73. public async Task RequestsCorrectUrlWithRepositoryIdWithApiOptions()
  74. {
  75. var connection = Substitute.For<IApiConnection>();
  76. var client = new RepositoryCommentsClient(connection);
  77. var options = new ApiOptions
  78. {
  79. StartPage = 1,
  80. PageCount = 1,
  81. PageSize = 1
  82. };
  83. await client.GetAllForRepository(1, options);
  84. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/comments"), Arg.Any<Dictionary<string, string>>(), "application/vnd.github.squirrel-girl-preview", options);
  85. }
  86. [Fact]
  87. public async Task EnsuresNonNullArguments()
  88. {
  89. var connection = Substitute.For<IApiConnection>();
  90. var client = new RepositoryCommentsClient(connection);
  91. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name", ApiOptions.None));
  92. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null, ApiOptions.None));
  93. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null));
  94. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
  95. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
  96. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForRepository(1, null));
  97. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name"));
  98. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", ""));
  99. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("", "name", ApiOptions.None));
  100. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForRepository("owner", "", ApiOptions.None));
  101. }
  102. }
  103. public class TheGetAllForCommitMethod
  104. {
  105. [Fact]
  106. public async Task RequestsCorrectUrl()
  107. {
  108. var connection = Substitute.For<IApiConnection>();
  109. var client = new RepositoryCommentsClient(connection);
  110. await client.GetAllForCommit("fake", "repo", "sha");
  111. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/commits/sha/comments"),
  112. Arg.Any<Dictionary<string, string>>(),
  113. "application/vnd.github.squirrel-girl-preview",
  114. Args.ApiOptions);
  115. }
  116. [Fact]
  117. public async Task RequestsCorrectUrlWithRepositoryId()
  118. {
  119. var connection = Substitute.For<IApiConnection>();
  120. var client = new RepositoryCommentsClient(connection);
  121. await client.GetAllForCommit(1, "sha");
  122. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/commits/sha/comments"),
  123. Arg.Any<Dictionary<string, string>>(),
  124. "application/vnd.github.squirrel-girl-preview",
  125. Args.ApiOptions);
  126. }
  127. [Fact]
  128. public async Task RequestsCorrectUrlWithApiOptions()
  129. {
  130. var connection = Substitute.For<IApiConnection>();
  131. var client = new RepositoryCommentsClient(connection);
  132. var options = new ApiOptions
  133. {
  134. StartPage = 1,
  135. PageCount = 1,
  136. PageSize = 1
  137. };
  138. await client.GetAllForCommit("fake", "repo", "sha", options);
  139. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/commits/sha/comments"), Arg.Any<Dictionary<string, string>>(),
  140. "application/vnd.github.squirrel-girl-preview", options);
  141. }
  142. [Fact]
  143. public async Task RequestsCorrectUrlWithRepositoryIdWithApiOptions()
  144. {
  145. var connection = Substitute.For<IApiConnection>();
  146. var client = new RepositoryCommentsClient(connection);
  147. var options = new ApiOptions
  148. {
  149. StartPage = 1,
  150. PageCount = 1,
  151. PageSize = 1
  152. };
  153. await client.GetAllForCommit(1, "sha", options);
  154. connection.Received().GetAll<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/commits/sha/comments"),
  155. Arg.Any<Dictionary<string, string>>(),
  156. "application/vnd.github.squirrel-girl-preview", options);
  157. }
  158. [Fact]
  159. public async Task EnsuresNonNullArguments()
  160. {
  161. var connection = Substitute.For<IApiConnection>();
  162. var client = new RepositoryCommentsClient(connection);
  163. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit(null, "name", "sha"));
  164. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit("owner", null, "sha"));
  165. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit("owner", "name", null));
  166. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit(null, "name", "sha", ApiOptions.None));
  167. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit("owner", null, "sha", ApiOptions.None));
  168. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit("owner", "name", null, ApiOptions.None));
  169. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit("owner", "name", "sha", null));
  170. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit(1, null, ApiOptions.None));
  171. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForCommit(1, "sha", null));
  172. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForCommit("", "name", "sha"));
  173. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForCommit("owner", "", "sha"));
  174. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForCommit("owner", "name", ""));
  175. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForCommit("", "name", "sha", ApiOptions.None));
  176. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForCommit("owner", "", "sha", ApiOptions.None));
  177. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForCommit("owner", "name", "", ApiOptions.None));
  178. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForCommit(1, "", ApiOptions.None));
  179. }
  180. }
  181. public class TheCreateMethod
  182. {
  183. [Fact]
  184. public async Task PostsToCorrectUrl()
  185. {
  186. var newComment = new NewCommitComment("body");
  187. var connection = Substitute.For<IApiConnection>();
  188. var client = new RepositoryCommentsClient(connection);
  189. await client.Create("fake", "repo", "sha", newComment);
  190. connection.Received().Post<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/commits/sha/comments"), Arg.Any<object>());
  191. }
  192. [Fact]
  193. public async Task PostsToCorrectUrlWithRepositoryId()
  194. {
  195. var newComment = new NewCommitComment("body");
  196. var connection = Substitute.For<IApiConnection>();
  197. var client = new RepositoryCommentsClient(connection);
  198. await client.Create(1, "sha", newComment);
  199. connection.Received().Post<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/commits/sha/comments"), Arg.Any<object>());
  200. }
  201. [Fact]
  202. public async Task EnsuresNonNullArguments()
  203. {
  204. var connection = Substitute.For<IApiConnection>();
  205. var client = new RepositoryCommentsClient(connection);
  206. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(null, "name", "sha", new NewCommitComment("body")));
  207. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", null, "sha", new NewCommitComment("body")));
  208. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", "name", null, new NewCommitComment("body")));
  209. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("owner", "name", "sha", null));
  210. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(1, null, new NewCommitComment("body")));
  211. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(1, "sha", null));
  212. await Assert.ThrowsAsync<ArgumentException>(() => client.Create("", "name", "sha", new NewCommitComment("body")));
  213. await Assert.ThrowsAsync<ArgumentException>(() => client.Create("owner", "", "sha", new NewCommitComment("body")));
  214. await Assert.ThrowsAsync<ArgumentException>(() => client.Create("owner", "name", "", new NewCommitComment("body")));
  215. await Assert.ThrowsAsync<ArgumentException>(() => client.Create(1, "", new NewCommitComment("body")));
  216. }
  217. }
  218. public class TheUpdateMethod
  219. {
  220. [Fact]
  221. public async Task PostsToCorrectUrl()
  222. {
  223. const string issueCommentUpdate = "updated comment";
  224. var connection = Substitute.For<IApiConnection>();
  225. var client = new RepositoryCommentsClient(connection);
  226. await client.Update("fake", "repo", 42, issueCommentUpdate);
  227. connection.Received().Patch<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/comments/42"), Arg.Any<object>());
  228. }
  229. [Fact]
  230. public async Task PostsToCorrectUrlWithRepositoryId()
  231. {
  232. const string issueCommentUpdate = "updated comment";
  233. var connection = Substitute.For<IApiConnection>();
  234. var client = new RepositoryCommentsClient(connection);
  235. await client.Update(1, 42, issueCommentUpdate);
  236. connection.Received().Patch<CommitComment>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/comments/42"), Arg.Any<object>());
  237. }
  238. [Fact]
  239. public async Task EnsuresNonNullArguments()
  240. {
  241. var connection = Substitute.For<IApiConnection>();
  242. var client = new RepositoryCommentsClient(connection);
  243. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Update(null, "name", 42, "updated comment"));
  244. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Update("owner", null, 42, "updated comment"));
  245. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Update("owner", "name", 42, null));
  246. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Update(1, 42, null));
  247. await Assert.ThrowsAsync<ArgumentException>(() => client.Update("", "name", 42, "updated comment"));
  248. await Assert.ThrowsAsync<ArgumentException>(() => client.Update("owner", "", 42, "updated comment"));
  249. }
  250. }
  251. public class TheDeleteMethod
  252. {
  253. [Fact]
  254. public async Task DeletesCorrectUrl()
  255. {
  256. var connection = Substitute.For<IApiConnection>();
  257. var client = new RepositoryCommentsClient(connection);
  258. await client.Delete("fake", "repo", 42);
  259. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/comments/42"));
  260. }
  261. [Fact]
  262. public async Task DeletesCorrectUrlWithRepositoryId()
  263. {
  264. var connection = Substitute.For<IApiConnection>();
  265. var client = new RepositoryCommentsClient(connection);
  266. await client.Delete(1, 42);
  267. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "repositories/1/comments/42"));
  268. }
  269. [Fact]
  270. public async Task EnsuresNonNullArgumentsOrEmpty()
  271. {
  272. var connection = Substitute.For<IApiConnection>();
  273. var client = new RepositoryCommentsClient(connection);
  274. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Delete(null, "name", 42));
  275. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Delete("owner", null, 42));
  276. await Assert.ThrowsAsync<ArgumentException>(() => client.Delete("", "name", 42));
  277. await Assert.ThrowsAsync<ArgumentException>(() => client.Delete("owner", "", 42));
  278. }
  279. }
  280. public class TheCtor
  281. {
  282. [Fact]
  283. public void EnsuresNonNullArguments()
  284. {
  285. Assert.Throws<ArgumentNullException>(() => new RepositoryCommentsClient(null));
  286. }
  287. }
  288. [Fact]
  289. public void CanDeserializeCommitComment()
  290. {
  291. const string commitCommentResponseJson =
  292. "{\"html_url\": \"https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e#commitcomment-1\"," +
  293. "\"url\": \"https://api.github.com/repos/octocat/Hello-World/comments/1\"," +
  294. "\"id\": 1," +
  295. "\"body\": \"Me too\"," +
  296. "\"path\": \"file1.txt\"," +
  297. "\"position\": 4," +
  298. "\"line\": 14," +
  299. "\"commit_id\": \"6dcb09b5b57875f334f61aebed695e2e4193db5e\"," +
  300. "\"user\": {" +
  301. "\"login\": \"octocat\"," +
  302. "\"id\": 1," +
  303. "\"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\"," +
  304. "\"gravatar_id\": \"somehexcode\"," +
  305. "\"url\": \"https://api.github.com/users/octocat\"" +
  306. "}," +
  307. "\"created_at\": \"2011-04-14T16:00:49Z\"," +
  308. "\"updated_at\": \"2011-04-14T16:00:49Z\"" +
  309. "}";
  310. var httpResponse = new Response(
  311. HttpStatusCode.OK,
  312. commitCommentResponseJson,
  313. new Dictionary<string, string>(),
  314. "application/json");
  315. var jsonPipeline = new JsonHttpPipeline();
  316. var response = jsonPipeline.DeserializeResponse<CommitComment>(httpResponse);
  317. Assert.NotNull(response.Body);
  318. Assert.Equal(commitCommentResponseJson, response.HttpResponse.Body);
  319. Assert.Equal(1, response.Body.Id);
  320. }
  321. }