Skip to content

Commit cd9f801

Browse files
authored
Adapt mocks a bit so that they behave more like NGitLab (#842)
* Adapt mocks a bit so that they behave more like NGitLab * Remove mock exceptions and use GitLabExceptions * fix
1 parent 22611c9 commit cd9f801

37 files changed

+189
-243
lines changed

Diff for: NGitLab.Mock.Tests/CommitsMockTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public void Test_create_commit_with_start_branch_and_start_sha()
186186
},
187187
});
188188

189-
Assert.That(handler, Throws.TypeOf<GitLabBadRequestException>()
189+
Assert.That(handler, Throws.TypeOf<GitLabException>()
190190
.With.Message.Contains("start_branch, start_sha are mutually exclusive."));
191191
}
192192

@@ -221,7 +221,7 @@ public void Test_create_commit_with_existing_branch()
221221
},
222222
});
223223

224-
Assert.That(handler, Throws.TypeOf<GitLabBadRequestException>()
224+
Assert.That(handler, Throws.TypeOf<GitLabException>()
225225
.With.Message.Contains("A branch called 'test-branch' already exists."));
226226
}
227227

Diff for: NGitLab.Mock.Tests/GroupsMockTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void Test_page_groups_with_invalid_perpage_throws()
124124
{
125125
using var server = CreateGroupHierarchy();
126126
var client = server.CreateClient("user1");
127-
Assert.ThrowsAsync<Clients.GitLabBadRequestException>(() => client.Groups.PageAsync(new(perPage: 0)));
127+
Assert.ThrowsAsync<GitLabException>(() => client.Groups.PageAsync(new(perPage: 0)));
128128
}
129129

130130
[Test]
@@ -276,7 +276,7 @@ public void Test_page_subgroups_with_invalid_perpage_throws()
276276
{
277277
using var server = CreateGroupHierarchy();
278278
var client = server.CreateClient("user1");
279-
Assert.ThrowsAsync<Clients.GitLabBadRequestException>(() => client.Groups.PageSubgroupsAsync(1, new(page: 1, perPage: 0)));
279+
Assert.ThrowsAsync<GitLabException>(() => client.Groups.PageSubgroupsAsync(1, new(page: 1, perPage: 0)));
280280
}
281281

282282
[Test]

Diff for: NGitLab.Mock.Tests/ProjectsMockTests.cs

+39
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,45 @@ public void Test_projects_created_can_be_found()
4747
Assert.That(project.Namespace.FullPath, Is.EqualTo("testgroup"));
4848
}
4949

50+
[Test]
51+
public void GetProjectAsync_WhenProjectDoesNotExist_ShouldThrowNotFound()
52+
{
53+
// Arrange
54+
using var server = new GitLabConfig()
55+
.WithUser("TestUser", isDefault: true)
56+
.BuildServer();
57+
var gitLabClient = server.CreateClient();
58+
var projectClient = gitLabClient.Projects;
59+
60+
// Act/Assert
61+
var ex = Assert.ThrowsAsync<GitLabException>(() => projectClient.GetAsync("baz1234"));
62+
63+
Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
64+
}
65+
66+
[Test]
67+
public void GetProjectAsync_WhenProjectInaccessible_ShouldThrowNotFound()
68+
{
69+
// Arrange
70+
using var server = new GitLabConfig()
71+
.WithUser("TestUser1", isDefault: true)
72+
.WithUser("TestUser2")
73+
.BuildServer();
74+
var testUser1ProjectClient = server.CreateClient("TestUser1").Projects;
75+
var testUser2ProjectClient = server.CreateClient("TestUser2").Projects;
76+
77+
var testUser2Project = testUser2ProjectClient.Create(new ProjectCreate
78+
{
79+
Name = $"Project_Test_{Guid.NewGuid()}",
80+
VisibilityLevel = VisibilityLevel.Private,
81+
});
82+
83+
// Act/Assert
84+
var ex = Assert.ThrowsAsync<GitLabException>(() => testUser1ProjectClient.GetAsync(testUser2Project.Id));
85+
86+
Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
87+
}
88+
5089
[Test]
5190
public void Test_project_can_be_cloned_by_default()
5291
{

Diff for: NGitLab.Mock.Tests/RepositoryMockTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Test_create_commit_in_new_branch_fails_if_both_start_branch_and_sha_
3333
FilePath = "README.md",
3434
},
3535
},
36-
}), Throws.TypeOf<GitLabBadRequestException>()
36+
}), Throws.TypeOf<GitLabException>()
3737
.With.Message.Contains("GitLab server returned an error (BadRequest): start_branch, start_sha are mutually exclusive."));
3838
}
3939

@@ -66,7 +66,7 @@ public void Test_create_a_new_commit_with_start_branch_fails_if_branch_already_e
6666
FilePath = "README.md",
6767
},
6868
},
69-
}), Throws.TypeOf<GitLabBadRequestException>()
69+
}), Throws.TypeOf<GitLabException>()
7070
.With.Message.Contains($"A branch called '{newBranch}' already exists."));
7171
}
7272

@@ -99,7 +99,7 @@ public void Test_create_a_new_commit_with_start_sha_fails_if_branch_already_exis
9999
FilePath = "README.md",
100100
},
101101
},
102-
}), Throws.TypeOf<GitLabBadRequestException>()
102+
}), Throws.TypeOf<GitLabException>()
103103
.With.Message.Contains($"A branch called '{newBranch}' already exists."));
104104
}
105105

@@ -190,7 +190,7 @@ public void Test_create_a_new_commit_on_nonexistent_branch()
190190
FilePath = "README.md",
191191
},
192192
},
193-
}), Throws.TypeOf<GitLabBadRequestException>()
193+
}), Throws.TypeOf<GitLabException>()
194194
.With.Message.Contains("You can only create or edit files when you are on a branch."));
195195
}
196196

Diff for: NGitLab.Mock/Clients/BranchClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Branch this[string name]
6969
var project = GetProject(_projectId, ProjectPermission.View);
7070
var branch = project.Repository.GetBranch(name);
7171
if (branch == null)
72-
throw new GitLabNotFoundException();
72+
throw GitLabException.NotFound();
7373

7474
return branch.ToBranchClient(project);
7575
}

Diff for: NGitLab.Mock/Clients/ClientBase.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected Group GetGroup(object id, GroupPermission permissions)
3838
};
3939

4040
if (group == null || !group.CanUserViewGroup(Context.User))
41-
throw new GitLabNotFoundException("Group does not exist or user doesn't have permission to view it");
41+
throw GitLabException.NotFound("Group does not exist or user doesn't have permission to view it");
4242

4343
switch (permissions)
4444
{
@@ -48,12 +48,12 @@ protected Group GetGroup(object id, GroupPermission permissions)
4848

4949
case GroupPermission.Edit:
5050
if (!group.CanUserEditGroup(Context.User))
51-
throw new GitLabForbiddenException($"User '{Context.User.Name}' does not have the permission to edit the group '{group.Name}'");
51+
throw GitLabException.Forbidden($"User '{Context.User.Name}' does not have the permission to edit the group '{group.Name}'");
5252
break;
5353

5454
case GroupPermission.Delete:
5555
if (!group.CanUserDeleteGroup(Context.User))
56-
throw new GitLabForbiddenException($"User '{Context.User.Name}' does not have the permission to delete the group '{group.Name}'");
56+
throw GitLabException.Forbidden($"User '{Context.User.Name}' does not have the permission to delete the group '{group.Name}'");
5757
break;
5858

5959
default:
@@ -85,8 +85,8 @@ protected Project GetProject(object id, ProjectPermission permissions)
8585
_ => throw new ArgumentException($"Id of type '{id.GetType()}' is not supported"),
8686
};
8787

88-
if (project == null || !project.CanUserViewProject(Context.User))
89-
throw new GitLabNotFoundException("Project does not exist or User doesn't have permission to view it");
88+
if (project is null || !project.CanUserViewProject(Context.User))
89+
throw GitLabException.NotFound("Project does not exist or User doesn't have permission to view it");
9090

9191
switch (permissions)
9292
{
@@ -96,17 +96,17 @@ protected Project GetProject(object id, ProjectPermission permissions)
9696

9797
case ProjectPermission.Contribute:
9898
if (!project.CanUserContributeToProject(Context.User))
99-
throw new GitLabForbiddenException($"User '{Context.User.Name}' does not have the permission to contribute to the project '{project.Name}'");
99+
throw GitLabException.Forbidden($"User '{Context.User.Name}' does not have the permission to contribute to the project '{project.Name}'");
100100
break;
101101

102102
case ProjectPermission.Edit:
103103
if (!project.CanUserEditProject(Context.User))
104-
throw new GitLabForbiddenException($"User '{Context.User.Name}' does not have the permission to edit the project '{project.Name}'");
104+
throw GitLabException.Forbidden($"User '{Context.User.Name}' does not have the permission to edit the project '{project.Name}'");
105105
break;
106106

107107
case ProjectPermission.Delete:
108108
if (!project.CanUserDeleteProject(Context.User))
109-
throw new GitLabForbiddenException($"User '{Context.User.Name}' does not have the permission to delete the project '{project.Name}'");
109+
throw GitLabException.Forbidden($"User '{Context.User.Name}' does not have the permission to delete the project '{project.Name}'");
110110
break;
111111

112112
default:
@@ -120,7 +120,7 @@ protected User GetUser(long userId)
120120
{
121121
var user = Server.Users.GetById(userId);
122122
if (user == null)
123-
throw new GitLabNotFoundException();
123+
throw GitLabException.NotFound();
124124

125125
return user;
126126
}
@@ -130,7 +130,7 @@ protected Issue GetIssue(long projectId, long issueId)
130130
var project = GetProject(projectId, ProjectPermission.View);
131131
var issue = project.Issues.GetByIid(issueId);
132132
if (issue == null)
133-
throw new GitLabNotFoundException();
133+
throw GitLabException.NotFound();
134134

135135
return issue;
136136
}
@@ -140,7 +140,7 @@ protected Milestone GetMilestone(long projectId, long milestoneId)
140140
var project = GetProject(projectId, ProjectPermission.View);
141141
var milestone = project.Milestones.GetByIid(milestoneId);
142142
if (milestone == null)
143-
throw new GitLabNotFoundException();
143+
throw GitLabException.NotFound();
144144

145145
return milestone;
146146
}
@@ -150,7 +150,7 @@ protected MergeRequest GetMergeRequest(long projectId, long mergeRequestIid)
150150
var project = GetProject(projectId, ProjectPermission.View);
151151
var mergeRequest = project.MergeRequests.GetByIid(mergeRequestIid);
152152
if (mergeRequest == null)
153-
throw new GitLabNotFoundException();
153+
throw GitLabException.NotFound();
154154

155155
return mergeRequest;
156156
}

Diff for: NGitLab.Mock/Clients/FileClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public bool FileExists(string filePath, string @ref)
9999
{
100100
return Get(filePath, @ref) != null;
101101
}
102-
catch (GitLabNotFoundException)
102+
catch (GitLabException ex) when (ex.StatusCode is HttpStatusCode.NotFound)
103103
{
104104
return false;
105105
}

Diff for: NGitLab.Mock/Clients/GitLabBadRequestException.cs

-36
This file was deleted.

Diff for: NGitLab.Mock/Clients/GitLabForbiddenException.cs

-36
This file was deleted.

Diff for: NGitLab.Mock/Clients/GitLabNotFoundException.cs

-36
This file was deleted.

Diff for: NGitLab.Mock/Clients/GlobalJobsClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public GlobalJobsClient(ClientContext context)
2020
var job = Server.AllProjects.SelectMany(p => p.Jobs).FirstOrDefault(j => string.Equals(j.JobToken, token, StringComparison.Ordinal));
2121

2222
if (job == null)
23-
throw new GitLabNotFoundException();
23+
throw GitLabException.NotFound();
2424

2525
return job.ToJobClient();
2626
}

Diff for: NGitLab.Mock/Clients/GroupBadgeClient.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Models.Badge this[long id]
2323
var group = GetGroup(_groupId, GroupPermission.View);
2424
var badge = group.Badges.GetById(id);
2525
if (badge == null)
26-
throw new GitLabNotFoundException($"Badge with id '{id}' does not exist in group with id '{_groupId}'");
26+
throw GitLabException.NotFound($"Badge with id '{id}' does not exist in group with id '{_groupId}'");
2727

2828
return badge.ToBadgeModel();
2929
}
@@ -62,7 +62,7 @@ public void Delete(long id)
6262
var badgeToRemove = GetGroup(_groupId, GroupPermission.View).Badges.FirstOrDefault(b => b.Id == id);
6363
if (badgeToRemove == null)
6464
{
65-
throw new GitLabNotFoundException($"Badge with id '{id}' does not exist in group with id '{_groupId}'");
65+
throw GitLabException.NotFound($"Badge with id '{id}' does not exist in group with id '{_groupId}'");
6666
}
6767

6868
GetGroup(_groupId, GroupPermission.Edit).Badges.Remove(badgeToRemove);
@@ -76,7 +76,7 @@ public Models.Badge Update(long id, BadgeUpdate badge)
7676
var badgeToUpdate = GetGroup(_groupId, GroupPermission.Edit).Badges.FirstOrDefault(b => b.Id == id);
7777
if (badgeToUpdate == null)
7878
{
79-
throw new GitLabNotFoundException($"Badge with id '{id}' does not exist in group with id '{_groupId}'");
79+
throw GitLabException.NotFound($"Badge with id '{id}' does not exist in group with id '{_groupId}'");
8080
}
8181

8282
badgeToUpdate.LinkUrl = badge.LinkUrl;

0 commit comments

Comments
 (0)