PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Octokit.Tests/Reactive/ObservableRepoCollaboratorsClientTests.cs

https://gitlab.com/WoomyNightClub/GitHub-API-.NET
C# | 488 lines | 397 code | 85 blank | 6 comment | 16 complexity | 22757a59ea598df4435f2a4319856639 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using System.Threading.Tasks;
  5. using NSubstitute;
  6. using Octokit.Reactive;
  7. using Octokit.Tests.Helpers;
  8. using Xunit;
  9. namespace Octokit.Tests.Reactive
  10. {
  11. public class ObservableRepoCollaboratorsClientTests
  12. {
  13. public class TheCtor
  14. {
  15. [Fact]
  16. public void EnsuresNonNullArguments()
  17. {
  18. Assert.Throws<ArgumentNullException>(() => new ObservableRepoCollaboratorsClient(null));
  19. }
  20. }
  21. public class TheGetAllMethod
  22. {
  23. private readonly IGitHubClient _githubClient;
  24. private readonly IObservableRepoCollaboratorsClient _client;
  25. private const string owner = "owner";
  26. private const string name = "name";
  27. private const int repositoryId = 1;
  28. public TheGetAllMethod()
  29. {
  30. _githubClient = Substitute.For<IGitHubClient>();
  31. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  32. }
  33. [Fact]
  34. public void EnsuresNonNullArguments()
  35. {
  36. Assert.Throws<ArgumentNullException>(() => _client.GetAll(null, name));
  37. Assert.Throws<ArgumentNullException>(() => _client.GetAll(owner, null));
  38. Assert.Throws<ArgumentNullException>(() => _client.GetAll(owner, name, null));
  39. Assert.Throws<ArgumentNullException>(() => _client.GetAll(repositoryId, null));
  40. }
  41. [Fact]
  42. public void EnsuresNonEmptyArguments()
  43. {
  44. Assert.Throws<ArgumentException>(() => _client.GetAll("", name));
  45. Assert.Throws<ArgumentException>(() => _client.GetAll(owner, ""));
  46. }
  47. [Fact]
  48. public async Task EnsuresNonWhitespaceArguments()
  49. {
  50. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  51. async whitespace => await _client.GetAll(whitespace, name));
  52. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  53. async whitespace => await _client.GetAll(owner, whitespace));
  54. }
  55. [Fact]
  56. public void RequestsCorrectUrl()
  57. {
  58. var expectedUrl = string.Format("repos/{0}/{1}/collaborators", owner, name);
  59. _client.GetAll(owner, name);
  60. _githubClient.Connection.Received(1)
  61. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  62. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 0),
  63. Arg.Any<string>());
  64. }
  65. [Fact]
  66. public void RequestsCorrectUrlWithRepositoryId()
  67. {
  68. var expectedUrl = string.Format("repositories/{0}/collaborators", repositoryId);
  69. _client.GetAll(repositoryId);
  70. _githubClient.Connection.Received(1)
  71. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  72. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 0),
  73. Arg.Any<string>());
  74. }
  75. [Fact]
  76. public void RequestsCorrectUrlWithApiOptions()
  77. {
  78. var expectedUrl = string.Format("repos/{0}/{1}/collaborators", owner, name);
  79. // all properties are setted => only 2 options (StartPage, PageSize) in dictionary
  80. var options = new ApiOptions
  81. {
  82. StartPage = 1,
  83. PageCount = 1,
  84. PageSize = 1
  85. };
  86. _client.GetAll(owner, name, options);
  87. _githubClient.Connection.Received(1)
  88. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  89. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 2),
  90. null);
  91. // StartPage is setted => only 1 option (StartPage) in dictionary
  92. options = new ApiOptions
  93. {
  94. StartPage = 1
  95. };
  96. _client.GetAll(owner, name, options);
  97. _githubClient.Connection.Received(1)
  98. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  99. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 1),
  100. null);
  101. // PageCount is setted => none of options in dictionary
  102. options = new ApiOptions
  103. {
  104. PageCount = 1
  105. };
  106. _client.GetAll(owner, name, options);
  107. _githubClient.Connection.Received(1)
  108. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  109. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 0),
  110. null);
  111. }
  112. [Fact]
  113. public void RequestsCorrectUrlWithApiOptionsAndRepositoryId()
  114. {
  115. var expectedUrl = string.Format("repositories/{0}/collaborators", repositoryId);
  116. // all properties are setted => only 2 options (StartPage, PageSize) in dictionary
  117. var options = new ApiOptions
  118. {
  119. StartPage = 1,
  120. PageCount = 1,
  121. PageSize = 1
  122. };
  123. _client.GetAll(repositoryId, options);
  124. _githubClient.Connection.Received(1)
  125. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  126. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 2),
  127. null);
  128. // StartPage is setted => only 1 option (StartPage) in dictionary
  129. options = new ApiOptions
  130. {
  131. StartPage = 1
  132. };
  133. _client.GetAll(repositoryId, options);
  134. _githubClient.Connection.Received(1)
  135. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  136. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 1),
  137. null);
  138. // PageCount is setted => none of options in dictionary
  139. options = new ApiOptions
  140. {
  141. PageCount = 1
  142. };
  143. _client.GetAll(repositoryId, options);
  144. _githubClient.Connection.Received(1)
  145. .Get<List<User>>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
  146. Arg.Is<IDictionary<string, string>>(dictionary => dictionary.Count == 0),
  147. null);
  148. }
  149. }
  150. public class TheIsCollaboratorMethod
  151. {
  152. private readonly IGitHubClient _githubClient;
  153. private IObservableRepoCollaboratorsClient _client;
  154. public TheIsCollaboratorMethod()
  155. {
  156. _githubClient = Substitute.For<IGitHubClient>();
  157. }
  158. private void SetupWithoutNonReactiveClient()
  159. {
  160. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  161. }
  162. private void SetupWithNonReactiveClient()
  163. {
  164. var deploymentsClient = new RepoCollaboratorsClient(Substitute.For<IApiConnection>());
  165. _githubClient.Repository.Collaborator.Returns(deploymentsClient);
  166. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  167. }
  168. [Fact]
  169. public void EnsuresNonNullArguments()
  170. {
  171. SetupWithNonReactiveClient();
  172. Assert.Throws<ArgumentNullException>(() => _client.IsCollaborator(null, "repo", "user"));
  173. Assert.Throws<ArgumentNullException>(() => _client.IsCollaborator("owner", null, "user"));
  174. Assert.Throws<ArgumentNullException>(() => _client.IsCollaborator("owner", "repo", null));
  175. Assert.Throws<ArgumentNullException>(() => _client.IsCollaborator(1, null));
  176. }
  177. [Fact]
  178. public void EnsuresNonEmptyArguments()
  179. {
  180. SetupWithNonReactiveClient();
  181. Assert.Throws<ArgumentException>(() => _client.IsCollaborator("", "repo", "user"));
  182. Assert.Throws<ArgumentException>(() => _client.IsCollaborator("owner", "", "user"));
  183. Assert.Throws<ArgumentException>(() => _client.IsCollaborator(1, ""));
  184. }
  185. [Fact]
  186. public async Task EnsuresNonWhitespaceArguments()
  187. {
  188. SetupWithNonReactiveClient();
  189. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  190. async whitespace => await _client.IsCollaborator(whitespace, "repo", "user"));
  191. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  192. async whitespace => await _client.IsCollaborator("owner", whitespace, "user"));
  193. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  194. async whitespace => await _client.IsCollaborator(1, whitespace));
  195. }
  196. [Fact]
  197. public void CallsCreateOnRegularDeploymentsClient()
  198. {
  199. SetupWithoutNonReactiveClient();
  200. _client.IsCollaborator(1, "user");
  201. _githubClient.Repository.Collaborator.Received(1).IsCollaborator(Arg.Is(1),
  202. Arg.Is("user"));
  203. }
  204. [Fact]
  205. public void CallsCreateOnRegularDeploymentsClientWithRepositoryId()
  206. {
  207. SetupWithoutNonReactiveClient();
  208. _client.IsCollaborator(1, "user");
  209. _githubClient.Repository.Collaborator.Received(1).IsCollaborator(Arg.Is(1),
  210. Arg.Is("user"));
  211. }
  212. }
  213. public class TheAddMethod
  214. {
  215. private readonly IGitHubClient _githubClient;
  216. private IObservableRepoCollaboratorsClient _client;
  217. public TheAddMethod()
  218. {
  219. _githubClient = Substitute.For<IGitHubClient>();
  220. }
  221. private void SetupWithoutNonReactiveClient()
  222. {
  223. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  224. }
  225. private void SetupWithNonReactiveClient()
  226. {
  227. var collaboratorsClient = new RepoCollaboratorsClient(Substitute.For<IApiConnection>());
  228. _githubClient.Repository.Collaborator.Returns(collaboratorsClient);
  229. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  230. }
  231. [Fact]
  232. public void EnsuresNonNullArguments()
  233. {
  234. SetupWithNonReactiveClient();
  235. Assert.Throws<ArgumentNullException>(() => _client.Add(null, "repo", "user"));
  236. Assert.Throws<ArgumentNullException>(() => _client.Add("owner", null, "user"));
  237. Assert.Throws<ArgumentNullException>(() => _client.Add("owner", "repo", null));
  238. Assert.Throws<ArgumentNullException>(() => _client.Add(1, null));
  239. }
  240. [Fact]
  241. public void EnsuresNonEmptyArguments()
  242. {
  243. SetupWithNonReactiveClient();
  244. Assert.Throws<ArgumentException>(() => _client.Add("", "repo", "user"));
  245. Assert.Throws<ArgumentException>(() => _client.Add("owner", "", "user"));
  246. Assert.Throws<ArgumentException>(() => _client.Add(1, ""));
  247. }
  248. [Fact]
  249. public async Task EnsuresNonWhitespaceArguments()
  250. {
  251. SetupWithNonReactiveClient();
  252. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  253. async whitespace => await _client.Add(whitespace, "repo", "user"));
  254. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  255. async whitespace => await _client.Add("owner", whitespace, "user"));
  256. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  257. async whitespace => await _client.Add(1, whitespace));
  258. }
  259. [Fact]
  260. public void CallsCreateOnRegularDeploymentsClient()
  261. {
  262. SetupWithoutNonReactiveClient();
  263. _client.Add("owner", "repo", "user");
  264. _githubClient.Repository.Collaborator.Received(1).Add(Arg.Is("owner"),
  265. Arg.Is("repo"),
  266. Arg.Is("user"));
  267. }
  268. [Fact]
  269. public void CallsCreateOnRegularDeploymentsClientWithRepositoryId()
  270. {
  271. SetupWithoutNonReactiveClient();
  272. _client.Add(1, "user");
  273. _githubClient.Repository.Collaborator.Received(1).Add(Arg.Is(1),
  274. Arg.Is("user"));
  275. }
  276. }
  277. public class TheInviteMethod
  278. {
  279. private readonly IGitHubClient _githubClient;
  280. private IObservableRepoCollaboratorsClient _client;
  281. public TheInviteMethod()
  282. {
  283. _githubClient = Substitute.For<IGitHubClient>();
  284. }
  285. private void SetupWithoutNonReactiveClient()
  286. {
  287. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  288. }
  289. private void SetupWithNonReactiveClient()
  290. {
  291. var collaboratorsClient = new RepoCollaboratorsClient(Substitute.For<IApiConnection>());
  292. _githubClient.Repository.Collaborator.Returns(collaboratorsClient);
  293. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  294. }
  295. [Fact]
  296. public void EnsuresNonNullArguments()
  297. {
  298. SetupWithNonReactiveClient();
  299. var permission = new CollaboratorRequest(Permission.Push);
  300. Assert.Throws<ArgumentNullException>(() => _client.Invite(null, "repo", "user", permission));
  301. Assert.Throws<ArgumentNullException>(() => _client.Invite("owner", null, "user", permission));
  302. Assert.Throws<ArgumentNullException>(() => _client.Invite("owner", "repo", null, permission));
  303. Assert.Throws<ArgumentNullException>(() => _client.Invite("owner", "repo", "user", null));
  304. }
  305. [Fact]
  306. public void EnsuresNonEmptyArguments()
  307. {
  308. SetupWithNonReactiveClient();
  309. var permission = new CollaboratorRequest(Permission.Push);
  310. Assert.Throws<ArgumentException>(() => _client.Invite("", "repo", "user", permission));
  311. Assert.Throws<ArgumentException>(() => _client.Invite("owner", "", "user", permission));
  312. Assert.Throws<ArgumentException>(() => _client.Invite("owner", "repo", "", permission));
  313. }
  314. [Fact]
  315. public async Task EnsuresNonWhitespaceArguments()
  316. {
  317. SetupWithNonReactiveClient();
  318. var permission = new CollaboratorRequest(Permission.Push);
  319. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  320. async whitespace => await _client.Invite(whitespace, "repo", "user", permission));
  321. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  322. async whitespace => await _client.Invite("owner", whitespace, "user", permission));
  323. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  324. async whitespace => await _client.Invite("owner", "repo", whitespace, permission));
  325. }
  326. [Fact]
  327. public void CallsInviteOnRegularDeploymentsClient()
  328. {
  329. SetupWithoutNonReactiveClient();
  330. var permission = new CollaboratorRequest(Permission.Push);
  331. _client.Invite("owner", "repo", "user", permission);
  332. _githubClient.Repository.Collaborator.Received(1).Invite(Arg.Is("owner"),
  333. Arg.Is("repo"),
  334. Arg.Is("user"),
  335. Arg.Is(permission));
  336. }
  337. }
  338. public class TheDeleteMethod
  339. {
  340. private readonly IGitHubClient _githubClient;
  341. private IObservableRepoCollaboratorsClient _client;
  342. public TheDeleteMethod()
  343. {
  344. _githubClient = Substitute.For<IGitHubClient>();
  345. }
  346. private void SetupWithoutNonReactiveClient()
  347. {
  348. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  349. }
  350. private void SetupWithNonReactiveClient()
  351. {
  352. var deploymentsClient = new RepoCollaboratorsClient(Substitute.For<IApiConnection>());
  353. _githubClient.Repository.Collaborator.Returns(deploymentsClient);
  354. _client = new ObservableRepoCollaboratorsClient(_githubClient);
  355. }
  356. [Fact]
  357. public void EnsuresNonNullArguments()
  358. {
  359. SetupWithNonReactiveClient();
  360. Assert.Throws<ArgumentNullException>(() => _client.Delete(null, "repo", "user"));
  361. Assert.Throws<ArgumentNullException>(() => _client.Delete("owner", null, "user"));
  362. Assert.Throws<ArgumentNullException>(() => _client.Delete("owner", "repo", null));
  363. Assert.Throws<ArgumentNullException>(() => _client.Delete(1, null));
  364. }
  365. [Fact]
  366. public void EnsuresNonEmptyArguments()
  367. {
  368. SetupWithNonReactiveClient();
  369. Assert.Throws<ArgumentException>(() => _client.Delete("", "repo", "user"));
  370. Assert.Throws<ArgumentException>(() => _client.Delete("owner", "", "user"));
  371. Assert.Throws<ArgumentException>(() => _client.Delete(1, ""));
  372. }
  373. [Fact]
  374. public async Task EnsuresNonWhitespaceArguments()
  375. {
  376. SetupWithNonReactiveClient();
  377. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  378. async whitespace => await _client.Delete(whitespace, "repo", "user"));
  379. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  380. async whitespace => await _client.Delete("owner", whitespace, "user"));
  381. await AssertEx.ThrowsWhenGivenWhitespaceArgument(
  382. async whitespace => await _client.Delete(1, whitespace));
  383. }
  384. [Fact]
  385. public void CallsCreateOnRegularDeploymentsClient()
  386. {
  387. SetupWithoutNonReactiveClient();
  388. _client.Delete("owner", "repo", "user");
  389. _githubClient.Repository.Collaborator.Received(1).Delete(Arg.Is("owner"),
  390. Arg.Is("repo"),
  391. Arg.Is("user"));
  392. }
  393. [Fact]
  394. public void CallsCreateOnRegularDeploymentsClientWithRepositoryId()
  395. {
  396. SetupWithoutNonReactiveClient();
  397. _client.Delete(1, "user");
  398. _githubClient.Repository.Collaborator.Received(1).Delete(Arg.Is(1),
  399. Arg.Is("user"));
  400. }
  401. }
  402. }
  403. }