Skip to content

Commit

Permalink
Merge pull request #142 from aau-giraf/test-auth-fix
Browse files Browse the repository at this point in the history
Fix for authentication in testing
  • Loading branch information
LokeSGJ authored Dec 10, 2024
2 parents 5a7500a + 059816d commit 59621c8
Show file tree
Hide file tree
Showing 38 changed files with 1,357 additions and 2,063 deletions.
318 changes: 171 additions & 147 deletions Giraf.IntegrationTests/Endpoints/ActivityEndpointTests.cs

Large diffs are not rendered by default.

258 changes: 118 additions & 140 deletions Giraf.IntegrationTests/Endpoints/CitizenEndpointTests.cs

Large diffs are not rendered by default.

324 changes: 114 additions & 210 deletions Giraf.IntegrationTests/Endpoints/GradeEndpointTests.cs

Large diffs are not rendered by default.

448 changes: 258 additions & 190 deletions Giraf.IntegrationTests/Endpoints/InvitationEndpointTests.cs

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions Giraf.IntegrationTests/Endpoints/LoginEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ public class LoginEndpointTests
public async Task Login_ReturnsOk_WithValidCredentials()
{
// Arrange
var factory = new GirafWebApplicationFactory(sp => new LoginUserSeeder(sp.GetRequiredService<UserManager<GirafUser>>()));
var factory = new GirafWebApplicationFactory();
var seeder = new EmptyDb();
var scope = factory.Services.CreateScope();
seeder.SeedUsers(scope.ServiceProvider.GetRequiredService<UserManager<GirafUser>>());
var client = factory.CreateClient();

var loginDto = new LoginDTO
{
Username = LoginUserSeeder.SeededUserName,
Password = LoginUserSeeder.SeededUserPassword
Username = seeder.Users["member"].UserName,
Password = "Password123!"
};

// Act
Expand All @@ -37,13 +40,16 @@ public async Task Login_ReturnsOk_WithValidCredentials()
public async Task Login_ReturnsBadRequest_WithInvalidUsername()
{
// Arrange
var factory = new GirafWebApplicationFactory(sp => new LoginUserSeeder(sp.GetRequiredService<UserManager<GirafUser>>()));
var factory = new GirafWebApplicationFactory();
var seeder = new EmptyDb();
var scope = factory.Services.CreateScope();
seeder.SeedUsers(scope.ServiceProvider.GetRequiredService<UserManager<GirafUser>>());
var client = factory.CreateClient();

var loginDto = new LoginDTO
{
Username = "invaliduser",
Password = LoginUserSeeder.SeededUserPassword
Password = "Password123!"
};

// Act
Expand All @@ -57,12 +63,15 @@ public async Task Login_ReturnsBadRequest_WithInvalidUsername()
public async Task Login_ReturnsBadRequest_WithInvalidPassword()
{
// Arrange
var factory = new GirafWebApplicationFactory(sp => new LoginUserSeeder(sp.GetRequiredService<UserManager<GirafUser>>()));
var factory = new GirafWebApplicationFactory();
var seeder = new EmptyDb();
var scope = factory.Services.CreateScope();
seeder.SeedUsers(scope.ServiceProvider.GetRequiredService<UserManager<GirafUser>>());
var client = factory.CreateClient();

var loginDto = new LoginDTO
{
Username = LoginUserSeeder.SeededUserName,
Username = seeder.Users["member"].UserName,
Password = "WrongPassword!"
};

Expand Down
281 changes: 95 additions & 186 deletions Giraf.IntegrationTests/Endpoints/OrganizationEndpointTests.cs

Large diffs are not rendered by default.

131 changes: 65 additions & 66 deletions Giraf.IntegrationTests/Endpoints/PictogramEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
using Giraf.IntegrationTests.Utils.DbSeeders;
using GirafAPI.Data;
using GirafAPI.Entities.Pictograms.DTOs;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Giraf.IntegrationTests.Endpoints
{
Expand All @@ -21,18 +19,15 @@ public class PictogramEndpointsTests
public async Task CreatePictogram_ReturnsOk_WhenPictogramIsValid()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new BasicOrganizationSeeder());
var factory = new GirafWebApplicationFactory();
var seeder = new BaseCaseDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

int organizationId;
client.AttachClaimsToken(scope, seeder.Users["member"]);

using (var scope = factory.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<GirafDbContext>();
var organization = await context.Organizations.FirstOrDefaultAsync();
Assert.NotNull(organization);
organizationId = organization.Id;
}
int organizationId = seeder.Organizations[0].Id;

// Prepare multipart form data
var formData = new MultipartFormDataContent();
Expand Down Expand Up @@ -68,9 +63,14 @@ public async Task CreatePictogram_ReturnsOk_WhenPictogramIsValid()
public async Task CreatePictogram_ReturnsBadRequest_WhenImageIsMissing()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new BasicOrganizationSeeder());
var factory = new GirafWebApplicationFactory();
var seeder = new OnlyUsersAndOrgDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

client.AttachClaimsToken(scope, seeder.Users["member"]);

// Prepare multipart form data without image
var formData = new MultipartFormDataContent();
formData.Add(new StringContent("1"), "organizationId");
Expand All @@ -87,9 +87,14 @@ public async Task CreatePictogram_ReturnsBadRequest_WhenImageIsMissing()
public async Task CreatePictogram_ReturnsBadRequest_WhenOrganizationIdIsMissing()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new EmptyDb());
var factory = new GirafWebApplicationFactory();
var seeder = new OnlyUsersAndOrgDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

client.AttachClaimsToken(scope, seeder.Users["member"]);

// Prepare multipart form data without organizationId
var formData = new MultipartFormDataContent();

Expand All @@ -112,18 +117,15 @@ public async Task CreatePictogram_ReturnsBadRequest_WhenOrganizationIdIsMissing(
public async Task CreatePictogram_ReturnsBadRequest_WhenPictogramNameIsMissing()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new BasicOrganizationSeeder());
var factory = new GirafWebApplicationFactory();
var seeder = new OnlyUsersAndOrgDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

int organizationId;
client.AttachClaimsToken(scope, seeder.Users["member"]);

using (var scope = factory.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<GirafDbContext>();
var organization = await context.Organizations.FirstOrDefaultAsync();
Assert.NotNull(organization);
organizationId = organization.Id;
}
int organizationId = seeder.Organizations[0].Id;

// Prepare multipart form data without pictogramName
var formData = new MultipartFormDataContent();
Expand Down Expand Up @@ -151,18 +153,15 @@ public async Task CreatePictogram_ReturnsBadRequest_WhenPictogramNameIsMissing()
public async Task GetPictogramById_ReturnsPictogram_WhenPictogramExists()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new BasicPictogramSeeder());
var factory = new GirafWebApplicationFactory();
var seeder = new BaseCaseDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

int pictogramId;
client.AttachClaimsToken(scope, seeder.Users["member"]);

using (var scope = factory.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<GirafDbContext>();
var pictogram = await context.Pictograms.FirstOrDefaultAsync();
Assert.NotNull(pictogram);
pictogramId = pictogram.Id;
}
int pictogramId = seeder.Pictograms[0].Id;

// Act
var response = await client.GetAsync($"/pictograms/{pictogramId}");
Expand All @@ -178,8 +177,14 @@ public async Task GetPictogramById_ReturnsPictogram_WhenPictogramExists()
public async Task GetPictogramById_ReturnsNotFound_WhenPictogramDoesNotExist()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new EmptyDb());
var factory = new GirafWebApplicationFactory();
var seeder = new OnlyUsersAndOrgDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

client.AttachClaimsToken(scope, seeder.Users["member"]);

int nonExistentPictogramId = 9999;

// Act
Expand All @@ -197,16 +202,15 @@ public async Task GetPictogramById_ReturnsNotFound_WhenPictogramDoesNotExist()
public async Task GetPictogramsByOrganizationId_ReturnsPictograms_WhenPictogramsExist()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new BasicPictogramSeeder());
var factory = new GirafWebApplicationFactory();
var seeder = new BaseCaseDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

int organizationId;
client.AttachClaimsToken(scope, seeder.Users["member"]);

using var scope = factory.Services.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<GirafDbContext>();
var organization = await context.Organizations.FirstOrDefaultAsync();
Assert.NotNull(organization);
organizationId = organization.Id;
int organizationId = seeder.Organizations[0].Id;

var currentPage = 1;
var pageSize = 10;
Expand All @@ -225,28 +229,21 @@ public async Task GetPictogramsByOrganizationId_ReturnsPictograms_WhenPictograms
public async Task GetPictogramsByOrganizationId_ReturnsEmptyList_WhenNoPictogramsExist()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new BasicOrganizationSeeder());
var factory = new GirafWebApplicationFactory();
var seeder = new OnlyUsersAndOrgDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

int organizationId;
client.AttachClaimsToken(scope, seeder.Users["member"]);

using (var scope = factory.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<GirafDbContext>();
var organization = await context.Organizations.FirstOrDefaultAsync();
Assert.NotNull(organization);
organizationId = organization.Id;
}

// Set up the test claims
TestAuthHandler.TestClaims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, "test-user-id"),
new Claim("OrgMember", organizationId.ToString())
};
int organizationId = seeder.Organizations[0].Id;

var currentPage = 1;
var pageSize = 10;

// Act
var response = await client.GetAsync($"/pictograms/organization/{organizationId}");
var response = await client.GetAsync($"/pictograms/organizationId:int?organizationId={organizationId}&currentPage={currentPage}&pageSize={pageSize}");

// Assert
response.EnsureSuccessStatusCode();
Expand All @@ -263,18 +260,15 @@ public async Task GetPictogramsByOrganizationId_ReturnsEmptyList_WhenNoPictogram
public async Task DeletePictogram_ReturnsOk_WhenPictogramExists()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new BasicPictogramSeeder());
var factory = new GirafWebApplicationFactory();
var seeder = new BaseCaseDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

int pictogramId;
client.AttachClaimsToken(scope, seeder.Users["owner"]);

using (var scope = factory.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<GirafDbContext>();
var pictogram = await context.Pictograms.FirstOrDefaultAsync();
Assert.NotNull(pictogram);
pictogramId = pictogram.Id;
}
int pictogramId = seeder.Pictograms[0].Id;

// Act
var response = await client.DeleteAsync($"/pictograms/{pictogramId}");
Expand All @@ -295,9 +289,14 @@ public async Task DeletePictogram_ReturnsOk_WhenPictogramExists()
public async Task DeletePictogram_ReturnsNotFound_WhenPictogramDoesNotExist()
{
// Arrange
var factory = new GirafWebApplicationFactory(_ => new EmptyDb());
var factory = new GirafWebApplicationFactory();
var seeder = new OnlyUsersAndOrgDb();
var scope = factory.Services.CreateScope();
factory.SeedDb(scope, seeder);
var client = factory.CreateClient();

client.AttachClaimsToken(scope, seeder.Users["owner"]);

int nonExistentPictogramId = 9999;

// Act
Expand Down
Loading

0 comments on commit 59621c8

Please sign in to comment.