Skip to content

Commit

Permalink
PM-13763 Move ResetPasswordEnrolled to response model (#4983)
Browse files Browse the repository at this point in the history
to adhere to Liskov Substitution Principle. Ensures request models inherit only relevant properties.
  • Loading branch information
JimmyVo16 authored and cyprain-okeke committed Nov 12, 2024
1 parent e06d0f6 commit 87d2cb1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/Api/AdminConsole/Public/Models/MemberBaseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public MemberBaseModel(OrganizationUser user)

Type = user.Type;
ExternalId = user.ExternalId;
ResetPasswordEnrolled = user.ResetPasswordKey != null;

if (Type == OrganizationUserType.Custom)
{
Expand All @@ -35,7 +34,6 @@ public MemberBaseModel(OrganizationUserUserDetails user)

Type = user.Type;
ExternalId = user.ExternalId;
ResetPasswordEnrolled = user.ResetPasswordKey != null;

if (Type == OrganizationUserType.Custom)
{
Expand All @@ -55,11 +53,7 @@ public MemberBaseModel(OrganizationUserUserDetails user)
/// <example>external_id_123456</example>
[StringLength(300)]
public string ExternalId { get; set; }
/// <summary>
/// Returns <c>true</c> if the member has enrolled in Password Reset assistance within the organization
/// </summary>
[Required]
public bool ResetPasswordEnrolled { get; set; }

/// <summary>
/// The member's custom permissions if the member has a Custom role. If not supplied, all custom permissions will
/// default to false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public MemberResponseModel(OrganizationUser user, IEnumerable<CollectionAccessSe
Email = user.Email;
Status = user.Status;
Collections = collections?.Select(c => new AssociationWithPermissionsResponseModel(c));
ResetPasswordEnrolled = user.ResetPasswordKey != null;
}

public MemberResponseModel(OrganizationUserUserDetails user, bool twoFactorEnabled,
Expand All @@ -45,6 +46,7 @@ public MemberResponseModel(OrganizationUserUserDetails user, bool twoFactorEnabl
TwoFactorEnabled = twoFactorEnabled;
Status = user.Status;
Collections = collections?.Select(c => new AssociationWithPermissionsResponseModel(c));
ResetPasswordEnrolled = user.ResetPasswordKey != null;
}

/// <summary>
Expand Down Expand Up @@ -93,4 +95,10 @@ public MemberResponseModel(OrganizationUserUserDetails user, bool twoFactorEnabl
/// The associated collections that this member can access.
/// </summary>
public IEnumerable<AssociationWithPermissionsResponseModel> Collections { get; set; }

/// <summary>
/// Returns <c>true</c> if the member has enrolled in Password Reset assistance within the organization
/// </summary>
[Required]
public bool ResetPasswordEnrolled { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Bit.Api.AdminConsole.Public.Models.Response;
using Bit.Core.Entities;
using Bit.Core.Models.Data;
using NSubstitute;
using Xunit;

namespace Bit.Api.Test.AdminConsole.Public.Models.Response;


public class MemberResponseModelTests
{
[Fact]
public void ResetPasswordEnrolled_ShouldBeTrue_WhenUserHasResetPasswordKey()
{
// Arrange
var user = Substitute.For<OrganizationUser>();
var collections = Substitute.For<IEnumerable<CollectionAccessSelection>>();
user.ResetPasswordKey = "none-empty";


// Act
var sut = new MemberResponseModel(user, collections);

// Assert
Assert.True(sut.ResetPasswordEnrolled);
}

[Fact]
public void ResetPasswordEnrolled_ShouldBeFalse_WhenUserDoesNotHaveResetPasswordKey()
{
// Arrange
var user = Substitute.For<OrganizationUser>();
var collections = Substitute.For<IEnumerable<CollectionAccessSelection>>();

// Act
var sut = new MemberResponseModel(user, collections);

// Assert
Assert.False(sut.ResetPasswordEnrolled);
}
}

0 comments on commit 87d2cb1

Please sign in to comment.