PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Octokit.Tests/Clients/RepositoryContentsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 705 lines | 555 code | 150 blank | 0 comment | 72 complexity | 60704517b2f2532210b234073c0b90d5 MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using NSubstitute;
  5. using Xunit;
  6. using System.Collections.Generic;
  7. namespace Octokit.Tests.Clients
  8. {
  9. public class RepositoryContentsClientTests
  10. {
  11. public class TheCtor
  12. {
  13. [Fact]
  14. public void EnsuresNonNullArguments()
  15. {
  16. Assert.Throws<ArgumentNullException>(
  17. () => new RepositoryContentsClient(null));
  18. }
  19. }
  20. public class TheGetReadmeMethod
  21. {
  22. [Fact]
  23. public async Task ReturnsReadme()
  24. {
  25. string encodedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello world"));
  26. var readmeInfo = new ReadmeResponse(
  27. encodedContent,
  28. "README.md",
  29. "https://github.example.com/readme",
  30. "https://github.example.com/readme.md",
  31. "base64");
  32. var connection = Substitute.For<IApiConnection>();
  33. connection.Get<ReadmeResponse>(Args.Uri, null).Returns(Task.FromResult(readmeInfo));
  34. connection.GetHtml(Args.Uri, null).Returns(Task.FromResult("<html>README</html>"));
  35. var contentsClient = new RepositoryContentsClient(connection);
  36. var readme = await contentsClient.GetReadme("fake", "repo");
  37. Assert.Equal("README.md", readme.Name);
  38. connection.Received().Get<ReadmeResponse>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/readme"),
  39. null);
  40. connection.DidNotReceive().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme"),
  41. null);
  42. var htmlReadme = await readme.GetHtmlContent();
  43. Assert.Equal("<html>README</html>", htmlReadme);
  44. connection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme.md"), null);
  45. }
  46. [Fact]
  47. public async Task ReturnsReadmeWithRepositoryId()
  48. {
  49. string encodedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello world"));
  50. var readmeInfo = new ReadmeResponse(
  51. encodedContent,
  52. "README.md",
  53. "https://github.example.com/readme",
  54. "https://github.example.com/readme.md",
  55. "base64");
  56. var connection = Substitute.For<IApiConnection>();
  57. connection.Get<ReadmeResponse>(Args.Uri, null).Returns(Task.FromResult(readmeInfo));
  58. connection.GetHtml(Args.Uri, null).Returns(Task.FromResult("<html>README</html>"));
  59. var contentsClient = new RepositoryContentsClient(connection);
  60. var readme = await contentsClient.GetReadme(1);
  61. Assert.Equal("README.md", readme.Name);
  62. connection.Received().Get<ReadmeResponse>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/readme"),
  63. null);
  64. connection.DidNotReceive().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme"),
  65. null);
  66. var htmlReadme = await readme.GetHtmlContent();
  67. Assert.Equal("<html>README</html>", htmlReadme);
  68. connection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "https://github.example.com/readme.md"), null);
  69. }
  70. [Fact]
  71. public async Task EnsuresNonNullArguments()
  72. {
  73. var connection = Substitute.For<IApiConnection>();
  74. var client = new RepositoryContentsClient(connection);
  75. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetReadme(null, "name"));
  76. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetReadme("owner", null));
  77. await Assert.ThrowsAsync<ArgumentException>(() => client.GetReadme("", "name"));
  78. await Assert.ThrowsAsync<ArgumentException>(() => client.GetReadme("owner", ""));
  79. }
  80. }
  81. public class TheGetReadmeHtmlMethod
  82. {
  83. [Fact]
  84. public async Task ReturnsReadmeHtml()
  85. {
  86. var connection = Substitute.For<IApiConnection>();
  87. connection.GetHtml(Args.Uri, null).Returns(Task.FromResult("<html>README</html>"));
  88. var contentsClient = new RepositoryContentsClient(connection);
  89. var readme = await contentsClient.GetReadmeHtml("fake", "repo");
  90. connection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/readme"), null);
  91. Assert.Equal("<html>README</html>", readme);
  92. }
  93. [Fact]
  94. public async Task ReturnsReadmeHtmlWithRepositoryId()
  95. {
  96. var connection = Substitute.For<IApiConnection>();
  97. connection.GetHtml(Args.Uri, null).Returns(Task.FromResult("<html>README</html>"));
  98. var contentsClient = new RepositoryContentsClient(connection);
  99. var readme = await contentsClient.GetReadmeHtml(1);
  100. connection.Received().GetHtml(Arg.Is<Uri>(u => u.ToString() == "repositories/1/readme"), null);
  101. Assert.Equal("<html>README</html>", readme);
  102. }
  103. [Fact]
  104. public async Task EnsuresNonNullArguments()
  105. {
  106. var connection = Substitute.For<IApiConnection>();
  107. var client = new RepositoryContentsClient(connection);
  108. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetReadmeHtml(null, "name"));
  109. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetReadmeHtml("owner", null));
  110. await Assert.ThrowsAsync<ArgumentException>(() => client.GetReadmeHtml("", "name"));
  111. await Assert.ThrowsAsync<ArgumentException>(() => client.GetReadmeHtml("owner", ""));
  112. }
  113. }
  114. public class TheGetContentsMethod
  115. {
  116. [Fact]
  117. public async Task ReturnsContents()
  118. {
  119. var result = new List<RepositoryContent> { new RepositoryContent() };
  120. var connection = Substitute.For<IApiConnection>();
  121. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  122. var contentsClient = new RepositoryContentsClient(connection);
  123. var contents = await contentsClient.GetAllContents("fake", "repo", "readme.md");
  124. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/readme.md"));
  125. Assert.Equal(1, contents.Count);
  126. }
  127. [Fact]
  128. public async Task ReturnsContentsWithRepositoryId()
  129. {
  130. var result = new List<RepositoryContent> { new RepositoryContent() };
  131. var connection = Substitute.For<IApiConnection>();
  132. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  133. var contentsClient = new RepositoryContentsClient(connection);
  134. var contents = await contentsClient.GetAllContents(1, "readme.md");
  135. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/readme.md"));
  136. Assert.Equal(1, contents.Count);
  137. }
  138. [Fact]
  139. public async Task ReturnsAllContents()
  140. {
  141. var result = new List<RepositoryContent> { new RepositoryContent() };
  142. var connection = Substitute.For<IApiConnection>();
  143. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  144. var contentsClient = new RepositoryContentsClient(connection);
  145. var contents = await contentsClient.GetAllContents("fake", "repo");
  146. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/"));
  147. Assert.Equal(1, contents.Count);
  148. }
  149. [Fact]
  150. public async Task ReturnsAllContentsWithRepositoryId()
  151. {
  152. var result = new List<RepositoryContent> { new RepositoryContent() };
  153. var connection = Substitute.For<IApiConnection>();
  154. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  155. var contentsClient = new RepositoryContentsClient(connection);
  156. var contents = await contentsClient.GetAllContents(1);
  157. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/"));
  158. Assert.Equal(1, contents.Count);
  159. }
  160. [Fact]
  161. public async Task EnsuresNonNullArguments()
  162. {
  163. var connection = Substitute.For<IApiConnection>();
  164. var client = new RepositoryContentsClient(connection);
  165. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContents(null, "name"));
  166. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContents("owner", null));
  167. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContents(null, "name", "path"));
  168. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContents("owner", null, "path"));
  169. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContents("owner", "name", null));
  170. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContents(1, null));
  171. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContents("", "name"));
  172. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContents("owner", ""));
  173. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContents("", "name", "path"));
  174. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContents("owner", "", "path"));
  175. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContents("owner", "name", ""));
  176. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContents(1, ""));
  177. }
  178. }
  179. public class TheGetContentsByRefMethod
  180. {
  181. [Fact]
  182. public async Task ReturnsContentsByRef()
  183. {
  184. var result = new List<RepositoryContent> { new RepositoryContent() };
  185. var connection = Substitute.For<IApiConnection>();
  186. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  187. var contentsClient = new RepositoryContentsClient(connection);
  188. var contents = await contentsClient.GetAllContentsByRef("fake", "repo", "readme.md", "master");
  189. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/readme.md?ref=master"));
  190. Assert.Equal(1, contents.Count);
  191. }
  192. [Fact]
  193. public async Task ReturnsContentsByRefWithRepositoryId()
  194. {
  195. var result = new List<RepositoryContent> { new RepositoryContent() };
  196. var connection = Substitute.For<IApiConnection>();
  197. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  198. var contentsClient = new RepositoryContentsClient(connection);
  199. var contents = await contentsClient.GetAllContentsByRef(1, "readme.md", "master");
  200. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/readme.md?ref=master"));
  201. Assert.Equal(1, contents.Count);
  202. }
  203. [Fact]
  204. public async Task ReturnsAllContentsByRef()
  205. {
  206. var result = new List<RepositoryContent> { new RepositoryContent() };
  207. var connection = Substitute.For<IApiConnection>();
  208. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  209. var contentsClient = new RepositoryContentsClient(connection);
  210. var contents = await contentsClient.GetAllContentsByRef("fake", "repo", "master");
  211. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/contents/?ref=master"));
  212. Assert.Equal(1, contents.Count);
  213. }
  214. [Fact]
  215. public async Task ReturnsAllContentsByRefWithRepositoryId()
  216. {
  217. var result = new List<RepositoryContent> { new RepositoryContent() };
  218. var connection = Substitute.For<IApiConnection>();
  219. connection.GetAll<RepositoryContent>(Args.Uri).Returns(Task.FromResult(result.AsReadOnly() as IReadOnlyList<RepositoryContent>));
  220. var contentsClient = new RepositoryContentsClient(connection);
  221. var contents = await contentsClient.GetAllContentsByRef(1, "master");
  222. connection.Received().GetAll<RepositoryContent>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/contents/?ref=master"));
  223. Assert.Equal(1, contents.Count);
  224. }
  225. [Fact]
  226. public async Task EnsuresNonNullArguments()
  227. {
  228. var connection = Substitute.For<IApiConnection>();
  229. var client = new RepositoryContentsClient(connection);
  230. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef(null, "name", "ref"));
  231. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef("owner", null, "ref"));
  232. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef("owner", "name", null));
  233. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef(null, "name", "path", "reference"));
  234. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef("owner", null, "path", "reference"));
  235. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef("owner", "name", null, "reference"));
  236. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef("owner", "name", "path", null));
  237. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef(1, null, "reference"));
  238. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef(1, "path", null));
  239. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAllContentsByRef(1, null));
  240. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef("", "name", "ref"));
  241. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef("owner", "", "ref"));
  242. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef("owner", "name", ""));
  243. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef("", "name", "path", "reference"));
  244. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef("owner", "", "path", "reference"));
  245. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef("owner", "name", "", "reference"));
  246. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef("owner", "name", "path", ""));
  247. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef(1, "", "reference"));
  248. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef(1, "path", ""));
  249. await Assert.ThrowsAsync<ArgumentException>(() => client.GetAllContentsByRef(1, ""));
  250. }
  251. }
  252. public class TheCreateFileMethod
  253. {
  254. [Fact]
  255. public async Task RequestsCorrectUrl()
  256. {
  257. var connection = Substitute.For<IApiConnection>();
  258. var client = new RepositoryContentsClient(connection);
  259. string expectedUri = "repos/org/repo/contents/path/to/file";
  260. await client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  261. connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  262. }
  263. [Fact]
  264. public async Task RequestsCorrectUrlWithRepositoryId()
  265. {
  266. var connection = Substitute.For<IApiConnection>();
  267. var client = new RepositoryContentsClient(connection);
  268. string expectedUri = "repositories/1/contents/path/to/file";
  269. await client.CreateFile(1, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  270. connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  271. }
  272. [Fact]
  273. public async Task PassesRequestObject()
  274. {
  275. var connection = Substitute.For<IApiConnection>();
  276. var client = new RepositoryContentsClient(connection);
  277. await client.CreateFile("org", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  278. connection.Received().Put<RepositoryContentChangeSet>(
  279. Arg.Any<Uri>(),
  280. Arg.Is<CreateFileRequest>(a =>
  281. a.Message == "message"
  282. && a.Content == "myfilecontents"
  283. && a.Branch == "mybranch"));
  284. }
  285. [Fact]
  286. public async Task PassesRequestObjectWithRepositoryId()
  287. {
  288. var connection = Substitute.For<IApiConnection>();
  289. var client = new RepositoryContentsClient(connection);
  290. await client.CreateFile(1, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch"));
  291. connection.Received().Put<RepositoryContentChangeSet>(
  292. Arg.Any<Uri>(),
  293. Arg.Is<CreateFileRequest>(a =>
  294. a.Message == "message"
  295. && a.Content == "myfilecontents"
  296. && a.Branch == "mybranch"));
  297. }
  298. [Fact]
  299. public async Task EnsuresNonNullArguments()
  300. {
  301. var connection = Substitute.For<IApiConnection>();
  302. var client = new RepositoryContentsClient(connection);
  303. await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile(null, "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  304. await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", null, "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  305. await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", "repo", null, new CreateFileRequest("message", "myfilecontents", "mybranch")));
  306. await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile("org", "repo", "path/to/file", null));
  307. await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile(1, null, new CreateFileRequest("message", "myfilecontents", "mybranch")));
  308. await Assert.ThrowsAsync<ArgumentNullException>(() => client.CreateFile(1, "path/to/file", null));
  309. await Assert.ThrowsAsync<ArgumentException>(() => client.CreateFile("", "repo", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  310. await Assert.ThrowsAsync<ArgumentException>(() => client.CreateFile("org", "", "path/to/file", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  311. await Assert.ThrowsAsync<ArgumentException>(() => client.CreateFile("org", "repo", "", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  312. await Assert.ThrowsAsync<ArgumentException>(() => client.CreateFile(1, "", new CreateFileRequest("message", "myfilecontents", "mybranch")));
  313. }
  314. }
  315. public class TheDeleteFileMethod
  316. {
  317. [Fact]
  318. public async Task RequestsCorrectUrl()
  319. {
  320. var connection = Substitute.For<IApiConnection>();
  321. var client = new RepositoryContentsClient(connection);
  322. string expectedUri = "repos/org/repo/contents/path/to/file";
  323. await client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  324. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  325. }
  326. [Fact]
  327. public async Task RequestsCorrectUrlWithRepositoryId()
  328. {
  329. var connection = Substitute.For<IApiConnection>();
  330. var client = new RepositoryContentsClient(connection);
  331. string expectedUri = "repositories/1/contents/path/to/file";
  332. await client.DeleteFile(1, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  333. connection.Received().Delete(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  334. }
  335. [Fact]
  336. public async Task PassesRequestObject()
  337. {
  338. var connection = Substitute.For<IApiConnection>();
  339. var client = new RepositoryContentsClient(connection);
  340. await client.DeleteFile("org", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  341. connection.Received().Delete(
  342. Arg.Any<Uri>(),
  343. Arg.Is<DeleteFileRequest>(a =>
  344. a.Message == "message"
  345. && a.Sha == "1234abc"
  346. && a.Branch == "mybranch"));
  347. }
  348. [Fact]
  349. public async Task PassesRequestObjectWithRepositoryId()
  350. {
  351. var connection = Substitute.For<IApiConnection>();
  352. var client = new RepositoryContentsClient(connection);
  353. await client.DeleteFile(1, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch"));
  354. connection.Received().Delete(
  355. Arg.Any<Uri>(),
  356. Arg.Is<DeleteFileRequest>(a =>
  357. a.Message == "message"
  358. && a.Sha == "1234abc"
  359. && a.Branch == "mybranch"));
  360. }
  361. [Fact]
  362. public async Task EnsuresNonNullArguments()
  363. {
  364. var connection = Substitute.For<IApiConnection>();
  365. var client = new RepositoryContentsClient(connection);
  366. await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile(null, "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  367. await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", null, "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  368. await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", "repo", null, new DeleteFileRequest("message", "1234abc", "mybranch")));
  369. await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile("org", "repo", "path/to/file", null));
  370. await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile(1, null, new DeleteFileRequest("message", "1234abc", "mybranch")));
  371. await Assert.ThrowsAsync<ArgumentNullException>(() => client.DeleteFile(1, "path/to/file", null));
  372. await Assert.ThrowsAsync<ArgumentException>(() => client.DeleteFile("", "repo", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  373. await Assert.ThrowsAsync<ArgumentException>(() => client.DeleteFile("org", "", "path/to/file", new DeleteFileRequest("message", "1234abc", "mybranch")));
  374. await Assert.ThrowsAsync<ArgumentException>(() => client.DeleteFile("org", "repo", "", new DeleteFileRequest("message", "1234abc", "mybranch")));
  375. await Assert.ThrowsAsync<ArgumentException>(() => client.DeleteFile(1, "", new DeleteFileRequest("message", "1234abc", "mybranch")));
  376. }
  377. }
  378. public class TheUpdateFileMethod
  379. {
  380. [Fact]
  381. public async Task RequestsCorrectUrl()
  382. {
  383. var connection = Substitute.For<IApiConnection>();
  384. var client = new RepositoryContentsClient(connection);
  385. string expectedUri = "repos/org/repo/contents/path/to/file";
  386. await client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  387. connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  388. }
  389. [Fact]
  390. public async Task RequestsCorrectUrlWithRepositoryId()
  391. {
  392. var connection = Substitute.For<IApiConnection>();
  393. var client = new RepositoryContentsClient(connection);
  394. string expectedUri = "repositories/1/contents/path/to/file";
  395. await client.UpdateFile(1, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  396. connection.Received().Put<RepositoryContentChangeSet>(Arg.Is<Uri>(u => u.ToString() == expectedUri), Arg.Any<object>());
  397. }
  398. [Fact]
  399. public async Task PassesRequestObject()
  400. {
  401. var connection = Substitute.For<IApiConnection>();
  402. var client = new RepositoryContentsClient(connection);
  403. await client.UpdateFile("org", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  404. connection.Received().Put<RepositoryContentChangeSet>(
  405. Arg.Any<Uri>(),
  406. Arg.Is<UpdateFileRequest>(a =>
  407. a.Message == "message"
  408. && a.Content == "myfilecontents"
  409. && a.Sha == "1234abc"
  410. && a.Branch == "mybranch"));
  411. }
  412. [Fact]
  413. public async Task PassesRequestObjectWithRepositoriesId()
  414. {
  415. var connection = Substitute.For<IApiConnection>();
  416. var client = new RepositoryContentsClient(connection);
  417. await client.UpdateFile(1, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch"));
  418. connection.Received().Put<RepositoryContentChangeSet>(
  419. Arg.Any<Uri>(),
  420. Arg.Is<UpdateFileRequest>(a =>
  421. a.Message == "message"
  422. && a.Content == "myfilecontents"
  423. && a.Sha == "1234abc"
  424. && a.Branch == "mybranch"));
  425. }
  426. [Fact]
  427. public async Task EnsuresNonNullArguments()
  428. {
  429. var connection = Substitute.For<IApiConnection>();
  430. var client = new RepositoryContentsClient(connection);
  431. await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile(null, "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  432. await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", null, "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  433. await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", "repo", null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  434. await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile("org", "repo", "path/to/file", null));
  435. await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile(1, null, new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  436. await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateFile(1, "path/to/file", null));
  437. await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateFile("", "repo", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  438. await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateFile("org", "", "path/to/file", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  439. await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateFile("org", "repo", "", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  440. await Assert.ThrowsAsync<ArgumentException>(() => client.UpdateFile(1, "", new UpdateFileRequest("message", "myfilecontents", "1234abc", "mybranch")));
  441. }
  442. }
  443. public class TheGetArchiveMethod
  444. {
  445. [Fact]
  446. public async Task RequestsCorrectUrl1()
  447. {
  448. var connection = Substitute.For<IApiConnection>();
  449. var client = new RepositoryContentsClient(connection);
  450. await client.GetArchive("org", "repo");
  451. const string expectedUri = "repos/org/repo/tarball/";
  452. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  453. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  454. }
  455. [Fact]
  456. public async Task RequestsCorrectUrl1WithRepositoryId()
  457. {
  458. var connection = Substitute.For<IApiConnection>();
  459. var client = new RepositoryContentsClient(connection);
  460. await client.GetArchive(1);
  461. const string expectedUri = "repositories/1/tarball/";
  462. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  463. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  464. }
  465. [Fact]
  466. public async Task RequestsCorrectUrl2()
  467. {
  468. var connection = Substitute.For<IApiConnection>();
  469. var client = new RepositoryContentsClient(connection);
  470. await client.GetArchive("org", "repo", ArchiveFormat.Zipball);
  471. const string expectedUri = "repos/org/repo/zipball/";
  472. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  473. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  474. }
  475. [Fact]
  476. public async Task RequestsCorrectUrl2WithRepositoryId()
  477. {
  478. var connection = Substitute.For<IApiConnection>();
  479. var client = new RepositoryContentsClient(connection);
  480. await client.GetArchive(1, ArchiveFormat.Zipball);
  481. const string expectedUri = "repositories/1/zipball/";
  482. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  483. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  484. }
  485. [Fact]
  486. public async Task RequestsCorrectUrl3()
  487. {
  488. var connection = Substitute.For<IApiConnection>();
  489. var client = new RepositoryContentsClient(connection);
  490. await client.GetArchive("org", "repo", ArchiveFormat.Zipball, "ref");
  491. const string expectedUri = "repos/org/repo/zipball/ref";
  492. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  493. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  494. }
  495. [Fact]
  496. public async Task RequestsCorrectUrl3WithRepositoryId()
  497. {
  498. var connection = Substitute.For<IApiConnection>();
  499. var client = new RepositoryContentsClient(connection);
  500. await client.GetArchive(1, ArchiveFormat.Zipball, "ref");
  501. const string expectedUri = "repositories/1/zipball/ref";
  502. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  503. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  504. }
  505. [Fact]
  506. public async Task RequestsCorrectUrl4()
  507. {
  508. var connection = Substitute.For<IApiConnection>();
  509. var client = new RepositoryContentsClient(connection);
  510. await client.GetArchive("org", "repo", ArchiveFormat.Zipball, "ref", TimeSpan.FromMinutes(60));
  511. const string expectedUri = "repos/org/repo/zipball/ref";
  512. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  513. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  514. }
  515. [Fact]
  516. public async Task RequestsCorrectUrl4WithRepositoryId()
  517. {
  518. var connection = Substitute.For<IApiConnection>();
  519. var client = new RepositoryContentsClient(connection);
  520. await client.GetArchive(1, ArchiveFormat.Zipball, "ref", TimeSpan.FromMinutes(60));
  521. const string expectedUri = "repositories/1/zipball/ref";
  522. var expectedTimeSpan = TimeSpan.FromMinutes(60);
  523. connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
  524. }
  525. [Fact]
  526. public async Task EnsuresNonNullArguments()
  527. {
  528. var connection = Substitute.For<IApiConnection>();
  529. var client = new RepositoryContentsClient(connection);
  530. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive(null, "repo"));
  531. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive("org", null));
  532. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive(null, "repo", ArchiveFormat.Tarball));
  533. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive("org", null, ArchiveFormat.Tarball));
  534. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive(null, "repo", ArchiveFormat.Tarball, "ref"));
  535. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive("org", null, ArchiveFormat.Tarball, "ref"));
  536. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive("org", "repo", ArchiveFormat.Tarball, null));
  537. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive(null, "repo", ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  538. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive("org", null, ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  539. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive("org", "repo", ArchiveFormat.Tarball, null, TimeSpan.MaxValue));
  540. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive(1, ArchiveFormat.Tarball, null));
  541. await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetArchive(1, ArchiveFormat.Tarball, null, TimeSpan.MaxValue));
  542. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("", "repo"));
  543. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("org", ""));
  544. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("", "repo", ArchiveFormat.Tarball));
  545. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("org", "", ArchiveFormat.Tarball));
  546. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("", "repo", ArchiveFormat.Tarball, "ref"));
  547. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("org", "", ArchiveFormat.Tarball, "ref"));
  548. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("", "repo", ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  549. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("org", "", ArchiveFormat.Tarball, "ref", TimeSpan.MaxValue));
  550. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive("org", "repo", ArchiveFormat.Tarball, "ref", TimeSpan.Zero));
  551. await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive(1, ArchiveFormat.Tarball, "ref", TimeSpan.Zero));
  552. }
  553. }
  554. }
  555. }