Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt mocks a bit so that they behave more like NGitLab #842

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions NGitLab.Mock.Tests/CommitsMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void Test_create_commit_with_start_branch_and_start_sha()
},
});

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

Expand Down Expand Up @@ -221,7 +221,7 @@ public void Test_create_commit_with_existing_branch()
},
});

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

Expand Down
4 changes: 2 additions & 2 deletions NGitLab.Mock.Tests/GroupsMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void Test_page_groups_with_invalid_perpage_throws()
{
using var server = CreateGroupHierarchy();
var client = server.CreateClient("user1");
Assert.ThrowsAsync<Clients.GitLabBadRequestException>(() => client.Groups.PageAsync(new(perPage: 0)));
Assert.ThrowsAsync<GitLabException>(() => client.Groups.PageAsync(new(perPage: 0)));
}

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

[Test]
Expand Down
39 changes: 39 additions & 0 deletions NGitLab.Mock.Tests/ProjectsMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,45 @@ public void Test_projects_created_can_be_found()
Assert.That(project.Namespace.FullPath, Is.EqualTo("testgroup"));
}

[Test]
public void GetProjectAsync_WhenProjectDoesNotExist_ShouldThrowNotFound()
{
// Arrange
using var server = new GitLabConfig()
.WithUser("TestUser", isDefault: true)
.BuildServer();
var gitLabClient = server.CreateClient();
var projectClient = gitLabClient.Projects;

// Act/Assert
var ex = Assert.ThrowsAsync<GitLabException>(() => projectClient.GetAsync("baz1234"));

Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}

[Test]
public void GetProjectAsync_WhenProjectInaccessible_ShouldThrowNotFound()
{
// Arrange
using var server = new GitLabConfig()
.WithUser("TestUser1", isDefault: true)
.WithUser("TestUser2")
.BuildServer();
var testUser1ProjectClient = server.CreateClient("TestUser1").Projects;
var testUser2ProjectClient = server.CreateClient("TestUser2").Projects;

var testUser2Project = testUser2ProjectClient.Create(new ProjectCreate
{
Name = $"Project_Test_{Guid.NewGuid()}",
VisibilityLevel = VisibilityLevel.Private,
});

// Act/Assert
var ex = Assert.ThrowsAsync<GitLabException>(() => testUser1ProjectClient.GetAsync(testUser2Project.Id));

Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}

[Test]
public void Test_project_can_be_cloned_by_default()
{
Expand Down
8 changes: 4 additions & 4 deletions NGitLab.Mock.Tests/RepositoryMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Test_create_commit_in_new_branch_fails_if_both_start_branch_and_sha_
FilePath = "README.md",
},
},
}), Throws.TypeOf<GitLabBadRequestException>()
}), Throws.TypeOf<GitLabException>()
.With.Message.Contains("GitLab server returned an error (BadRequest): start_branch, start_sha are mutually exclusive."));
}

Expand Down Expand Up @@ -66,7 +66,7 @@ public void Test_create_a_new_commit_with_start_branch_fails_if_branch_already_e
FilePath = "README.md",
},
},
}), Throws.TypeOf<GitLabBadRequestException>()
}), Throws.TypeOf<GitLabException>()
.With.Message.Contains($"A branch called '{newBranch}' already exists."));
}

Expand Down Expand Up @@ -99,7 +99,7 @@ public void Test_create_a_new_commit_with_start_sha_fails_if_branch_already_exis
FilePath = "README.md",
},
},
}), Throws.TypeOf<GitLabBadRequestException>()
}), Throws.TypeOf<GitLabException>()
.With.Message.Contains($"A branch called '{newBranch}' already exists."));
}

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

Expand Down
2 changes: 1 addition & 1 deletion NGitLab.Mock/Clients/BranchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Branch this[string name]
var project = GetProject(_projectId, ProjectPermission.View);
var branch = project.Repository.GetBranch(name);
if (branch == null)
throw new GitLabNotFoundException();
throw GitLabException.NotFound();

return branch.ToBranchClient(project);
}
Expand Down
24 changes: 12 additions & 12 deletions NGitLab.Mock/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected Group GetGroup(object id, GroupPermission permissions)
};

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

switch (permissions)
{
Expand All @@ -48,12 +48,12 @@ protected Group GetGroup(object id, GroupPermission permissions)

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

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

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

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

switch (permissions)
{
Expand All @@ -96,17 +96,17 @@ protected Project GetProject(object id, ProjectPermission permissions)

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

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

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

default:
Expand All @@ -120,7 +120,7 @@ protected User GetUser(long userId)
{
var user = Server.Users.GetById(userId);
if (user == null)
throw new GitLabNotFoundException();
throw GitLabException.NotFound();

return user;
}
Expand All @@ -130,7 +130,7 @@ protected Issue GetIssue(long projectId, long issueId)
var project = GetProject(projectId, ProjectPermission.View);
var issue = project.Issues.GetByIid(issueId);
if (issue == null)
throw new GitLabNotFoundException();
throw GitLabException.NotFound();

return issue;
}
Expand All @@ -140,7 +140,7 @@ protected Milestone GetMilestone(long projectId, long milestoneId)
var project = GetProject(projectId, ProjectPermission.View);
var milestone = project.Milestones.GetByIid(milestoneId);
if (milestone == null)
throw new GitLabNotFoundException();
throw GitLabException.NotFound();

return milestone;
}
Expand All @@ -150,7 +150,7 @@ protected MergeRequest GetMergeRequest(long projectId, long mergeRequestIid)
var project = GetProject(projectId, ProjectPermission.View);
var mergeRequest = project.MergeRequests.GetByIid(mergeRequestIid);
if (mergeRequest == null)
throw new GitLabNotFoundException();
throw GitLabException.NotFound();

return mergeRequest;
}
Expand Down
2 changes: 1 addition & 1 deletion NGitLab.Mock/Clients/FileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public bool FileExists(string filePath, string @ref)
{
return Get(filePath, @ref) != null;
}
catch (GitLabNotFoundException)
catch (GitLabException ex) when (ex.StatusCode is HttpStatusCode.NotFound)
{
return false;
}
Expand Down
36 changes: 0 additions & 36 deletions NGitLab.Mock/Clients/GitLabBadRequestException.cs

This file was deleted.

36 changes: 0 additions & 36 deletions NGitLab.Mock/Clients/GitLabForbiddenException.cs

This file was deleted.

36 changes: 0 additions & 36 deletions NGitLab.Mock/Clients/GitLabNotFoundException.cs

This file was deleted.

2 changes: 1 addition & 1 deletion NGitLab.Mock/Clients/GlobalJobsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public GlobalJobsClient(ClientContext context)
var job = Server.AllProjects.SelectMany(p => p.Jobs).FirstOrDefault(j => string.Equals(j.JobToken, token, StringComparison.Ordinal));

if (job == null)
throw new GitLabNotFoundException();
throw GitLabException.NotFound();

return job.ToJobClient();
}
Expand Down
6 changes: 3 additions & 3 deletions NGitLab.Mock/Clients/GroupBadgeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Models.Badge this[long id]
var group = GetGroup(_groupId, GroupPermission.View);
var badge = group.Badges.GetById(id);
if (badge == null)
throw new GitLabNotFoundException($"Badge with id '{id}' does not exist in group with id '{_groupId}'");
throw GitLabException.NotFound($"Badge with id '{id}' does not exist in group with id '{_groupId}'");

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

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

badgeToUpdate.LinkUrl = badge.LinkUrl;
Expand Down
Loading