Skip to content

Commit

Permalink
Adapt mocks a bit so that they behave more like NGitLab
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-z committed Feb 4, 2025
1 parent c2b9223 commit 8363f64
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
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 testUser2ProjectClient = server.CreateClient("TestUser2").Projects;
var testUser1ProjectClient = server.CreateClient("TestUser1").Projects;

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

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

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

[Test]
public void Test_project_can_be_cloned_by_default()
{
Expand Down
11 changes: 9 additions & 2 deletions NGitLab.Mock/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ 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 new GitLabException("GitLab server returned an error (NotFound): 404 Project Not Found.")
{
MethodType = Impl.MethodType.Get,
StatusCode = HttpStatusCode.NotFound,
ErrorMessage = "404 Project Not Found",
};
}

switch (permissions)
{
Expand Down
21 changes: 21 additions & 0 deletions NGitLab.Tests/ProjectsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ public async Task GetProjectAsync_WhenProjectDoesNotExist_ShouldThrowNotFound()
Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}

[Test]
[NGitLabRetry]
public async Task GetProjectAsync_WhenProjectInaccessible_ShouldThrowNotFound()
{
// Arrange
using var context = await GitLabTestContext.CreateAsync();
var adminProjectClient = context.AdminClient.Projects;
var userProjectClient = context.Client.Projects;

var adminProject = adminProjectClient.Create(new ProjectCreate
{
Name = "Project_Test_" + context.GetRandomNumber().ToStringInvariant(),
VisibilityLevel = VisibilityLevel.Private,
});

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

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

[Test]
[NGitLabRetry]
public async Task GetProjectsAsync()
Expand Down

0 comments on commit 8363f64

Please sign in to comment.