Skip to content

Commit

Permalink
Merge pull request #4422 from dfe-analytical-services/master
Browse files Browse the repository at this point in the history
Merge Master back into dev
  • Loading branch information
N-moh authored Nov 20, 2023
2 parents 39cf5d5 + c4a1948 commit c8804c9
Show file tree
Hide file tree
Showing 19 changed files with 776 additions and 337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PreReleaseUserServicePermissionTests
public async Task GetPreReleaseUsers()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanAssignPrereleaseContactsToSpecificRelease)
.SetupResourceCheckToFail(_release, CanAssignPreReleaseUsersToSpecificRelease)
.AssertForbidden(
userService =>
{
Expand All @@ -48,7 +48,7 @@ await PolicyCheckBuilder<SecurityPolicies>()
public async Task GetPreReleaseUsersInvitePlan()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanAssignPrereleaseContactsToSpecificRelease)
.SetupResourceCheckToFail(_release, CanAssignPreReleaseUsersToSpecificRelease)
.AssertForbidden(
userService =>
{
Expand All @@ -66,7 +66,7 @@ await PolicyCheckBuilder<SecurityPolicies>()
public async Task InvitePreReleaseUsers()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanAssignPrereleaseContactsToSpecificRelease)
.SetupResourceCheckToFail(_release, CanAssignPreReleaseUsersToSpecificRelease)
.AssertForbidden(
userService =>
{
Expand All @@ -84,7 +84,7 @@ await PolicyCheckBuilder<SecurityPolicies>()
public async Task RemovePreReleaseUser()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanAssignPrereleaseContactsToSpecificRelease)
.SetupResourceCheckToFail(_release, CanAssignPreReleaseUsersToSpecificRelease)
.AssertForbidden(
userService =>
{
Expand All @@ -95,21 +95,6 @@ await PolicyCheckBuilder<SecurityPolicies>()
);
}

[Fact]
public async Task SendPreReleaseUserInviteEmails()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanUpdateSpecificRelease)
.AssertForbidden(
userService =>
{
var service = SetupPreReleaseUserService(
userService: userService.Object);
return service.SendPreReleaseUserInviteEmails(_release.Id);
}
);
}

private Mock<IPersistenceHelper<ContentDbContext>> DefaultPersistenceHelperMock()
{
return MockPersistenceHelper<ContentDbContext, Release>(_release.Id, _release);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public async Task InvitePreReleaseUsers_FailsSendingEmail_ExistingUser_ApprovedR

await using (var context = InMemoryApplicationDbContext(contextId))
{
Assert.Empty(context.UserReleaseRoles);
Assert.Empty(context.UserReleaseRoles);
Assert.Empty(context.UserReleaseInvites);
}
}
Expand Down Expand Up @@ -1739,99 +1739,6 @@ await userAndRolesDbContext.AddAsync(
}
}

[Fact]
public async Task SendPreReleaseUserInviteEmails()
{
var release = new Release
{
ReleaseName = "2020",
TimePeriodCoverage = TimeIdentifier.CalendarYear,
Publication = new Publication {Title = "Test publication"},
PublishScheduled = PublishedScheduledStartOfDay
};
var contextId = Guid.NewGuid().ToString();

await using (var context = InMemoryApplicationDbContext(contextId))
{
await context.AddRangeAsync(
release,
new User
{
Email = "[email protected]",
},
new UserReleaseInvite
{
Release = release,
Role = ReleaseRole.PrereleaseViewer,
Email = "[email protected]",
EmailSent = false,
},
new User
{
Email = "[email protected]"
},
new UserReleaseInvite
{
Release = release,
Role = ReleaseRole.PrereleaseViewer,
Email = "[email protected]",
EmailSent = true,
}
);

await context.SaveChangesAsync();
}

var emailService = new Mock<IEmailService>(MockBehavior.Strict);

var expectedTemplateValues = GetExpectedPreReleaseTemplateValues(release, newUser: false);

emailService.Setup(mock => mock.SendEmail(
"[email protected]",
PreReleaseTemplateId,
expectedTemplateValues
))
.Returns(Unit.Instance);

var preReleaseService = new Mock<IPreReleaseService>(MockBehavior.Strict);
SetupGetPrereleaseWindow(preReleaseService, release);

await using (var context = InMemoryApplicationDbContext(contextId))
await using (var userAndRolesDbContext = InMemoryUserAndRolesDbContext(contextId))
{
var service = SetupPreReleaseUserService(
context,
usersAndRolesDbContext: userAndRolesDbContext,
preReleaseService: preReleaseService.Object,
emailService: emailService.Object
);

var result = await service.SendPreReleaseUserInviteEmails(release.Id);

emailService.Verify(
s => s.SendEmail(
"[email protected]",
PreReleaseTemplateId,
expectedTemplateValues
), Times.Once
);

VerifyAllMocks(emailService, preReleaseService);

result.AssertRight();
}

await using (var context = InMemoryApplicationDbContext(contextId))
{
var updatedInvite = context.UserReleaseInvites
.Single(i => i.Email == "[email protected]");

Assert.Equal(release.Id, updatedInvite.ReleaseId);
Assert.Equal(ReleaseRole.PrereleaseViewer, updatedInvite.Role);
Assert.True(updatedInvite.EmailSent);
}
}

private static Dictionary<string, dynamic> GetExpectedPreReleaseTemplateValues(Release release, bool newUser)
{
return new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,25 @@
using GovUk.Education.ExploreEducationStatistics.Common.Model;
using GovUk.Education.ExploreEducationStatistics.Common.Services;
using GovUk.Education.ExploreEducationStatistics.Common.Services.Interfaces.Security;
using GovUk.Education.ExploreEducationStatistics.Common.Utils;
using GovUk.Education.ExploreEducationStatistics.Content.Model;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Database;
using GovUk.Education.ExploreEducationStatistics.Content.Model.Repository.Interfaces;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
using static GovUk.Education.ExploreEducationStatistics.Admin.Security.SecurityPolicies;
using static GovUk.Education.ExploreEducationStatistics.Common.Tests.Utils.MockUtils;
using static GovUk.Education.ExploreEducationStatistics.Admin.Tests.Services.DbUtils;
using static GovUk.Education.ExploreEducationStatistics.Common.Tests.Utils.PermissionTestUtils;
using IReleaseRepository = GovUk.Education.ExploreEducationStatistics.Admin.Services.Interfaces.IReleaseRepository;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Tests.Services
{
public class ReleaseApprovalServicePermissionTests
{
private static readonly Publication Publication = new()
{
Id = Guid.NewGuid()
};

private readonly Release _release = new()
{
Id = Guid.NewGuid(),
PublicationId = Publication.Id,
Publication = new Publication(),
Published = DateTime.Now,
TimePeriodCoverage = TimeIdentifier.April
};
Expand All @@ -42,52 +36,79 @@ public class ReleaseApprovalServicePermissionTests
public async Task GetReleaseStatuses()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanViewReleaseStatusHistory)
.AssertForbidden(
userService =>
.SetupResourceCheckToFailWithMatcher<Release>(r => r.Id == _release.Id, CanViewReleaseStatusHistory)
.AssertForbidden(async userService =>
{
var contentDbContextId = Guid.NewGuid().ToString();
await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
var service = BuildService(userService.Object);
return service.GetReleaseStatuses(_release.Id);
await contentDbContext.AddRangeAsync(_release);
await contentDbContext.SaveChangesAsync();
}
);

await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
var service = BuildService(contentDbContext: contentDbContext, userService.Object);
return await service.GetReleaseStatuses(_release.Id);
}
});
}

[Fact]
public async Task UpdateReleaseStatus_Draft()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanMarkSpecificReleaseAsDraft)
.AssertForbidden(
userService =>
.SetupResourceCheckToFailWithMatcher<Release>(r => r.Id == _release.Id, CanMarkSpecificReleaseAsDraft)
.AssertForbidden(async userService =>
{
var contentDbContextId = Guid.NewGuid().ToString();
await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
var service = BuildService(userService.Object);
return service.CreateReleaseStatus(
await contentDbContext.AddRangeAsync(_release);
await contentDbContext.SaveChangesAsync();
}

await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
var service = BuildService(contentDbContext: contentDbContext, userService.Object);

return await service.CreateReleaseStatus(
_release.Id,
new ReleaseStatusCreateRequest
{
ApprovalStatus = ReleaseApprovalStatus.Draft
}
);
}
);
});
}

[Fact]
public async Task UpdateReleaseStatus_HigherLevelReview()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanSubmitSpecificReleaseToHigherReview)
.AssertForbidden(
userService =>
.SetupResourceCheckToFailWithMatcher<Release>(r => r.Id == _release.Id, CanSubmitSpecificReleaseToHigherReview)
.AssertForbidden(async userService =>
{
var service = BuildService(userService.Object);
return service.CreateReleaseStatus(
_release.Id,
new ReleaseStatusCreateRequest
{
ApprovalStatus = ReleaseApprovalStatus.HigherLevelReview
}
);
var contentDbContextId = Guid.NewGuid().ToString();
await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
await contentDbContext.AddRangeAsync(_release);
await contentDbContext.SaveChangesAsync();
}

await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
var service = BuildService(contentDbContext: contentDbContext, userService.Object);

return await service.CreateReleaseStatus(
_release.Id,
new ReleaseStatusCreateRequest
{
ApprovalStatus = ReleaseApprovalStatus.HigherLevelReview
}
);
}
}
);
}
Expand All @@ -96,27 +117,34 @@ await PolicyCheckBuilder<SecurityPolicies>()
public async Task UpdateReleaseStatus_Approve()
{
await PolicyCheckBuilder<SecurityPolicies>()
.SetupResourceCheckToFail(_release, CanApproveSpecificRelease)
.AssertForbidden(
userService =>
.SetupResourceCheckToFailWithMatcher<Release>(r => r.Id == _release.Id, CanApproveSpecificRelease)
.AssertForbidden(async userService =>
{
var contentDbContextId = Guid.NewGuid().ToString();
await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
var service = BuildService(userService.Object);
return service.CreateReleaseStatus(
await contentDbContext.AddRangeAsync(_release);
await contentDbContext.SaveChangesAsync();
}

await using (var contentDbContext = InMemoryApplicationDbContext(contentDbContextId))
{
var service = BuildService(contentDbContext: contentDbContext, userService.Object);
return await service.CreateReleaseStatus(
_release.Id,
new ReleaseStatusCreateRequest
{
ApprovalStatus = ReleaseApprovalStatus.Approved
}
);
}
);
});
}

private ReleaseApprovalService BuildService(IUserService userService)
private ReleaseApprovalService BuildService(ContentDbContext contentDbContext, IUserService userService)
{
return new ReleaseApprovalService(
Mock.Of<ContentDbContext>(),
DefaultPersistenceHelperMock().Object,
contentDbContext,
new DateTimeProvider(),
userService,
Mock.Of<IPublishingService>(),
Expand All @@ -128,17 +156,9 @@ private ReleaseApprovalService BuildService(IUserService userService)
Mock.Of<IReleaseRepository>(),
Options.Create(new ReleaseApprovalOptions()),
Mock.Of<IUserReleaseRoleService>(),
Mock.Of<IEmailTemplateService>()
Mock.Of<IEmailTemplateService>(),
Mock.Of<IUserRepository>()
);
}

private Mock<IPersistenceHelper<ContentDbContext>> DefaultPersistenceHelperMock()
{
var mock = MockPersistenceHelper<ContentDbContext, Release>();
SetupCall(mock, _release.Id, _release);
SetupCall(mock, Publication.Id, Publication);

return mock;
}
}
}
Loading

0 comments on commit c8804c9

Please sign in to comment.