Skip to content

Commit

Permalink
Fixed & refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HansDahle committed Nov 14, 2024
1 parent 690ccaf commit 6c57fd0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ static InternalRequestData()

foreach (var department in SupportedDepartments)
{
var user = PeopleServiceMock.AddTestProfile().SaveProfile();
LineOrgServiceMock.AddTestUser().MergeWithProfile(user).AsResourceOwner().WithFullDepartment(department).SaveProfile();
var user = PeopleServiceMock.AddTestProfile()
.WithFullDepartment(department)
.AsResourceOwner()
.SaveProfile();

LineOrgServiceMock.AddOrgUnit(department);
LineOrgServiceMock.AddOrgUnitManager(department, user);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
using Fusion.Testing;
using Fusion.Testing.Authentication.User;
using Fusion.Testing.Mocks;
using Fusion.Testing.Mocks.LineOrgService;
using Fusion.Testing.Mocks.OrgService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Fusion.Testing.Mocks.ProfileService;
using Xunit;
using Xunit.Abstractions;
using Fusion.Services.LineOrg.ApiModels;
Expand Down Expand Up @@ -44,14 +42,7 @@ public DepartmentRequestsTests(ResourceApiFixture fixture, ITestOutputHelper out

public async Task InitializeAsync()
{
user = fixture.AddProfile(FusionAccountType.Employee);
user.Department = TimelineDepartment;
user.FullDepartment = TimelineDepartment;

LineOrgServiceMock.AddTestUser()
.MergeWithProfile(user)
.AsResourceOwner()
.SaveProfile();
user = fixture.AddResourceOwner(TimelineDepartment);

userOrgUnit = fixture.AddOrgUnit(user.FullDepartment);

Expand Down Expand Up @@ -577,7 +568,7 @@ public async Task GetTimeline_ShouldNotIncludeTaskDetailsForOtherResourceOwner()
var timelineEnd = new DateTime(2020, 03, 31);

var response = await Client.TestClientGetAsync<TestResponse>(
$"/departments/{user.Department}/resources/personnel/?$expand=timeline&{ApiVersion}&timelineStart={timelineStart:O}&timelineEnd={timelineEnd:O}"
$"/departments/{TimelineDepartment}/resources/personnel/?$expand=timeline&{ApiVersion}&timelineStart={timelineStart:O}&timelineEnd={timelineEnd:O}"
);

var person = response.Value.value
Expand All @@ -600,15 +591,16 @@ public async Task GetTimeline_ShouldNotIncludeTaskDetailsForOtherResourceOwner()
public async Task GetTimelineShouldNotIncludePositionsOutsideTimeframe()
{
const string department = "PDP BTAD AWQ";
fixture.EnsureDepartment(department);
fixture.AddOrgUnit(department);

var project = new FusionTestProjectBuilder()
.WithPositions(10, 50)
.AddToMockService();

var profile = PeopleServiceMock.AddTestProfile().WithFullDepartment(department);
foreach (var position in project.Positions) profile.WithPosition(position);
profile.SaveProfile();
var profile = fixture.AddProfile(s => {
s.WithFullDepartment(department);
s.WithPositions(project.Positions);
});

using var scope = fixture.AdminScope();
var timelineStart = new DateTime(2020, 03, 01);
Expand Down Expand Up @@ -708,15 +700,13 @@ public async Task GetTimeline_ShouldNotIncludeRequestsOutsideTimeframe()
public async Task GetRequests_ShouldNotIncludeRequestsOutsideCurrentAllocations()
{
const string department = "PDP BTAD AWQ";
fixture.EnsureDepartment(department);
fixture.AddOrgUnit(department);

var project = new FusionTestProjectBuilder()
.WithPositions(10, 50)
.AddToMockService();

var profile = PeopleServiceMock.AddTestProfile().WithFullDepartment(department);
foreach (var position in project.Positions) profile.WithPosition(position);
profile.SaveProfile();
var profile = fixture.AddProfile(s => s.WithFullDepartment(department).WithPositions(project.Positions));

using var scope = fixture.AdminScope();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@


<ItemGroup>
<PackageReference Include="Bogus" Version="35.6.1"/>
<PackageReference Include="Bogus" Version="35.6.1" />
<PackageReference Include="Fusion.Integration.Profile.Abstractions" Version="8.0.6" />
<PackageReference Include="Fusion.ApiClients.Org" Version="8.0.4"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10"/>
<PackageReference Include="Fusion.ApiClients.Org" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Fusion.AspNetCore" Version="8.0.6"/>
<PackageReference Include="Fusion.AspNetCore" Version="8.0.6" />
<PackageReference Include="Microsoft.Data.OData" Version="5.8.5" />
<PackageReference Include="NetStandardHashLib" Version="1.0.0" />

</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using static System.Net.Mime.MediaTypeNames;
using System.Threading.Tasks;
using HashLib;
using HashLib.Checksum;

namespace Fusion.Testing.Mocks.ProfileService
{
Expand Down Expand Up @@ -58,9 +62,59 @@ public FusionTestUserBuilder WithPreferredContactMail(string mail)
return this;
}

/// <summary>
/// Will flag the profile as resource owner by setting the bool property to true.
/// The profile will also be added to the department above or CEC.
///
/// A LineOrg role will be added as well.
///
/// Should be called after the full department has been updated.
/// </summary>
/// <returns></returns>
public FusionTestUserBuilder AsResourceOwner()
{
profile.IsResourceOwner = true;

try
{
var depTokens = profile.FullDepartment.Split(' ');
// Lift manager 1 level up.

var parentFullDepartment = string.Join(" ", depTokens.SkipLast(1));
var parentDepartment = string.Join(" ", depTokens.SkipLast(1).TakeLast(3));

if (string.IsNullOrEmpty(parentDepartment))
parentDepartment = "CEC";
if (string.IsNullOrEmpty(parentFullDepartment))
parentFullDepartment = "CEC";

profile.FullDepartment = parentFullDepartment;
profile.Department = parentDepartment;

}
catch (Exception) { /* */ }

// Must add roles.. Create SAP id
if (profile.Roles is null)
profile.Roles = new List<ApiPersonRoleV3>();

// Generate sapId same way as lineorg mock
var hash = HashFactory.Checksum.CreateCRC32(0xF0F0F0F0);
var hashResult = hash.ComputeString(profile.FullDepartment);
var sapId = $"{Math.Abs(hashResult.GetInt())}";


profile.Roles = new List<ApiPersonRoleV3>
{
new ApiPersonRoleV3
{
Name = "Fusion.LineOrg.Manager",
Scope = new ApiPersonRoleScopeV3 { Type = "OrgUnit", Value = sapId },
IsActive = true,
OnDemandSupport = false
}
};

return this;
}
public FusionTestUserBuilder WithAccountType(FusionAccountType type)
Expand Down Expand Up @@ -93,6 +147,13 @@ public FusionTestUserBuilder WithDepartment(string department)
return this;
}

public FusionTestUserBuilder WithPositions(IEnumerable<ApiPositionV2> positions)
{
foreach (var position in positions)
WithPosition(position);
return this;
}

public FusionTestUserBuilder WithPosition(ApiPositionV2 position)
{
position.Instances.ForEach(instance => profile.Positions.Add(new ApiPersonPositionV3
Expand Down

0 comments on commit 6c57fd0

Please sign in to comment.