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

Dim@6043 unittests toevoegen #718

Merged
merged 17 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
71 changes: 70 additions & 1 deletion Kiss.Bff.Test/AuthorizationCheckTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Testing;
using static System.Net.HttpStatusCode;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Net;
using Kiss.Bff.ZaakGerichtWerken.Contactmomenten;
using Kiss.Bff.Beheer.Faq;
using Kiss.Bff.Beheer.Data;
using Microsoft.EntityFrameworkCore;
using Kiss.Bff.Beheer.Gespreksresultaten.Controllers;
using Kiss.Bff.Beheer.Links.Controllers;
using static Kiss.Bff.Beheer.Links.Controllers.LinksController;
using Kiss.Bff.NieuwsEnWerkinstructies.Controllers;
using Kiss.Bff.Beheer.Verwerking;

namespace Kiss.Bff.Test
{
Expand All @@ -23,15 +37,70 @@ public static void ClassCleanup()
s_factory?.Dispose();
}

public static IEnumerable<object[]> GetControllersWithAuthorizeAttributeAndMethods()
{
// Define the controllers and methods to test here
var controllersWithMethodsToTest = new List<(Type controllerType, string methodName, Type[] parameterTypes)>
{
(typeof(ReadContactmomentenDetails), "Get", new Type[0]),
(typeof(GespreksresultatenController), "PutGespreksresultaat", new[] { typeof(Guid), typeof(GespreksresultaatModel), typeof(CancellationToken) }),
(typeof(GespreksresultatenController), "PostGespreksresultaat", new[] { typeof(GespreksresultaatModel), typeof(CancellationToken)}),
(typeof(GespreksresultatenController), "DeleteGespreksresultaat", new[] { typeof(Guid), typeof(CancellationToken)}),
(typeof(LinksController), "PutLink", new[] { typeof(int), typeof(LinkPutModel),typeof(CancellationToken)}),
(typeof(LinksController), "PostLink", new[] { typeof(LinkPostModel) }),
(typeof(LinksController), "DeleteLink", new[] { typeof(int) }),
(typeof(SkillsController), "PutSkill", new[] { typeof(int), typeof(SkillPutModel), typeof(CancellationToken) }),
(typeof(SkillsController), "PostSkill", new[] { typeof(SkillPostModel), typeof(CancellationToken) }),
(typeof(GetVerwerkingsLogs), "Get", new Type[0]),
// Add more controller, method, and parameter combinations as needed
};

foreach (var (controllerType, methodName, parameterTypes) in controllersWithMethodsToTest)
{
yield return new object[] { controllerType, methodName, parameterTypes };
}
}

[DataTestMethod]
[DataRow("/api/postcontactmomenten", "post")]
[DataRow("/api/internetaak/api/version/objects", "post")]
[DataRow("/api/faq")]
[DataRow("/api/contactmomentendetails?id=1")]
public async Task Test(string url, string method = "get")
{
using var request = new HttpRequestMessage(new(method), url);
using var response = await s_client.SendAsync(request);
Assert.AreEqual(Unauthorized, response.StatusCode);
}

[DataTestMethod]
[DynamicData(nameof(GetControllersWithAuthorizeAttributeAndMethods), DynamicDataSourceType.Method)]
public async Task TestAuthorizeAttribute(Type controllerType, string methodName, Type[] parameterTypes)
{
// Manually create an instance of the controller
var dbContextOptions = new DbContextOptionsBuilder<BeheerDbContext>()
.UseInMemoryDatabase(databaseName: "TestDatabase")
.Options;
var dbContext = new BeheerDbContext(dbContextOptions);
var controller = Activator.CreateInstance(controllerType, dbContext) as ControllerBase;

// Assert that the controller instance is not null
Assert.IsNotNull(controller);

// Retrieve the method to test
var method = controllerType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, parameterTypes, null);

// Assert that the method exists
Assert.IsNotNull(method);

// Retrieve the Authorize attribute
var authorizeAttribute = method.GetCustomAttributes(typeof(AuthorizeAttribute), true)
.FirstOrDefault() as AuthorizeAttribute;

// Assert that the Authorize attribute exists and has the expected policy
Assert.IsNotNull(authorizeAttribute);
Assert.AreEqual(Policies.RedactiePolicy, authorizeAttribute.Policy);
}
}
}

27 changes: 9 additions & 18 deletions Kiss.Bff.Test/GetFaqTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,28 @@
namespace Kiss.Bff.Test
{
[TestClass]
public class GetFaqTests
public class GetFaqTests : TestHelper
{
private BeheerDbContext _dbContext;

[TestInitialize]
public void Initialize()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();

var options = new DbContextOptionsBuilder<BeheerDbContext>()
.UseInMemoryDatabase(databaseName: "TestDatabase")
.UseInternalServiceProvider(serviceProvider)
.Options;

_dbContext = new BeheerDbContext(options);
InitializeDatabase();
}

[TestCleanup]
public void Cleanup()
{
_dbContext.Database.EnsureDeleted();
_dbContext.Dispose();
using var dbContext = new BeheerDbContext(_dbContextOptions);
dbContext.Database.EnsureDeleted();
dbContext.Dispose();
}

[TestMethod]
public void Get_ReturnsTopQuestions()
PascalIcatt marked this conversation as resolved.
Show resolved Hide resolved
{
using var dbContext = new BeheerDbContext(_dbContextOptions);
// Arrange
var controller = new GetFaq(_dbContext);
var controller = new GetFaq(dbContext);

var testData = new List<ContactmomentDetails>
{
Expand All @@ -62,8 +53,8 @@ public void Get_ReturnsTopQuestions()
},
};

_dbContext.ContactMomentDetails.AddRange(testData);
_dbContext.SaveChanges();
dbContext.ContactMomentDetails.AddRange(testData);
dbContext.SaveChanges();

// Act
var result = controller.Get() as OkObjectResult;
Expand Down
68 changes: 28 additions & 40 deletions Kiss.Bff.Test/ReadContactmomentenDetailsTests.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
using Kiss.Bff.Beheer.Data;
using System;
using System.Linq.Expressions;
using System.Security.Claims;
using Kiss.Bff.Beheer.Data;
using Kiss.Bff.ZaakGerichtWerken.Contactmomenten;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace Kiss.Bff.Test
{
[TestClass]
public class ReadContactmomentenDetailsTests
public class ReadContactmomentenDetailsTests : TestHelper
{
private BeheerDbContext _dbContext;
private IServiceProvider? _serviceProvider;

[TestInitialize]
public void Initialize()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();

var options = new DbContextOptionsBuilder<BeheerDbContext>()
.UseInMemoryDatabase(databaseName: "TestDatabase")
.UseInternalServiceProvider(serviceProvider)
.Options;

_dbContext = new BeheerDbContext(options);
InitializeDatabase();
SeedTestData();

var services = new ServiceCollection();
var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.NameIdentifier, "testuser"),
new Claim(ClaimTypes.Role, Policies.RedactiePolicy)
}));
services.AddSingleton<IHttpContextAccessor>(_ => new HttpContextAccessor { HttpContext = new DefaultHttpContext { User = user } });
_serviceProvider = services.BuildServiceProvider();
}

[TestCleanup]
public void Cleanup()
{
_dbContext.Database.EnsureDeleted();
_dbContext.Dispose();
using var dbContext = new BeheerDbContext(_dbContextOptions);
dbContext.Database.EnsureDeleted();
dbContext.Dispose();
}

private void SeedTestData()
{
using var dbContext = new BeheerDbContext(_dbContextOptions);
var contactmoment1 = new ContactmomentDetails
{
Id = "1",
Expand All @@ -54,15 +61,16 @@ private void SeedTestData()
Vraag = "Question 2"
};

_dbContext.ContactMomentDetails.AddRange(contactmoment1, contactmoment2);
_dbContext.SaveChanges();
dbContext.ContactMomentDetails.AddRange(contactmoment1, contactmoment2);
dbContext.SaveChanges();
}

[TestMethod]
public async Task Get_ValidId_ReturnsOk()
{
using var dbContext = new BeheerDbContext(_dbContextOptions);
// Arrange
var controller = new ReadContactmomentenDetails(_dbContext);
var controller = new ReadContactmomentenDetails(dbContext);
var validId = "1";

// Act
Expand All @@ -80,8 +88,9 @@ public async Task Get_ValidId_ReturnsOk()
[TestMethod]
public async Task Get_InvalidId_ReturnsNotFound()
{
using var dbContext = new BeheerDbContext(_dbContextOptions);
// Arrange
var controller = new ReadContactmomentenDetails(_dbContext);
var controller = new ReadContactmomentenDetails(dbContext);
var invalidId = "nonexistent";

// Act
Expand All @@ -91,26 +100,5 @@ public async Task Get_InvalidId_ReturnsNotFound()
Assert.IsNotNull(result);
Assert.AreEqual(404, result.StatusCode);
}

[TestMethod]
public void Get_All_ReturnsOkWithContactmomenten()
{
// Arrange
var controller = new ReadContactmomentenDetails(_dbContext);

// Act
var result = controller.Get() as OkObjectResult;

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(200, result.StatusCode);

var contactmomenten = result.Value as IEnumerable<ContactmomentDetails>;
Assert.IsNotNull(contactmomenten);

var contactmomentList = contactmomenten.ToList();
Assert.IsNotNull(contactmomentList);
Assert.AreEqual(2, contactmomentList.Count());
}
}
}
35 changes: 14 additions & 21 deletions Kiss.Bff.Test/ReadContactverzoekenVragensetsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,55 @@
using Kiss.Bff.ZaakGerichtWerken.Contactverzoeken;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace Kiss.Bff.Test
{
[TestClass]
public class ReadContactverzoekenVragenSetsTests
public class ReadContactverzoekenVragenSetsTests : TestHelper
{
private BeheerDbContext _dbContext;

[TestInitialize]
public void Initialize()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();

var options = new DbContextOptionsBuilder<BeheerDbContext>()
.UseInMemoryDatabase(databaseName: "TestDatabase")
.UseInternalServiceProvider(serviceProvider)
.Options;

_dbContext = new BeheerDbContext(options);
InitializeDatabase();
}

[TestCleanup]
public void Cleanup()
{
_dbContext.Database.EnsureDeleted();
_dbContext.Dispose();
using var dbContext = new BeheerDbContext(_dbContextOptions);
dbContext.Database.EnsureDeleted();
dbContext.Dispose();
}

[TestMethod]
public async Task Get_ContactVerzoekVragenSetsExist_ReturnsOkWithList()
{
using var dbContext = new BeheerDbContext(_dbContextOptions);
// Arrange
var controller = new ReadContactverzoekenVragenSets(_dbContext);
var controller = new ReadContactverzoekenVragenSets(dbContext);

var vragenSets = new List<ContactVerzoekVragenSet>
{
new ContactVerzoekVragenSet
{
Id = 1,
Naam = "VragenSet 1",
Titel = "VragenSet 1",
JsonVragen = "{ \"Question1\": \"Answer1\" }",
AfdelingId = "Dept1"
},
new ContactVerzoekVragenSet
{
Id = 2,
Naam = "VragenSet 2",
Titel = "VragenSet 2",
JsonVragen = "{ \"Question2\": \"Answer2\" }",
AfdelingId = "Dept2"
}
};

await _dbContext.ContactVerzoekVragenSets.AddRangeAsync(vragenSets);
await _dbContext.SaveChangesAsync();
await dbContext.ContactVerzoekVragenSets.AddRangeAsync(vragenSets);
await dbContext.SaveChangesAsync();

// Act
var result = await controller.Get(CancellationToken.None) as OkObjectResult;
Expand All @@ -75,8 +67,9 @@ public async Task Get_ContactVerzoekVragenSetsExist_ReturnsOkWithList()
[TestMethod]
public async Task Get_NoContactVerzoekVragenSets_ReturnsOkWithEmptyList()
{
using var dbContext = new BeheerDbContext(_dbContextOptions);
// Arrange
var controller = new ReadContactverzoekenVragenSets(_dbContext);
var controller = new ReadContactverzoekenVragenSets(dbContext);

// Act
var result = await controller.Get(CancellationToken.None) as OkObjectResult;
Expand Down
Loading
Loading