|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Net.Http; |
| 4 | +using FakeItEasy; |
| 5 | +using FluentAssertions; |
| 6 | +using GitLabApiClient.Internal.Http; |
| 7 | +using GitLabApiClient.Internal.Http.Serialization; |
| 8 | +using GitLabApiClient.Internal.Queries; |
| 9 | +using GitLabApiClient.Test.TestUtilities; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace GitLabApiClient.Test |
| 13 | +{ |
| 14 | + [ExcludeFromCodeCoverage] |
| 15 | + public class CommitsClientMockedTest |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public async void GetCommitBySha() |
| 19 | + { |
| 20 | + string gitlabServer = "http://fake-gitlab.com/"; |
| 21 | + string projectId = "id"; |
| 22 | + string sha = "6104942438c14ec7bd21c6cd5bd995272b3faff6"; |
| 23 | + string url = $"/projects/{projectId}/repository/commits/{sha}"; |
| 24 | + |
| 25 | + var handler = A.Fake<MockHandler>(opt => opt.CallsBaseMethods()); |
| 26 | + A.CallTo(() => handler.SendAsync(HttpMethod.Get, url)) |
| 27 | + .ReturnsLazily(() => HttpResponseMessageProducer.Success( |
| 28 | + $"{{\"id\": \"{sha}\", }}")); |
| 29 | + using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) }) |
| 30 | + { |
| 31 | + var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client); |
| 32 | + var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder()); |
| 33 | + |
| 34 | + var commitFromClient = await commitsClient.GetAsync(projectId, sha); |
| 35 | + commitFromClient.Id.Should().BeEquivalentTo(sha); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async void GetCommitsByRefName() |
| 41 | + { |
| 42 | + string gitlabServer = "http://fake-gitlab.com/"; |
| 43 | + string projectId = "id"; |
| 44 | + string refName = "6104942438c14ec7bd21c6cd5bd995272b3faff6"; |
| 45 | + string url = $"/projects/id/repository/commits?ref_name={refName}&per_page=100&page=1"; |
| 46 | + |
| 47 | + var handler = A.Fake<MockHandler>(opt => opt.CallsBaseMethods()); |
| 48 | + A.CallTo(() => handler.SendAsync(HttpMethod.Get, url)) |
| 49 | + .ReturnsLazily(() => HttpResponseMessageProducer.Success( |
| 50 | + $"[ {{ \"id\": \"id1\",}},\n {{\"id\": \"id2\",}}]")); |
| 51 | + using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) }) |
| 52 | + { |
| 53 | + var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client); |
| 54 | + var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder()); |
| 55 | + |
| 56 | + var commitsFromClient = await commitsClient.GetAsync(projectId, o => o.RefName = refName); |
| 57 | + commitsFromClient[0].Id.Should().BeEquivalentTo("id1"); |
| 58 | + commitsFromClient[1].Id.Should().BeEquivalentTo("id2"); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async void GetDiffsForCommit() |
| 65 | + { |
| 66 | + string gitlabServer = "http://fake-gitlab.com/"; |
| 67 | + string projectId = "id"; |
| 68 | + string sha = "6104942438c14ec7bd21c6cd5bd995272b3faff6"; |
| 69 | + string url = $"/projects/id/repository/commits/{sha}/diff?per_page=100&page=1"; |
| 70 | + |
| 71 | + var handler = A.Fake<MockHandler>(opt => opt.CallsBaseMethods()); |
| 72 | + A.CallTo(() => handler.SendAsync(HttpMethod.Get, url)) |
| 73 | + .ReturnsLazily(() => HttpResponseMessageProducer.Success( |
| 74 | + $"[ {{ \"diff\": \"diff1\", \"new_path\": \"new_path1\", \"old_path\": \"old_path1\", \"a_mode\": \"a_mode1\", \"b_mode\": \"b_mode1\", \"new_file\": \"true\", \"renamed_file\": \"false\", \"deleted_file\": \"false\" }},\n {{\"diff\": \"diff2\", \"new_path\": \"new_path2\", \"old_path\": \"old_path2\", \"a_mode\": \"a_mode2\", \"b_mode\": \"b_mode2\", \"new_file\": \"false\", \"renamed_file\": \"true\", \"deleted_file\": \"true\"}}]")); |
| 75 | + using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) }) |
| 76 | + { |
| 77 | + var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client); |
| 78 | + var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder()); |
| 79 | + |
| 80 | + var diffsFromClient = await commitsClient.GetDiffsAsync(projectId, sha); |
| 81 | + diffsFromClient[0].DiffText.Should().BeEquivalentTo("diff1"); |
| 82 | + diffsFromClient[0].NewPath.Should().BeEquivalentTo("new_path1"); |
| 83 | + diffsFromClient[0].OldPath.Should().BeEquivalentTo("old_path1"); |
| 84 | + diffsFromClient[0].AMode.Should().BeEquivalentTo("a_mode1"); |
| 85 | + diffsFromClient[0].BMode.Should().BeEquivalentTo("b_mode1"); |
| 86 | + diffsFromClient[0].IsNewFile.Should().BeTrue(); |
| 87 | + diffsFromClient[0].IsRenamedFile.Should().BeFalse(); |
| 88 | + diffsFromClient[0].IsDeletedFile.Should().BeFalse(); |
| 89 | + |
| 90 | + diffsFromClient[1].DiffText.Should().BeEquivalentTo("diff2"); |
| 91 | + diffsFromClient[1].NewPath.Should().BeEquivalentTo("new_path2"); |
| 92 | + diffsFromClient[1].OldPath.Should().BeEquivalentTo("old_path2"); |
| 93 | + diffsFromClient[1].AMode.Should().BeEquivalentTo("a_mode2"); |
| 94 | + diffsFromClient[1].BMode.Should().BeEquivalentTo("b_mode2"); |
| 95 | + diffsFromClient[1].IsNewFile.Should().BeFalse(); |
| 96 | + diffsFromClient[1].IsRenamedFile.Should().BeTrue(); |
| 97 | + diffsFromClient[1].IsDeletedFile.Should().BeTrue(); |
| 98 | + |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public async void GetStatusesForCommit() |
| 104 | + { |
| 105 | + string gitlabServer = "http://fake-gitlab.com/"; |
| 106 | + string projectId = "id"; |
| 107 | + string sha = "6104942438c14ec7bd21c6cd5bd995272b3faff6"; |
| 108 | + string Name = "name1"; |
| 109 | + string url = $"/projects/id/repository/commits/{sha}/statuses?name={Name}&per_page=100&page=1"; |
| 110 | + |
| 111 | + var handler = A.Fake<MockHandler>(opt => opt.CallsBaseMethods()); |
| 112 | + A.CallTo(() => handler.SendAsync(HttpMethod.Get, url)) |
| 113 | + .ReturnsLazily(() => HttpResponseMessageProducer.Success( |
| 114 | + $"[ {{\"id\":1,\"sha\":\"{sha}\",\"ref \":\"\",\"status\":\"success\",\"name\":\"name1\",\"target_url\":\"target_url1\",\"description\":\"success\",\"created_at\":\"2020-04-08T11:57:49.136+05:30\",\"started_at\":\"2020-04-08T11:58:00.362+05:30\",\"finished_at\":\"2020-04-08T11:58:06.121+05:30\",\"allow_failure\":false,\"coverage\":null,\"author\":{{\"id\":1,\"name\":\"name\",\"username\":\"username\",\"state\":\"active\",\"avatar_url\":\"avatar_url1\",\"web_url\":\"web_url1\"}} }},{{\"id\":2,\"sha\":\"{sha}\",\"ref \":\"\",\"status\":\"success\",\"name\":\"name2\",\"target_url\":\"target_url2\",\"description\":\"success\",\"created_at\":\"2020-04-08T11:57:49.136+05:30\",\"started_at\":\"2020-04-08T11:58:00.362+05:30\",\"finished_at\":\"2020-04-08T11:58:06.121+05:30\",\"allow_failure\":false,\"coverage\":null,\"author\":{{\"id\":2,\"name\":\"name2\",\"username\":\"username2\",\"state\":\"activ2\",\"avatar_url2\":\"avatar_url2\",\"web_url\":\"web_url2\"}} }}]")); |
| 115 | + using (var client = new HttpClient(handler) { BaseAddress = new Uri(gitlabServer) }) |
| 116 | + { |
| 117 | + var gitlabHttpFacade = new GitLabHttpFacade(new RequestsJsonSerializer(), client); |
| 118 | + var commitsClient = new CommitsClient(gitlabHttpFacade, new CommitQueryBuilder(), new CommitRefsQueryBuilder(), new CommitStatusesQueryBuilder()); |
| 119 | + |
| 120 | + var statusesFromClient = await commitsClient.GetStatusesAsync(projectId, sha, o => o.Name = Name); |
| 121 | + statusesFromClient[0].Status.Should().BeEquivalentTo("success"); |
| 122 | + statusesFromClient[0].Name.Should().BeEquivalentTo("name1"); |
| 123 | + statusesFromClient[0].TargetUrl.Should().BeEquivalentTo("target_url1"); |
| 124 | + statusesFromClient[0].Id.Should().BeEquivalentTo("1"); |
| 125 | + |
| 126 | + statusesFromClient[1].Status.Should().BeEquivalentTo("success"); |
| 127 | + statusesFromClient[1].Name.Should().BeEquivalentTo("name2"); |
| 128 | + statusesFromClient[1].TargetUrl.Should().BeEquivalentTo("target_url2"); |
| 129 | + statusesFromClient[1].Id.Should().BeEquivalentTo("2"); |
| 130 | + |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments