Skip to content

Commit

Permalink
add single test for authorized parties returning instance delegations
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasisnes committed Oct 23, 2024
1 parent a1699a9 commit 6ff5fc2
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class AuthorizedPartyExternal
/// <summary>
/// Gets or sets a collection of all Authorized Instances
/// </summary>
public List<string> AuthorizedInstances { get; set; } = [];
public List<Resource> AuthorizedInstances { get; set; } = [];

/// <summary>
/// Gets or sets a set of subunits of this party, which the authorized subject also has some access to.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Net.Http.Json;
using Altinn.AccessManagement.Controllers;
using Altinn.AccessManagement.Core.Helpers.Extensions;
using Altinn.AccessManagement.Core.Models;
using Altinn.AccessManagement.Models;
using Altinn.AccessManagement.Tests.Fixtures;
using Altinn.AccessManagement.Tests.Scenarios;
using Altinn.AccessManagement.Tests.Seeds;
using Docker.DotNet.Models;

namespace Altinn.AccessManagement.Tests.Controllers;

/// <summary>
/// <see cref="AuthorizedPartiesController"/>
/// </summary>
public class V2AuthorizedPartiesControllerTest(WebApplicationFixture fixture) : IClassFixture<WebApplicationFixture>
{
private WebApplicationFixture Fixture { get; } = fixture;

private static Action<AcceptanceCriteriaComposer> WithAssertDbContainsDelegations(IParty from, IAccessManagementResource resource) => test =>
{
test.ApiAssertions.Add(async host =>
{
var delegations = await host.Repository.DelegationMetadataRepository.GetAllCurrentAppDelegationChanges(from.Party.PartyId.SingleToList(), resource.DbResource.ResourceRegistryId.SingleToList());
Assert.True(
delegations.Count > 0,
$"Couldn't find any delegations from {from.Party.PartyId} to app {resource.DbResource.ResourceRegistryId}");
});
};

private static Action<AcceptanceCriteriaComposer> WithAssertResponseContainsInstance(string resourceId, string instanceId) => test =>
{
test.ResponseAssertions.Add(async response =>
{
var delegations = await response.Content.ReadFromJsonAsync<IEnumerable<AuthorizedPartyExternal>>();
var result = delegations.Any(delegation => delegation.AuthorizedInstances.Any(instance => instance.ResourceId == resourceId && instance.InstanceId == instanceId));
Assert.True(result, $"Response don't contains instance delegations with resource Id {resourceId} and instance id {instanceId}");
});
};

/// <summary>
/// Seeds for <see cref="GET_AuthorizedParties"/>
/// </summary>
/// <param name="acceptanceCriteria">Acceptance Criteria</param>
/// <param name="actions">modifiers for <see cref="AcceptanceCriteriaComposer"/></param>
public class GetAuthorizedParties(string acceptanceCriteria, params Action<AcceptanceCriteriaComposer>[] actions) : AcceptanceCriteriaComposer(
acceptanceCriteria,
actions,
WithRequestRoute("accessmanagement", "api", "v1", "authorizedparties"),
WithRequestVerb(HttpMethod.Get))
{
/// <summary>
/// Seeds
/// </summary>
public static TheoryData<GetAuthorizedParties> Seeds() => [
new(
/* Acceptance Critieria */ @"
GIVEN that organization Voss has shared an instance with DAGL Olav for Orstad Accounting
WHEN DAGL Olav for Orstad Accounting requests authorized parties
THEN Organization should be in the list of authorized parties
AND the instance and resource iod should be included in list containing instances",

WithScenarios(
DelegationScenarios.Defaults,
DelegationScenarios.WithInstanceDelegation(OrganizationSeeds.VossAccounting.Defaults, PersonSeeds.Paula.Defaults, ResourceSeeds.ChalkboardResource.Defaults, "1337"),
TokenScenario.PersonToken(PersonSeeds.Olav.Defaults)),

WithAssertResponseStatusCodeSuccessful,
WithAssertResponseContainsInstance(ResourceSeeds.ChalkboardResource.Defaults.Identifier, "1337")),
];
}

/// <summary>
/// <see cref="AuthorizedPartiesController.GetAuthorizedParties(bool, CancellationToken)"/>
/// </summary>
/// <param name="acceptanceCriteria">acceptance test</param>
[Theory]
[MemberData(nameof(GetAuthorizedParties.Seeds), MemberType = typeof(GetAuthorizedParties))]
public async Task GET_AuthorizedParties(GetAuthorizedParties acceptanceCriteria) => await acceptanceCriteria.Test(Fixture);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data;
using System.Linq;
using Altinn.AccessManagement.Core.Enums;
using Altinn.AccessManagement.Core.Models;
Expand Down Expand Up @@ -132,6 +133,28 @@ public static Scenario WithRevokedDelegationToUser(IParty organization, IUserPro
]);
};

public static Scenario WithInstanceDelegation(IParty from, IParty to, IAccessManagementResource resource, string instanceId) => async mock =>
{
resource ??= ResourceSeeds.AltinnApp.Defaults;

mock.DbSeeds.AddRange([
async postgres => await postgres.DelegationMetadataRepository.InsertInstanceDelegation(
new()
{
DelegationChangeType = DelegationChangeType.Grant,
BlobStoragePolicyPath = "https://blob.storage.no",
BlobStorageVersionId = "v1",
ResourceId = resource.Resource.Identifier,
InstanceId = instanceId,
FromUuid = (Guid)from.Party.PartyUuid,
FromUuidType = string.IsNullOrEmpty(from.Party.SSN) ? Enums.UuidType.Organization : Enums.UuidType.Person,
ToUuid = (Guid)to.Party.PartyUuid,
ToUuidType = string.IsNullOrEmpty(to.Party.SSN) ? Enums.UuidType.Organization : Enums.UuidType.Person,
InstanceDelegationMode = InstanceDelegationMode.Normal,
}),
]);
};

/// <summary>
/// Adds mock context and db seeds. for given organization, person and resource
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions test/Altinn.AccessManagement.Tests/Seeds/ResourceSeeds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ public class ResourceBase : ServiceResource, IAccessManagementResource
};
}

public class ChalkboardResource : ResourceBase
{
public new static readonly ResourceType ResourceType = ResourceType.GenericAccessResource;

public new static readonly string Identifier = "chalkboard";

public static ChalkboardResource Defaults { get; } = new MaskinportenSchema();

public ChalkboardResource(params Action<ServiceResource>[] modifiers)
{
base.ResourceType = ResourceType.Systemresource;
base.Identifier = Identifier;

foreach (var modifer in modifiers)
{
modifer(this);
}
}
}

public class MaskinportenSchema : ResourceBase
{
public new static readonly string Identifier = "maskinportenschema";
Expand Down

0 comments on commit 6ff5fc2

Please sign in to comment.