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

DP 486 - Editing person / person invite roles #553

Merged
merged 17 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
29810b9
Initial work on change person invite role form, pending api client me…
andymantell Sep 3, 2024
040b435
Move hardcoded scopes into constants
andymantell Sep 4, 2024
a86138f
Merge branch 'main' into feature/dp-486-user-management-change-users-…
andymantell Sep 4, 2024
57fb391
Merge branch 'main' into feature/dp-486-user-management-change-users-…
andymantell Sep 5, 2024
1997586
Fix AddUser tests to cope with scopes that were moved to constants
andymantell Sep 5, 2024
7ba5c68
Integrate person invite update form with new api client method
andymantell Sep 5, 2024
34297c0
add tests for the change user role form in person invite mode
andymantell Sep 6, 2024
19a0458
Merge branch 'main' into feature/dp-486-user-management-change-users-…
andymantell Sep 6, 2024
fc9165c
Initial work integrating api client methods to update organisation_pe…
andymantell Sep 6, 2024
aaec4c1
Merge branch 'main' into feature/dp-486-user-management-change-users-…
andymantell Sep 9, 2024
4579f5c
Tweak code to accomodate fixed api client
andymantell Sep 9, 2024
ec04d96
Add tests for changing a persons user role
andymantell Sep 9, 2024
5ad23cb
Split change user role person and person invite tests into separate f…
andymantell Sep 9, 2024
f1797bd
change user role form markup tweaks
andymantell Sep 9, 2024
dd7322b
Fix bug whereby list of users was not populated when submitting empty…
andymantell Sep 9, 2024
35397ec
Replace some hardcoded instances of scopes with the proper constants
andymantell Sep 9, 2024
5f7641a
Fix user summary page tests to cope with post endpoint that now corre…
andymantell Sep 9, 2024
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
25 changes: 13 additions & 12 deletions Frontend/CO.CDP.OrganisationApp.Tests/Pages/Users/AddUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using CO.CDP.OrganisationApp.Pages.Users;
using Microsoft.AspNetCore.Mvc.RazorPages;
using CO.CDP.OrganisationApp.Constants;

namespace CO.CDP.OrganisationApp.Tests.Pages.Users;

Expand All @@ -24,7 +25,7 @@ public void OnGet_ShouldInitializeModel_WhenPersonInviteStateExists()
FirstName = "John",
LastName = "Johnson",
Email = "john@johnson.com",
Scopes = new List<string> { AddUserModel.ScopeAdmin, AddUserModel.ScopeEditor }
Scopes = new List<string> { PersonScopes.Admin, PersonScopes.Editor }
};

_mockSession
Expand All @@ -37,7 +38,7 @@ public void OnGet_ShouldInitializeModel_WhenPersonInviteStateExists()
Assert.Equal("Johnson", _addUserModel.LastName);
Assert.Equal("john@johnson.com", _addUserModel.Email);
Assert.True(_addUserModel.IsAdmin);
Assert.Equal(AddUserModel.ScopeEditor, _addUserModel.Role);
Assert.Equal(PersonScopes.Editor, _addUserModel.Role);
Assert.IsType<PageResult>(result);
}

Expand Down Expand Up @@ -73,7 +74,7 @@ public void OnPost_ShouldUpdateSessionAndRedirect_WhenModelStateIsValid()
_addUserModel.LastName = "Johnson";
_addUserModel.Email = "john@johnson.com";
_addUserModel.IsAdmin = true;
_addUserModel.Role = AddUserModel.ScopeEditor;
_addUserModel.Role = PersonScopes.Editor;

var initialState = new PersonInviteState();
_mockSession.Setup(s => s.Get<PersonInviteState>(PersonInviteState.TempDataKey)).Returns(initialState);
Expand All @@ -85,8 +86,8 @@ public void OnPost_ShouldUpdateSessionAndRedirect_WhenModelStateIsValid()
state.FirstName == "John" &&
state.LastName == "Johnson" &&
state.Email == "john@johnson.com" &&
state.Scopes.Contains(AddUserModel.ScopeAdmin) &&
state.Scopes.Contains(AddUserModel.ScopeEditor)
state.Scopes.Contains(PersonScopes.Admin) &&
state.Scopes.Contains(PersonScopes.Editor)
)), Times.Once);

var redirectResult = Assert.IsType<RedirectToPageResult>(result);
Expand Down Expand Up @@ -114,18 +115,18 @@ public void UpdateFields_ShouldUpdateAllFields_WhenFieldsAreNotEmpty()
public void UpdateScopes_ShouldUpdateScopes_WhenIsAdminAndRoleIsEditor()
{
_addUserModel.IsAdmin = true;
_addUserModel.Role = AddUserModel.ScopeEditor;
_addUserModel.Role = PersonScopes.Editor;

var initialState = new PersonInviteState
{
Scopes = new List<string> { AddUserModel.ScopeViewer }
Scopes = new List<string> { PersonScopes.Viewer }
};

var updatedState = _addUserModel.UpdateScopes(initialState);

Assert.Contains(AddUserModel.ScopeAdmin, updatedState.Scopes ?? []);
Assert.Contains(AddUserModel.ScopeEditor, updatedState.Scopes ?? []);
Assert.DoesNotContain(AddUserModel.ScopeViewer, updatedState.Scopes ?? []);
Assert.Contains(PersonScopes.Admin, updatedState.Scopes ?? []);
Assert.Contains(PersonScopes.Editor, updatedState.Scopes ?? []);
Assert.DoesNotContain(PersonScopes.Viewer, updatedState.Scopes ?? []);
}

[Fact]
Expand All @@ -136,7 +137,7 @@ public void InitModel_ShouldSetModelPropertiesFromState_WhenStateHasData()
FirstName = "John",
LastName = "Johnson",
Email = "john@johnson.com",
Scopes = new List<string> { AddUserModel.ScopeAdmin, AddUserModel.ScopeEditor }
Scopes = new List<string> { PersonScopes.Admin, PersonScopes.Editor }
};

_addUserModel.InitModel(state);
Expand All @@ -145,6 +146,6 @@ public void InitModel_ShouldSetModelPropertiesFromState_WhenStateHasData()
Assert.Equal("Johnson", _addUserModel.LastName);
Assert.Equal("john@johnson.com", _addUserModel.Email);
Assert.True(_addUserModel.IsAdmin);
Assert.Equal(AddUserModel.ScopeEditor, _addUserModel.Role);
Assert.Equal(PersonScopes.Editor, _addUserModel.Role);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using Moq;
using CO.CDP.OrganisationApp.Pages.Users;
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Constants;
using CO.CDP.OrganisationApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CO.CDP.OrganisationApp.Tests.Pages.Users;

public class ChangePersonInviteUserRoleTests
{
private readonly Mock<ISession> _mockSession;
private readonly Mock<IOrganisationClient> _mockOrganisationClient;
private readonly ChangeUserRoleModel _changeUserRoleModel;

public ChangePersonInviteUserRoleTests()
{
_mockSession = new Mock<ISession>();
_mockSession.Setup(s => s.Get<UserDetails>(Session.UserDetailsKey))
.Returns(new UserDetails() { UserUrn = "whatever" });

_mockOrganisationClient = new Mock<IOrganisationClient>();
_changeUserRoleModel = new ChangeUserRoleModel(_mockOrganisationClient.Object, _mockSession.Object);
}

[Fact]
public async Task OnGet_ShouldInitializeModel_WhenPersonInviteExists()
{
var mockPersonInvite = new PersonInviteModel(
"a@b.com",
"John",
new Guid(),
"Smith",
new List<string> { PersonScopes.Admin, PersonScopes.Editor }
);

_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonInvitesAsync(mockPersonInvite.Id))
.ReturnsAsync(new List<PersonInviteModel>() { mockPersonInvite });

var result = await _changeUserRoleModel.OnGetPersonInvite();

Assert.Equal("John Smith", _changeUserRoleModel.UserFullName);
Assert.True(_changeUserRoleModel.IsAdmin);
Assert.Equal(PersonScopes.Editor, _changeUserRoleModel.Role);
}

[Fact]
public async Task OnGet_RedirectsToNotFound_WhenPersonInviteDoesntExist()
{
_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonInvitesAsync(It.IsAny<Guid>()))
.ThrowsAsync(new CO.CDP.Organisation.WebApiClient.ApiException("message", 404, "response", null, null));

var result = await _changeUserRoleModel.OnGetPersonInvite();

var redirectResult = Assert.IsType<RedirectResult>(result);

Assert.Equal("/page-not-found", redirectResult.Url);
}

[Fact]
public async Task OnPostPersonInvite_ShouldReturnPageResult_WhenModelStateIsInvalid()
{
var mockPersonInvite = new PersonInviteModel(
"a@b.com",
"John",
new Guid(),
"Smith",
new List<string> { PersonScopes.Admin, PersonScopes.Editor }
);

_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonInvitesAsync(mockPersonInvite.Id))
.ReturnsAsync(new List<PersonInviteModel>() { mockPersonInvite });

_changeUserRoleModel.IsAdmin = true;
_changeUserRoleModel.Role = null;

_changeUserRoleModel.ModelState.AddModelError("Role", "Required");

var result = await _changeUserRoleModel.OnPostPersonInvite();
Assert.IsType<PageResult>(result);
}

[Fact]
public async Task OnPostPersonInvite_RedirectsToNotFound_WhenPersonInviteDoesntExist()
{
_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonInvitesAsync(It.IsAny<Guid>()))
.ThrowsAsync(new CO.CDP.Organisation.WebApiClient.ApiException("message", 404, "response", null, null));

var result = await _changeUserRoleModel.OnPostPersonInvite();

var redirectResult = Assert.IsType<RedirectResult>(result);

Assert.Equal("/page-not-found", redirectResult.Url);
}

[Fact]
public async Task OnPostPersonInvite_ShouldUpdatePersonInviteTableAndRedirect_WhenModelStateIsValid()
{
var mockPersonInvite = new PersonInviteModel(
"a@b.com",
"John",
new Guid(),
"Smith",
new List<string> { PersonScopes.Admin, PersonScopes.Editor }
);

_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonInvitesAsync(mockPersonInvite.Id))
.ReturnsAsync(new List<PersonInviteModel>() { mockPersonInvite });

_changeUserRoleModel.IsAdmin = true;
_changeUserRoleModel.Role = PersonScopes.Viewer;

var result = await _changeUserRoleModel.OnPostPersonInvite();

var redirectResult = Assert.IsType<RedirectToPageResult>(result);
Assert.Equal("UserSummary", redirectResult.PageName);

_mockOrganisationClient.Verify(s => s.UpdatePersonInviteAsync(
It.IsAny<Guid>(),
It.IsAny<Guid>(),
It.Is<UpdateInvitedPersonToOrganisation>(u =>
u.Scopes.Contains(PersonScopes.Viewer) &&
u.Scopes.Contains(PersonScopes.Admin) &&
u.Scopes.Count == 2
)
),
Times.Once
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using Moq;
using CO.CDP.OrganisationApp.Pages.Users;
using CO.CDP.Organisation.WebApiClient;
using CO.CDP.OrganisationApp.Constants;
using CO.CDP.OrganisationApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CO.CDP.OrganisationApp.Tests.Pages.Users;

public class ChangePersonUserRoleTests
{
private readonly Mock<ISession> _mockSession;
private readonly Mock<IOrganisationClient> _mockOrganisationClient;
private readonly ChangeUserRoleModel _changeUserRoleModel;

public ChangePersonUserRoleTests()
{
_mockSession = new Mock<ISession>();
_mockSession.Setup(s => s.Get<UserDetails>(Session.UserDetailsKey))
.Returns(new UserDetails() { UserUrn = "whatever" });

_mockOrganisationClient = new Mock<IOrganisationClient>();
_changeUserRoleModel = new ChangeUserRoleModel(_mockOrganisationClient.Object, _mockSession.Object);
}

[Fact]
public async Task OnGet_ShouldInitializeModel_WhenPersonExists()
{
var mockPerson = new Organisation.WebApiClient.Person(
"a@b.com",
"John",
new Guid(),
"Smith",
new List<string> { PersonScopes.Admin, PersonScopes.Editor }
);

_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonsAsync(mockPerson.Id))
.ReturnsAsync(new List<Organisation.WebApiClient.Person>() { mockPerson });

var result = await _changeUserRoleModel.OnGetPerson();

Assert.Equal("John Smith", _changeUserRoleModel.UserFullName);
Assert.True(_changeUserRoleModel.IsAdmin);
Assert.Equal(PersonScopes.Editor, _changeUserRoleModel.Role);
}

[Fact]
public async Task OnGet_RedirectsToNotFound_WhenPersonDoesntExist()
{
_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonsAsync(It.IsAny<Guid>()))
.ThrowsAsync(new CO.CDP.Organisation.WebApiClient.ApiException("message", 404, "response", null, null));

var result = await _changeUserRoleModel.OnGetPerson();

var redirectResult = Assert.IsType<RedirectResult>(result);

Assert.Equal("/page-not-found", redirectResult.Url);
}

[Fact]
public async Task OnPostPerson_ShouldReturnPageResult_WhenModelStateIsInvalid()
{
var mockPerson = new Organisation.WebApiClient.Person(
"a@b.com",
"John",
new Guid(),
"Smith",
new List<string> { PersonScopes.Admin, PersonScopes.Editor }
);

_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonsAsync(mockPerson.Id))
.ReturnsAsync(new List<Organisation.WebApiClient.Person>() { mockPerson });

_changeUserRoleModel.IsAdmin = true;
_changeUserRoleModel.Role = null;

_changeUserRoleModel.ModelState.AddModelError("Role", "Required");

var result = await _changeUserRoleModel.OnPostPerson();
Assert.IsType<PageResult>(result);
}

[Fact]
public async Task OnPostPerson_RedirectsToNotFound_WhenPersonInviteDoesntExist()
{
_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonsAsync(It.IsAny<Guid>()))
.ThrowsAsync(new CO.CDP.Organisation.WebApiClient.ApiException("message", 404, "response", null, null));

var result = await _changeUserRoleModel.OnPostPerson();

var redirectResult = Assert.IsType<RedirectResult>(result);

Assert.Equal("/page-not-found", redirectResult.Url);
}

[Fact]
public async Task OnPostPerson_ShouldUpdatePersonTableAndRedirect_WhenModelStateIsValid()
{
var mockPerson = new Organisation.WebApiClient.Person(
"a@b.com",
"John",
new Guid(),
"Smith",
new List<string> { PersonScopes.Admin, PersonScopes.Editor }
);

_mockOrganisationClient
.Setup(s => s.GetOrganisationPersonsAsync(mockPerson.Id))
.ReturnsAsync(new List<Organisation.WebApiClient.Person>() { mockPerson });

_changeUserRoleModel.IsAdmin = true;
_changeUserRoleModel.Role = PersonScopes.Viewer;

var result = await _changeUserRoleModel.OnPostPerson();

var redirectResult = Assert.IsType<RedirectToPageResult>(result);
Assert.Equal("UserSummary", redirectResult.PageName);

_mockOrganisationClient.Verify(s => s.UpdateOrganisationPersonAsync(
It.IsAny<Guid>(),
It.IsAny<Guid>(),
It.Is<UpdatePersonToOrganisation>(u =>
u.Scopes.Contains(PersonScopes.Viewer) &&
u.Scopes.Contains(PersonScopes.Admin) &&
u.Scopes.Count == 2
)
),
Times.Once
);
}
}
Loading