PageRenderTime 74ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Octokit.Tests/Clients/RepositoriesClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 1144 lines | 919 code | 221 blank | 4 comment | 62 complexity | 0637963fb5a9683f8209fd51c3c12a4d 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 Xunit;
  7. namespace Octokit.Tests.Clients
  8. {
  9. /// <summary>
  10. /// Client tests mostly just need to make sure they call the IApiConnection with the correct
  11. /// relative Uri. No need to fake up the response. All *those* tests are in ApiConnectionTests.cs.
  12. /// </summary>
  13. public class RepositoriesClientTests
  14. {
  15. public class TheCtor
  16. {
  17. [Fact]
  18. public void EnsuresNonNullArguments()
  19. {
  20. Assert.Throws<ArgumentNullException>(() => new RepositoriesClient(null));
  21. }
  22. }
  23. public class TheCreateMethodForUser
  24. {
  25. [Fact]
  26. public async Task EnsuresNonNullArguments()
  27. {
  28. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  29. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(null));
  30. }
  31. [Fact]
  32. public void UsesTheUserReposUrl()
  33. {
  34. var connection = Substitute.For<IApiConnection>();
  35. var client = new RepositoriesClient(connection);
  36. client.Create(new NewRepository("aName"));
  37. connection.Received().Post<Repository>(Arg.Is<Uri>(u => u.ToString() == "user/repos"), Arg.Any<NewRepository>());
  38. }
  39. [Fact]
  40. public void TheNewRepositoryDescription()
  41. {
  42. var connection = Substitute.For<IApiConnection>();
  43. var client = new RepositoriesClient(connection);
  44. var newRepository = new NewRepository("aName");
  45. client.Create(newRepository);
  46. connection.Received().Post<Repository>(Args.Uri, newRepository);
  47. }
  48. [Fact]
  49. public async Task ThrowsRepositoryExistsExceptionWhenRepositoryExistsForCurrentUser()
  50. {
  51. var newRepository = new NewRepository("aName");
  52. var response = Substitute.For<IResponse>();
  53. response.StatusCode.Returns((HttpStatusCode)422);
  54. response.Body.Returns(@"{""message"":""Validation Failed"",""documentation_url"":"
  55. + @"""http://developer.github.com/v3/repos/#create"",""errors"":[{""resource"":""Repository"","
  56. + @"""code"":""custom"",""field"":""name"",""message"":""name already exists on this account""}]}");
  57. var credentials = new Credentials("haacked", "pwd");
  58. var connection = Substitute.For<IApiConnection>();
  59. connection.Connection.BaseAddress.Returns(GitHubClient.GitHubApiUrl);
  60. connection.Connection.Credentials.Returns(credentials);
  61. connection.Post<Repository>(Args.Uri, newRepository)
  62. .Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
  63. var client = new RepositoriesClient(connection);
  64. var exception = await Assert.ThrowsAsync<RepositoryExistsException>(
  65. () => client.Create(newRepository));
  66. Assert.False(exception.OwnerIsOrganization);
  67. Assert.Null(exception.Organization);
  68. Assert.Equal("aName", exception.RepositoryName);
  69. Assert.Null(exception.ExistingRepositoryWebUrl);
  70. }
  71. [Fact]
  72. public async Task ThrowsExceptionWhenPrivateRepositoryQuotaExceeded()
  73. {
  74. var newRepository = new NewRepository("aName") { Private = true };
  75. var response = Substitute.For<IResponse>();
  76. response.StatusCode.Returns((HttpStatusCode)422);
  77. response.Body.Returns(@"{""message"":""Validation Failed"",""documentation_url"":"
  78. + @"""http://developer.github.com/v3/repos/#create"",""errors"":[{""resource"":""Repository"","
  79. + @"""code"":""custom"",""field"":""name"",""message"":"
  80. + @"""name can't be private. You are over your quota.""}]}");
  81. var credentials = new Credentials("haacked", "pwd");
  82. var connection = Substitute.For<IApiConnection>();
  83. connection.Connection.BaseAddress.Returns(GitHubClient.GitHubApiUrl);
  84. connection.Connection.Credentials.Returns(credentials);
  85. connection.Post<Repository>(Args.Uri, newRepository)
  86. .Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
  87. var client = new RepositoriesClient(connection);
  88. var exception = await Assert.ThrowsAsync<PrivateRepositoryQuotaExceededException>(
  89. () => client.Create(newRepository));
  90. Assert.NotNull(exception);
  91. }
  92. }
  93. public class TheCreateMethodForOrganization
  94. {
  95. [Fact]
  96. public async Task EnsuresNonNullArguments()
  97. {
  98. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  99. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create(null, new NewRepository("aName")));
  100. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Create("aLogin", null));
  101. }
  102. [Fact]
  103. public async Task UsesTheOrganizationsReposUrl()
  104. {
  105. var connection = Substitute.For<IApiConnection>();
  106. var client = new RepositoriesClient(connection);
  107. await client.Create("theLogin", new NewRepository("aName"));
  108. connection.Received().Post<Repository>(
  109. Arg.Is<Uri>(u => u.ToString() == "orgs/theLogin/repos"),
  110. Args.NewRepository);
  111. }
  112. [Fact]
  113. public async Task TheNewRepositoryDescription()
  114. {
  115. var connection = Substitute.For<IApiConnection>();
  116. var client = new RepositoriesClient(connection);
  117. var newRepository = new NewRepository("aName");
  118. await client.Create("aLogin", newRepository);
  119. connection.Received().Post<Repository>(Args.Uri, newRepository);
  120. }
  121. [Fact]
  122. public async Task ThrowsRepositoryExistsExceptionWhenRepositoryExistsForSpecifiedOrg()
  123. {
  124. var newRepository = new NewRepository("aName");
  125. var response = Substitute.For<IResponse>();
  126. response.StatusCode.Returns((HttpStatusCode)422);
  127. response.Body.Returns(@"{""message"":""Validation Failed"",""documentation_url"":"
  128. + @"""http://developer.github.com/v3/repos/#create"",""errors"":[{""resource"":""Repository"","
  129. + @"""code"":""custom"",""field"":""name"",""message"":""name already exists on this account""}]}");
  130. var connection = Substitute.For<IApiConnection>();
  131. connection.Connection.BaseAddress.Returns(GitHubClient.GitHubApiUrl);
  132. connection.Post<Repository>(Args.Uri, newRepository)
  133. .Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
  134. var client = new RepositoriesClient(connection);
  135. var exception = await Assert.ThrowsAsync<RepositoryExistsException>(
  136. () => client.Create("illuminati", newRepository));
  137. Assert.True(exception.OwnerIsOrganization);
  138. Assert.Equal("illuminati", exception.Organization);
  139. Assert.Equal("aName", exception.RepositoryName);
  140. Assert.Equal(new Uri("https://github.com/illuminati/aName"), exception.ExistingRepositoryWebUrl);
  141. Assert.Equal("There is already a repository named 'aName' in the organization 'illuminati'.",
  142. exception.Message);
  143. }
  144. [Fact]
  145. public async Task ThrowsValidationException()
  146. {
  147. var newRepository = new NewRepository("aName");
  148. var response = Substitute.For<IResponse>();
  149. response.StatusCode.Returns((HttpStatusCode)422);
  150. response.Body.Returns(@"{""message"":""Validation Failed"",""documentation_url"":"
  151. + @"""http://developer.github.com/v3/repos/#create"",""errors"":[]}");
  152. var connection = Substitute.For<IApiConnection>();
  153. connection.Connection.BaseAddress.Returns(GitHubClient.GitHubApiUrl);
  154. connection.Post<Repository>(Args.Uri, newRepository)
  155. .Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
  156. var client = new RepositoriesClient(connection);
  157. var exception = await Assert.ThrowsAsync<ApiValidationException>(
  158. () => client.Create("illuminati", newRepository));
  159. Assert.Null(exception as RepositoryExistsException);
  160. }
  161. [Fact]
  162. public async Task ThrowsRepositoryExistsExceptionForEnterpriseInstance()
  163. {
  164. var newRepository = new NewRepository("aName");
  165. var response = Substitute.For<IResponse>();
  166. response.StatusCode.Returns((HttpStatusCode)422);
  167. response.Body.Returns(@"{""message"":""Validation Failed"",""documentation_url"":"
  168. + @"""http://developer.github.com/v3/repos/#create"",""errors"":[{""resource"":""Repository"","
  169. + @"""code"":""custom"",""field"":""name"",""message"":""name already exists on this account""}]}");
  170. var connection = Substitute.For<IApiConnection>();
  171. connection.Connection.BaseAddress.Returns(new Uri("https://example.com"));
  172. connection.Post<Repository>(Args.Uri, newRepository)
  173. .Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
  174. var client = new RepositoriesClient(connection);
  175. var exception = await Assert.ThrowsAsync<RepositoryExistsException>(
  176. () => client.Create("illuminati", newRepository));
  177. Assert.Equal("aName", exception.RepositoryName);
  178. Assert.Equal(new Uri("https://example.com/illuminati/aName"), exception.ExistingRepositoryWebUrl);
  179. }
  180. }
  181. public class TheDeleteMethod
  182. {
  183. [Fact]
  184. public async Task RequestsCorrectUrl()
  185. {
  186. var connection = Substitute.For<IApiConnection>();
  187. var client = new RepositoriesClient(connection);
  188. await client.Delete("owner", "name");
  189. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name"));
  190. }
  191. [Fact]
  192. public async Task RequestsCorrectUrlWithRepositoryId()
  193. {
  194. var connection = Substitute.For<IApiConnection>();
  195. var client = new RepositoriesClient(connection);
  196. await client.Delete(1);
  197. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == "repositories/1"));
  198. }
  199. [Fact]
  200. public async Task EnsuresNonNullArguments()
  201. {
  202. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  203. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Delete(null, "name"));
  204. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Delete("owner", null));
  205. await Assert.ThrowsAsync<ArgumentException>(() => client.Delete("", "name"));
  206. await Assert.ThrowsAsync<ArgumentException>(() => client.Delete("owner", ""));
  207. }
  208. }
  209. public class TheGetMethod
  210. {
  211. [Fact]
  212. public async Task RequestsCorrectUrl()
  213. {
  214. var connection = Substitute.For<IApiConnection>();
  215. var client = new RepositoriesClient(connection);
  216. await client.Get("owner", "name");
  217. connection.Received().Get<Repository>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name"));
  218. }
  219. [Fact]
  220. public async Task RequestsCorrectUrlWithRepositoryId()
  221. {
  222. var connection = Substitute.For<IApiConnection>();
  223. var client = new RepositoriesClient(connection);
  224. await client.Get(1);
  225. connection.Received().Get<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories/1"));
  226. }
  227. [Fact]
  228. public async Task EnsuresNonNullArguments()
  229. {
  230. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  231. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get(null, "name"));
  232. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get("owner", null));
  233. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("", "name"));
  234. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("owner", ""));
  235. }
  236. }
  237. public class TheGetAllPublicMethod
  238. {
  239. [Fact]
  240. public async Task RequestsTheCorrectUrlAndReturnsRepositories()
  241. {
  242. var connection = Substitute.For<IApiConnection>();
  243. var client = new RepositoriesClient(connection);
  244. await client.GetAllPublic();
  245. connection.Received()
  246. .GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories"));
  247. }
  248. }
  249. public class TheGetAllPublicSinceMethod
  250. {
  251. [Fact]
  252. public async Task RequestsTheCorrectUrl()
  253. {
  254. var connection = Substitute.For<IApiConnection>();
  255. var client = new RepositoriesClient(connection);
  256. await client.GetAllPublic(new PublicRepositoryRequest(364));
  257. connection.Received()
  258. .GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories?since=364"));
  259. }
  260. [Fact]
  261. public async Task SendsTheCorrectParameter()
  262. {
  263. var connection = Substitute.For<IApiConnection>();
  264. var client = new RepositoriesClient(connection);
  265. await client.GetAllPublic(new PublicRepositoryRequest(364));
  266. connection.Received()
  267. .GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories?since=364"));
  268. }
  269. }
  270. public class TheGetAllForCurrentMethod
  271. {
  272. [Fact]
  273. public async Task RequestsTheCorrectUrlAndReturnsRepositories()
  274. {
  275. var connection = Substitute.For<IApiConnection>();
  276. var client = new RepositoriesClient(connection);
  277. await client.GetAllForCurrent();
  278. connection.Received()
  279. .GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "user/repos"), Args.ApiOptions);
  280. }
  281. [Fact]
  282. public async Task CanFilterByType()
  283. {
  284. var connection = Substitute.For<IApiConnection>();
  285. var client = new RepositoriesClient(connection);
  286. var request = new RepositoryRequest
  287. {
  288. Type = RepositoryType.All
  289. };
  290. await client.GetAllForCurrent(request);
  291. connection.Received()
  292. .GetAll<Repository>(
  293. Arg.Is<Uri>(u => u.ToString() == "user/repos"),
  294. Arg.Is<Dictionary<string, string>>(d => d["type"] == "all"),
  295. Args.ApiOptions);
  296. }
  297. [Fact]
  298. public async Task CanFilterBySort()
  299. {
  300. var connection = Substitute.For<IApiConnection>();
  301. var client = new RepositoriesClient(connection);
  302. var request = new RepositoryRequest
  303. {
  304. Type = RepositoryType.Private,
  305. Sort = RepositorySort.FullName
  306. };
  307. await client.GetAllForCurrent(request);
  308. connection.Received()
  309. .GetAll<Repository>(
  310. Arg.Is<Uri>(u => u.ToString() == "user/repos"),
  311. Arg.Is<Dictionary<string, string>>(d =>
  312. d["type"] == "private" && d["sort"] == "full_name"),
  313. Args.ApiOptions);
  314. }
  315. [Fact]
  316. public async Task CanFilterBySortDirection()
  317. {
  318. var connection = Substitute.For<IApiConnection>();
  319. var client = new RepositoriesClient(connection);
  320. var request = new RepositoryRequest
  321. {
  322. Type = RepositoryType.Member,
  323. Sort = RepositorySort.Updated,
  324. Direction = SortDirection.Ascending
  325. };
  326. await client.GetAllForCurrent(request);
  327. connection.Received()
  328. .GetAll<Repository>(
  329. Arg.Is<Uri>(u => u.ToString() == "user/repos"),
  330. Arg.Is<Dictionary<string, string>>(d =>
  331. d["type"] == "member" && d["sort"] == "updated" && d["direction"] == "asc"),
  332. Args.ApiOptions);
  333. }
  334. [Fact]
  335. public async Task CanFilterByVisibility()
  336. {
  337. var connection = Substitute.For<IApiConnection>();
  338. var client = new RepositoriesClient(connection);
  339. var request = new RepositoryRequest
  340. {
  341. Visibility = RepositoryVisibility.Private
  342. };
  343. await client.GetAllForCurrent(request);
  344. connection.Received()
  345. .GetAll<Repository>(
  346. Arg.Is<Uri>(u => u.ToString() == "user/repos"),
  347. Arg.Is<Dictionary<string, string>>(d =>
  348. d["visibility"] == "private"),
  349. Args.ApiOptions);
  350. }
  351. [Fact]
  352. public async Task CanFilterByAffiliation()
  353. {
  354. var connection = Substitute.For<IApiConnection>();
  355. var client = new RepositoriesClient(connection);
  356. var request = new RepositoryRequest
  357. {
  358. Affiliation = RepositoryAffiliation.Owner,
  359. Sort = RepositorySort.FullName
  360. };
  361. await client.GetAllForCurrent(request);
  362. connection.Received()
  363. .GetAll<Repository>(
  364. Arg.Is<Uri>(u => u.ToString() == "user/repos"),
  365. Arg.Is<Dictionary<string, string>>(d =>
  366. d["affiliation"] == "owner" && d["sort"] == "full_name"),
  367. Args.ApiOptions);
  368. }
  369. }
  370. public class TheGetAllForUserMethod
  371. {
  372. [Fact]
  373. public async Task RequestsTheCorrectUrlAndReturnsRepositories()
  374. {
  375. var connection = Substitute.For<IApiConnection>();
  376. var client = new RepositoriesClient(connection);
  377. await client.GetAllForUser("username");
  378. connection.Received()
  379. .GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "users/username/repos"), Args.ApiOptions);
  380. }
  381. [Fact]
  382. public async Task EnsuresNonNullArguments()
  383. {
  384. var reposEndpoint = new RepositoriesClient(Substitute.For<IApiConnection>());
  385. await Assert.ThrowsAsync<ArgumentNullException>(() => reposEndpoint.GetAllForUser(null));
  386. await Assert.ThrowsAsync<ArgumentException>(() => reposEndpoint.GetAllForUser(""));
  387. await Assert.ThrowsAsync<ArgumentNullException>(() => reposEndpoint.GetAllForUser(null, ApiOptions.None));
  388. await Assert.ThrowsAsync<ArgumentNullException>(() => reposEndpoint.GetAllForUser("user", null));
  389. await Assert.ThrowsAsync<ArgumentException>(() => reposEndpoint.GetAllForUser("", ApiOptions.None));
  390. }
  391. }
  392. public class TheGetAllForOrgMethod
  393. {
  394. [Fact]
  395. public async Task RequestsTheCorrectUrl()
  396. {
  397. var connection = Substitute.For<IApiConnection>();
  398. var client = new RepositoriesClient(connection);
  399. await client.GetAllForOrg("orgname");
  400. connection.Received()
  401. .GetAll<Repository>(Arg.Is<Uri>(u => u.ToString() == "orgs/orgname/repos"), Args.ApiOptions);
  402. }
  403. [Fact]
  404. public async Task EnsuresNonNullArguments()
  405. {
  406. var reposEndpoint = new RepositoriesClient(Substitute.For<IApiConnection>());
  407. await Assert.ThrowsAsync<ArgumentNullException>(() => reposEndpoint.GetAllForOrg(null));
  408. await Assert.ThrowsAsync<ArgumentException>(() => reposEndpoint.GetAllForOrg(""));
  409. await Assert.ThrowsAsync<ArgumentNullException>(() => reposEndpoint.GetAllForOrg(null, ApiOptions.None));
  410. await Assert.ThrowsAsync<ArgumentNullException>(() => reposEndpoint.GetAllForOrg("org", null));
  411. await Assert.ThrowsAsync<ArgumentException>(() => reposEndpoint.GetAllForOrg("", ApiOptions.None));
  412. }
  413. }
  414. public class TheGetAllBranchesMethod
  415. {
  416. [Fact]
  417. public async Task RequestsTheCorrectUrl()
  418. {
  419. var connection = Substitute.For<IApiConnection>();
  420. var client = new RepositoriesClient(connection);
  421. await client.GetAllBranches("owner", "name");
  422. connection.Received()
  423. .GetAll<Branch>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/branches"), null, "application/vnd.github.loki-preview+json", Args.ApiOptions);
  424. }
  425. [Fact]
  426. public async Task RequestsTheCorrectUrlWithRepositoryId()
  427. {
  428. var connection = Substitute.For<IApiConnection>();
  429. var client = new RepositoriesClient(connection);
  430. await client.GetAllBranches(1);
  431. connection.Received()
  432. .GetAll<Branch>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches"), null, "application/vnd.github.loki-preview+json", Args.ApiOptions);
  433. }
  434. [Fact]
  435. public async Task RequestsTheCorrectUrlWithApiOptions()
  436. {
  437. var connection = Substitute.For<IApiConnection>();
  438. var client = new RepositoriesClient(connection);
  439. var options = new ApiOptions
  440. {
  441. PageCount = 1,
  442. StartPage = 1,
  443. PageSize = 1
  444. };
  445. await client.GetAllBranches("owner", "name", options);
  446. connection.Received()
  447. .GetAll<Branch>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/branches"), null, "application/vnd.github.loki-preview+json", options);
  448. }
  449. [Fact]
  450. public async Task RequestsTheCorrectUrlWithRepositoryIdWithApiOptions()
  451. {
  452. var connection = Substitute.For<IApiConnection>();
  453. var client = new RepositoriesClient(connection);
  454. var options = new ApiOptions
  455. {
  456. PageCount = 1,
  457. StartPage = 1,
  458. PageSize = 1
  459. };
  460. await client.GetAllBranches(1, options);
  461. connection.Received()
  462. .GetAll<Branch>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches"), null, "application/vnd.github.loki-preview+json", options);
  463. }
  464. [Fact]
  465. public async Task EnsuresNonNullArguments()
  466. {
  467. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  468. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllBranches(null, "name"));
  469. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllBranches("owner", null));
  470. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllBranches(null, "name", ApiOptions.None));
  471. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllBranches("owner", null, ApiOptions.None));
  472. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllBranches("owner", "name", null));
  473. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllBranches(1, null));
  474. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllBranches("", "name"));
  475. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllBranches("owner", ""));
  476. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllBranches("", "name", ApiOptions.None));
  477. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllBranches("owner", "", ApiOptions.None));
  478. }
  479. }
  480. public class TheGetAllContributorsMethod
  481. {
  482. [Fact]
  483. public async Task RequestsTheCorrectUrl()
  484. {
  485. var connection = Substitute.For<IApiConnection>();
  486. var client = new RepositoriesClient(connection);
  487. await client.GetAllContributors("owner", "name");
  488. connection.Received()
  489. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/contributors"), Arg.Any<IDictionary<string, string>>(), Args.ApiOptions);
  490. }
  491. [Fact]
  492. public async Task RequestsTheCorrectUrlWithRepositoryId()
  493. {
  494. var connection = Substitute.For<IApiConnection>();
  495. var client = new RepositoriesClient(connection);
  496. await client.GetAllContributors(1);
  497. connection.Received()
  498. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contributors"), Arg.Any<IDictionary<string, string>>(), Args.ApiOptions);
  499. }
  500. [Fact]
  501. public async Task RequestsTheCorrectUrlWithApiOptions()
  502. {
  503. var connection = Substitute.For<IApiConnection>();
  504. var client = new RepositoriesClient(connection);
  505. var options = new ApiOptions
  506. {
  507. PageCount = 1,
  508. StartPage = 1,
  509. PageSize = 1
  510. };
  511. await client.GetAllContributors("owner", "name", options);
  512. connection.Received()
  513. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/contributors"), Arg.Any<IDictionary<string, string>>(), options);
  514. }
  515. [Fact]
  516. public void RequestsTheCorrectUrlWithRepositoryIdWithApiOptions()
  517. {
  518. var connection = Substitute.For<IApiConnection>();
  519. var client = new RepositoriesClient(connection);
  520. var options = new ApiOptions
  521. {
  522. PageCount = 1,
  523. StartPage = 1,
  524. PageSize = 1
  525. };
  526. client.GetAllContributors(1, options);
  527. connection.Received()
  528. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contributors"), Arg.Any<IDictionary<string, string>>(), options);
  529. }
  530. [Fact]
  531. public async Task RequestsTheCorrectUrlIncludeAnonymous()
  532. {
  533. var connection = Substitute.For<IApiConnection>();
  534. var client = new RepositoriesClient(connection);
  535. await client.GetAllContributors("owner", "name", true);
  536. connection.Received()
  537. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/contributors"), Arg.Is<IDictionary<string, string>>(d => d["anon"] == "1"), Args.ApiOptions);
  538. }
  539. [Fact]
  540. public async Task RequestsTheCorrectUrlWithRepositoryIdIncludeAnonymous()
  541. {
  542. var connection = Substitute.For<IApiConnection>();
  543. var client = new RepositoriesClient(connection);
  544. await client.GetAllContributors(1, true);
  545. connection.Received()
  546. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contributors"), Arg.Is<IDictionary<string, string>>(d => d["anon"] == "1"), Args.ApiOptions);
  547. }
  548. [Fact]
  549. public async Task RequestsTheCorrectUrlWithApiOptionsIncludeAnonymous()
  550. {
  551. var connection = Substitute.For<IApiConnection>();
  552. var client = new RepositoriesClient(connection);
  553. var options = new ApiOptions
  554. {
  555. PageCount = 1,
  556. StartPage = 1,
  557. PageSize = 1
  558. };
  559. await client.GetAllContributors("owner", "name", true, options);
  560. connection.Received()
  561. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/contributors"), Arg.Is<IDictionary<string, string>>(d => d["anon"] == "1"), options);
  562. }
  563. [Fact]
  564. public async Task RequestsTheCorrectUrlWithRepositoryIdWithApiOptionsIncludeAnonymous()
  565. {
  566. var connection = Substitute.For<IApiConnection>();
  567. var client = new RepositoriesClient(connection);
  568. var options = new ApiOptions
  569. {
  570. PageCount = 1,
  571. StartPage = 1,
  572. PageSize = 1
  573. };
  574. await client.GetAllContributors(1, true, options);
  575. connection.Received()
  576. .GetAll<RepositoryContributor>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contributors"), Arg.Is<IDictionary<string, string>>(d => d["anon"] == "1"), options);
  577. }
  578. [Fact]
  579. public async Task EnsuresNonNullArguments()
  580. {
  581. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  582. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors(null, "repo"));
  583. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors("owner", null));
  584. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors(null, "repo", ApiOptions.None));
  585. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors("owner", null, ApiOptions.None));
  586. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors("owner", "repo", null));
  587. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors(null, "repo", false, ApiOptions.None));
  588. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors("owner", null, false, ApiOptions.None));
  589. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors("owner", "repo", false, null));
  590. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors(1, null));
  591. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContributors(1, false, null));
  592. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContributors("", "repo"));
  593. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContributors("owner", ""));
  594. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContributors("", "repo", ApiOptions.None));
  595. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContributors("owner", "", ApiOptions.None));
  596. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContributors("", "repo", false, ApiOptions.None));
  597. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContributors("owner", "", false, ApiOptions.None));
  598. }
  599. }
  600. public class TheGetAllLanguagesMethod
  601. {
  602. [Fact]
  603. public void RequestsTheCorrectUrl()
  604. {
  605. var connection = Substitute.For<IApiConnection>();
  606. var client = new RepositoriesClient(connection);
  607. client.GetAllLanguages("owner", "name");
  608. connection.Received()
  609. .Get<Dictionary<string, long>>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/languages"));
  610. }
  611. [Fact]
  612. public void RequestsTheCorrectUrlWithRepositoryId()
  613. {
  614. var connection = Substitute.For<IApiConnection>();
  615. var client = new RepositoriesClient(connection);
  616. client.GetAllLanguages(1);
  617. connection.Received()
  618. .Get<Dictionary<string, long>>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/languages"));
  619. }
  620. [Fact]
  621. public async Task EnsuresNonNullArguments()
  622. {
  623. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  624. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllLanguages(null, "repo"));
  625. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllLanguages("owner", null));
  626. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllLanguages("", "repo"));
  627. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllLanguages("owner", ""));
  628. }
  629. }
  630. public class TheGetAllTeamsMethod
  631. {
  632. [Fact]
  633. public async Task RequestsTheCorrectUrl()
  634. {
  635. var connection = Substitute.For<IApiConnection>();
  636. var client = new RepositoriesClient(connection);
  637. await client.GetAllTeams("owner", "name");
  638. connection.Received()
  639. .GetAll<Team>(
  640. Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/teams"),
  641. Args.ApiOptions);
  642. }
  643. [Fact]
  644. public async Task RequestsTheCorrectUrlWithRepositoryId()
  645. {
  646. var connection = Substitute.For<IApiConnection>();
  647. var client = new RepositoriesClient(connection);
  648. await client.GetAllTeams(1);
  649. connection.Received()
  650. .GetAll<Team>(
  651. Arg.Is<Uri>(u => u.ToString() == "repositories/1/teams"),
  652. Args.ApiOptions);
  653. }
  654. [Fact]
  655. public async Task RequestsTheCorrectUrlWithApiOptions()
  656. {
  657. var connection = Substitute.For<IApiConnection>();
  658. var client = new RepositoriesClient(connection);
  659. var options = new ApiOptions
  660. {
  661. PageCount = 1,
  662. StartPage = 1,
  663. PageSize = 1
  664. };
  665. await client.GetAllTeams("owner", "name", options);
  666. connection.Received()
  667. .GetAll<Team>(
  668. Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/teams"),
  669. options);
  670. }
  671. [Fact]
  672. public async Task RequestsTheCorrectUrlWithRepositoryIdWithApiOptions()
  673. {
  674. var connection = Substitute.For<IApiConnection>();
  675. var client = new RepositoriesClient(connection);
  676. var options = new ApiOptions
  677. {
  678. PageCount = 1,
  679. StartPage = 1,
  680. PageSize = 1
  681. };
  682. await client.GetAllTeams(1, options);
  683. connection.Received()
  684. .GetAll<Team>(
  685. Arg.Is<Uri>(u => u.ToString() == "repositories/1/teams"),
  686. options);
  687. }
  688. [Fact]
  689. public async Task EnsuresNonNullArguments()
  690. {
  691. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  692. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTeams(null, "repo"));
  693. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTeams("owner", null));
  694. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTeams(null, "repo", ApiOptions.None));
  695. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTeams("owner", null, ApiOptions.None));
  696. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTeams("owner", "repo", null));
  697. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTeams(1, null));
  698. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTeams("", "repo"));
  699. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTeams("owner", ""));
  700. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTeams("", "repo", ApiOptions.None));
  701. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTeams("owner", "", ApiOptions.None));
  702. }
  703. }
  704. public class TheGetAllTagsMethod
  705. {
  706. [Fact]
  707. public async Task RequestsTheCorrectUrl()
  708. {
  709. var connection = Substitute.For<IApiConnection>();
  710. var client = new RepositoriesClient(connection);
  711. await client.GetAllTags("owner", "name");
  712. connection.Received()
  713. .GetAll<RepositoryTag>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/tags"), Args.ApiOptions);
  714. }
  715. [Fact]
  716. public async Task RequestsTheCorrectUrlWithRepositoryId()
  717. {
  718. var connection = Substitute.For<IApiConnection>();
  719. var client = new RepositoriesClient(connection);
  720. await client.GetAllTags(1);
  721. connection.Received()
  722. .GetAll<RepositoryTag>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/tags"), Args.ApiOptions);
  723. }
  724. [Fact]
  725. public async Task RequestsTheCorrectUrlWithApiOptions()
  726. {
  727. var connection = Substitute.For<IApiConnection>();
  728. var client = new RepositoriesClient(connection);
  729. var options = new ApiOptions
  730. {
  731. PageCount = 1,
  732. StartPage = 1,
  733. PageSize = 1
  734. };
  735. await client.GetAllTags("owner", "name", options);
  736. connection.Received()
  737. .GetAll<RepositoryTag>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/tags"), options);
  738. }
  739. [Fact]
  740. public async Task RequestsTheCorrectUrlWithApiOptionsWithRepositoryId()
  741. {
  742. var connection = Substitute.For<IApiConnection>();
  743. var client = new RepositoriesClient(connection);
  744. var options = new ApiOptions
  745. {
  746. PageCount = 1,
  747. StartPage = 1,
  748. PageSize = 1
  749. };
  750. await client.GetAllTags(1, options);
  751. connection.Received()
  752. .GetAll<RepositoryTag>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/tags"), options);
  753. }
  754. [Fact]
  755. public async Task EnsuresNonNullArguments()
  756. {
  757. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  758. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTags(null, "repo"));
  759. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTags("owner", null));
  760. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTags(null, "repo", ApiOptions.None));
  761. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTags("owner", null, ApiOptions.None));
  762. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTags("owner", "repo", null));
  763. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllTags(1, null));
  764. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTags("", "repo"));
  765. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTags("owner", ""));
  766. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTags("", "repo", ApiOptions.None));
  767. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllTags("owner", "", ApiOptions.None));
  768. }
  769. }
  770. public class TheGetBranchMethod
  771. {
  772. [Fact]
  773. public async Task RequestsTheCorrectUrl()
  774. {
  775. var connection = Substitute.For<IApiConnection>();
  776. var client = new RepositoriesClient(connection);
  777. await client.GetBranch("owner", "repo", "branch");
  778. connection.Received()
  779. .Get<Branch>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch"), null, "application/vnd.github.loki-preview+json");
  780. }
  781. [Fact]
  782. public async Task RequestsTheCorrectUrlWithRepositoryId()
  783. {
  784. var connection = Substitute.For<IApiConnection>();
  785. var client = new RepositoriesClient(connection);
  786. await client.GetBranch(1, "branch");
  787. connection.Received()
  788. .Get<Branch>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch"), null, "application/vnd.github.loki-preview+json");
  789. }
  790. [Fact]
  791. public async Task EnsuresNonNullArguments()
  792. {
  793. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  794. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetBranch(null, "repo", "branch"));
  795. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetBranch("owner", null, "branch"));
  796. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetBranch("owner", "repo", null));
  797. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetBranch(1, null));
  798. await Assert.ThrowsAsync<ArgumentException>(() => client.GetBranch("", "repo", "branch"));
  799. await Assert.ThrowsAsync<ArgumentException>(() => client.GetBranch("owner", "", "branch"));
  800. await Assert.ThrowsAsync<ArgumentException>(() => client.GetBranch("owner", "repo", ""));
  801. }
  802. }
  803. public class TheEditMethod
  804. {
  805. [Fact]
  806. public void PatchesCorrectUrl()
  807. {
  808. var connection = Substitute.For<IApiConnection>();
  809. var client = new RepositoriesClient(connection);
  810. var update = new RepositoryUpdate();
  811. client.Edit("owner", "repo", update);
  812. connection.Received()
  813. .Patch<Repository>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo"), Arg.Any<RepositoryUpdate>());
  814. }
  815. [Fact]
  816. public void PatchesCorrectUrlWithRepositoryId()
  817. {
  818. var connection = Substitute.For<IApiConnection>();
  819. var client = new RepositoriesClient(connection);
  820. var update = new RepositoryUpdate();
  821. client.Edit(1, update);
  822. connection.Received()
  823. .Patch<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories/1"), Arg.Any<RepositoryUpdate>());
  824. }
  825. [Fact]
  826. public async Task EnsuresNonNullArguments()
  827. {
  828. var client = new RepositoriesClient(Substitute.For<IApiConnection>());
  829. var update = new RepositoryUpdate();
  830. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit(null, "repo", update));
  831. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit("owner", null, update));
  832. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit("owner", "repo", null));
  833. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Edit(1, null));
  834. await Assert.ThrowsAsync<ArgumentException>(() => client.Edit("", "repo", update));
  835. await Assert.ThrowsAsync<ArgumentException>(() => client.Edit("owner", "", update));
  836. }
  837. }
  838. public class TheCompareMethod
  839. {
  840. [Fact]
  841. public async Task EnsureNonNullArguments()
  842. {
  843. var client = new RepositoryCommitsClient(Substitute.For<IApiConnection>());
  844. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare(null, "repo", "base", "head"));
  845. await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("", "repo", "base", "head"));
  846. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", null, "base", "head"));
  847. await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("owner", "", "base", "head"));
  848. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", "repo", null, "head"));
  849. await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("owner", "repo", "", "head"));
  850. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Compare("owner", "repo", "base", null));
  851. await Assert.ThrowsAsync<ArgumentException>(() => client.Compare("owner", "repo", "base", ""));
  852. }
  853. [Fact]
  854. public void RequestsTheCorrectUrl()
  855. {
  856. var connection = Substitute.For<IApiConnection>();
  857. var client = new RepositoryCommitsClient(connection);
  858. client.Compare("owner", "repo", "base", "head");
  859. connection.Received()
  860. .Get<CompareResult>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/compare/base...head"));
  861. }
  862. [Fact]
  863. public void EncodesUrl()
  864. {
  865. var connection = Substitute.For<IApiConnection>();
  866. var client = new RepositoryCommitsClient(connection);
  867. client.Compare("owner", "repo", "base", "shiftkey/my-cool-branch");
  868. connection.Received()
  869. .Get<CompareResult>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/compare/base...shiftkey%2Fmy-cool-branch"));
  870. }
  871. }
  872. public class TheGetCommitMethod
  873. {
  874. [Fact]
  875. public async Task EnsureNonNullArguments()
  876. {
  877. var client = new RepositoryCommitsClient(Substitute.For<IApiConnection>());
  878. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get(null, "repo", "reference"));
  879. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("", "repo", "reference"));
  880. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get("owner", null, "reference"));
  881. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("owner", "", "reference"));
  882. await Assert.ThrowsAsync<ArgumentNullException>(() => client.Get("owner", "repo", null));
  883. await Assert.ThrowsAsync<ArgumentException>(() => client.Get("owner", "repo", ""));
  884. }
  885. [Fact]
  886. public void RequestsTheCorrectUrl()
  887. {
  888. var connection = Substitute.For<IApiConnection>();
  889. var client = new RepositoryCommitsClient(connection);
  890. client.Get("owner", "name", "reference");
  891. connection.Received()
  892. .Get<GitHubCommit>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name/commits/reference"));
  893. }
  894. }
  895. public class TheGetAllCommitsMethod
  896. {
  897. [Fact]
  898. public async Task EnsureNonNullArguments()
  899. {
  900. var client = new RepositoryCommitsClient(Substitute.For<IApiConnection>());
  901. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null, "repo"));
  902. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll("", "repo"));
  903. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll("owner", null));
  904. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAll("owner", ""));
  905. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll("owner", "repo", null, ApiOptions.None));
  906. }
  907. [Fact]