Skip to content

Commit

Permalink
Merge branch 'main' into feature/exclusions-section-content-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
andymantell authored Oct 7, 2024
2 parents 39e21d6 + d88a7d9 commit 75198b6
Show file tree
Hide file tree
Showing 46 changed files with 438 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ [ organisation ]
public static IEnumerable<object[]> TestCases()
{
yield return new object[] { $"/organisation/{testOrganisationId}/users/user-summary", new string[] { "Organisation has 2 users" } };
yield return new object[] { $"/organisation/{testOrganisationId}/users/{personInviteGuid}/change-role?handler=personInvite", new string[] { "Person invite Last name", "Can add, remove and edit users" } };
yield return new object[] { $"/organisation/{testOrganisationId}/users/{personInviteGuid}/change-role?handler=personInvite", new string[] { "Person invite Last name", "Select a role" } };
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void GetAnswer_ShouldReturnNull_WhenIsNotRequiredAndHasNoValue()
var model = new FormElementUrlInputModel
{
IsRequired = false,
HasValue = false
HasValue = null
};

var result = model.GetAnswer();
Expand Down Expand Up @@ -41,25 +41,14 @@ public void GetAnswer_ShouldReturnAnswer_WhenTextInputIsProvided()
public void SetAnswer_ShouldSetTextInputAndHasValue_WhenAnswerIsProvided()
{
var model = new FormElementUrlInputModel();
var answer = new FormAnswer { TextValue = "https://example.com" };
var answer = new FormAnswer { TextValue = "https://example.com", BoolValue = true };

model.SetAnswer(answer);

model.TextInput.Should().Be("https://example.com");
model.HasValue.Should().BeTrue();
}

[Fact]
public void SetAnswer_ShouldSetHasValueToFalse_WhenAnswerIsNullAndRedirectFromCheckYourAnswerPage()
{
var model = new FormElementUrlInputModel();
model.Initialize(new FormQuestion { IsRequired = false }, true);

model.SetAnswer(null);

model.HasValue.Should().BeFalse();
}

[Fact]
public void Validate_ShouldReturnError_WhenHasValueIsNullAndIsNotRequired()
{
Expand Down
17 changes: 7 additions & 10 deletions Frontend/CO.CDP.OrganisationApp.Tests/Pages/Users/AddUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void OnPost_ShouldUpdateSessionAndRedirect_WhenModelStateIsValid()
_addUserModel.FirstName = "John";
_addUserModel.LastName = "Johnson";
_addUserModel.Email = "[email protected]";
_addUserModel.IsAdmin = true;
_addUserModel.Role = OrganisationPersonScopes.Editor;

var initialState = new PersonInviteState();
Expand All @@ -86,7 +85,6 @@ public void OnPost_ShouldUpdateSessionAndRedirect_WhenModelStateIsValid()
state.FirstName == "John" &&
state.LastName == "Johnson" &&
state.Email == "[email protected]" &&
state.Scopes.Contains(OrganisationPersonScopes.Admin) &&
state.Scopes.Contains(OrganisationPersonScopes.Editor)
)), Times.Once);

Expand All @@ -111,11 +109,13 @@ public void UpdateFields_ShouldUpdateAllFields_WhenFieldsAreNotEmpty()
Assert.Equal("[email protected]", updatedState.Email);
}

[Fact]
public void UpdateScopes_ShouldUpdateScopes_WhenIsAdminAndRoleIsEditor()
[Theory]
[InlineData(OrganisationPersonScopes.Viewer)]
[InlineData(OrganisationPersonScopes.Editor)]
[InlineData(OrganisationPersonScopes.Admin)]
public void UpdateScopes_ShouldUpdateScopes_WhenRoleIsSelected(string organisationPersonScope)
{
_addUserModel.IsAdmin = true;
_addUserModel.Role = OrganisationPersonScopes.Editor;
_addUserModel.Role = organisationPersonScope;

var initialState = new PersonInviteState
{
Expand All @@ -124,10 +124,7 @@ public void UpdateScopes_ShouldUpdateScopes_WhenIsAdminAndRoleIsEditor()

var updatedState = _addUserModel.UpdateScopes(initialState);

Assert.Contains(OrganisationPersonScopes.Admin, updatedState.Scopes ?? []);
Assert.Contains(OrganisationPersonScopes.Editor, updatedState.Scopes ?? []);
Assert.Contains(OrganisationPersonScopes.Responder, updatedState.Scopes ?? []);
Assert.DoesNotContain(OrganisationPersonScopes.Viewer, updatedState.Scopes ?? []);
Assert.Contains(organisationPersonScope, updatedState.Scopes ?? []);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public async Task OnPostPersonInvite_ShouldReturnPageResult_WhenModelStateIsInva
_changeUserRoleModel.IsAdmin = true;
_changeUserRoleModel.Role = null;

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

var result = await _changeUserRoleModel.OnPostPersonInvite();
Assert.IsType<PageResult>(result);
Expand Down Expand Up @@ -106,14 +106,13 @@ public async Task OnPostPersonInvite_ShouldUpdatePersonInviteTableAndRedirect_Wh
"John",
new Guid(),
"Smith",
new List<string> { OrganisationPersonScopes.Admin, OrganisationPersonScopes.Editor }
new List<string> { OrganisationPersonScopes.Editor }
);

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

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

var result = await _changeUserRoleModel.OnPostPersonInvite();
Expand All @@ -126,8 +125,7 @@ public async Task OnPostPersonInvite_ShouldUpdatePersonInviteTableAndRedirect_Wh
It.IsAny<Guid>(),
It.Is<UpdateInvitedPersonToOrganisation>(u =>
u.Scopes.Contains(OrganisationPersonScopes.Viewer) &&
u.Scopes.Contains(OrganisationPersonScopes.Admin) &&
u.Scopes.Count == 2
u.Scopes.Count == 1
)
),
Times.Once
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ public async Task OnPostPerson_ShouldUpdatePersonTableAndRedirect_WhenModelState
"John",
new Guid(),
"Smith",
new List<string> { OrganisationPersonScopes.Admin, OrganisationPersonScopes.Editor }
new List<string> { OrganisationPersonScopes.Editor }
);

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

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

var result = await _changeUserRoleModel.OnPostPerson();
Expand All @@ -126,8 +125,7 @@ public async Task OnPostPerson_ShouldUpdatePersonTableAndRedirect_WhenModelState
It.IsAny<Guid>(),
It.Is<UpdatePersonToOrganisation>(u =>
u.Scopes.Contains(OrganisationPersonScopes.Viewer) &&
u.Scopes.Contains(OrganisationPersonScopes.Admin) &&
u.Scopes.Count == 2
u.Scopes.Count == 1
)
),
Times.Once
Expand Down
18 changes: 9 additions & 9 deletions Frontend/CO.CDP.OrganisationApp/Models/DynamicForms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public class QuestionAnswer

public class FormAnswer
{
public bool? BoolValue { get; init; }
public double? NumericValue { get; init; }
public DateTimeOffset? DateValue { get; init; }
public DateTimeOffset? StartValue { get; init; }
public DateTimeOffset? EndValue { get; init; }
public string? TextValue { get; init; }
public string? OptionValue { get; init; }
public Address? AddressValue { get; init; }
public string? JsonValue { get; init; }
public bool? BoolValue { get; set; }
public double? NumericValue { get; set; }
public DateTimeOffset? DateValue { get; set; }
public DateTimeOffset? StartValue { get; set; }
public DateTimeOffset? EndValue { get; set; }
public string? TextValue { get; set; }
public string? OptionValue { get; set; }
public Address? AddressValue { get; set; }
public string? JsonValue { get; set; }
}

public class FormQuestionGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,37 @@ public class FormElementDateInputModel : FormElementModel, IValidatableObject

public override FormAnswer? GetAnswer()
{
if (IsRequired == false && HasValue == false)
FormAnswer? formAnswer = null;

if (HasValue != null)
{
return null;
formAnswer = new FormAnswer { BoolValue = HasValue };
}

if (!string.IsNullOrWhiteSpace(Day) && !string.IsNullOrWhiteSpace(Month) && !string.IsNullOrWhiteSpace(Year))
{
var dateString = $"{Year}-{Month!.PadLeft(2, '0')}-{Day!.PadLeft(2, '0')}";
if (DateTime.TryParseExact(dateString, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsedDate))
{
return new FormAnswer { DateValue = parsedDate };
formAnswer ??= new FormAnswer();
formAnswer.DateValue = parsedDate;
}
}
return null;

return formAnswer;
}

public override void SetAnswer(FormAnswer? answer)
{
if (answer == null) return;

HasValue = answer.BoolValue;
if (answer?.DateValue != null)
{
Day = answer.DateValue.Value.Day.ToString();
Month = answer.DateValue.Value.Month.ToString();
Year = answer.DateValue.Value.Year.ToString();
}
else if (RedirectFromCheckYourAnswerPage && IsRequired == false)
{
HasValue = false;
}
}

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@ public class FormElementFileUploadModel : FormElementModel, IValidatableObject

public override FormAnswer? GetAnswer()
{
if (IsRequired == false && HasValue == false)
FormAnswer? formAnswer = null;

if (HasValue != null)
{
return null;
formAnswer = new FormAnswer { BoolValue = HasValue };
}

return string.IsNullOrWhiteSpace(UploadedFileName) ? null : new FormAnswer { TextValue = UploadedFileName };
if (HasValue != false && !string.IsNullOrWhiteSpace(UploadedFileName))
{
formAnswer ??= new FormAnswer();
formAnswer.TextValue = UploadedFileName;
}

return formAnswer;
}

public override void SetAnswer(FormAnswer? answer)
{
if (answer?.TextValue != null)
{
UploadedFileName = answer.TextValue;
HasValue = true;
}
else if (RedirectFromCheckYourAnswerPage && IsRequired == false)
{
HasValue = false;
}
if (answer == null) return;

HasValue = answer.BoolValue;
UploadedFileName = answer.TextValue;
}

public (IFormFile formFile, string filename, string contentType)? GetUploadedFileInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface IFormElementModel
{
FormQuestionType? CurrentFormQuestionType { get; set; }

void Initialize(FormQuestion question, bool redirectFromCheckYourAnswerPage);
void Initialize(FormQuestion question);

FormAnswer? GetAnswer();

Expand All @@ -21,23 +21,20 @@ public abstract class FormElementModel : IFormElementModel

public string? Caption { get; set; }

protected bool RedirectFromCheckYourAnswerPage { get; set; }

public FormQuestionType? CurrentFormQuestionType { get; set; }

public bool IsRequired { get; set; }

public FormQuestionOptions? Options { get; set; }

public virtual void Initialize(FormQuestion question, bool redirectFromCheckYourAnswerPage)
public virtual void Initialize(FormQuestion question)
{
Heading = question.Title;
Description = question.Description;
Caption = question.Caption;
CurrentFormQuestionType = question.Type;
IsRequired = question.IsRequired;
Options = question.Options;
RedirectFromCheckYourAnswerPage = redirectFromCheckYourAnswerPage;
}

public abstract FormAnswer? GetAnswer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@ public class FormElementTextInputModel : FormElementModel, IValidatableObject

public override FormAnswer? GetAnswer()
{
if (IsRequired == false && HasValue == false)
FormAnswer? formAnswer = null;

if (HasValue != null)
{
return null;
formAnswer = new FormAnswer { BoolValue = HasValue };
}

return string.IsNullOrWhiteSpace(TextInput) ? null : new FormAnswer { TextValue = TextInput };
if (!string.IsNullOrWhiteSpace(TextInput))
{
formAnswer ??= new FormAnswer();
formAnswer.TextValue = TextInput;
}

return formAnswer;
}

public override void SetAnswer(FormAnswer? answer)
{
if (answer?.TextValue != null)
{
TextInput = answer.TextValue;
HasValue = true;
}
else if (RedirectFromCheckYourAnswerPage && IsRequired == false)
{
HasValue = false;
}
if (answer == null) return;

HasValue = answer.BoolValue;
TextInput = answer.TextValue;
}

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,28 @@ public class FormElementUrlInputModel : FormElementModel, IValidatableObject

public override FormAnswer? GetAnswer()
{
if (IsRequired == false && HasValue == false)
FormAnswer? formAnswer = null;

if (HasValue != null)
{
return null;
formAnswer = new FormAnswer { BoolValue = HasValue };
}

return string.IsNullOrWhiteSpace(TextInput) ? null : new FormAnswer { TextValue = TextInput };
if (!string.IsNullOrWhiteSpace(TextInput))
{
formAnswer ??= new FormAnswer();
formAnswer.TextValue = TextInput;
}

return formAnswer;
}

public override void SetAnswer(FormAnswer? answer)
{
if (answer?.TextValue != null)
{
TextInput = answer.TextValue;
HasValue = true;
}
else if (RedirectFromCheckYourAnswerPage && IsRequired == false)
{
HasValue = false;
}
if (answer == null) return;

HasValue = answer.BoolValue;
TextInput = answer.TextValue;
}

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
Expand Down
Loading

0 comments on commit 75198b6

Please sign in to comment.