Skip to content
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
7 changes: 7 additions & 0 deletions sparkly-server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,24 @@

<ItemGroup>
<None Remove="Properties\launchSettings.json" />
<None Remove="sparkly-server.test\config\**" />
</ItemGroup>

<ItemGroup>
<Content Include=".github\workflows\ci.yml" />
<Content Remove="sparkly-server.test\config\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="sparkly-server.test\HealthzTest.cs" />
<Compile Remove="sparkly-server.test\UserTest.cs" />
<Compile Remove="sparkly-server.test\ProjectTest.cs" />
<Compile Remove="sparkly-server.test\TestWebAppliactionFactory.cs" />
<Compile Remove="sparkly-server.test\config\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="sparkly-server.test\config\**" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion sparkly-server.test/HealthzTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using sparkly_server.test.config;
using System.Net;

namespace sparkly_server.test
{
Expand Down
53 changes: 50 additions & 3 deletions sparkly-server.test/ProjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
using sparkly_server.Enum;
using sparkly_server.Infrastructure;
using sparkly_server.Services.Auth;
using sparkly_server.test.config;
using System.Net;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace sparkly_server.test
{
Expand Down Expand Up @@ -76,7 +79,7 @@ private async Task<Guid> AuthenticateAsTestUserAsync()
/// </summary>
/// <param name="projectName">The name of the project to create.</param>
/// <returns>A <see cref="ProjectResponse"/> containing the details of the created project, or null if creation fails.</returns>
private async Task<ProjectResponse?> CreateProjectAsync(string projectName)
private async Task<ProjectResponse> CreateProjectAsync(string projectName)
{
var payload = new CreateProjectRequest(
ProjectName: projectName,
Expand All @@ -93,7 +96,9 @@ private async Task<Guid> AuthenticateAsTestUserAsync()
response.EnsureSuccessStatusCode();

var created = await response.Content.ReadFromJsonAsync<ProjectResponse>();
return created;

return created ?? throw new XunitException("CreateProjectAsync: response body deserialized to null");

}

// Tests
Expand All @@ -107,7 +112,7 @@ public async Task CreateProject_Should_Create_Project_For_Authenticated_User()
var created = await CreateProjectAsync(projectName);

Assert.NotNull(created);
Assert.Equal(projectName, created!.ProjectName);
Assert.Equal(projectName, created.ProjectName);

using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
Expand All @@ -119,5 +124,47 @@ public async Task CreateProject_Should_Create_Project_For_Authenticated_User()
Assert.Equal(projectName, project.ProjectName);
Assert.Equal(userId, project.OwnerId);
}

[Fact]
public async Task CreateProject_Should_Fail_For_Unauthenticated_User()
{
var request = new CreateProjectRequest("MyTestProject", "Test project", ProjectVisibility.Public);

var response = await _client.PostAsJsonAsync("/api/v1/projects/create", request);

Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
}

[Fact]
public async Task GetProjectById_Should_Return_Null_For_Nonexistent_Project()
{
var response = await _client.GetAsync("/api/v1/projects/1234567890abcdef");

Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

// FIXME: Why NotFound??
// [Fact]
// public async Task GetProjectById_Should_Return_Project()
// {
// await AuthenticateAsTestUserAsync();
// var projectName = "MyTestProject1";
//
// var project = await CreateProjectAsync(projectName);
// var projectId = project.Id;
//
// _output.WriteLine($"[Test] Created projectId = {projectId}");
//
// using (var scope = _factory.Services.CreateScope())
// {
// var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
// var exists = await db.Projects.AnyAsync(p => p.Id == projectId);
// _output.WriteLine($"[Test] Project exists in DB: {exists}");
// }
//
// var response = await _client.GetAsync($"/api/v1/projects/{projectId}");
//
// Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// }
}
}
1 change: 1 addition & 0 deletions sparkly-server.test/UserTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using sparkly_server.DTO.Auth;
using sparkly_server.Infrastructure;
using sparkly_server.test.config;
using System.Text;
using System.Text.Json;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;

namespace sparkly_server.test
namespace sparkly_server.test.config
{
public class TestWebApplicationFactory : WebApplicationFactory<Program>
{
Expand Down
6 changes: 5 additions & 1 deletion src/Controllers/Projects/ProjectsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public async Task<IActionResult> GetRandomProjects([FromQuery] int take = 20, Ca
public async Task<IActionResult> GetProjectById(Guid projectId, CancellationToken ct = default)
{
var project = await _projects.GetProjectByIdAsync(projectId, ct);
return Ok(project);

if (project is null)
return NotFound();

return Ok(new ProjectResponse(project));
}

// Create project
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Projects/IProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Task<Project> CreateProjectAsync(
ProjectVisibility visibility,
CancellationToken cancellationToken = default);

Task<Project> GetProjectByIdAsync(
Task<Project?> GetProjectByIdAsync(
Guid projectId, CancellationToken
cancellationToken = default);

Expand Down
7 changes: 2 additions & 5 deletions src/Services/Projects/ProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ public async Task<Project> CreateProjectAsync(string name, string description, P
/// <exception cref="InvalidOperationException">
/// Thrown if the project with the specified identifier is not found.
/// </exception>
public async Task<Project> GetProjectByIdAsync(Guid projectId, CancellationToken cancellationToken = default)
public Task<Project?> GetProjectByIdAsync(Guid projectId, CancellationToken cancellationToken = default)
{
var project = await _projects.GetByIdAsync(projectId, cancellationToken)
?? throw new InvalidOperationException("Project not found");

return project;
return _projects.GetByIdAsync(projectId, cancellationToken);
}

/// <summary>
Expand Down