Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/DevBetterWeb.Web/Pages/Admin/User.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
</div>
</div>
<div class="card-body">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label class="font-weight-bold">Email</label>
<input asp-for="UserPersonalUpdateModel.Email" type="text" value="@Model.UserPersonalUpdateModel.Email" class="form-control form-control-sm" />
Expand Down
5 changes: 4 additions & 1 deletion src/DevBetterWeb.Web/Pages/Admin/User.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ public async Task<IActionResult> OnPostUpdatePersonalInfoAsync(string userId)
member.UpdatePEInfo(UserPersonalUpdateModel.PEFriendCode, UserPersonalUpdateModel.PEUsername, false);
member.UpdateAboutInfo(UserPersonalUpdateModel.AboutInfo, false);
member.UpdateAddress(UserPersonalUpdateModel.Address, false);
member.UpdateShippingAddress(UserPersonalUpdateModel.Address!, UserPersonalUpdateModel.City!, UserPersonalUpdateModel.State!, UserPersonalUpdateModel.PostalCode!, UserPersonalUpdateModel.Country!, false);
if (UserPersonalUpdateModel.HasAnyAddressField())
{
member.UpdateShippingAddress(UserPersonalUpdateModel.Address!, UserPersonalUpdateModel.City!, UserPersonalUpdateModel.State!, UserPersonalUpdateModel.PostalCode!, UserPersonalUpdateModel.Country!, false);
}
member.UpdateDiscord(UserPersonalUpdateModel.DiscordUsername, false);
member.UpdateEmail(UserPersonalUpdateModel.Email, false);

Expand Down
33 changes: 27 additions & 6 deletions src/DevBetterWeb.Web/Pages/User/UserPersonalUpdateModel.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Metrics;
using DevBetterWeb.Core.Entities;

namespace DevBetterWeb.Web.Pages.User;

public class UserPersonalUpdateModel
public class UserPersonalUpdateModel : IValidatableObject
{

[Required]
public string? FirstName { get; set; }
[Required]
public string? LastName { get; set; }
[Required]
public string? Address { get; set; }
[Required]
public string? City { get; set; }
public string? State { get; set; }
[Required]
public string? Country { get; set; }
[Required]
public string? PostalCode { get; set; }
[Range(1, 31)]
[BirthdayDay]
Expand Down Expand Up @@ -60,4 +57,28 @@ public UserPersonalUpdateModel(Member member)
PEUsername = member.PEUsername;
DiscordUsername = member.DiscordUsername;
}

public bool HasAnyAddressField() =>
!string.IsNullOrWhiteSpace(Address) ||
!string.IsNullOrWhiteSpace(City) ||
!string.IsNullOrWhiteSpace(Country) ||
!string.IsNullOrWhiteSpace(PostalCode) ||
!string.IsNullOrWhiteSpace(State);

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (!HasAnyAddressField()) yield break;

if (string.IsNullOrWhiteSpace(Address))
yield return new ValidationResult("The Address field is required when providing address information.", new[] { nameof(Address) });

if (string.IsNullOrWhiteSpace(City))
yield return new ValidationResult("The City field is required when providing address information.", new[] { nameof(City) });

if (string.IsNullOrWhiteSpace(Country))
yield return new ValidationResult("The Country field is required when providing address information.", new[] { nameof(Country) });

if (string.IsNullOrWhiteSpace(PostalCode))
yield return new ValidationResult("The Postal Code field is required when providing address information.", new[] { nameof(PostalCode) });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DevBetterWeb.Web.Pages.User;
using Xunit;

namespace DevBetterWeb.UnitTests.Web.Models;

public class UserPersonalUpdateModelValidateTests
{
private static List<ValidationResult> Validate(UserPersonalUpdateModel model)
{
var results = new List<ValidationResult>();
var context = new ValidationContext(model);
Validator.TryValidateObject(model, context, results, validateAllProperties: true);
return results;
}

[Fact]
public void NoAddressFields_NoValidationErrors()
{
var model = new UserPersonalUpdateModel
{
FirstName = "Jane",
LastName = "Doe",
Email = "jane@example.com"
};

var errors = Validate(model);

Assert.Empty(errors);
}

[Fact]
public void AllAddressFieldsProvided_NoValidationErrors()
{
var model = new UserPersonalUpdateModel
{
FirstName = "Jane",
LastName = "Doe",
Address = "123 Main St",
City = "Springfield",
Country = "US",
PostalCode = "12345",
State = "IL"
};

var errors = Validate(model);

Assert.Empty(errors);
}

[Fact]
public void OnlyCityProvided_ValidationErrorsForOtherAddressFields()
{
var model = new UserPersonalUpdateModel
{
FirstName = "Jane",
LastName = "Doe",
City = "Springfield"
};

var errors = Validate(model);

Assert.Contains(errors, e => e.MemberNames != null && System.Linq.Enumerable.Contains(e.MemberNames, nameof(UserPersonalUpdateModel.Address)));
Assert.Contains(errors, e => e.MemberNames != null && System.Linq.Enumerable.Contains(e.MemberNames, nameof(UserPersonalUpdateModel.Country)));
Assert.Contains(errors, e => e.MemberNames != null && System.Linq.Enumerable.Contains(e.MemberNames, nameof(UserPersonalUpdateModel.PostalCode)));
}

[Fact]
public void HasAnyAddressField_ReturnsFalseWhenAllEmpty()
{
var model = new UserPersonalUpdateModel();

Assert.False(model.HasAnyAddressField());
}

[Fact]
public void HasAnyAddressField_ReturnsTrueWhenAddressSet()
{
var model = new UserPersonalUpdateModel { Address = "123 Main St" };

Assert.True(model.HasAnyAddressField());
}

[Fact]
public void HasAnyAddressField_ReturnsTrueWhenOnlyCitySet()
{
var model = new UserPersonalUpdateModel { City = "Springfield" };

Assert.True(model.HasAnyAddressField());
}

[Fact]
public void HasAnyAddressField_ReturnsFalseForWhitespaceOnly()
{
var model = new UserPersonalUpdateModel { Address = " ", City = " " };

Assert.False(model.HasAnyAddressField());
}
}
Loading