Files
server/test/Api.IntegrationTest/AdminConsole/Controllers/OrganizationUsersControllerResetPasswordEnrollmentTests.cs
Rui Tomé 1323d0fb6a [PM-25690] Create UpdateUserResetPasswordEnrollment command (#7594)
* Implement UpdateUserResetPasswordEnrollment command and interface for managing user password reset enrollment in organizations

* Add unit tests for UpdateUserResetPasswordEnrollmentCommand to validate user enrollment and error handling

* Add IUpdateUserResetPasswordEnrollmentCommand to service collection for user password reset enrollment management

* Add integration tests for OrganizationUsersController reset password enrollment functionality

* Refactor OrganizationUsersController to use IUpdateUserResetPasswordEnrollmentCommand for password reset enrollment updates

* Remove UpdateUserResetPasswordEnrollmentAsync method and related dependencies from IOrganizationService and OrganizationService implementations

* Update IUpdateUserResetPasswordEnrollmentCommand and UpdateUserResetPasswordEnrollmentCommand to support nullable resetPasswordKey

* Refactor unit tests for UpdateUserResetPasswordEnrollmentCommand to improve naming conventions and enhance clarity in test cases
2026-05-11 16:22:57 +01:00

98 lines
3.5 KiB
C#

using System.Net;
using Bit.Api.IntegrationTest.Factories;
using Bit.Api.IntegrationTest.Helpers;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Enums;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Billing.Enums;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Xunit;
namespace Bit.Api.IntegrationTest.AdminConsole.Controllers;
public class OrganizationUsersControllerResetPasswordEnrollmentTests
: IClassFixture<ApiApplicationFactory>, IAsyncLifetime
{
private readonly HttpClient _client;
private readonly ApiApplicationFactory _factory;
private readonly LoginHelper _loginHelper;
private Organization _organization = null!;
private string _ownerEmail = null!;
public OrganizationUsersControllerResetPasswordEnrollmentTests(ApiApplicationFactory apiFactory)
{
_factory = apiFactory;
_client = _factory.CreateClient();
_loginHelper = new LoginHelper(_factory, _client);
}
public async Task InitializeAsync()
{
_ownerEmail = $"reset-pw-enrollment-{Guid.NewGuid()}@example.com";
await _factory.LoginWithNewAccount(_ownerEmail);
(_organization, _) = await OrganizationTestHelpers.SignUpAsync(_factory,
plan: PlanType.EnterpriseAnnually, ownerEmail: _ownerEmail,
passwordManagerSeats: 5, paymentMethod: PaymentMethodType.Card);
var organizationRepository = _factory.GetService<IOrganizationRepository>();
_organization.UseResetPassword = true;
_organization.UsePolicies = true;
await organizationRepository.ReplaceAsync(_organization);
var policyRepository = _factory.GetService<IPolicyRepository>();
await policyRepository.CreateAsync(new Policy
{
OrganizationId = _organization.Id,
Type = PolicyType.ResetPassword,
Enabled = true,
Data = "{}"
});
}
public Task DisposeAsync()
{
_client.Dispose();
return Task.CompletedTask;
}
[Fact]
public async Task PutResetPasswordEnrollment_WhenUserEnrollsSelf_ReturnsOk()
{
var (memberEmail, memberOrgUser) = await OrganizationTestHelpers.CreateNewUserWithAccountAsync(
_factory, _organization.Id, OrganizationUserType.User);
await _loginHelper.LoginAsync(memberEmail);
var request = new { ResetPasswordKey = "2.enc-key", MasterPasswordHash = "master_password_hash" };
var response = await _client.PutAsJsonAsync(
$"organizations/{_organization.Id}/users/{memberOrgUser.UserId}/reset-password-enrollment",
request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task PutResetPasswordEnrollment_WhenUserWithdrawsSelf_ReturnsOk()
{
var (memberEmail, memberOrgUser) = await OrganizationTestHelpers.CreateNewUserWithAccountAsync(
_factory, _organization.Id, OrganizationUserType.User);
var organizationUserRepository = _factory.GetService<IOrganizationUserRepository>();
memberOrgUser.ResetPasswordKey = "existing-reset-password-key";
await organizationUserRepository.ReplaceAsync(memberOrgUser);
await _loginHelper.LoginAsync(memberEmail);
var request = new { ResetPasswordKey = (string?)null, MasterPasswordHash = (string?)null };
var response = await _client.PutAsJsonAsync(
$"organizations/{_organization.Id}/users/{memberOrgUser.UserId}/reset-password-enrollment",
request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}