PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests/Reactive/ObservableNotificationsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 476 lines | 383 code | 93 blank | 0 comment | 45 complexity | 14a17d1a30d50e7738a2c6b5dc3e6d9f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using NSubstitute;
  5. using Octokit.Reactive;
  6. using Xunit;
  7. namespace Octokit.Tests.Reactive
  8. {
  9. public class ObservableNotificationsClientTests
  10. {
  11. public class TheCtor
  12. {
  13. [Fact]
  14. public void EnsuresNonNullArguments()
  15. {
  16. Assert.Throws<ArgumentNullException>(
  17. () => new ObservableNotificationsClient(null));
  18. }
  19. }
  20. public class TheGetAllForCurrentMethod
  21. {
  22. [Fact]
  23. public void RequestsCorrectUrl()
  24. {
  25. var endpoint = new Uri("notifications", UriKind.Relative);
  26. var connection = Substitute.For<IConnection>();
  27. var gitHubClient = new GitHubClient(connection);
  28. var client = new ObservableNotificationsClient(gitHubClient);
  29. client.GetAllForCurrent();
  30. connection.Received().Get<List<Notification>>(endpoint, Args.EmptyDictionary, null);
  31. }
  32. [Fact]
  33. public void RequestsCorrectUrlApiOptions()
  34. {
  35. var endpoint = new Uri("notifications", UriKind.Relative);
  36. var connection = Substitute.For<IConnection>();
  37. var gitHubClient = new GitHubClient(connection);
  38. var client = new ObservableNotificationsClient(gitHubClient);
  39. var options = new ApiOptions
  40. {
  41. PageCount = 1,
  42. StartPage = 1,
  43. PageSize = 1
  44. };
  45. client.GetAllForCurrent(options);
  46. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<Dictionary<string, string>>(d => d.Count == 2), null);
  47. }
  48. [Fact]
  49. public void RequestsCorrectUrlNotificationRequest()
  50. {
  51. var endpoint = new Uri("notifications", UriKind.Relative);
  52. var connection = Substitute.For<IConnection>();
  53. var gitHubClient = new GitHubClient(connection);
  54. var client = new ObservableNotificationsClient(gitHubClient);
  55. var notificationsRequest = new NotificationsRequest { All = true };
  56. client.GetAllForCurrent(notificationsRequest);
  57. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<IDictionary<string, string>>(d => d.Count == 2
  58. && d["all"] == "true" && d["participating"] == "false"), null);
  59. }
  60. [Fact]
  61. public void RequestsCorrectUrlNotificationRequestWithApiOptions()
  62. {
  63. var endpoint = new Uri("notifications", UriKind.Relative);
  64. var connection = Substitute.For<IConnection>();
  65. var gitHubClient = new GitHubClient(connection);
  66. var client = new ObservableNotificationsClient(gitHubClient);
  67. var notificationsRequest = new NotificationsRequest { All = true };
  68. var options = new ApiOptions
  69. {
  70. PageCount = 1,
  71. StartPage = 1,
  72. PageSize = 1
  73. };
  74. client.GetAllForCurrent(notificationsRequest, options);
  75. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<IDictionary<string, string>>(d => d.Count == 4
  76. && d["all"] == "true" && d["participating"] == "false"
  77. && d["page"] == "1" && d["per_page"] == "1"), null);
  78. }
  79. [Fact]
  80. public async Task EnsuresNonNullArguments()
  81. {
  82. var client = new ObservableNotificationsClient(Substitute.For<IGitHubClient>());
  83. Assert.Throws<ArgumentNullException>(() => client.GetAllForCurrent((ApiOptions)null));
  84. Assert.Throws<ArgumentNullException>(() => client.GetAllForCurrent((NotificationsRequest)null));
  85. }
  86. }
  87. public class TheGetAllForRepository
  88. {
  89. [Fact]
  90. public void RequestsCorrectUrl()
  91. {
  92. var endpoint = new Uri("repos/banana/split/notifications", UriKind.Relative);
  93. var connection = Substitute.For<IConnection>();
  94. var gitHubClient = new GitHubClient(connection);
  95. var client = new ObservableNotificationsClient(gitHubClient);
  96. client.GetAllForRepository("banana", "split");
  97. connection.Received().Get<List<Notification>>(endpoint, Args.EmptyDictionary, null);
  98. }
  99. [Fact]
  100. public void RequestsCorrectUrlWithRepositoryId()
  101. {
  102. var endpoint = new Uri("repositories/1/notifications", UriKind.Relative);
  103. var connection = Substitute.For<IConnection>();
  104. var gitHubClient = new GitHubClient(connection);
  105. var client = new ObservableNotificationsClient(gitHubClient);
  106. client.GetAllForRepository(1);
  107. connection.Received().Get<List<Notification>>(endpoint, Args.EmptyDictionary, null);
  108. }
  109. [Fact]
  110. public void RequestsCorrectUrlWithApiOptions()
  111. {
  112. var endpoint = new Uri("repos/banana/split/notifications", UriKind.Relative);
  113. var connection = Substitute.For<IConnection>();
  114. var gitHubClient = new GitHubClient(connection);
  115. var client = new ObservableNotificationsClient(gitHubClient);
  116. var options = new ApiOptions
  117. {
  118. PageCount = 1,
  119. StartPage = 1,
  120. PageSize = 1
  121. };
  122. client.GetAllForRepository("banana", "split", options);
  123. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<Dictionary<string, string>>(d => d.Count == 2), null);
  124. }
  125. [Fact]
  126. public void RequestsCorrectUrlWithApiOptionsWithRepositoryId()
  127. {
  128. var endpoint = new Uri("repositories/1/notifications", UriKind.Relative);
  129. var connection = Substitute.For<IConnection>();
  130. var gitHubClient = new GitHubClient(connection);
  131. var client = new ObservableNotificationsClient(gitHubClient);
  132. var options = new ApiOptions
  133. {
  134. PageCount = 1,
  135. StartPage = 1,
  136. PageSize = 1
  137. };
  138. client.GetAllForRepository(1, options);
  139. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<Dictionary<string, string>>(d => d.Count == 2), null);
  140. }
  141. [Fact]
  142. public void RequestsCorrectUrlNotificationRequest()
  143. {
  144. var endpoint = new Uri("repos/banana/split/notifications", UriKind.Relative);
  145. var connection = Substitute.For<IConnection>();
  146. var gitHubClient = new GitHubClient(connection);
  147. var client = new ObservableNotificationsClient(gitHubClient);
  148. var notificationsRequest = new NotificationsRequest { All = true };
  149. client.GetAllForRepository("banana", "split", notificationsRequest);
  150. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<Dictionary<string, string>>(
  151. d => d.Count == 2 && d["all"] == "true" && d["participating"] == "false"),
  152. null);
  153. }
  154. [Fact]
  155. public void RequestsCorrectUrlNotificationRequestWithRepositoryId()
  156. {
  157. var endpoint = new Uri("repositories/1/notifications", UriKind.Relative);
  158. var connection = Substitute.For<IConnection>();
  159. var gitHubClient = new GitHubClient(connection);
  160. var client = new ObservableNotificationsClient(gitHubClient);
  161. var notificationsRequest = new NotificationsRequest { All = true };
  162. client.GetAllForRepository(1, notificationsRequest);
  163. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<Dictionary<string, string>>(
  164. d => d.Count == 2 && d["all"] == "true" && d["participating"] == "false"),
  165. null);
  166. }
  167. [Fact]
  168. public void RequestsCorrectUrlNotificationRequestWithApiOptions()
  169. {
  170. var endpoint = new Uri("repos/banana/split/notifications", UriKind.Relative);
  171. var connection = Substitute.For<IConnection>();
  172. var gitHubClient = new GitHubClient(connection);
  173. var client = new ObservableNotificationsClient(gitHubClient);
  174. var notificationsRequest = new NotificationsRequest { All = true };
  175. var options = new ApiOptions
  176. {
  177. PageCount = 1,
  178. StartPage = 1,
  179. PageSize = 1
  180. };
  181. client.GetAllForRepository("banana", "split", notificationsRequest, options);
  182. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<Dictionary<string, string>>(
  183. d => d.Count == 4 && d["all"] == "true" && d["participating"] == "false"
  184. && d["page"] == "1" && d["per_page"] == "1"),
  185. null);
  186. }
  187. [Fact]
  188. public void RequestsCorrectUrlNotificationRequestWithApiOptionsWithRepositoryId()
  189. {
  190. var endpoint = new Uri("repositories/1/notifications", UriKind.Relative);
  191. var connection = Substitute.For<IConnection>();
  192. var gitHubClient = new GitHubClient(connection);
  193. var client = new ObservableNotificationsClient(gitHubClient);
  194. var notificationsRequest = new NotificationsRequest { All = true };
  195. var options = new ApiOptions
  196. {
  197. PageCount = 1,
  198. StartPage = 1,
  199. PageSize = 1
  200. };
  201. client.GetAllForRepository(1, notificationsRequest, options);
  202. connection.Received().Get<List<Notification>>(endpoint, Arg.Is<Dictionary<string, string>>(
  203. d => d.Count == 4 && d["all"] == "true" && d["participating"] == "false"
  204. && d["page"] == "1" && d["per_page"] == "1"),
  205. null);
  206. }
  207. [Fact]
  208. public async Task EnsuresNonNullArguments()
  209. {
  210. var client = new ObservableNotificationsClient(Substitute.For<IGitHubClient>());
  211. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name"));
  212. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null));
  213. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", ApiOptions.None));
  214. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, ApiOptions.None));
  215. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (ApiOptions)null));
  216. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new NotificationsRequest()));
  217. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new NotificationsRequest()));
  218. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", (NotificationsRequest)null));
  219. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(null, "name", new NotificationsRequest(), ApiOptions.None));
  220. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", null, new NotificationsRequest(), ApiOptions.None));
  221. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", null, ApiOptions.None));
  222. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository("owner", "name", new NotificationsRequest(), null));
  223. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (ApiOptions)null));
  224. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, (NotificationsRequest)null));
  225. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, null, ApiOptions.None));
  226. Assert.Throws<ArgumentNullException>(() => client.GetAllForRepository(1, new NotificationsRequest(), null));
  227. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name"));
  228. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", ""));
  229. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", ApiOptions.None));
  230. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", ApiOptions.None));
  231. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", new NotificationsRequest()));
  232. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", new NotificationsRequest()));
  233. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("", "name", new NotificationsRequest(), ApiOptions.None));
  234. Assert.Throws<ArgumentException>(() => client.GetAllForRepository("owner", "", new NotificationsRequest(), ApiOptions.None));
  235. }
  236. }
  237. public class TheMarkAsRead
  238. {
  239. [Fact]
  240. public void RequestsCorrectUrl()
  241. {
  242. var endpoint = new Uri("notifications", UriKind.Relative);
  243. var connection = Substitute.For<IConnection>();
  244. var gitHubClient = new GitHubClient(connection);
  245. var client = new ObservableNotificationsClient(gitHubClient);
  246. client.MarkAsRead();
  247. connection.Received().Put(endpoint);
  248. }
  249. }
  250. public class TheMarkAsReadForRepository
  251. {
  252. [Fact]
  253. public void RequestsCorrectUrl()
  254. {
  255. var endpoint = new Uri("repos/banana/split/notifications", UriKind.Relative);
  256. var connection = Substitute.For<IConnection>();
  257. var gitHubClient = new GitHubClient(connection);
  258. var client = new ObservableNotificationsClient(gitHubClient);
  259. client.MarkAsReadForRepository("banana", "split");
  260. connection.Received().Put(endpoint);
  261. }
  262. [Fact]
  263. public void RequestsCorrectUrlWithRepositoryId()
  264. {
  265. var endpoint = new Uri("repositories/1/notifications", UriKind.Relative);
  266. var connection = Substitute.For<IConnection>();
  267. var gitHubClient = new GitHubClient(connection);
  268. var client = new ObservableNotificationsClient(gitHubClient);
  269. client.MarkAsReadForRepository(1);
  270. connection.Received().Put(endpoint);
  271. }
  272. [Fact]
  273. public void RequestsCorrectUrlParameterized()
  274. {
  275. var endpoint = new Uri("repos/banana/split/notifications", UriKind.Relative);
  276. var connection = Substitute.For<IConnection>();
  277. var gitHubClient = new GitHubClient(connection);
  278. var client = new ObservableNotificationsClient(gitHubClient);
  279. var markAsReadRequest = new MarkAsReadRequest();
  280. client.MarkAsReadForRepository("banana", "split", markAsReadRequest);
  281. connection.Received().Put<object>(endpoint, markAsReadRequest);
  282. }
  283. [Fact]
  284. public void RequestsCorrectUrlParameterizedWithRepositoryId()
  285. {
  286. var endpoint = new Uri("repositories/1/notifications", UriKind.Relative);
  287. var connection = Substitute.For<IConnection>();
  288. var gitHubClient = new GitHubClient(connection);
  289. var client = new ObservableNotificationsClient(gitHubClient);
  290. var markAsReadRequest = new MarkAsReadRequest();
  291. client.MarkAsReadForRepository(1, markAsReadRequest);
  292. connection.Received().Put<object>(endpoint, markAsReadRequest);
  293. }
  294. [Fact]
  295. public async Task EnsuresNonNullArguments()
  296. {
  297. var client = new ObservableNotificationsClient(Substitute.For<IGitHubClient>());
  298. Assert.Throws<ArgumentNullException>(() => client.MarkAsReadForRepository(null, "name"));
  299. Assert.Throws<ArgumentNullException>(() => client.MarkAsReadForRepository("owner", null));
  300. Assert.Throws<ArgumentNullException>(() => client.MarkAsReadForRepository(null, "name", new MarkAsReadRequest()));
  301. Assert.Throws<ArgumentNullException>(() => client.MarkAsReadForRepository("owner", null, new MarkAsReadRequest()));
  302. Assert.Throws<ArgumentNullException>(() => client.MarkAsReadForRepository("owner", "name", null));
  303. Assert.Throws<ArgumentNullException>(() => client.MarkAsReadForRepository(1, null));
  304. Assert.Throws<ArgumentException>(() => client.MarkAsReadForRepository("", "name"));
  305. Assert.Throws<ArgumentException>(() => client.MarkAsReadForRepository("owner", ""));
  306. Assert.Throws<ArgumentException>(() => client.MarkAsReadForRepository("", "name", new MarkAsReadRequest()));
  307. Assert.Throws<ArgumentException>(() => client.MarkAsReadForRepository("owner", "", new MarkAsReadRequest()));
  308. }
  309. }
  310. public class TheGetNotification
  311. {
  312. [Fact]
  313. public void RequestsCorrectUrl()
  314. {
  315. var endpoint = new Uri("notifications/threads/1", UriKind.Relative);
  316. var connection = Substitute.For<IConnection>();
  317. var gitHubClient = new GitHubClient(connection);
  318. var client = new ObservableNotificationsClient(gitHubClient);
  319. client.Get(1);
  320. connection.Received().Get<Notification>(endpoint, null, null);
  321. }
  322. }
  323. public class TheMarkNotificationAsRead
  324. {
  325. [Fact]
  326. public void RequestsCorrectUrl()
  327. {
  328. var endpoint = new Uri("notifications/threads/1", UriKind.Relative);
  329. var connection = Substitute.For<IConnection>();
  330. var gitHubClient = new GitHubClient(connection);
  331. var client = new ObservableNotificationsClient(gitHubClient);
  332. client.MarkAsRead(1);
  333. connection.Received().Patch(endpoint);
  334. }
  335. }
  336. public class TheGetThreadSubscription
  337. {
  338. [Fact]
  339. public void RequestsCorrectUrl()
  340. {
  341. var endpoint = new Uri("notifications/threads/1/subscription", UriKind.Relative);
  342. var connection = Substitute.For<IConnection>();
  343. var gitHubClient = new GitHubClient(connection);
  344. var client = new ObservableNotificationsClient(gitHubClient);
  345. client.GetThreadSubscription(1);
  346. connection.Received().Get<ThreadSubscription>(endpoint, null, null);
  347. }
  348. }
  349. public class TheSetThreadSubscription
  350. {
  351. [Fact]
  352. public void RequestsCorrectUrl()
  353. {
  354. var endpoint = new Uri("notifications/threads/1/subscription", UriKind.Relative);
  355. var connection = Substitute.For<IConnection>();
  356. var gitHubClient = new GitHubClient(connection);
  357. var client = new ObservableNotificationsClient(gitHubClient);
  358. var data = new NewThreadSubscription();
  359. client.SetThreadSubscription(1, data);
  360. connection.Received().Put<ThreadSubscription>(endpoint, data);
  361. }
  362. [Fact]
  363. public async Task EnsuresNonNullArguments()
  364. {
  365. var client = new ObservableNotificationsClient(Substitute.For<IGitHubClient>());
  366. Assert.Throws<ArgumentNullException>(() => client.SetThreadSubscription(1, null));
  367. }
  368. }
  369. public class TheDeleteThreadSubscription
  370. {
  371. [Fact]
  372. public void RequestsCorrectUrl()
  373. {
  374. var endpoint = new Uri("notifications/threads/1/subscription", UriKind.Relative);
  375. var connection = Substitute.For<IConnection>();
  376. var gitHubClient = new GitHubClient(connection);
  377. var client = new ObservableNotificationsClient(gitHubClient);
  378. client.DeleteThreadSubscription(1);
  379. connection.Received().Delete(endpoint);
  380. }
  381. }
  382. }
  383. }