2022-06-30 01:46:41 +02:00
using System.Security.Claims ;
2023-12-14 21:05:19 +01:00
using Bit.Api.AdminConsole.Models.Request.Organizations ;
2023-12-05 18:05:51 +01:00
using Bit.Api.Auth.Controllers ;
using Bit.Api.Auth.Models.Request ;
2023-04-14 19:25:56 +02:00
using Bit.Api.Auth.Models.Request.Accounts ;
2024-06-17 20:46:57 +02:00
using Bit.Api.Auth.Models.Request.WebAuthn ;
2023-12-05 18:05:51 +01:00
using Bit.Api.Auth.Validators ;
2023-12-12 17:58:34 +01:00
using Bit.Api.Tools.Models.Request ;
2023-12-06 14:46:36 +01:00
using Bit.Api.Vault.Models.Request ;
2024-10-28 17:12:13 +01:00
using Bit.Core ;
2023-10-26 19:38:29 +02:00
using Bit.Core.AdminConsole.Repositories ;
2023-11-22 22:07:37 +01:00
using Bit.Core.AdminConsole.Services ;
2023-12-05 18:05:51 +01:00
using Bit.Core.Auth.Entities ;
2023-04-14 19:25:56 +02:00
using Bit.Core.Auth.Models.Api.Request.Accounts ;
2024-06-17 20:46:57 +02:00
using Bit.Core.Auth.Models.Data ;
2024-07-23 20:53:08 +02:00
using Bit.Core.Auth.UserFeatures.TdeOffboardingPassword.Interfaces ;
2023-11-09 20:56:08 +01:00
using Bit.Core.Auth.UserFeatures.UserKey ;
Auth/PM-3275 - Changes to support TDE User without MP being able to Set a Password + misc refactoring (#3242)
* PM-3275 - Add new GetMasterPasswordPolicy endpoint which will allow authenticated clients to get an enabled MP org policy if it exists for the purposes of enforcing those policy requirements when setting a password.
* PM-3275 - AccountsController.cs - PostSetPasswordAsync - (1) Convert UserService.setPasswordAsync into new SetInitialMasterPasswordCommand (2) Refactor SetInitialMasterPasswordCommand to only accept post SSO users who are in the invited state
(3) Add TODOs for more cleanup work and more commands
* PM-3275 - Update AccountsControllerTests.cs to add new SetInitialMasterPasswordCommand
* PM-3275 - UserService.cs - Remove non implemented ChangePasswordAsync method
* PM-3275 - The new SetInitialMasterPasswordCommand leveraged the OrganizationService.cs AcceptUserAsync method so while I was in here I converted the AcceptUserAsync methods into a new AcceptOrgUserCommand.cs and turned the private method which accepted an existing org user public for use in the SetInitialMasterPasswordCommand
* PM-3275 - Dotnet format
* PM-3275 - Test SetInitialMasterPasswordCommand
* Dotnet format
* PM-3275 - In process AcceptOrgUserCommandTests.cs
* PM-3275 - Migrate changes from AC-244 / #3199 over into new AcceptOrgUserCommand
* PM-3275 - AcceptOrgUserCommand.cs - create data protector specifically for this command
* PM-3275 - Add TODO for renaming / removing overloading of methods to improve readability / clarity
* PM-3275 - AcceptOrgUserCommand.cs - refactor AcceptOrgUserAsync by OrgId to retrieve orgUser with _organizationUserRepository.GetByOrganizationAsync which gets a single user instead of a collection
* PM-3275 - AcceptOrgUserCommand.cs - update name in TODO for evaluation later
* PM-3275 / PM-1196 - (1) Slightly refactor SsoEmail2faSessionTokenable to provide public static GetTokenLifeTime() method for testing (2) Add missed tests to SsoEmail2faSessionTokenable in preparation for building tests for new OrgUserInviteTokenable.cs
* PM-3275 / PM-1196 - Removing SsoEmail2faSessionTokenable.cs changes + tests as I've handled that separately in a new PR (#3270) for newly created task PM-3925
* PM-3275 - ExpiringTokenable.cs - add clarifying comments to help distinguish between the Valid property and the TokenIsValid method.
* PM-3275 - Create OrgUserInviteTokenable.cs and add tests in OrgUserInviteTokenableTests.cs
* PM-3275 - OrganizationService.cs - Refactor Org User Invite methods to use new OrgUserInviteTokenable instead of manual creation of a token
* PM-3275 - OrgUserInviteTokenable.cs - clarify backwards compat note
* PM-3275 - AcceptOrgUserCommand.cs - Add TODOs + minor name refactor
* PM-3275 - AcceptOrgUserCommand.cs - replace method overloading with more easily readable names.
* PM-3275 - AcceptOrgUserCommand.cs - Update ValidateOrgUserInviteToken to add new token validation while maintaining backwards compatibility for 1 release.
* dotnet format
* PM-3275 - AcceptOrgUserCommand.cs - Move private method below where it is used
* PM-3275 - ServiceCollectionExtensions.cs - Must register IDataProtectorTokenFactory<OrgUserInviteTokenable> for new tokenable
* PM-3275 - OrgUserInviteTokenable needed access to global settings to set its token lifetime to the _globalSettings.OrganizationInviteExpirationHours value. Creating a factory seemed the most straightforward way to encapsulate the desired creation logic. Unsure if in the correct location in ServiceCollectionExtensions.cs but will figure that out later.
* PM-3275 - In process work of creating AcceptOrgUserCommandTests.cs
* PM-3275 - Remove no longer relevant AcceptOrgUser tests from OrganizationServiceTests.cs
* PM-3275 - Register OrgUserInviteTokenableFactory alongside tokenizer
* PM-3275 - AcceptOrgUserCommandTests.cs - AcceptOrgUserAsync basic test suite completed.
* PM-3275 - AcceptOrgUserCommandTests.cs - tweak test names
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Remove old tests from OrganizationServiceTests as no longer needed to reference (2) Add summary for SetupCommonAcceptOrgUserMocks (3) Get AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail passing
* PM-3275 - Create interface for OrgUserInviteTokenableFactory b/c that's the right thing to do + enables test substitution
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Start work on AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail (2) Create and use SetupCommonAcceptOrgUserByTokenMocks() (3) Create generic FakeDataProtectorTokenFactory for tokenable testing
* PM-3275 - (1) Get AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail test passing (2) Move FakeDataProtectorTokenFactory to own file
* PM-3275 - AcceptOrgUserCommandTests.cs - Finish up tests for AcceptOrgUserByTokenAsync
* PM-3275 - Add pseudo section comments
* PM-3275 - Clean up unused params on AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest test
* PM-3275 - (1) Tests written for AcceptOrgUserByOrgSsoIdAsync (2) Refactor happy path assertions into helper function AssertValidAcceptedOrgUser to reduce code duplication
* PM-3275 - Finish up testing AcceptOrgUserCommandTests.cs by adding tests for AcceptOrgUserByOrgIdAsync
* PM-3275 - Tweaking test naming to ensure consistency.
* PM-3275 - Bugfix - OrgUserInviteTokenableFactory implementation required when declaring singleton service in ServiceCollectionExtensions.cs
* PM-3275 - Resolve failing OrganizationServiceTests.cs
* dotnet format
* PM-3275 - PoliciesController.cs - GetMasterPasswordPolicy bugfix - for orgs without a MP policy, policy comes back as null and we should return notFound in that case.
* PM-3275 - Add PoliciesControllerTests.cs specifically for new GetMasterPasswordPolicy(...) endpoint.
* PM-3275 - dotnet format PoliciesControllerTests.cs
* PM-3275 - PoliciesController.cs - (1) Add tech debt task number (2) Properly flag endpoint as deprecated
* PM-3275 - Add new hasManageResetPasswordPermission property to ProfileResponseModel.cs primarily for sync so that we can condition client side if TDE user obtains elevated permissions
* PM-3275 - Fix AccountsControllerTests.cs
* PM-3275 - OrgUserInviteTokenable.cs - clarify TODO
* PM-3275 - AcceptOrgUserCommand.cs - Refactor token validation to use short circuiting to only run old token validation if new token validation fails.
* PM-3275 - OrgUserInviteTokenable.cs - (1) Add new static methods to centralize validation logic to avoid repetition (2) Add new token validation method so we can avoid having to pass in a full org user (and hitting the db to do so)
* PM-3275 - Realized that the old token validation was used in the PoliciesController.cs (existing user clicks invite link in email and goes to log in) and UserService.cs (user clicks invite link in email and registers for a new acct). Added tech debt item for cleaning up backwards compatibility in future.
* dotnet format
* PM-3275 - (1) AccountsController.cs - Update PostSetPasswordAsync SetPasswordRequestModel to allow null keys for the case where we have a TDE user who obtains elevated permissions - they already have a user public and user encrypted private key saved in the db. (2) AccountsControllerTests.cs - test PostSetPasswordAsync scenarios to ensure changes will work as expected.
* PM-3275 - PR review feedback - (1) set CurrentContext to private (2) Refactor GetProfile to use variables to improve clarity and simplify debugging.
* PM-3275 - SyncController.cs - PR Review Feedback - Set current context as private instead of protected.
* PM-3275 - CurrentContextExtensions.cs - PR Feedback - move parenthesis up from own line.
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - Replace unnecessary variable
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - PR Feedback - Add expected outcome statement to test name
* PM-3275 - Set Initial Password command and tests - PR Feedback changes - (1) Rename orgIdentifier --> OrgSsoIdentifier for clarity (2) Update SetInitialMasterPasswordAsync to not allow null orgSsoId with explicit message saying this vs letting null org trigger invalid organization (3) Add test to cover this new scenario.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Move summary from implementation to interface to better respect standards and the fact that the interface is the more seen piece of code.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, rename AcceptOrgUserByTokenAsync -> AcceptOrgUserByEmailTokenAsync + replace generic name token with emailToken
* PM-3275 - OrganizationService.cs - Per PR feedback, remove dupe line
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove new lines in error messages for consistency.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Per PR feedback, adjust formatting of constructor for improved readability.
* PM-3275 - CurrentContextExtensions.cs - Refactor AnyOrgUserHasManageResetPasswordPermission per PR feedback to remove unnecessary var.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove completed TODO
* PM-3275 - PoliciesController.cs - Per PR feedback, update GetByInvitedUser param to be guid instead of string.
* PM-3275 - OrgUserInviteTokenable.cs - per PR feedback, add tech debt item info.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, use const purpose from tokenable instead of magic string.
* PM-3275 - Restore non duplicate line to fix tests
* PM-3275 - Per PR feedback, revert all sync controller changes as the ProfileResponseModel.organizations array has org objects which have permissions which have the ManageResetPassword permission. So, I have the information that I need clientside already to determine if the user has the ManageResetPassword in any org.
* PM-3275 - PoliciesControllerTests.cs - Update imports as the PoliciesController was moved under the admin console team's domain.
* PM-3275 - Resolve issues from merge conflict resolutions to get solution building.
* PM-3275 / PM-4633 - PoliciesController.cs - use orgUserId to look up user instead of orgId. Oops.
* Fix user service tests
* Resolve merge conflict
2023-11-02 16:02:25 +01:00
using Bit.Core.Auth.UserFeatures.UserMasterPassword.Interfaces ;
2024-05-23 16:17:00 +02:00
using Bit.Core.Billing.Services ;
2024-02-09 17:58:37 +01:00
using Bit.Core.Context ;
2022-01-11 10:40:51 +01:00
using Bit.Core.Entities ;
2020-05-01 06:47:38 +02:00
using Bit.Core.Exceptions ;
using Bit.Core.Repositories ;
using Bit.Core.Services ;
2021-02-22 22:35:16 +01:00
using Bit.Core.Settings ;
2023-12-12 17:58:34 +01:00
using Bit.Core.Tools.Entities ;
2023-04-18 14:05:17 +02:00
using Bit.Core.Tools.Services ;
2023-12-06 14:46:36 +01:00
using Bit.Core.Vault.Entities ;
Auth/PM-3275 - Changes to support TDE User without MP being able to Set a Password + misc refactoring (#3242)
* PM-3275 - Add new GetMasterPasswordPolicy endpoint which will allow authenticated clients to get an enabled MP org policy if it exists for the purposes of enforcing those policy requirements when setting a password.
* PM-3275 - AccountsController.cs - PostSetPasswordAsync - (1) Convert UserService.setPasswordAsync into new SetInitialMasterPasswordCommand (2) Refactor SetInitialMasterPasswordCommand to only accept post SSO users who are in the invited state
(3) Add TODOs for more cleanup work and more commands
* PM-3275 - Update AccountsControllerTests.cs to add new SetInitialMasterPasswordCommand
* PM-3275 - UserService.cs - Remove non implemented ChangePasswordAsync method
* PM-3275 - The new SetInitialMasterPasswordCommand leveraged the OrganizationService.cs AcceptUserAsync method so while I was in here I converted the AcceptUserAsync methods into a new AcceptOrgUserCommand.cs and turned the private method which accepted an existing org user public for use in the SetInitialMasterPasswordCommand
* PM-3275 - Dotnet format
* PM-3275 - Test SetInitialMasterPasswordCommand
* Dotnet format
* PM-3275 - In process AcceptOrgUserCommandTests.cs
* PM-3275 - Migrate changes from AC-244 / #3199 over into new AcceptOrgUserCommand
* PM-3275 - AcceptOrgUserCommand.cs - create data protector specifically for this command
* PM-3275 - Add TODO for renaming / removing overloading of methods to improve readability / clarity
* PM-3275 - AcceptOrgUserCommand.cs - refactor AcceptOrgUserAsync by OrgId to retrieve orgUser with _organizationUserRepository.GetByOrganizationAsync which gets a single user instead of a collection
* PM-3275 - AcceptOrgUserCommand.cs - update name in TODO for evaluation later
* PM-3275 / PM-1196 - (1) Slightly refactor SsoEmail2faSessionTokenable to provide public static GetTokenLifeTime() method for testing (2) Add missed tests to SsoEmail2faSessionTokenable in preparation for building tests for new OrgUserInviteTokenable.cs
* PM-3275 / PM-1196 - Removing SsoEmail2faSessionTokenable.cs changes + tests as I've handled that separately in a new PR (#3270) for newly created task PM-3925
* PM-3275 - ExpiringTokenable.cs - add clarifying comments to help distinguish between the Valid property and the TokenIsValid method.
* PM-3275 - Create OrgUserInviteTokenable.cs and add tests in OrgUserInviteTokenableTests.cs
* PM-3275 - OrganizationService.cs - Refactor Org User Invite methods to use new OrgUserInviteTokenable instead of manual creation of a token
* PM-3275 - OrgUserInviteTokenable.cs - clarify backwards compat note
* PM-3275 - AcceptOrgUserCommand.cs - Add TODOs + minor name refactor
* PM-3275 - AcceptOrgUserCommand.cs - replace method overloading with more easily readable names.
* PM-3275 - AcceptOrgUserCommand.cs - Update ValidateOrgUserInviteToken to add new token validation while maintaining backwards compatibility for 1 release.
* dotnet format
* PM-3275 - AcceptOrgUserCommand.cs - Move private method below where it is used
* PM-3275 - ServiceCollectionExtensions.cs - Must register IDataProtectorTokenFactory<OrgUserInviteTokenable> for new tokenable
* PM-3275 - OrgUserInviteTokenable needed access to global settings to set its token lifetime to the _globalSettings.OrganizationInviteExpirationHours value. Creating a factory seemed the most straightforward way to encapsulate the desired creation logic. Unsure if in the correct location in ServiceCollectionExtensions.cs but will figure that out later.
* PM-3275 - In process work of creating AcceptOrgUserCommandTests.cs
* PM-3275 - Remove no longer relevant AcceptOrgUser tests from OrganizationServiceTests.cs
* PM-3275 - Register OrgUserInviteTokenableFactory alongside tokenizer
* PM-3275 - AcceptOrgUserCommandTests.cs - AcceptOrgUserAsync basic test suite completed.
* PM-3275 - AcceptOrgUserCommandTests.cs - tweak test names
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Remove old tests from OrganizationServiceTests as no longer needed to reference (2) Add summary for SetupCommonAcceptOrgUserMocks (3) Get AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail passing
* PM-3275 - Create interface for OrgUserInviteTokenableFactory b/c that's the right thing to do + enables test substitution
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Start work on AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail (2) Create and use SetupCommonAcceptOrgUserByTokenMocks() (3) Create generic FakeDataProtectorTokenFactory for tokenable testing
* PM-3275 - (1) Get AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail test passing (2) Move FakeDataProtectorTokenFactory to own file
* PM-3275 - AcceptOrgUserCommandTests.cs - Finish up tests for AcceptOrgUserByTokenAsync
* PM-3275 - Add pseudo section comments
* PM-3275 - Clean up unused params on AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest test
* PM-3275 - (1) Tests written for AcceptOrgUserByOrgSsoIdAsync (2) Refactor happy path assertions into helper function AssertValidAcceptedOrgUser to reduce code duplication
* PM-3275 - Finish up testing AcceptOrgUserCommandTests.cs by adding tests for AcceptOrgUserByOrgIdAsync
* PM-3275 - Tweaking test naming to ensure consistency.
* PM-3275 - Bugfix - OrgUserInviteTokenableFactory implementation required when declaring singleton service in ServiceCollectionExtensions.cs
* PM-3275 - Resolve failing OrganizationServiceTests.cs
* dotnet format
* PM-3275 - PoliciesController.cs - GetMasterPasswordPolicy bugfix - for orgs without a MP policy, policy comes back as null and we should return notFound in that case.
* PM-3275 - Add PoliciesControllerTests.cs specifically for new GetMasterPasswordPolicy(...) endpoint.
* PM-3275 - dotnet format PoliciesControllerTests.cs
* PM-3275 - PoliciesController.cs - (1) Add tech debt task number (2) Properly flag endpoint as deprecated
* PM-3275 - Add new hasManageResetPasswordPermission property to ProfileResponseModel.cs primarily for sync so that we can condition client side if TDE user obtains elevated permissions
* PM-3275 - Fix AccountsControllerTests.cs
* PM-3275 - OrgUserInviteTokenable.cs - clarify TODO
* PM-3275 - AcceptOrgUserCommand.cs - Refactor token validation to use short circuiting to only run old token validation if new token validation fails.
* PM-3275 - OrgUserInviteTokenable.cs - (1) Add new static methods to centralize validation logic to avoid repetition (2) Add new token validation method so we can avoid having to pass in a full org user (and hitting the db to do so)
* PM-3275 - Realized that the old token validation was used in the PoliciesController.cs (existing user clicks invite link in email and goes to log in) and UserService.cs (user clicks invite link in email and registers for a new acct). Added tech debt item for cleaning up backwards compatibility in future.
* dotnet format
* PM-3275 - (1) AccountsController.cs - Update PostSetPasswordAsync SetPasswordRequestModel to allow null keys for the case where we have a TDE user who obtains elevated permissions - they already have a user public and user encrypted private key saved in the db. (2) AccountsControllerTests.cs - test PostSetPasswordAsync scenarios to ensure changes will work as expected.
* PM-3275 - PR review feedback - (1) set CurrentContext to private (2) Refactor GetProfile to use variables to improve clarity and simplify debugging.
* PM-3275 - SyncController.cs - PR Review Feedback - Set current context as private instead of protected.
* PM-3275 - CurrentContextExtensions.cs - PR Feedback - move parenthesis up from own line.
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - Replace unnecessary variable
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - PR Feedback - Add expected outcome statement to test name
* PM-3275 - Set Initial Password command and tests - PR Feedback changes - (1) Rename orgIdentifier --> OrgSsoIdentifier for clarity (2) Update SetInitialMasterPasswordAsync to not allow null orgSsoId with explicit message saying this vs letting null org trigger invalid organization (3) Add test to cover this new scenario.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Move summary from implementation to interface to better respect standards and the fact that the interface is the more seen piece of code.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, rename AcceptOrgUserByTokenAsync -> AcceptOrgUserByEmailTokenAsync + replace generic name token with emailToken
* PM-3275 - OrganizationService.cs - Per PR feedback, remove dupe line
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove new lines in error messages for consistency.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Per PR feedback, adjust formatting of constructor for improved readability.
* PM-3275 - CurrentContextExtensions.cs - Refactor AnyOrgUserHasManageResetPasswordPermission per PR feedback to remove unnecessary var.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove completed TODO
* PM-3275 - PoliciesController.cs - Per PR feedback, update GetByInvitedUser param to be guid instead of string.
* PM-3275 - OrgUserInviteTokenable.cs - per PR feedback, add tech debt item info.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, use const purpose from tokenable instead of magic string.
* PM-3275 - Restore non duplicate line to fix tests
* PM-3275 - Per PR feedback, revert all sync controller changes as the ProfileResponseModel.organizations array has org objects which have permissions which have the ManageResetPassword permission. So, I have the information that I need clientside already to determine if the user has the ManageResetPassword in any org.
* PM-3275 - PoliciesControllerTests.cs - Update imports as the PoliciesController was moved under the admin console team's domain.
* PM-3275 - Resolve issues from merge conflict resolutions to get solution building.
* PM-3275 / PM-4633 - PoliciesController.cs - use orgUserId to look up user instead of orgId. Oops.
* Fix user service tests
* Resolve merge conflict
2023-11-02 16:02:25 +01:00
using Bit.Test.Common.AutoFixture.Attributes ;
2020-05-01 06:47:38 +02:00
using Microsoft.AspNetCore.Identity ;
using NSubstitute ;
using Xunit ;
2023-12-05 18:05:51 +01:00
namespace Bit.Api.Test.Auth.Controllers ;
2022-08-29 22:06:55 +02:00
2020-05-01 06:47:38 +02:00
public class AccountsControllerTests : IDisposable
{
2020-08-26 20:12:04 +02:00
private readonly AccountsController _sut ;
2020-05-01 06:47:38 +02:00
private readonly GlobalSettings _globalSettings ;
2020-08-26 20:12:04 +02:00
private readonly IOrganizationService _organizationService ;
2020-05-01 06:47:38 +02:00
private readonly IOrganizationUserRepository _organizationUserRepository ;
private readonly IPaymentService _paymentService ;
2021-07-01 22:27:03 +02:00
private readonly IUserService _userService ;
2021-06-30 09:35:26 +02:00
private readonly IProviderUserRepository _providerUserRepository ;
2023-04-17 16:35:47 +02:00
private readonly IPolicyService _policyService ;
Auth/PM-3275 - Changes to support TDE User without MP being able to Set a Password + misc refactoring (#3242)
* PM-3275 - Add new GetMasterPasswordPolicy endpoint which will allow authenticated clients to get an enabled MP org policy if it exists for the purposes of enforcing those policy requirements when setting a password.
* PM-3275 - AccountsController.cs - PostSetPasswordAsync - (1) Convert UserService.setPasswordAsync into new SetInitialMasterPasswordCommand (2) Refactor SetInitialMasterPasswordCommand to only accept post SSO users who are in the invited state
(3) Add TODOs for more cleanup work and more commands
* PM-3275 - Update AccountsControllerTests.cs to add new SetInitialMasterPasswordCommand
* PM-3275 - UserService.cs - Remove non implemented ChangePasswordAsync method
* PM-3275 - The new SetInitialMasterPasswordCommand leveraged the OrganizationService.cs AcceptUserAsync method so while I was in here I converted the AcceptUserAsync methods into a new AcceptOrgUserCommand.cs and turned the private method which accepted an existing org user public for use in the SetInitialMasterPasswordCommand
* PM-3275 - Dotnet format
* PM-3275 - Test SetInitialMasterPasswordCommand
* Dotnet format
* PM-3275 - In process AcceptOrgUserCommandTests.cs
* PM-3275 - Migrate changes from AC-244 / #3199 over into new AcceptOrgUserCommand
* PM-3275 - AcceptOrgUserCommand.cs - create data protector specifically for this command
* PM-3275 - Add TODO for renaming / removing overloading of methods to improve readability / clarity
* PM-3275 - AcceptOrgUserCommand.cs - refactor AcceptOrgUserAsync by OrgId to retrieve orgUser with _organizationUserRepository.GetByOrganizationAsync which gets a single user instead of a collection
* PM-3275 - AcceptOrgUserCommand.cs - update name in TODO for evaluation later
* PM-3275 / PM-1196 - (1) Slightly refactor SsoEmail2faSessionTokenable to provide public static GetTokenLifeTime() method for testing (2) Add missed tests to SsoEmail2faSessionTokenable in preparation for building tests for new OrgUserInviteTokenable.cs
* PM-3275 / PM-1196 - Removing SsoEmail2faSessionTokenable.cs changes + tests as I've handled that separately in a new PR (#3270) for newly created task PM-3925
* PM-3275 - ExpiringTokenable.cs - add clarifying comments to help distinguish between the Valid property and the TokenIsValid method.
* PM-3275 - Create OrgUserInviteTokenable.cs and add tests in OrgUserInviteTokenableTests.cs
* PM-3275 - OrganizationService.cs - Refactor Org User Invite methods to use new OrgUserInviteTokenable instead of manual creation of a token
* PM-3275 - OrgUserInviteTokenable.cs - clarify backwards compat note
* PM-3275 - AcceptOrgUserCommand.cs - Add TODOs + minor name refactor
* PM-3275 - AcceptOrgUserCommand.cs - replace method overloading with more easily readable names.
* PM-3275 - AcceptOrgUserCommand.cs - Update ValidateOrgUserInviteToken to add new token validation while maintaining backwards compatibility for 1 release.
* dotnet format
* PM-3275 - AcceptOrgUserCommand.cs - Move private method below where it is used
* PM-3275 - ServiceCollectionExtensions.cs - Must register IDataProtectorTokenFactory<OrgUserInviteTokenable> for new tokenable
* PM-3275 - OrgUserInviteTokenable needed access to global settings to set its token lifetime to the _globalSettings.OrganizationInviteExpirationHours value. Creating a factory seemed the most straightforward way to encapsulate the desired creation logic. Unsure if in the correct location in ServiceCollectionExtensions.cs but will figure that out later.
* PM-3275 - In process work of creating AcceptOrgUserCommandTests.cs
* PM-3275 - Remove no longer relevant AcceptOrgUser tests from OrganizationServiceTests.cs
* PM-3275 - Register OrgUserInviteTokenableFactory alongside tokenizer
* PM-3275 - AcceptOrgUserCommandTests.cs - AcceptOrgUserAsync basic test suite completed.
* PM-3275 - AcceptOrgUserCommandTests.cs - tweak test names
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Remove old tests from OrganizationServiceTests as no longer needed to reference (2) Add summary for SetupCommonAcceptOrgUserMocks (3) Get AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail passing
* PM-3275 - Create interface for OrgUserInviteTokenableFactory b/c that's the right thing to do + enables test substitution
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Start work on AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail (2) Create and use SetupCommonAcceptOrgUserByTokenMocks() (3) Create generic FakeDataProtectorTokenFactory for tokenable testing
* PM-3275 - (1) Get AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail test passing (2) Move FakeDataProtectorTokenFactory to own file
* PM-3275 - AcceptOrgUserCommandTests.cs - Finish up tests for AcceptOrgUserByTokenAsync
* PM-3275 - Add pseudo section comments
* PM-3275 - Clean up unused params on AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest test
* PM-3275 - (1) Tests written for AcceptOrgUserByOrgSsoIdAsync (2) Refactor happy path assertions into helper function AssertValidAcceptedOrgUser to reduce code duplication
* PM-3275 - Finish up testing AcceptOrgUserCommandTests.cs by adding tests for AcceptOrgUserByOrgIdAsync
* PM-3275 - Tweaking test naming to ensure consistency.
* PM-3275 - Bugfix - OrgUserInviteTokenableFactory implementation required when declaring singleton service in ServiceCollectionExtensions.cs
* PM-3275 - Resolve failing OrganizationServiceTests.cs
* dotnet format
* PM-3275 - PoliciesController.cs - GetMasterPasswordPolicy bugfix - for orgs without a MP policy, policy comes back as null and we should return notFound in that case.
* PM-3275 - Add PoliciesControllerTests.cs specifically for new GetMasterPasswordPolicy(...) endpoint.
* PM-3275 - dotnet format PoliciesControllerTests.cs
* PM-3275 - PoliciesController.cs - (1) Add tech debt task number (2) Properly flag endpoint as deprecated
* PM-3275 - Add new hasManageResetPasswordPermission property to ProfileResponseModel.cs primarily for sync so that we can condition client side if TDE user obtains elevated permissions
* PM-3275 - Fix AccountsControllerTests.cs
* PM-3275 - OrgUserInviteTokenable.cs - clarify TODO
* PM-3275 - AcceptOrgUserCommand.cs - Refactor token validation to use short circuiting to only run old token validation if new token validation fails.
* PM-3275 - OrgUserInviteTokenable.cs - (1) Add new static methods to centralize validation logic to avoid repetition (2) Add new token validation method so we can avoid having to pass in a full org user (and hitting the db to do so)
* PM-3275 - Realized that the old token validation was used in the PoliciesController.cs (existing user clicks invite link in email and goes to log in) and UserService.cs (user clicks invite link in email and registers for a new acct). Added tech debt item for cleaning up backwards compatibility in future.
* dotnet format
* PM-3275 - (1) AccountsController.cs - Update PostSetPasswordAsync SetPasswordRequestModel to allow null keys for the case where we have a TDE user who obtains elevated permissions - they already have a user public and user encrypted private key saved in the db. (2) AccountsControllerTests.cs - test PostSetPasswordAsync scenarios to ensure changes will work as expected.
* PM-3275 - PR review feedback - (1) set CurrentContext to private (2) Refactor GetProfile to use variables to improve clarity and simplify debugging.
* PM-3275 - SyncController.cs - PR Review Feedback - Set current context as private instead of protected.
* PM-3275 - CurrentContextExtensions.cs - PR Feedback - move parenthesis up from own line.
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - Replace unnecessary variable
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - PR Feedback - Add expected outcome statement to test name
* PM-3275 - Set Initial Password command and tests - PR Feedback changes - (1) Rename orgIdentifier --> OrgSsoIdentifier for clarity (2) Update SetInitialMasterPasswordAsync to not allow null orgSsoId with explicit message saying this vs letting null org trigger invalid organization (3) Add test to cover this new scenario.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Move summary from implementation to interface to better respect standards and the fact that the interface is the more seen piece of code.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, rename AcceptOrgUserByTokenAsync -> AcceptOrgUserByEmailTokenAsync + replace generic name token with emailToken
* PM-3275 - OrganizationService.cs - Per PR feedback, remove dupe line
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove new lines in error messages for consistency.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Per PR feedback, adjust formatting of constructor for improved readability.
* PM-3275 - CurrentContextExtensions.cs - Refactor AnyOrgUserHasManageResetPasswordPermission per PR feedback to remove unnecessary var.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove completed TODO
* PM-3275 - PoliciesController.cs - Per PR feedback, update GetByInvitedUser param to be guid instead of string.
* PM-3275 - OrgUserInviteTokenable.cs - per PR feedback, add tech debt item info.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, use const purpose from tokenable instead of magic string.
* PM-3275 - Restore non duplicate line to fix tests
* PM-3275 - Per PR feedback, revert all sync controller changes as the ProfileResponseModel.organizations array has org objects which have permissions which have the ManageResetPassword permission. So, I have the information that I need clientside already to determine if the user has the ManageResetPassword in any org.
* PM-3275 - PoliciesControllerTests.cs - Update imports as the PoliciesController was moved under the admin console team's domain.
* PM-3275 - Resolve issues from merge conflict resolutions to get solution building.
* PM-3275 / PM-4633 - PoliciesController.cs - use orgUserId to look up user instead of orgId. Oops.
* Fix user service tests
* Resolve merge conflict
2023-11-02 16:02:25 +01:00
private readonly ISetInitialMasterPasswordCommand _setInitialMasterPasswordCommand ;
2023-11-09 20:56:08 +01:00
private readonly IRotateUserKeyCommand _rotateUserKeyCommand ;
2024-07-23 20:53:08 +02:00
private readonly ITdeOffboardingPasswordCommand _tdeOffboardingPasswordCommand ;
2023-11-09 20:56:08 +01:00
private readonly IFeatureService _featureService ;
2024-05-23 16:17:00 +02:00
private readonly ISubscriberService _subscriberService ;
2024-02-09 17:58:37 +01:00
private readonly IReferenceEventService _referenceEventService ;
private readonly ICurrentContext _currentContext ;
2023-11-09 20:56:08 +01:00
2023-12-06 14:46:36 +01:00
private readonly IRotationValidator < IEnumerable < CipherWithIdRequestModel > , IEnumerable < Cipher > > _cipherValidator ;
private readonly IRotationValidator < IEnumerable < FolderWithIdRequestModel > , IEnumerable < Folder > > _folderValidator ;
2023-12-12 17:58:34 +01:00
private readonly IRotationValidator < IEnumerable < SendWithIdRequestModel > , IReadOnlyList < Send > > _sendValidator ;
2023-12-05 18:05:51 +01:00
private readonly IRotationValidator < IEnumerable < EmergencyAccessWithIdRequestModel > , IEnumerable < EmergencyAccess > >
_emergencyAccessValidator ;
2023-12-14 21:05:19 +01:00
private readonly IRotationValidator < IEnumerable < ResetPasswordWithOrgIdRequestModel > ,
IReadOnlyList < OrganizationUser > >
_resetPasswordValidator ;
2024-06-17 20:46:57 +02:00
private readonly IRotationValidator < IEnumerable < WebAuthnLoginRotateKeyRequestModel > , IEnumerable < WebAuthnLoginRotateKeyData > >
_webauthnKeyRotationValidator ;
2023-12-05 18:05:51 +01:00
2022-08-29 22:06:55 +02:00
2020-05-01 06:47:38 +02:00
public AccountsControllerTests ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_userService = Substitute . For < IUserService > ( ) ;
_organizationService = Substitute . For < IOrganizationService > ( ) ;
_organizationUserRepository = Substitute . For < IOrganizationUserRepository > ( ) ;
_providerUserRepository = Substitute . For < IProviderUserRepository > ( ) ;
_paymentService = Substitute . For < IPaymentService > ( ) ;
_globalSettings = new GlobalSettings ( ) ;
2023-04-17 16:35:47 +02:00
_policyService = Substitute . For < IPolicyService > ( ) ;
Auth/PM-3275 - Changes to support TDE User without MP being able to Set a Password + misc refactoring (#3242)
* PM-3275 - Add new GetMasterPasswordPolicy endpoint which will allow authenticated clients to get an enabled MP org policy if it exists for the purposes of enforcing those policy requirements when setting a password.
* PM-3275 - AccountsController.cs - PostSetPasswordAsync - (1) Convert UserService.setPasswordAsync into new SetInitialMasterPasswordCommand (2) Refactor SetInitialMasterPasswordCommand to only accept post SSO users who are in the invited state
(3) Add TODOs for more cleanup work and more commands
* PM-3275 - Update AccountsControllerTests.cs to add new SetInitialMasterPasswordCommand
* PM-3275 - UserService.cs - Remove non implemented ChangePasswordAsync method
* PM-3275 - The new SetInitialMasterPasswordCommand leveraged the OrganizationService.cs AcceptUserAsync method so while I was in here I converted the AcceptUserAsync methods into a new AcceptOrgUserCommand.cs and turned the private method which accepted an existing org user public for use in the SetInitialMasterPasswordCommand
* PM-3275 - Dotnet format
* PM-3275 - Test SetInitialMasterPasswordCommand
* Dotnet format
* PM-3275 - In process AcceptOrgUserCommandTests.cs
* PM-3275 - Migrate changes from AC-244 / #3199 over into new AcceptOrgUserCommand
* PM-3275 - AcceptOrgUserCommand.cs - create data protector specifically for this command
* PM-3275 - Add TODO for renaming / removing overloading of methods to improve readability / clarity
* PM-3275 - AcceptOrgUserCommand.cs - refactor AcceptOrgUserAsync by OrgId to retrieve orgUser with _organizationUserRepository.GetByOrganizationAsync which gets a single user instead of a collection
* PM-3275 - AcceptOrgUserCommand.cs - update name in TODO for evaluation later
* PM-3275 / PM-1196 - (1) Slightly refactor SsoEmail2faSessionTokenable to provide public static GetTokenLifeTime() method for testing (2) Add missed tests to SsoEmail2faSessionTokenable in preparation for building tests for new OrgUserInviteTokenable.cs
* PM-3275 / PM-1196 - Removing SsoEmail2faSessionTokenable.cs changes + tests as I've handled that separately in a new PR (#3270) for newly created task PM-3925
* PM-3275 - ExpiringTokenable.cs - add clarifying comments to help distinguish between the Valid property and the TokenIsValid method.
* PM-3275 - Create OrgUserInviteTokenable.cs and add tests in OrgUserInviteTokenableTests.cs
* PM-3275 - OrganizationService.cs - Refactor Org User Invite methods to use new OrgUserInviteTokenable instead of manual creation of a token
* PM-3275 - OrgUserInviteTokenable.cs - clarify backwards compat note
* PM-3275 - AcceptOrgUserCommand.cs - Add TODOs + minor name refactor
* PM-3275 - AcceptOrgUserCommand.cs - replace method overloading with more easily readable names.
* PM-3275 - AcceptOrgUserCommand.cs - Update ValidateOrgUserInviteToken to add new token validation while maintaining backwards compatibility for 1 release.
* dotnet format
* PM-3275 - AcceptOrgUserCommand.cs - Move private method below where it is used
* PM-3275 - ServiceCollectionExtensions.cs - Must register IDataProtectorTokenFactory<OrgUserInviteTokenable> for new tokenable
* PM-3275 - OrgUserInviteTokenable needed access to global settings to set its token lifetime to the _globalSettings.OrganizationInviteExpirationHours value. Creating a factory seemed the most straightforward way to encapsulate the desired creation logic. Unsure if in the correct location in ServiceCollectionExtensions.cs but will figure that out later.
* PM-3275 - In process work of creating AcceptOrgUserCommandTests.cs
* PM-3275 - Remove no longer relevant AcceptOrgUser tests from OrganizationServiceTests.cs
* PM-3275 - Register OrgUserInviteTokenableFactory alongside tokenizer
* PM-3275 - AcceptOrgUserCommandTests.cs - AcceptOrgUserAsync basic test suite completed.
* PM-3275 - AcceptOrgUserCommandTests.cs - tweak test names
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Remove old tests from OrganizationServiceTests as no longer needed to reference (2) Add summary for SetupCommonAcceptOrgUserMocks (3) Get AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail passing
* PM-3275 - Create interface for OrgUserInviteTokenableFactory b/c that's the right thing to do + enables test substitution
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Start work on AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail (2) Create and use SetupCommonAcceptOrgUserByTokenMocks() (3) Create generic FakeDataProtectorTokenFactory for tokenable testing
* PM-3275 - (1) Get AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail test passing (2) Move FakeDataProtectorTokenFactory to own file
* PM-3275 - AcceptOrgUserCommandTests.cs - Finish up tests for AcceptOrgUserByTokenAsync
* PM-3275 - Add pseudo section comments
* PM-3275 - Clean up unused params on AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest test
* PM-3275 - (1) Tests written for AcceptOrgUserByOrgSsoIdAsync (2) Refactor happy path assertions into helper function AssertValidAcceptedOrgUser to reduce code duplication
* PM-3275 - Finish up testing AcceptOrgUserCommandTests.cs by adding tests for AcceptOrgUserByOrgIdAsync
* PM-3275 - Tweaking test naming to ensure consistency.
* PM-3275 - Bugfix - OrgUserInviteTokenableFactory implementation required when declaring singleton service in ServiceCollectionExtensions.cs
* PM-3275 - Resolve failing OrganizationServiceTests.cs
* dotnet format
* PM-3275 - PoliciesController.cs - GetMasterPasswordPolicy bugfix - for orgs without a MP policy, policy comes back as null and we should return notFound in that case.
* PM-3275 - Add PoliciesControllerTests.cs specifically for new GetMasterPasswordPolicy(...) endpoint.
* PM-3275 - dotnet format PoliciesControllerTests.cs
* PM-3275 - PoliciesController.cs - (1) Add tech debt task number (2) Properly flag endpoint as deprecated
* PM-3275 - Add new hasManageResetPasswordPermission property to ProfileResponseModel.cs primarily for sync so that we can condition client side if TDE user obtains elevated permissions
* PM-3275 - Fix AccountsControllerTests.cs
* PM-3275 - OrgUserInviteTokenable.cs - clarify TODO
* PM-3275 - AcceptOrgUserCommand.cs - Refactor token validation to use short circuiting to only run old token validation if new token validation fails.
* PM-3275 - OrgUserInviteTokenable.cs - (1) Add new static methods to centralize validation logic to avoid repetition (2) Add new token validation method so we can avoid having to pass in a full org user (and hitting the db to do so)
* PM-3275 - Realized that the old token validation was used in the PoliciesController.cs (existing user clicks invite link in email and goes to log in) and UserService.cs (user clicks invite link in email and registers for a new acct). Added tech debt item for cleaning up backwards compatibility in future.
* dotnet format
* PM-3275 - (1) AccountsController.cs - Update PostSetPasswordAsync SetPasswordRequestModel to allow null keys for the case where we have a TDE user who obtains elevated permissions - they already have a user public and user encrypted private key saved in the db. (2) AccountsControllerTests.cs - test PostSetPasswordAsync scenarios to ensure changes will work as expected.
* PM-3275 - PR review feedback - (1) set CurrentContext to private (2) Refactor GetProfile to use variables to improve clarity and simplify debugging.
* PM-3275 - SyncController.cs - PR Review Feedback - Set current context as private instead of protected.
* PM-3275 - CurrentContextExtensions.cs - PR Feedback - move parenthesis up from own line.
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - Replace unnecessary variable
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - PR Feedback - Add expected outcome statement to test name
* PM-3275 - Set Initial Password command and tests - PR Feedback changes - (1) Rename orgIdentifier --> OrgSsoIdentifier for clarity (2) Update SetInitialMasterPasswordAsync to not allow null orgSsoId with explicit message saying this vs letting null org trigger invalid organization (3) Add test to cover this new scenario.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Move summary from implementation to interface to better respect standards and the fact that the interface is the more seen piece of code.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, rename AcceptOrgUserByTokenAsync -> AcceptOrgUserByEmailTokenAsync + replace generic name token with emailToken
* PM-3275 - OrganizationService.cs - Per PR feedback, remove dupe line
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove new lines in error messages for consistency.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Per PR feedback, adjust formatting of constructor for improved readability.
* PM-3275 - CurrentContextExtensions.cs - Refactor AnyOrgUserHasManageResetPasswordPermission per PR feedback to remove unnecessary var.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove completed TODO
* PM-3275 - PoliciesController.cs - Per PR feedback, update GetByInvitedUser param to be guid instead of string.
* PM-3275 - OrgUserInviteTokenable.cs - per PR feedback, add tech debt item info.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, use const purpose from tokenable instead of magic string.
* PM-3275 - Restore non duplicate line to fix tests
* PM-3275 - Per PR feedback, revert all sync controller changes as the ProfileResponseModel.organizations array has org objects which have permissions which have the ManageResetPassword permission. So, I have the information that I need clientside already to determine if the user has the ManageResetPassword in any org.
* PM-3275 - PoliciesControllerTests.cs - Update imports as the PoliciesController was moved under the admin console team's domain.
* PM-3275 - Resolve issues from merge conflict resolutions to get solution building.
* PM-3275 / PM-4633 - PoliciesController.cs - use orgUserId to look up user instead of orgId. Oops.
* Fix user service tests
* Resolve merge conflict
2023-11-02 16:02:25 +01:00
_setInitialMasterPasswordCommand = Substitute . For < ISetInitialMasterPasswordCommand > ( ) ;
2023-11-09 20:56:08 +01:00
_rotateUserKeyCommand = Substitute . For < IRotateUserKeyCommand > ( ) ;
2024-07-23 20:53:08 +02:00
_tdeOffboardingPasswordCommand = Substitute . For < ITdeOffboardingPasswordCommand > ( ) ;
2023-11-09 20:56:08 +01:00
_featureService = Substitute . For < IFeatureService > ( ) ;
2024-05-23 16:17:00 +02:00
_subscriberService = Substitute . For < ISubscriberService > ( ) ;
2024-02-09 17:58:37 +01:00
_referenceEventService = Substitute . For < IReferenceEventService > ( ) ;
_currentContext = Substitute . For < ICurrentContext > ( ) ;
2023-12-06 14:46:36 +01:00
_cipherValidator =
Substitute . For < IRotationValidator < IEnumerable < CipherWithIdRequestModel > , IEnumerable < Cipher > > > ( ) ;
_folderValidator =
Substitute . For < IRotationValidator < IEnumerable < FolderWithIdRequestModel > , IEnumerable < Folder > > > ( ) ;
2023-12-12 17:58:34 +01:00
_sendValidator = Substitute . For < IRotationValidator < IEnumerable < SendWithIdRequestModel > , IReadOnlyList < Send > > > ( ) ;
2023-12-05 18:05:51 +01:00
_emergencyAccessValidator = Substitute . For < IRotationValidator < IEnumerable < EmergencyAccessWithIdRequestModel > ,
IEnumerable < EmergencyAccess > > > ( ) ;
2024-06-17 20:46:57 +02:00
_webauthnKeyRotationValidator = Substitute . For < IRotationValidator < IEnumerable < WebAuthnLoginRotateKeyRequestModel > , IEnumerable < WebAuthnLoginRotateKeyData > > > ( ) ;
2023-12-14 21:05:19 +01:00
_resetPasswordValidator = Substitute
. For < IRotationValidator < IEnumerable < ResetPasswordWithOrgIdRequestModel > ,
IReadOnlyList < OrganizationUser > > > ( ) ;
Auth/PM-3275 - Changes to support TDE User without MP being able to Set a Password + misc refactoring (#3242)
* PM-3275 - Add new GetMasterPasswordPolicy endpoint which will allow authenticated clients to get an enabled MP org policy if it exists for the purposes of enforcing those policy requirements when setting a password.
* PM-3275 - AccountsController.cs - PostSetPasswordAsync - (1) Convert UserService.setPasswordAsync into new SetInitialMasterPasswordCommand (2) Refactor SetInitialMasterPasswordCommand to only accept post SSO users who are in the invited state
(3) Add TODOs for more cleanup work and more commands
* PM-3275 - Update AccountsControllerTests.cs to add new SetInitialMasterPasswordCommand
* PM-3275 - UserService.cs - Remove non implemented ChangePasswordAsync method
* PM-3275 - The new SetInitialMasterPasswordCommand leveraged the OrganizationService.cs AcceptUserAsync method so while I was in here I converted the AcceptUserAsync methods into a new AcceptOrgUserCommand.cs and turned the private method which accepted an existing org user public for use in the SetInitialMasterPasswordCommand
* PM-3275 - Dotnet format
* PM-3275 - Test SetInitialMasterPasswordCommand
* Dotnet format
* PM-3275 - In process AcceptOrgUserCommandTests.cs
* PM-3275 - Migrate changes from AC-244 / #3199 over into new AcceptOrgUserCommand
* PM-3275 - AcceptOrgUserCommand.cs - create data protector specifically for this command
* PM-3275 - Add TODO for renaming / removing overloading of methods to improve readability / clarity
* PM-3275 - AcceptOrgUserCommand.cs - refactor AcceptOrgUserAsync by OrgId to retrieve orgUser with _organizationUserRepository.GetByOrganizationAsync which gets a single user instead of a collection
* PM-3275 - AcceptOrgUserCommand.cs - update name in TODO for evaluation later
* PM-3275 / PM-1196 - (1) Slightly refactor SsoEmail2faSessionTokenable to provide public static GetTokenLifeTime() method for testing (2) Add missed tests to SsoEmail2faSessionTokenable in preparation for building tests for new OrgUserInviteTokenable.cs
* PM-3275 / PM-1196 - Removing SsoEmail2faSessionTokenable.cs changes + tests as I've handled that separately in a new PR (#3270) for newly created task PM-3925
* PM-3275 - ExpiringTokenable.cs - add clarifying comments to help distinguish between the Valid property and the TokenIsValid method.
* PM-3275 - Create OrgUserInviteTokenable.cs and add tests in OrgUserInviteTokenableTests.cs
* PM-3275 - OrganizationService.cs - Refactor Org User Invite methods to use new OrgUserInviteTokenable instead of manual creation of a token
* PM-3275 - OrgUserInviteTokenable.cs - clarify backwards compat note
* PM-3275 - AcceptOrgUserCommand.cs - Add TODOs + minor name refactor
* PM-3275 - AcceptOrgUserCommand.cs - replace method overloading with more easily readable names.
* PM-3275 - AcceptOrgUserCommand.cs - Update ValidateOrgUserInviteToken to add new token validation while maintaining backwards compatibility for 1 release.
* dotnet format
* PM-3275 - AcceptOrgUserCommand.cs - Move private method below where it is used
* PM-3275 - ServiceCollectionExtensions.cs - Must register IDataProtectorTokenFactory<OrgUserInviteTokenable> for new tokenable
* PM-3275 - OrgUserInviteTokenable needed access to global settings to set its token lifetime to the _globalSettings.OrganizationInviteExpirationHours value. Creating a factory seemed the most straightforward way to encapsulate the desired creation logic. Unsure if in the correct location in ServiceCollectionExtensions.cs but will figure that out later.
* PM-3275 - In process work of creating AcceptOrgUserCommandTests.cs
* PM-3275 - Remove no longer relevant AcceptOrgUser tests from OrganizationServiceTests.cs
* PM-3275 - Register OrgUserInviteTokenableFactory alongside tokenizer
* PM-3275 - AcceptOrgUserCommandTests.cs - AcceptOrgUserAsync basic test suite completed.
* PM-3275 - AcceptOrgUserCommandTests.cs - tweak test names
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Remove old tests from OrganizationServiceTests as no longer needed to reference (2) Add summary for SetupCommonAcceptOrgUserMocks (3) Get AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail passing
* PM-3275 - Create interface for OrgUserInviteTokenableFactory b/c that's the right thing to do + enables test substitution
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Start work on AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail (2) Create and use SetupCommonAcceptOrgUserByTokenMocks() (3) Create generic FakeDataProtectorTokenFactory for tokenable testing
* PM-3275 - (1) Get AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail test passing (2) Move FakeDataProtectorTokenFactory to own file
* PM-3275 - AcceptOrgUserCommandTests.cs - Finish up tests for AcceptOrgUserByTokenAsync
* PM-3275 - Add pseudo section comments
* PM-3275 - Clean up unused params on AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest test
* PM-3275 - (1) Tests written for AcceptOrgUserByOrgSsoIdAsync (2) Refactor happy path assertions into helper function AssertValidAcceptedOrgUser to reduce code duplication
* PM-3275 - Finish up testing AcceptOrgUserCommandTests.cs by adding tests for AcceptOrgUserByOrgIdAsync
* PM-3275 - Tweaking test naming to ensure consistency.
* PM-3275 - Bugfix - OrgUserInviteTokenableFactory implementation required when declaring singleton service in ServiceCollectionExtensions.cs
* PM-3275 - Resolve failing OrganizationServiceTests.cs
* dotnet format
* PM-3275 - PoliciesController.cs - GetMasterPasswordPolicy bugfix - for orgs without a MP policy, policy comes back as null and we should return notFound in that case.
* PM-3275 - Add PoliciesControllerTests.cs specifically for new GetMasterPasswordPolicy(...) endpoint.
* PM-3275 - dotnet format PoliciesControllerTests.cs
* PM-3275 - PoliciesController.cs - (1) Add tech debt task number (2) Properly flag endpoint as deprecated
* PM-3275 - Add new hasManageResetPasswordPermission property to ProfileResponseModel.cs primarily for sync so that we can condition client side if TDE user obtains elevated permissions
* PM-3275 - Fix AccountsControllerTests.cs
* PM-3275 - OrgUserInviteTokenable.cs - clarify TODO
* PM-3275 - AcceptOrgUserCommand.cs - Refactor token validation to use short circuiting to only run old token validation if new token validation fails.
* PM-3275 - OrgUserInviteTokenable.cs - (1) Add new static methods to centralize validation logic to avoid repetition (2) Add new token validation method so we can avoid having to pass in a full org user (and hitting the db to do so)
* PM-3275 - Realized that the old token validation was used in the PoliciesController.cs (existing user clicks invite link in email and goes to log in) and UserService.cs (user clicks invite link in email and registers for a new acct). Added tech debt item for cleaning up backwards compatibility in future.
* dotnet format
* PM-3275 - (1) AccountsController.cs - Update PostSetPasswordAsync SetPasswordRequestModel to allow null keys for the case where we have a TDE user who obtains elevated permissions - they already have a user public and user encrypted private key saved in the db. (2) AccountsControllerTests.cs - test PostSetPasswordAsync scenarios to ensure changes will work as expected.
* PM-3275 - PR review feedback - (1) set CurrentContext to private (2) Refactor GetProfile to use variables to improve clarity and simplify debugging.
* PM-3275 - SyncController.cs - PR Review Feedback - Set current context as private instead of protected.
* PM-3275 - CurrentContextExtensions.cs - PR Feedback - move parenthesis up from own line.
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - Replace unnecessary variable
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - PR Feedback - Add expected outcome statement to test name
* PM-3275 - Set Initial Password command and tests - PR Feedback changes - (1) Rename orgIdentifier --> OrgSsoIdentifier for clarity (2) Update SetInitialMasterPasswordAsync to not allow null orgSsoId with explicit message saying this vs letting null org trigger invalid organization (3) Add test to cover this new scenario.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Move summary from implementation to interface to better respect standards and the fact that the interface is the more seen piece of code.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, rename AcceptOrgUserByTokenAsync -> AcceptOrgUserByEmailTokenAsync + replace generic name token with emailToken
* PM-3275 - OrganizationService.cs - Per PR feedback, remove dupe line
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove new lines in error messages for consistency.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Per PR feedback, adjust formatting of constructor for improved readability.
* PM-3275 - CurrentContextExtensions.cs - Refactor AnyOrgUserHasManageResetPasswordPermission per PR feedback to remove unnecessary var.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove completed TODO
* PM-3275 - PoliciesController.cs - Per PR feedback, update GetByInvitedUser param to be guid instead of string.
* PM-3275 - OrgUserInviteTokenable.cs - per PR feedback, add tech debt item info.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, use const purpose from tokenable instead of magic string.
* PM-3275 - Restore non duplicate line to fix tests
* PM-3275 - Per PR feedback, revert all sync controller changes as the ProfileResponseModel.organizations array has org objects which have permissions which have the ManageResetPassword permission. So, I have the information that I need clientside already to determine if the user has the ManageResetPassword in any org.
* PM-3275 - PoliciesControllerTests.cs - Update imports as the PoliciesController was moved under the admin console team's domain.
* PM-3275 - Resolve issues from merge conflict resolutions to get solution building.
* PM-3275 / PM-4633 - PoliciesController.cs - use orgUserId to look up user instead of orgId. Oops.
* Fix user service tests
* Resolve merge conflict
2023-11-02 16:02:25 +01:00
2020-05-01 06:47:38 +02:00
_sut = new AccountsController (
2020-08-26 20:12:04 +02:00
_globalSettings ,
2020-05-01 06:47:38 +02:00
_organizationService ,
_organizationUserRepository ,
_providerUserRepository ,
_paymentService ,
_userService ,
Auth/PM-3275 - Changes to support TDE User without MP being able to Set a Password + misc refactoring (#3242)
* PM-3275 - Add new GetMasterPasswordPolicy endpoint which will allow authenticated clients to get an enabled MP org policy if it exists for the purposes of enforcing those policy requirements when setting a password.
* PM-3275 - AccountsController.cs - PostSetPasswordAsync - (1) Convert UserService.setPasswordAsync into new SetInitialMasterPasswordCommand (2) Refactor SetInitialMasterPasswordCommand to only accept post SSO users who are in the invited state
(3) Add TODOs for more cleanup work and more commands
* PM-3275 - Update AccountsControllerTests.cs to add new SetInitialMasterPasswordCommand
* PM-3275 - UserService.cs - Remove non implemented ChangePasswordAsync method
* PM-3275 - The new SetInitialMasterPasswordCommand leveraged the OrganizationService.cs AcceptUserAsync method so while I was in here I converted the AcceptUserAsync methods into a new AcceptOrgUserCommand.cs and turned the private method which accepted an existing org user public for use in the SetInitialMasterPasswordCommand
* PM-3275 - Dotnet format
* PM-3275 - Test SetInitialMasterPasswordCommand
* Dotnet format
* PM-3275 - In process AcceptOrgUserCommandTests.cs
* PM-3275 - Migrate changes from AC-244 / #3199 over into new AcceptOrgUserCommand
* PM-3275 - AcceptOrgUserCommand.cs - create data protector specifically for this command
* PM-3275 - Add TODO for renaming / removing overloading of methods to improve readability / clarity
* PM-3275 - AcceptOrgUserCommand.cs - refactor AcceptOrgUserAsync by OrgId to retrieve orgUser with _organizationUserRepository.GetByOrganizationAsync which gets a single user instead of a collection
* PM-3275 - AcceptOrgUserCommand.cs - update name in TODO for evaluation later
* PM-3275 / PM-1196 - (1) Slightly refactor SsoEmail2faSessionTokenable to provide public static GetTokenLifeTime() method for testing (2) Add missed tests to SsoEmail2faSessionTokenable in preparation for building tests for new OrgUserInviteTokenable.cs
* PM-3275 / PM-1196 - Removing SsoEmail2faSessionTokenable.cs changes + tests as I've handled that separately in a new PR (#3270) for newly created task PM-3925
* PM-3275 - ExpiringTokenable.cs - add clarifying comments to help distinguish between the Valid property and the TokenIsValid method.
* PM-3275 - Create OrgUserInviteTokenable.cs and add tests in OrgUserInviteTokenableTests.cs
* PM-3275 - OrganizationService.cs - Refactor Org User Invite methods to use new OrgUserInviteTokenable instead of manual creation of a token
* PM-3275 - OrgUserInviteTokenable.cs - clarify backwards compat note
* PM-3275 - AcceptOrgUserCommand.cs - Add TODOs + minor name refactor
* PM-3275 - AcceptOrgUserCommand.cs - replace method overloading with more easily readable names.
* PM-3275 - AcceptOrgUserCommand.cs - Update ValidateOrgUserInviteToken to add new token validation while maintaining backwards compatibility for 1 release.
* dotnet format
* PM-3275 - AcceptOrgUserCommand.cs - Move private method below where it is used
* PM-3275 - ServiceCollectionExtensions.cs - Must register IDataProtectorTokenFactory<OrgUserInviteTokenable> for new tokenable
* PM-3275 - OrgUserInviteTokenable needed access to global settings to set its token lifetime to the _globalSettings.OrganizationInviteExpirationHours value. Creating a factory seemed the most straightforward way to encapsulate the desired creation logic. Unsure if in the correct location in ServiceCollectionExtensions.cs but will figure that out later.
* PM-3275 - In process work of creating AcceptOrgUserCommandTests.cs
* PM-3275 - Remove no longer relevant AcceptOrgUser tests from OrganizationServiceTests.cs
* PM-3275 - Register OrgUserInviteTokenableFactory alongside tokenizer
* PM-3275 - AcceptOrgUserCommandTests.cs - AcceptOrgUserAsync basic test suite completed.
* PM-3275 - AcceptOrgUserCommandTests.cs - tweak test names
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Remove old tests from OrganizationServiceTests as no longer needed to reference (2) Add summary for SetupCommonAcceptOrgUserMocks (3) Get AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail passing
* PM-3275 - Create interface for OrgUserInviteTokenableFactory b/c that's the right thing to do + enables test substitution
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Start work on AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail (2) Create and use SetupCommonAcceptOrgUserByTokenMocks() (3) Create generic FakeDataProtectorTokenFactory for tokenable testing
* PM-3275 - (1) Get AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail test passing (2) Move FakeDataProtectorTokenFactory to own file
* PM-3275 - AcceptOrgUserCommandTests.cs - Finish up tests for AcceptOrgUserByTokenAsync
* PM-3275 - Add pseudo section comments
* PM-3275 - Clean up unused params on AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest test
* PM-3275 - (1) Tests written for AcceptOrgUserByOrgSsoIdAsync (2) Refactor happy path assertions into helper function AssertValidAcceptedOrgUser to reduce code duplication
* PM-3275 - Finish up testing AcceptOrgUserCommandTests.cs by adding tests for AcceptOrgUserByOrgIdAsync
* PM-3275 - Tweaking test naming to ensure consistency.
* PM-3275 - Bugfix - OrgUserInviteTokenableFactory implementation required when declaring singleton service in ServiceCollectionExtensions.cs
* PM-3275 - Resolve failing OrganizationServiceTests.cs
* dotnet format
* PM-3275 - PoliciesController.cs - GetMasterPasswordPolicy bugfix - for orgs without a MP policy, policy comes back as null and we should return notFound in that case.
* PM-3275 - Add PoliciesControllerTests.cs specifically for new GetMasterPasswordPolicy(...) endpoint.
* PM-3275 - dotnet format PoliciesControllerTests.cs
* PM-3275 - PoliciesController.cs - (1) Add tech debt task number (2) Properly flag endpoint as deprecated
* PM-3275 - Add new hasManageResetPasswordPermission property to ProfileResponseModel.cs primarily for sync so that we can condition client side if TDE user obtains elevated permissions
* PM-3275 - Fix AccountsControllerTests.cs
* PM-3275 - OrgUserInviteTokenable.cs - clarify TODO
* PM-3275 - AcceptOrgUserCommand.cs - Refactor token validation to use short circuiting to only run old token validation if new token validation fails.
* PM-3275 - OrgUserInviteTokenable.cs - (1) Add new static methods to centralize validation logic to avoid repetition (2) Add new token validation method so we can avoid having to pass in a full org user (and hitting the db to do so)
* PM-3275 - Realized that the old token validation was used in the PoliciesController.cs (existing user clicks invite link in email and goes to log in) and UserService.cs (user clicks invite link in email and registers for a new acct). Added tech debt item for cleaning up backwards compatibility in future.
* dotnet format
* PM-3275 - (1) AccountsController.cs - Update PostSetPasswordAsync SetPasswordRequestModel to allow null keys for the case where we have a TDE user who obtains elevated permissions - they already have a user public and user encrypted private key saved in the db. (2) AccountsControllerTests.cs - test PostSetPasswordAsync scenarios to ensure changes will work as expected.
* PM-3275 - PR review feedback - (1) set CurrentContext to private (2) Refactor GetProfile to use variables to improve clarity and simplify debugging.
* PM-3275 - SyncController.cs - PR Review Feedback - Set current context as private instead of protected.
* PM-3275 - CurrentContextExtensions.cs - PR Feedback - move parenthesis up from own line.
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - Replace unnecessary variable
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - PR Feedback - Add expected outcome statement to test name
* PM-3275 - Set Initial Password command and tests - PR Feedback changes - (1) Rename orgIdentifier --> OrgSsoIdentifier for clarity (2) Update SetInitialMasterPasswordAsync to not allow null orgSsoId with explicit message saying this vs letting null org trigger invalid organization (3) Add test to cover this new scenario.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Move summary from implementation to interface to better respect standards and the fact that the interface is the more seen piece of code.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, rename AcceptOrgUserByTokenAsync -> AcceptOrgUserByEmailTokenAsync + replace generic name token with emailToken
* PM-3275 - OrganizationService.cs - Per PR feedback, remove dupe line
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove new lines in error messages for consistency.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Per PR feedback, adjust formatting of constructor for improved readability.
* PM-3275 - CurrentContextExtensions.cs - Refactor AnyOrgUserHasManageResetPasswordPermission per PR feedback to remove unnecessary var.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove completed TODO
* PM-3275 - PoliciesController.cs - Per PR feedback, update GetByInvitedUser param to be guid instead of string.
* PM-3275 - OrgUserInviteTokenable.cs - per PR feedback, add tech debt item info.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, use const purpose from tokenable instead of magic string.
* PM-3275 - Restore non duplicate line to fix tests
* PM-3275 - Per PR feedback, revert all sync controller changes as the ProfileResponseModel.organizations array has org objects which have permissions which have the ManageResetPassword permission. So, I have the information that I need clientside already to determine if the user has the ManageResetPassword in any org.
* PM-3275 - PoliciesControllerTests.cs - Update imports as the PoliciesController was moved under the admin console team's domain.
* PM-3275 - Resolve issues from merge conflict resolutions to get solution building.
* PM-3275 / PM-4633 - PoliciesController.cs - use orgUserId to look up user instead of orgId. Oops.
* Fix user service tests
* Resolve merge conflict
2023-11-02 16:02:25 +01:00
_policyService ,
2023-11-09 20:56:08 +01:00
_setInitialMasterPasswordCommand ,
2024-07-23 20:53:08 +02:00
_tdeOffboardingPasswordCommand ,
2023-11-09 20:56:08 +01:00
_rotateUserKeyCommand ,
_featureService ,
2024-05-23 16:17:00 +02:00
_subscriberService ,
2024-02-09 17:58:37 +01:00
_referenceEventService ,
_currentContext ,
2023-12-06 14:46:36 +01:00
_cipherValidator ,
_folderValidator ,
2023-12-12 17:58:34 +01:00
_sendValidator ,
2023-12-14 21:05:19 +01:00
_emergencyAccessValidator ,
2024-06-17 20:46:57 +02:00
_resetPasswordValidator ,
_webauthnKeyRotationValidator
2022-08-29 22:06:55 +02:00
) ;
}
2020-05-01 06:47:38 +02:00
public void Dispose ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_sut ? . Dispose ( ) ;
2022-08-29 22:06:55 +02:00
}
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostPasswordHint_ShouldNotifyUserService ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var email = "user@example.com" ;
await _sut . PostPasswordHint ( new PasswordHintRequestModel { Email = email } ) ;
await _userService . Received ( 1 ) . SendMasterPasswordHintAsync ( email ) ;
}
2022-08-29 22:06:55 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostEmailToken_ShouldInitiateEmailChange ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
ConfigureUserServiceToAcceptPasswordFor ( user ) ;
var newEmail = "example@user.com" ;
await _sut . PostEmailToken ( new EmailTokenRequestModel { NewEmail = newEmail } ) ;
await _userService . Received ( 1 ) . InitiateEmailChangeAsync ( user , newEmail ) ;
2022-08-29 21:53:48 +02:00
}
2020-05-01 06:47:38 +02:00
2024-10-28 17:12:13 +01:00
[Fact]
public async Task PostEmailToken_WithAccountDeprovisioningEnabled_WhenUserIsNotManagedByAnOrganization_ShouldInitiateEmailChange ( )
{
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
ConfigureUserServiceToAcceptPasswordFor ( user ) ;
_featureService . IsEnabled ( FeatureFlagKeys . AccountDeprovisioning ) . Returns ( true ) ;
_userService . IsManagedByAnyOrganizationAsync ( user . Id ) . Returns ( false ) ;
var newEmail = "example@user.com" ;
await _sut . PostEmailToken ( new EmailTokenRequestModel { NewEmail = newEmail } ) ;
await _userService . Received ( 1 ) . InitiateEmailChangeAsync ( user , newEmail ) ;
}
2020-05-01 06:47:38 +02:00
[Fact]
public async Task PostEmailToken_WhenNotAuthorized_ShouldThrowUnauthorizedAccessException ( )
2022-08-29 21:53:48 +02:00
{
2020-05-01 06:47:38 +02:00
ConfigureUserServiceToReturnNullPrincipal ( ) ;
await Assert . ThrowsAsync < UnauthorizedAccessException > (
( ) = > _sut . PostEmailToken ( new EmailTokenRequestModel ( ) )
2022-08-29 20:53:16 +02:00
) ;
2020-05-01 06:47:38 +02:00
}
[Fact]
public async Task PostEmailToken_WhenInvalidPasssword_ShouldThrowBadRequestException ( )
{
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
ConfigureUserServiceToRejectPasswordFor ( user ) ;
await Assert . ThrowsAsync < BadRequestException > (
( ) = > _sut . PostEmailToken ( new EmailTokenRequestModel ( ) )
) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
2024-10-28 17:12:13 +01:00
[Fact]
public async Task PostEmailToken_WithAccountDeprovisioningEnabled_WhenUserIsManagedByAnOrganization_ShouldThrowBadRequestException ( )
{
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
ConfigureUserServiceToAcceptPasswordFor ( user ) ;
_featureService . IsEnabled ( FeatureFlagKeys . AccountDeprovisioning ) . Returns ( true ) ;
_userService . IsManagedByAnyOrganizationAsync ( user . Id ) . Returns ( true ) ;
var result = await Assert . ThrowsAsync < BadRequestException > (
( ) = > _sut . PostEmailToken ( new EmailTokenRequestModel ( ) )
) ;
Assert . Equal ( "Cannot change emails for accounts owned by an organization. Contact your organization administrator for additional details." , result . Message ) ;
}
2022-08-29 22:06:55 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostEmail_ShouldChangeUserEmail ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
_userService . ChangeEmailAsync ( user , default , default , default , default , default )
. Returns ( Task . FromResult ( IdentityResult . Success ) ) ;
await _sut . PostEmail ( new EmailRequestModel ( ) ) ;
await _userService . Received ( 1 ) . ChangeEmailAsync ( user , default , default , default , default , default ) ;
}
2024-10-28 17:12:13 +01:00
[Fact]
public async Task PostEmail_WithAccountDeprovisioningEnabled_WhenUserIsNotManagedByAnOrganization_ShouldChangeUserEmail ( )
{
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
_userService . ChangeEmailAsync ( user , default , default , default , default , default )
. Returns ( Task . FromResult ( IdentityResult . Success ) ) ;
_featureService . IsEnabled ( FeatureFlagKeys . AccountDeprovisioning ) . Returns ( true ) ;
_userService . IsManagedByAnyOrganizationAsync ( user . Id ) . Returns ( false ) ;
await _sut . PostEmail ( new EmailRequestModel ( ) ) ;
await _userService . Received ( 1 ) . ChangeEmailAsync ( user , default , default , default , default , default ) ;
}
2020-05-01 06:47:38 +02:00
[Fact]
public async Task PostEmail_WhenNotAuthorized_ShouldThrownUnauthorizedAccessException ( )
2022-08-29 21:53:48 +02:00
{
2020-05-01 06:47:38 +02:00
ConfigureUserServiceToReturnNullPrincipal ( ) ;
await Assert . ThrowsAsync < UnauthorizedAccessException > (
( ) = > _sut . PostEmail ( new EmailRequestModel ( ) )
) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
2022-08-29 22:06:55 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostEmail_WhenEmailCannotBeChanged_ShouldThrowBadRequestException ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
_userService . ChangeEmailAsync ( user , default , default , default , default , default )
. Returns ( Task . FromResult ( IdentityResult . Failed ( ) ) ) ;
2022-08-29 22:06:55 +02:00
2020-05-01 06:47:38 +02:00
await Assert . ThrowsAsync < BadRequestException > (
( ) = > _sut . PostEmail ( new EmailRequestModel ( ) )
2022-08-29 22:06:55 +02:00
) ;
2020-05-01 06:47:38 +02:00
}
2024-10-28 17:12:13 +01:00
[Fact]
public async Task PostEmail_WithAccountDeprovisioningEnabled_WhenUserIsManagedByAnOrganization_ShouldThrowBadRequestException ( )
{
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
_featureService . IsEnabled ( FeatureFlagKeys . AccountDeprovisioning ) . Returns ( true ) ;
_userService . IsManagedByAnyOrganizationAsync ( user . Id ) . Returns ( true ) ;
var result = await Assert . ThrowsAsync < BadRequestException > (
( ) = > _sut . PostEmail ( new EmailRequestModel ( ) )
) ;
Assert . Equal ( "Cannot change emails for accounts owned by an organization. Contact your organization administrator for additional details." , result . Message ) ;
}
2022-08-29 20:53:16 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostVerifyEmail_ShouldSendEmailVerification ( )
2022-08-29 20:53:16 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
2022-07-11 15:28:14 +02:00
await _sut . PostVerifyEmail ( ) ;
2020-05-01 06:47:38 +02:00
await _userService . Received ( 1 ) . SendEmailVerificationAsync ( user ) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
2022-08-29 22:06:55 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostVerifyEmail_WhenNotAuthorized_ShouldThrownUnauthorizedAccessException ( )
{
ConfigureUserServiceToReturnNullPrincipal ( ) ;
await Assert . ThrowsAsync < UnauthorizedAccessException > (
( ) = > _sut . PostVerifyEmail ( )
) ;
2022-08-29 21:53:48 +02:00
}
2020-11-10 21:15:29 +01:00
[Fact]
public async Task PostVerifyEmailToken_ShouldConfirmEmail ( )
2022-08-29 21:53:48 +02:00
{
2020-11-10 21:15:29 +01:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidIdFor ( user ) ;
_userService . ConfirmEmailAsync ( user , Arg . Any < string > ( ) )
. Returns ( Task . FromResult ( IdentityResult . Success ) ) ;
await _sut . PostVerifyEmailToken ( new VerifyEmailRequestModel { UserId = "12345678-1234-1234-1234-123456789012" } ) ;
2020-05-01 06:47:38 +02:00
await _userService . Received ( 1 ) . ConfirmEmailAsync ( user , Arg . Any < string > ( ) ) ;
2022-08-29 21:53:48 +02:00
}
2020-11-10 21:15:29 +01:00
2022-08-29 22:06:55 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostVerifyEmailToken_WhenUserDoesNotExist_ShouldThrowUnauthorizedAccessException ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnNullUserId ( ) ;
await Assert . ThrowsAsync < UnauthorizedAccessException > (
( ) = > _sut . PostVerifyEmailToken ( new VerifyEmailRequestModel { UserId = "12345678-1234-1234-1234-123456789012" } )
) ;
}
2022-08-29 20:53:16 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostVerifyEmailToken_WhenEmailConfirmationFails_ShouldThrowBadRequestException ( )
2022-08-29 20:53:16 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidIdFor ( user ) ;
_userService . ConfirmEmailAsync ( user , Arg . Any < string > ( ) )
. Returns ( Task . FromResult ( IdentityResult . Failed ( ) ) ) ;
2022-08-29 22:06:55 +02:00
2020-11-10 21:15:29 +01:00
await Assert . ThrowsAsync < BadRequestException > (
2021-11-09 16:37:32 +01:00
( ) = > _sut . PostVerifyEmailToken ( new VerifyEmailRequestModel { UserId = "12345678-1234-1234-1234-123456789012" } )
2022-08-29 22:06:55 +02:00
) ;
}
2020-05-01 06:47:38 +02:00
[Fact]
public async Task PostPassword_ShouldChangePassword ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
_userService . ChangePasswordAsync ( user , default , default , default , default )
. Returns ( Task . FromResult ( IdentityResult . Success ) ) ;
2021-11-09 16:37:32 +01:00
await _sut . PostPassword ( new PasswordRequestModel ( ) ) ;
2020-05-01 06:47:38 +02:00
await _userService . Received ( 1 ) . ChangePasswordAsync ( user , default , default , default , default ) ;
2022-08-29 20:53:16 +02:00
}
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostPassword_WhenNotAuthorized_ShouldThrowUnauthorizedAccessException ( )
2022-08-29 21:53:48 +02:00
{
2020-11-10 21:15:29 +01:00
ConfigureUserServiceToReturnNullPrincipal ( ) ;
2022-08-29 20:53:16 +02:00
2020-05-01 06:47:38 +02:00
await Assert . ThrowsAsync < UnauthorizedAccessException > (
( ) = > _sut . PostPassword ( new PasswordRequestModel ( ) )
2022-08-29 20:53:16 +02:00
) ;
2020-05-01 06:47:38 +02:00
}
[Fact]
public async Task PostPassword_WhenPasswordChangeFails_ShouldBadRequestException ( )
{
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
_userService . ChangePasswordAsync ( user , default , default , default , default )
. Returns ( Task . FromResult ( IdentityResult . Failed ( ) ) ) ;
2022-08-29 22:06:55 +02:00
2020-11-10 21:15:29 +01:00
await Assert . ThrowsAsync < BadRequestException > (
2020-05-01 06:47:38 +02:00
( ) = > _sut . PostPassword ( new PasswordRequestModel ( ) )
2022-08-29 20:53:16 +02:00
) ;
}
2022-08-29 22:06:55 +02:00
[Fact]
2020-11-10 21:15:29 +01:00
public async Task GetApiKey_ShouldReturnApiKeyResponse ( )
2022-08-29 22:06:55 +02:00
{
2020-11-10 21:15:29 +01:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
2020-05-01 06:47:38 +02:00
ConfigureUserServiceToAcceptPasswordFor ( user ) ;
await _sut . ApiKey ( new SecretVerificationRequestModel ( ) ) ;
2022-08-29 20:53:16 +02:00
}
2022-08-29 22:06:55 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task GetApiKey_WhenUserDoesNotExist_ShouldThrowUnauthorizedAccessException ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
ConfigureUserServiceToReturnNullPrincipal ( ) ;
2022-08-29 20:53:16 +02:00
2020-05-01 06:47:38 +02:00
await Assert . ThrowsAsync < UnauthorizedAccessException > (
( ) = > _sut . ApiKey ( new SecretVerificationRequestModel ( ) )
) ;
2022-08-29 20:53:16 +02:00
}
2020-05-01 06:47:38 +02:00
[Fact]
public async Task GetApiKey_WhenPasswordCheckFails_ShouldThrowBadRequestException ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
ConfigureUserServiceToRejectPasswordFor ( user ) ;
await Assert . ThrowsAsync < BadRequestException > (
2021-11-09 16:37:32 +01:00
( ) = > _sut . ApiKey ( new SecretVerificationRequestModel ( ) )
2020-05-01 06:47:38 +02:00
) ;
2022-08-29 20:53:16 +02:00
}
2022-08-29 22:06:55 +02:00
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostRotateApiKey_ShouldRotateApiKey ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
ConfigureUserServiceToAcceptPasswordFor ( user ) ;
await _sut . RotateApiKey ( new SecretVerificationRequestModel ( ) ) ;
2022-08-29 22:06:55 +02:00
}
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostRotateApiKey_WhenUserDoesNotExist_ShouldThrowUnauthorizedAccessException ( )
2022-08-29 22:06:55 +02:00
{
2021-11-09 16:37:32 +01:00
ConfigureUserServiceToReturnNullPrincipal ( ) ;
2022-08-29 22:06:55 +02:00
2020-05-01 06:47:38 +02:00
await Assert . ThrowsAsync < UnauthorizedAccessException > (
( ) = > _sut . ApiKey ( new SecretVerificationRequestModel ( ) )
2022-08-29 22:06:55 +02:00
) ;
}
[Fact]
2020-05-01 06:47:38 +02:00
public async Task PostRotateApiKey_WhenPasswordCheckFails_ShouldThrowBadRequestException ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
var user = GenerateExampleUser ( ) ;
ConfigureUserServiceToReturnValidPrincipalFor ( user ) ;
ConfigureUserServiceToRejectPasswordFor ( user ) ;
await Assert . ThrowsAsync < BadRequestException > (
( ) = > _sut . ApiKey ( new SecretVerificationRequestModel ( ) )
2022-08-29 22:06:55 +02:00
) ;
2022-08-29 20:53:16 +02:00
}
Auth/PM-3275 - Changes to support TDE User without MP being able to Set a Password + misc refactoring (#3242)
* PM-3275 - Add new GetMasterPasswordPolicy endpoint which will allow authenticated clients to get an enabled MP org policy if it exists for the purposes of enforcing those policy requirements when setting a password.
* PM-3275 - AccountsController.cs - PostSetPasswordAsync - (1) Convert UserService.setPasswordAsync into new SetInitialMasterPasswordCommand (2) Refactor SetInitialMasterPasswordCommand to only accept post SSO users who are in the invited state
(3) Add TODOs for more cleanup work and more commands
* PM-3275 - Update AccountsControllerTests.cs to add new SetInitialMasterPasswordCommand
* PM-3275 - UserService.cs - Remove non implemented ChangePasswordAsync method
* PM-3275 - The new SetInitialMasterPasswordCommand leveraged the OrganizationService.cs AcceptUserAsync method so while I was in here I converted the AcceptUserAsync methods into a new AcceptOrgUserCommand.cs and turned the private method which accepted an existing org user public for use in the SetInitialMasterPasswordCommand
* PM-3275 - Dotnet format
* PM-3275 - Test SetInitialMasterPasswordCommand
* Dotnet format
* PM-3275 - In process AcceptOrgUserCommandTests.cs
* PM-3275 - Migrate changes from AC-244 / #3199 over into new AcceptOrgUserCommand
* PM-3275 - AcceptOrgUserCommand.cs - create data protector specifically for this command
* PM-3275 - Add TODO for renaming / removing overloading of methods to improve readability / clarity
* PM-3275 - AcceptOrgUserCommand.cs - refactor AcceptOrgUserAsync by OrgId to retrieve orgUser with _organizationUserRepository.GetByOrganizationAsync which gets a single user instead of a collection
* PM-3275 - AcceptOrgUserCommand.cs - update name in TODO for evaluation later
* PM-3275 / PM-1196 - (1) Slightly refactor SsoEmail2faSessionTokenable to provide public static GetTokenLifeTime() method for testing (2) Add missed tests to SsoEmail2faSessionTokenable in preparation for building tests for new OrgUserInviteTokenable.cs
* PM-3275 / PM-1196 - Removing SsoEmail2faSessionTokenable.cs changes + tests as I've handled that separately in a new PR (#3270) for newly created task PM-3925
* PM-3275 - ExpiringTokenable.cs - add clarifying comments to help distinguish between the Valid property and the TokenIsValid method.
* PM-3275 - Create OrgUserInviteTokenable.cs and add tests in OrgUserInviteTokenableTests.cs
* PM-3275 - OrganizationService.cs - Refactor Org User Invite methods to use new OrgUserInviteTokenable instead of manual creation of a token
* PM-3275 - OrgUserInviteTokenable.cs - clarify backwards compat note
* PM-3275 - AcceptOrgUserCommand.cs - Add TODOs + minor name refactor
* PM-3275 - AcceptOrgUserCommand.cs - replace method overloading with more easily readable names.
* PM-3275 - AcceptOrgUserCommand.cs - Update ValidateOrgUserInviteToken to add new token validation while maintaining backwards compatibility for 1 release.
* dotnet format
* PM-3275 - AcceptOrgUserCommand.cs - Move private method below where it is used
* PM-3275 - ServiceCollectionExtensions.cs - Must register IDataProtectorTokenFactory<OrgUserInviteTokenable> for new tokenable
* PM-3275 - OrgUserInviteTokenable needed access to global settings to set its token lifetime to the _globalSettings.OrganizationInviteExpirationHours value. Creating a factory seemed the most straightforward way to encapsulate the desired creation logic. Unsure if in the correct location in ServiceCollectionExtensions.cs but will figure that out later.
* PM-3275 - In process work of creating AcceptOrgUserCommandTests.cs
* PM-3275 - Remove no longer relevant AcceptOrgUser tests from OrganizationServiceTests.cs
* PM-3275 - Register OrgUserInviteTokenableFactory alongside tokenizer
* PM-3275 - AcceptOrgUserCommandTests.cs - AcceptOrgUserAsync basic test suite completed.
* PM-3275 - AcceptOrgUserCommandTests.cs - tweak test names
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Remove old tests from OrganizationServiceTests as no longer needed to reference (2) Add summary for SetupCommonAcceptOrgUserMocks (3) Get AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail passing
* PM-3275 - Create interface for OrgUserInviteTokenableFactory b/c that's the right thing to do + enables test substitution
* PM-3275 - AcceptOrgUserCommandTests.cs - (1) Start work on AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail (2) Create and use SetupCommonAcceptOrgUserByTokenMocks() (3) Create generic FakeDataProtectorTokenFactory for tokenable testing
* PM-3275 - (1) Get AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail test passing (2) Move FakeDataProtectorTokenFactory to own file
* PM-3275 - AcceptOrgUserCommandTests.cs - Finish up tests for AcceptOrgUserByTokenAsync
* PM-3275 - Add pseudo section comments
* PM-3275 - Clean up unused params on AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest test
* PM-3275 - (1) Tests written for AcceptOrgUserByOrgSsoIdAsync (2) Refactor happy path assertions into helper function AssertValidAcceptedOrgUser to reduce code duplication
* PM-3275 - Finish up testing AcceptOrgUserCommandTests.cs by adding tests for AcceptOrgUserByOrgIdAsync
* PM-3275 - Tweaking test naming to ensure consistency.
* PM-3275 - Bugfix - OrgUserInviteTokenableFactory implementation required when declaring singleton service in ServiceCollectionExtensions.cs
* PM-3275 - Resolve failing OrganizationServiceTests.cs
* dotnet format
* PM-3275 - PoliciesController.cs - GetMasterPasswordPolicy bugfix - for orgs without a MP policy, policy comes back as null and we should return notFound in that case.
* PM-3275 - Add PoliciesControllerTests.cs specifically for new GetMasterPasswordPolicy(...) endpoint.
* PM-3275 - dotnet format PoliciesControllerTests.cs
* PM-3275 - PoliciesController.cs - (1) Add tech debt task number (2) Properly flag endpoint as deprecated
* PM-3275 - Add new hasManageResetPasswordPermission property to ProfileResponseModel.cs primarily for sync so that we can condition client side if TDE user obtains elevated permissions
* PM-3275 - Fix AccountsControllerTests.cs
* PM-3275 - OrgUserInviteTokenable.cs - clarify TODO
* PM-3275 - AcceptOrgUserCommand.cs - Refactor token validation to use short circuiting to only run old token validation if new token validation fails.
* PM-3275 - OrgUserInviteTokenable.cs - (1) Add new static methods to centralize validation logic to avoid repetition (2) Add new token validation method so we can avoid having to pass in a full org user (and hitting the db to do so)
* PM-3275 - Realized that the old token validation was used in the PoliciesController.cs (existing user clicks invite link in email and goes to log in) and UserService.cs (user clicks invite link in email and registers for a new acct). Added tech debt item for cleaning up backwards compatibility in future.
* dotnet format
* PM-3275 - (1) AccountsController.cs - Update PostSetPasswordAsync SetPasswordRequestModel to allow null keys for the case where we have a TDE user who obtains elevated permissions - they already have a user public and user encrypted private key saved in the db. (2) AccountsControllerTests.cs - test PostSetPasswordAsync scenarios to ensure changes will work as expected.
* PM-3275 - PR review feedback - (1) set CurrentContext to private (2) Refactor GetProfile to use variables to improve clarity and simplify debugging.
* PM-3275 - SyncController.cs - PR Review Feedback - Set current context as private instead of protected.
* PM-3275 - CurrentContextExtensions.cs - PR Feedback - move parenthesis up from own line.
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - Replace unnecessary variable
* PM-3275 - SetInitialMasterPasswordCommandTests.cs - PR Feedback - Add expected outcome statement to test name
* PM-3275 - Set Initial Password command and tests - PR Feedback changes - (1) Rename orgIdentifier --> OrgSsoIdentifier for clarity (2) Update SetInitialMasterPasswordAsync to not allow null orgSsoId with explicit message saying this vs letting null org trigger invalid organization (3) Add test to cover this new scenario.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Move summary from implementation to interface to better respect standards and the fact that the interface is the more seen piece of code.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, rename AcceptOrgUserByTokenAsync -> AcceptOrgUserByEmailTokenAsync + replace generic name token with emailToken
* PM-3275 - OrganizationService.cs - Per PR feedback, remove dupe line
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove new lines in error messages for consistency.
* PM-3275 - SetInitialMasterPasswordCommand.cs - Per PR feedback, adjust formatting of constructor for improved readability.
* PM-3275 - CurrentContextExtensions.cs - Refactor AnyOrgUserHasManageResetPasswordPermission per PR feedback to remove unnecessary var.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, remove completed TODO
* PM-3275 - PoliciesController.cs - Per PR feedback, update GetByInvitedUser param to be guid instead of string.
* PM-3275 - OrgUserInviteTokenable.cs - per PR feedback, add tech debt item info.
* PM-3275 - AcceptOrgUserCommand.cs - Per PR feedback, use const purpose from tokenable instead of magic string.
* PM-3275 - Restore non duplicate line to fix tests
* PM-3275 - Per PR feedback, revert all sync controller changes as the ProfileResponseModel.organizations array has org objects which have permissions which have the ManageResetPassword permission. So, I have the information that I need clientside already to determine if the user has the ManageResetPassword in any org.
* PM-3275 - PoliciesControllerTests.cs - Update imports as the PoliciesController was moved under the admin console team's domain.
* PM-3275 - Resolve issues from merge conflict resolutions to get solution building.
* PM-3275 / PM-4633 - PoliciesController.cs - use orgUserId to look up user instead of orgId. Oops.
* Fix user service tests
* Resolve merge conflict
2023-11-02 16:02:25 +01:00
[Theory]
[BitAutoData(true, false)] // User has PublicKey and PrivateKey, and Keys in request are NOT null
[BitAutoData(true, true)] // User has PublicKey and PrivateKey, and Keys in request are null
[BitAutoData(false, false)] // User has neither PublicKey nor PrivateKey, and Keys in request are NOT null
[BitAutoData(false, true)] // User has neither PublicKey nor PrivateKey, and Keys in request are null
public async Task PostSetPasswordAsync_WhenUserExistsAndSettingPasswordSucceeds_ShouldHandleKeysCorrectlyAndReturn (
bool hasExistingKeys ,
bool shouldSetKeysToNull ,
User user ,
SetPasswordRequestModel setPasswordRequestModel )
{
// Arrange
const string existingPublicKey = "existingPublicKey" ;
const string existingEncryptedPrivateKey = "existingEncryptedPrivateKey" ;
const string newPublicKey = "newPublicKey" ;
const string newEncryptedPrivateKey = "newEncryptedPrivateKey" ;
if ( hasExistingKeys )
{
user . PublicKey = existingPublicKey ;
user . PrivateKey = existingEncryptedPrivateKey ;
}
else
{
user . PublicKey = null ;
user . PrivateKey = null ;
}
if ( shouldSetKeysToNull )
{
setPasswordRequestModel . Keys = null ;
}
else
{
setPasswordRequestModel . Keys = new KeysRequestModel ( )
{
PublicKey = newPublicKey ,
EncryptedPrivateKey = newEncryptedPrivateKey
} ;
}
_userService . GetUserByPrincipalAsync ( Arg . Any < ClaimsPrincipal > ( ) ) . Returns ( Task . FromResult ( user ) ) ;
_setInitialMasterPasswordCommand . SetInitialMasterPasswordAsync (
user ,
setPasswordRequestModel . MasterPasswordHash ,
setPasswordRequestModel . Key ,
setPasswordRequestModel . OrgIdentifier )
. Returns ( Task . FromResult ( IdentityResult . Success ) ) ;
// Act
await _sut . PostSetPasswordAsync ( setPasswordRequestModel ) ;
// Assert
await _setInitialMasterPasswordCommand . Received ( 1 )
. SetInitialMasterPasswordAsync (
Arg . Is < User > ( u = > u = = user ) ,
Arg . Is < string > ( s = > s = = setPasswordRequestModel . MasterPasswordHash ) ,
Arg . Is < string > ( s = > s = = setPasswordRequestModel . Key ) ,
Arg . Is < string > ( s = > s = = setPasswordRequestModel . OrgIdentifier ) ) ;
// Additional Assertions for User object modifications
Assert . Equal ( setPasswordRequestModel . MasterPasswordHint , user . MasterPasswordHint ) ;
Assert . Equal ( setPasswordRequestModel . Kdf , user . Kdf ) ;
Assert . Equal ( setPasswordRequestModel . KdfIterations , user . KdfIterations ) ;
Assert . Equal ( setPasswordRequestModel . KdfMemory , user . KdfMemory ) ;
Assert . Equal ( setPasswordRequestModel . KdfParallelism , user . KdfParallelism ) ;
Assert . Equal ( setPasswordRequestModel . Key , user . Key ) ;
if ( hasExistingKeys )
{
// User Keys should not be modified
Assert . Equal ( existingPublicKey , user . PublicKey ) ;
Assert . Equal ( existingEncryptedPrivateKey , user . PrivateKey ) ;
}
else if ( ! shouldSetKeysToNull )
{
// User had no keys so they should be set to the request model's keys
Assert . Equal ( setPasswordRequestModel . Keys . PublicKey , user . PublicKey ) ;
Assert . Equal ( setPasswordRequestModel . Keys . EncryptedPrivateKey , user . PrivateKey ) ;
}
else
{
// User had no keys and the request model's keys were null, so they should be set to null
Assert . Null ( user . PublicKey ) ;
Assert . Null ( user . PrivateKey ) ;
}
}
[Theory]
[BitAutoData]
public async Task PostSetPasswordAsync_WhenUserDoesNotExist_ShouldThrowUnauthorizedAccessException (
SetPasswordRequestModel setPasswordRequestModel )
{
// Arrange
_userService . GetUserByPrincipalAsync ( Arg . Any < ClaimsPrincipal > ( ) ) . Returns ( Task . FromResult ( ( User ) null ) ) ;
// Act & Assert
await Assert . ThrowsAsync < UnauthorizedAccessException > ( ( ) = > _sut . PostSetPasswordAsync ( setPasswordRequestModel ) ) ;
}
[Theory]
[BitAutoData]
public async Task PostSetPasswordAsync_WhenSettingPasswordFails_ShouldThrowBadRequestException (
User user ,
SetPasswordRequestModel model )
{
// Arrange
_userService . GetUserByPrincipalAsync ( Arg . Any < ClaimsPrincipal > ( ) ) . Returns ( Task . FromResult ( user ) ) ;
_setInitialMasterPasswordCommand . SetInitialMasterPasswordAsync ( Arg . Any < User > ( ) , Arg . Any < string > ( ) , Arg . Any < string > ( ) , Arg . Any < string > ( ) )
. Returns ( Task . FromResult ( IdentityResult . Failed ( new IdentityError { Description = "Some Error" } ) ) ) ;
// Act & Assert
await Assert . ThrowsAsync < BadRequestException > ( ( ) = > _sut . PostSetPasswordAsync ( model ) ) ;
}
2020-05-01 06:47:38 +02:00
// Below are helper functions that currently belong to this
// test class, but ultimately may need to be split out into
// something greater in order to share common test steps with
// other test suites. They are included here for the time being
// until that day comes.
private User GenerateExampleUser ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
return new User
2022-08-29 20:53:16 +02:00
{
2020-05-01 06:47:38 +02:00
Email = "user@example.com"
2022-08-29 22:06:55 +02:00
} ;
}
2021-11-09 16:37:32 +01:00
private void ConfigureUserServiceToReturnNullPrincipal ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_userService . GetUserByPrincipalAsync ( Arg . Any < ClaimsPrincipal > ( ) )
. Returns ( Task . FromResult ( ( User ) null ) ) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
private void ConfigureUserServiceToReturnValidPrincipalFor ( User user )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_userService . GetUserByPrincipalAsync ( Arg . Any < ClaimsPrincipal > ( ) )
. Returns ( Task . FromResult ( user ) ) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
private void ConfigureUserServiceToRejectPasswordFor ( User user )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_userService . CheckPasswordAsync ( user , Arg . Any < string > ( ) )
. Returns ( Task . FromResult ( false ) ) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
private void ConfigureUserServiceToAcceptPasswordFor ( User user )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_userService . CheckPasswordAsync ( user , Arg . Any < string > ( ) )
. Returns ( Task . FromResult ( true ) ) ;
2021-11-09 16:37:32 +01:00
_userService . VerifySecretAsync ( user , Arg . Any < string > ( ) )
2020-05-01 06:47:38 +02:00
. Returns ( Task . FromResult ( true ) ) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
private void ConfigureUserServiceToReturnValidIdFor ( User user )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_userService . GetUserByIdAsync ( Arg . Any < Guid > ( ) )
. Returns ( Task . FromResult ( user ) ) ;
2022-08-29 22:06:55 +02:00
}
2020-05-01 06:47:38 +02:00
private void ConfigureUserServiceToReturnNullUserId ( )
2022-08-29 22:06:55 +02:00
{
2020-05-01 06:47:38 +02:00
_userService . GetUserByIdAsync ( Arg . Any < Guid > ( ) )
. Returns ( Task . FromResult ( ( User ) null ) ) ;
}
}