PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests/Clients/GistsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 583 lines | 463 code | 120 blank | 0 comment | 29 complexity | 861ae36b410a293082cb2a6f0277a99f MD5 | raw file
  1. using NSubstitute;
  2. using Octokit.Internal;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Threading.Tasks;
  7. using Octokit;
  8. using Octokit.Tests;
  9. using Xunit;
  10. public class GistsClientTests
  11. {
  12. public static Dictionary<string, string> DictionaryWithSince
  13. {
  14. get { return Arg.Is<Dictionary<string, string>>(d => d.ContainsKey("since")); }
  15. }
  16. public class TheCtor
  17. {
  18. [Fact]
  19. public void EnsuresNonNullArguments()
  20. {
  21. Assert.Throws<ArgumentNullException>(() => new GistsClient(null));
  22. }
  23. [Fact]
  24. public void SetCommentsClient()
  25. {
  26. var apiConnection = Substitute.For<IApiConnection>();
  27. var client = new GistsClient(apiConnection);
  28. Assert.NotNull(client.Comment);
  29. }
  30. }
  31. public class TheGetMethod
  32. {
  33. [Fact]
  34. public void RequestsCorrectUrl()
  35. {
  36. var connection = Substitute.For<IApiConnection>();
  37. var client = new GistsClient(connection);
  38. client.Get("1");
  39. connection.Received().Get<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1"));
  40. }
  41. }
  42. public class TheGetAllMethod
  43. {
  44. [Fact]
  45. public void RequestsCorrectGetAllUrl()
  46. {
  47. var connection = Substitute.For<IApiConnection>();
  48. var client = new GistsClient(connection);
  49. client.GetAll();
  50. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"), Args.ApiOptions);
  51. }
  52. [Fact]
  53. public void RequestsCorrectGetAllUrlWithApiOptions()
  54. {
  55. var connection = Substitute.For<IApiConnection>();
  56. var client = new GistsClient(connection);
  57. var options = new ApiOptions
  58. {
  59. PageSize = 1,
  60. PageCount = 1,
  61. StartPage = 1
  62. };
  63. client.GetAll(options);
  64. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"), options);
  65. }
  66. [Fact]
  67. public void RequestsCorrectGetAllWithSinceUrl()
  68. {
  69. var connection = Substitute.For<IApiConnection>();
  70. var client = new GistsClient(connection);
  71. DateTimeOffset since = DateTimeOffset.Now;
  72. client.GetAll(since);
  73. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"),
  74. DictionaryWithSince, Args.ApiOptions);
  75. }
  76. [Fact]
  77. public void RequestsCorrectGetAllWithSinceUrlAndApiOptions()
  78. {
  79. var connection = Substitute.For<IApiConnection>();
  80. var client = new GistsClient(connection);
  81. var options = new ApiOptions
  82. {
  83. PageSize = 1,
  84. PageCount = 1,
  85. StartPage = 1
  86. };
  87. DateTimeOffset since = DateTimeOffset.Now;
  88. client.GetAll(since, options);
  89. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"),
  90. DictionaryWithSince, options);
  91. }
  92. [Fact]
  93. public async Task EnsureNonNullArguments()
  94. {
  95. var connection = Substitute.For<IApiConnection>();
  96. var client = new GistsClient(connection);
  97. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null));
  98. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(DateTimeOffset.Now, null));
  99. }
  100. }
  101. public class TheGetAllPublicMethod
  102. {
  103. [Fact]
  104. public void RequestsCorrectGetAllPublicUrl()
  105. {
  106. var connection = Substitute.For<IApiConnection>();
  107. var client = new GistsClient(connection);
  108. client.GetAllPublic();
  109. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/public"), Args.ApiOptions);
  110. }
  111. [Fact]
  112. public void RequestsCorrectGetAllPublicUrlWithApiOptions()
  113. {
  114. var connection = Substitute.For<IApiConnection>();
  115. var client = new GistsClient(connection);
  116. var options = new ApiOptions
  117. {
  118. PageSize = 1,
  119. PageCount = 1,
  120. StartPage = 1
  121. };
  122. client.GetAllPublic(options);
  123. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/public"), options);
  124. }
  125. [Fact]
  126. public void RequestsCorrectGetAllPublicWithSinceUrl()
  127. {
  128. var connection = Substitute.For<IApiConnection>();
  129. var client = new GistsClient(connection);
  130. DateTimeOffset since = DateTimeOffset.Now;
  131. client.GetAllPublic(since);
  132. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/public"),
  133. DictionaryWithSince, Args.ApiOptions);
  134. }
  135. [Fact]
  136. public void RequestsCorrectGetAllPublicWithSinceUrlAndApiOptions()
  137. {
  138. var connection = Substitute.For<IApiConnection>();
  139. var client = new GistsClient(connection);
  140. var options = new ApiOptions
  141. {
  142. PageSize = 1,
  143. PageCount = 1,
  144. StartPage = 1
  145. };
  146. DateTimeOffset since = DateTimeOffset.Now;
  147. client.GetAllPublic(since, options);
  148. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/public"),
  149. DictionaryWithSince, options);
  150. }
  151. [Fact]
  152. public async Task EnsureNonNullArguments()
  153. {
  154. var connection = Substitute.For<IApiConnection>();
  155. var client = new GistsClient(connection);
  156. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllPublic(null));
  157. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllPublic(DateTimeOffset.Now, null));
  158. }
  159. }
  160. public class TheGetAllStarredMethod
  161. {
  162. [Fact]
  163. public void RequestsCorrectGetAllStarredUrl()
  164. {
  165. var connection = Substitute.For<IApiConnection>();
  166. var client = new GistsClient(connection);
  167. client.GetAllStarred();
  168. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/starred"), Args.ApiOptions);
  169. }
  170. [Fact]
  171. public void RequestsCorrectGetAllStarredUrlWithApiOptions()
  172. {
  173. var connection = Substitute.For<IApiConnection>();
  174. var client = new GistsClient(connection);
  175. var options = new ApiOptions
  176. {
  177. PageSize = 1,
  178. PageCount = 1,
  179. StartPage = 1
  180. };
  181. client.GetAllStarred(options);
  182. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/starred"), options);
  183. }
  184. [Fact]
  185. public void RequestsCorrectGetAllStarredWithSinceUrl()
  186. {
  187. var connection = Substitute.For<IApiConnection>();
  188. var client = new GistsClient(connection);
  189. DateTimeOffset since = DateTimeOffset.Now;
  190. client.GetAllStarred(since);
  191. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/starred"),
  192. DictionaryWithSince, Args.ApiOptions);
  193. }
  194. [Fact]
  195. public void RequestsCorrectGetAllStarredWithSinceUrlAndApiOptions()
  196. {
  197. var connection = Substitute.For<IApiConnection>();
  198. var client = new GistsClient(connection);
  199. var options = new ApiOptions
  200. {
  201. PageSize = 1,
  202. PageCount = 1,
  203. StartPage = 1
  204. };
  205. DateTimeOffset since = DateTimeOffset.Now;
  206. client.GetAllStarred(since, options);
  207. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/starred"),
  208. DictionaryWithSince, options);
  209. }
  210. [Fact]
  211. public async Task EnsureNonNullArguments()
  212. {
  213. var connection = Substitute.For<IApiConnection>();
  214. var client = new GistsClient(connection);
  215. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllStarred(null));
  216. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllStarred(DateTimeOffset.Now, null));
  217. }
  218. }
  219. public class TheGetAllForUserMethod
  220. {
  221. [Fact]
  222. public void RequestsCorrectGetGistsForAUserUrl()
  223. {
  224. var connection = Substitute.For<IApiConnection>();
  225. var client = new GistsClient(connection);
  226. client.GetAllForUser("octokit");
  227. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "users/octokit/gists"), Args.ApiOptions);
  228. }
  229. [Fact]
  230. public void RequestsCorrectGetGistsForAUserUrlWithApiOptions()
  231. {
  232. var connection = Substitute.For<IApiConnection>();
  233. var client = new GistsClient(connection);
  234. var options = new ApiOptions
  235. {
  236. PageSize = 1,
  237. PageCount = 1,
  238. StartPage = 1
  239. };
  240. client.GetAllForUser("octokit", options);
  241. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "users/octokit/gists"), options);
  242. }
  243. [Fact]
  244. public void RequestsCorrectGetGistsForAUserWithSinceUrl()
  245. {
  246. var connection = Substitute.For<IApiConnection>();
  247. var client = new GistsClient(connection);
  248. DateTimeOffset since = DateTimeOffset.Now;
  249. client.GetAllForUser("octokit", since);
  250. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "users/octokit/gists"),
  251. DictionaryWithSince, Args.ApiOptions);
  252. }
  253. [Fact]
  254. public void RequestsCorrectGetGistsForAUserWithSinceUrlAndApiOptions()
  255. {
  256. var connection = Substitute.For<IApiConnection>();
  257. var client = new GistsClient(connection);
  258. var options = new ApiOptions
  259. {
  260. PageSize = 1,
  261. PageCount = 1,
  262. StartPage = 1
  263. };
  264. DateTimeOffset since = DateTimeOffset.Now;
  265. client.GetAllForUser("octokit", since, options);
  266. connection.Received().GetAll<Gist>(Arg.Is<Uri>(u => u.ToString() == "users/octokit/gists"),
  267. DictionaryWithSince, options);
  268. }
  269. [Fact]
  270. public async Task EnsureNonNullArguments()
  271. {
  272. var connection = Substitute.For<IApiConnection>();
  273. var client = new GistsClient(connection);
  274. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForUser(null));
  275. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForUser(""));
  276. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForUser("", DateTimeOffset.Now));
  277. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForUser("", DateTimeOffset.Now, ApiOptions.None));
  278. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForUser("user", DateTimeOffset.Now, null));
  279. }
  280. }
  281. public class TheGetAllCommitsMethod
  282. {
  283. [Fact]
  284. public void RequestsCorrectGetCommitsUrl()
  285. {
  286. var connection = Substitute.For<IApiConnection>();
  287. var client = new GistsClient(connection);
  288. client.GetAllCommits("9257657");
  289. connection.Received().GetAll<GistHistory>(Arg.Is<Uri>(u => u.ToString() == "gists/9257657/commits"), Args.ApiOptions);
  290. }
  291. [Fact]
  292. public void RequestsCorrectGetCommitsUrlWithApiOptions()
  293. {
  294. var connection = Substitute.For<IApiConnection>();
  295. var client = new GistsClient(connection);
  296. var options = new ApiOptions
  297. {
  298. PageSize = 1,
  299. PageCount = 1,
  300. StartPage = 1
  301. };
  302. client.GetAllCommits("9257657", options);
  303. connection.Received().GetAll<GistHistory>(Arg.Is<Uri>(u => u.ToString() == "gists/9257657/commits"), options);
  304. }
  305. [Fact]
  306. public async Task EnsureNonNullArguments()
  307. {
  308. var connection = Substitute.For<IApiConnection>();
  309. var client = new GistsClient(connection);
  310. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllCommits(null));
  311. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllCommits(""));
  312. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllCommits("id", null));
  313. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllCommits("", ApiOptions.None));
  314. }
  315. }
  316. public class TheGetAllForksMethod
  317. {
  318. [Fact]
  319. public void RequestsCorrectGetForksUrl()
  320. {
  321. var connection = Substitute.For<IApiConnection>();
  322. var client = new GistsClient(connection);
  323. client.GetAllForks("9257657");
  324. connection.Received().GetAll<GistFork>(Arg.Is<Uri>(u => u.ToString() == "gists/9257657/forks"), Args.ApiOptions);
  325. }
  326. [Fact]
  327. public void RequestsCorrectGetForksUrlWithApiOptions()
  328. {
  329. var connection = Substitute.For<IApiConnection>();
  330. var client = new GistsClient(connection);
  331. var options = new ApiOptions
  332. {
  333. PageSize = 1,
  334. PageCount = 1,
  335. StartPage = 1
  336. };
  337. client.GetAllForks("9257657", options);
  338. connection.Received().GetAll<GistFork>(Arg.Is<Uri>(u => u.ToString() == "gists/9257657/forks"), options);
  339. }
  340. [Fact]
  341. public async Task EnsureNonNullArguments()
  342. {
  343. var connection = Substitute.For<IApiConnection>();
  344. var client = new GistsClient(connection);
  345. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForks(null));
  346. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForks(""));
  347. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllForks("id", null));
  348. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllForks("", ApiOptions.None));
  349. }
  350. }
  351. public class TheCreateMethod
  352. {
  353. [Fact]
  354. public void PostsToTheCorrectUrl()
  355. {
  356. var connection = Substitute.For<IApiConnection>();
  357. var client = new GistsClient(connection);
  358. var newGist = new NewGist();
  359. newGist.Description = "my new gist";
  360. newGist.Public = true;
  361. newGist.Files.Add("myGistTestFile.cs", "new GistsClient(connection).Create();");
  362. client.Create(newGist);
  363. connection.Received().Post<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists"), Arg.Any<object>());
  364. }
  365. }
  366. public class TheDeleteMethod
  367. {
  368. [Fact]
  369. public void PostsToTheCorrectUrl()
  370. {
  371. var connection = Substitute.For<IApiConnection>();
  372. var client = new GistsClient(connection);
  373. client.Delete("1");
  374. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "gists/1"));
  375. }
  376. [Fact]
  377. public async Task EnsuresArgumentsNotNull()
  378. {
  379. var connection = Substitute.For<IApiConnection>();
  380. var client = new GistsClient(connection);
  381. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Delete(null));
  382. }
  383. }
  384. public class TheStarMethods
  385. {
  386. [Fact]
  387. public void RequestsCorrectStarUrl()
  388. {
  389. var connection = Substitute.For<IApiConnection>();
  390. var client = new GistsClient(connection);
  391. client.Star("1");
  392. connection.Received().Put(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"));
  393. }
  394. [Fact]
  395. public void RequestsCorrectUnstarUrl()
  396. {
  397. var connection = Substitute.For<IApiConnection>();
  398. var client = new GistsClient(connection);
  399. client.Unstar("1");
  400. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"));
  401. }
  402. [Theory]
  403. [InlineData(HttpStatusCode.NoContent, true)]
  404. [InlineData(HttpStatusCode.NotFound, false)]
  405. public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
  406. {
  407. var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
  408. new ApiResponse<object>(new Response(status, null, new Dictionary<string, string>(), "application/json")));
  409. var connection = Substitute.For<IConnection>();
  410. connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"),
  411. null, null).Returns(response);
  412. var apiConnection = Substitute.For<IApiConnection>();
  413. apiConnection.Connection.Returns(connection);
  414. var client = new GistsClient(apiConnection);
  415. var result = await client.IsStarred("1");
  416. Assert.Equal(expected, result);
  417. }
  418. [Fact]
  419. public async Task ThrowsExceptionForInvalidStatusCode()
  420. {
  421. var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
  422. new ApiResponse<object>(new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json")));
  423. var connection = Substitute.For<IConnection>();
  424. connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"),
  425. null, null).Returns(response);
  426. var apiConnection = Substitute.For<IApiConnection>();
  427. apiConnection.Connection.Returns(connection);
  428. var client = new GistsClient(apiConnection);
  429. await Assert.ThrowsAsync<ApiException>(() => client.IsStarred("1"));
  430. }
  431. }
  432. public class TheForkMethod
  433. {
  434. [Fact]
  435. public void RequestsCorrectUrl()
  436. {
  437. var connection = Substitute.For<IApiConnection>();
  438. var client = new GistsClient(connection);
  439. client.Fork("1");
  440. connection.Received().Post<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1/forks"),
  441. Arg.Any<object>());
  442. }
  443. }
  444. public class TheEditMethod
  445. {
  446. [Fact]
  447. public void PostsToTheCorrectUrl()
  448. {
  449. var connection = Substitute.For<IApiConnection>();
  450. var client = new GistsClient(connection);
  451. var updateGist = new GistUpdate();
  452. updateGist.Description = "my newly updated gist";
  453. var gistFileUpdate = new GistFileUpdate
  454. {
  455. NewFileName = "myNewGistTestFile.cs",
  456. Content = "new GistsClient(connection).Edit();"
  457. };
  458. updateGist.Files.Add("myGistTestFile.cs", gistFileUpdate);
  459. client.Edit("1", updateGist);
  460. connection.Received().Patch<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1"), Arg.Any<object>());
  461. }
  462. }
  463. }