2023-11-22 22:07:37 +01:00
|
|
|
|
using Bit.Core.AdminConsole.Enums;
|
|
|
|
|
using Bit.Core.AdminConsole.Services;
|
|
|
|
|
using Bit.Core.Auth.Models.Business.Tokenables;
|
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.Entities;
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
|
|
|
|
using Bit.Core.OrganizationFeatures.OrganizationUsers;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Core.Settings;
|
|
|
|
|
using Bit.Core.Test.AutoFixture.OrganizationFixtures;
|
|
|
|
|
using Bit.Core.Tokens;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Bit.Test.Common.Fakes;
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.OrganizationFeatures.OrganizationUsers;
|
|
|
|
|
|
|
|
|
|
// Note: test names follow MethodName_StateUnderTest_ExpectedBehavior pattern.
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class AcceptOrgUserCommandTests
|
|
|
|
|
{
|
|
|
|
|
private readonly IUserService _userService = Substitute.For<IUserService>();
|
|
|
|
|
private readonly IOrgUserInviteTokenableFactory _orgUserInviteTokenableFactory = Substitute.For<IOrgUserInviteTokenableFactory>();
|
|
|
|
|
private readonly IDataProtectorTokenFactory<OrgUserInviteTokenable> _orgUserInviteTokenDataFactory = new FakeDataProtectorTokenFactory<OrgUserInviteTokenable>();
|
|
|
|
|
|
|
|
|
|
// Base AcceptOrgUserAsync method tests ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUser_InvitedUserToSingleOrg_AcceptsOrgUser(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var resultOrgUser = await sutProvider.Sut.AcceptOrgUserAsync(orgUser, user, _userService);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
// Verify returned org user details
|
|
|
|
|
AssertValidAcceptedOrgUser(resultOrgUser, orgUser, user);
|
|
|
|
|
|
|
|
|
|
// Verify org repository called with updated orgUser
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().Received(1).ReplaceAsync(
|
|
|
|
|
Arg.Is<OrganizationUser>(ou => ou.Id == orgUser.Id && ou.Status == OrganizationUserStatusType.Accepted));
|
|
|
|
|
|
|
|
|
|
// Verify emails sent to admin
|
|
|
|
|
await sutProvider.GetDependency<IMailService>().Received(1).SendOrganizationAcceptedEmailAsync(
|
|
|
|
|
Arg.Is<Organization>(o => o.Id == org.Id),
|
|
|
|
|
Arg.Is<string>(e => e == user.Email),
|
|
|
|
|
Arg.Is<IEnumerable<string>>(a => a.Contains(adminUserDetails.Email))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUser_OrgUserStatusIsRevoked_ReturnsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Common setup
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
// Revoke user status
|
|
|
|
|
orgUser.Status = OrganizationUserStatusType.Revoked;
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
|
|
|
|
|
sutProvider.Sut.AcceptOrgUserAsync(orgUser, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Your organization access has been revoked.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserStatusType.Accepted)]
|
|
|
|
|
[BitAutoData(OrganizationUserStatusType.Confirmed)]
|
|
|
|
|
public async Task AcceptOrgUser_OrgUserStatusIsNotInvited_ThrowsBadRequest(
|
|
|
|
|
OrganizationUserStatusType orgUserStatus,
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
// Set status to something other than invited
|
|
|
|
|
orgUser.Status = orgUserStatus;
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
|
|
|
|
|
sutProvider.Sut.AcceptOrgUserAsync(orgUser, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Already accepted.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUser_UserJoiningOrgWithSingleOrgPolicyWhileInAnotherOrg_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
// Make user part of another org
|
|
|
|
|
var otherOrgUser = new OrganizationUser { UserId = user.Id, OrganizationId = Guid.NewGuid() }; // random org ID
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetManyByUserAsync(user.Id)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<OrganizationUser>>(new List<OrganizationUser> { otherOrgUser }));
|
|
|
|
|
|
|
|
|
|
// Make organization they are trying to join have the single org policy
|
|
|
|
|
var singleOrgPolicy = new OrganizationUserPolicyDetails { OrganizationId = orgUser.OrganizationId };
|
|
|
|
|
sutProvider.GetDependency<IPolicyService>()
|
|
|
|
|
.GetPoliciesApplicableToUserAsync(user.Id, PolicyType.SingleOrg, OrganizationUserStatusType.Invited)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<OrganizationUserPolicyDetails>>(
|
|
|
|
|
new List<OrganizationUserPolicyDetails> { singleOrgPolicy }));
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
|
|
|
|
|
sutProvider.Sut.AcceptOrgUserAsync(orgUser, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("You may not join this organization until you leave or remove all other organizations.",
|
|
|
|
|
exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserAsync_UserInOrgWithSingleOrgPolicyAlready_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
// Mock that user is part of an org that has the single org policy
|
|
|
|
|
sutProvider.GetDependency<IPolicyService>()
|
|
|
|
|
.AnyPoliciesApplicableToUserAsync(user.Id, PolicyType.SingleOrg)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
|
|
|
|
|
sutProvider.Sut.AcceptOrgUserAsync(orgUser, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(
|
|
|
|
|
"You cannot join this organization because you are a member of another organization which forbids it",
|
|
|
|
|
exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserAsync_UserWithout2FAJoining2FARequiredOrg_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
// User doesn't have 2FA enabled
|
|
|
|
|
_userService.TwoFactorIsEnabledAsync(user).Returns(false);
|
|
|
|
|
|
|
|
|
|
// Organization they are trying to join requires 2FA
|
|
|
|
|
var twoFactorPolicy = new OrganizationUserPolicyDetails { OrganizationId = orgUser.OrganizationId };
|
|
|
|
|
sutProvider.GetDependency<IPolicyService>()
|
|
|
|
|
.GetPoliciesApplicableToUserAsync(user.Id, PolicyType.TwoFactorAuthentication,
|
|
|
|
|
OrganizationUserStatusType.Invited)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<OrganizationUserPolicyDetails>>(
|
|
|
|
|
new List<OrganizationUserPolicyDetails> { twoFactorPolicy }));
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
|
|
|
|
|
sutProvider.Sut.AcceptOrgUserAsync(orgUser, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("You cannot join this organization until you enable two-step login on your user account.",
|
|
|
|
|
exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AcceptOrgUserByOrgIdAsync tests --------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[EphemeralDataProtectionAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByToken_OldToken_AcceptsUserAndVerifiesEmail(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
SetupCommonAcceptOrgUserByTokenMocks(sutProvider, user, orgUser);
|
|
|
|
|
|
|
|
|
|
var oldToken = CreateOldToken(sutProvider, orgUser);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var resultOrgUser = await sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUser.Id, user, oldToken, _userService);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
AssertValidAcceptedOrgUser(resultOrgUser, orgUser, user);
|
|
|
|
|
|
|
|
|
|
// Verify user email verified logic
|
|
|
|
|
Assert.True(user.EmailVerified);
|
|
|
|
|
await sutProvider.GetDependency<IUserRepository>().Received(1).ReplaceAsync(
|
|
|
|
|
Arg.Is<User>(u => u.Id == user.Id && u.Email == user.Email && user.EmailVerified == true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByToken_NewToken_AcceptsUserAndVerifiesEmail(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
// Setup FakeDataProtectorTokenFactory for creating new tokens - this must come first in order
|
|
|
|
|
// to avoid resetting mocks
|
|
|
|
|
sutProvider.SetDependency(_orgUserInviteTokenDataFactory, "orgUserInviteTokenDataFactory");
|
|
|
|
|
sutProvider.Create();
|
|
|
|
|
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
SetupCommonAcceptOrgUserByTokenMocks(sutProvider, user, orgUser);
|
|
|
|
|
|
|
|
|
|
// Must come after common mocks as they mutate the org user.
|
|
|
|
|
// Mock tokenable factory to return a token that expires in 5 days
|
|
|
|
|
_orgUserInviteTokenableFactory.CreateToken(orgUser).Returns(new OrgUserInviteTokenable(orgUser)
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = DateTime.UtcNow.Add(TimeSpan.FromDays(5))
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var newToken = CreateNewToken(orgUser);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var resultOrgUser = await sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUser.Id, user, newToken, _userService);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
AssertValidAcceptedOrgUser(resultOrgUser, orgUser, user);
|
|
|
|
|
|
|
|
|
|
// Verify user email verified logic
|
|
|
|
|
Assert.True(user.EmailVerified);
|
|
|
|
|
await sutProvider.GetDependency<IUserRepository>().Received(1).ReplaceAsync(
|
|
|
|
|
Arg.Is<User>(u => u.Id == user.Id && u.Email == user.Email && user.EmailVerified == true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByToken_NullOrgUser_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Guid orgUserId)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByIdAsync(orgUserId).Returns((OrganizationUser)null);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUserId, user, "token", _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("User invalid.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByToken_GenericInvalidToken_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, OrganizationUser orgUser)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByIdAsync(orgUser.Id)
|
|
|
|
|
.Returns(Task.FromResult(orgUser));
|
|
|
|
|
|
|
|
|
|
var invalidToken = "invalidToken";
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUser.Id, user, invalidToken, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Invalid token.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[EphemeralDataProtectionAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByToken_ExpiredOldToken_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
SetupCommonAcceptOrgUserByTokenMocks(sutProvider, user, orgUser);
|
|
|
|
|
|
|
|
|
|
// As the old token simply set a timestamp which was later compared against the
|
|
|
|
|
// OrganizationInviteExpirationHours global setting to determine if it was expired or not,
|
|
|
|
|
// we can simply set the expiration to 24 hours ago to simulate an expired token.
|
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>().OrganizationInviteExpirationHours.Returns(-24);
|
|
|
|
|
|
|
|
|
|
var oldToken = CreateOldToken(sutProvider, orgUser);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUser.Id, user, oldToken, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Invalid token.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByToken_ExpiredNewToken_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, OrganizationUser orgUser)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
// Setup FakeDataProtectorTokenFactory for creating new tokens - this must come first in order
|
|
|
|
|
// to avoid resetting mocks
|
|
|
|
|
sutProvider.SetDependency(_orgUserInviteTokenDataFactory, "orgUserInviteTokenDataFactory");
|
|
|
|
|
sutProvider.Create();
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByIdAsync(orgUser.Id)
|
|
|
|
|
.Returns(Task.FromResult(orgUser));
|
|
|
|
|
|
|
|
|
|
// Must come after common mocks as they mutate the org user.
|
|
|
|
|
// Mock tokenable factory to return a token that expired yesterday
|
|
|
|
|
_orgUserInviteTokenableFactory.CreateToken(orgUser).Returns(new OrgUserInviteTokenable(orgUser)
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = DateTime.UtcNow.Add(TimeSpan.FromDays(-1))
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var newToken = CreateNewToken(orgUser);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUser.Id, user, newToken, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Invalid token.", exception.Message);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserStatusType.Accepted,
|
|
|
|
|
"Invitation already accepted. You will receive an email when your organization membership is confirmed.")]
|
|
|
|
|
[BitAutoData(OrganizationUserStatusType.Confirmed,
|
|
|
|
|
"You are already part of this organization.")]
|
|
|
|
|
public async Task AcceptOrgUserByToken_UserAlreadyInOrg_ThrowsBadRequest(
|
|
|
|
|
OrganizationUserStatusType statusType,
|
|
|
|
|
string expectedErrorMessage,
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, OrganizationUser orgUser)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
// Setup FakeDataProtectorTokenFactory for creating new tokens - this must come first in order
|
|
|
|
|
// to avoid resetting mocks
|
|
|
|
|
sutProvider.SetDependency(_orgUserInviteTokenDataFactory, "orgUserInviteTokenDataFactory");
|
|
|
|
|
sutProvider.Create();
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByIdAsync(orgUser.Id)
|
|
|
|
|
.Returns(Task.FromResult(orgUser));
|
|
|
|
|
|
|
|
|
|
// Indicate that a user with the given email already exists in the organization
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetCountByOrganizationAsync(orgUser.OrganizationId, user.Email, true)
|
|
|
|
|
.Returns(1);
|
|
|
|
|
|
|
|
|
|
orgUser.Status = statusType;
|
|
|
|
|
|
|
|
|
|
// Must come after common mocks as they mutate the org user.
|
|
|
|
|
// Mock tokenable factory to return valid, new token that expires in 5 days
|
|
|
|
|
_orgUserInviteTokenableFactory.CreateToken(orgUser).Returns(new OrgUserInviteTokenable(orgUser)
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = DateTime.UtcNow.Add(TimeSpan.FromDays(5))
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var newToken = CreateNewToken(orgUser);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUser.Id, user, newToken, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(expectedErrorMessage, exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByToken_EmailMismatch_ThrowsBadRequest(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, OrganizationUser orgUser)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
// Setup FakeDataProtectorTokenFactory for creating new tokens - this must come first in order
|
|
|
|
|
// to avoid resetting mocks
|
|
|
|
|
sutProvider.SetDependency(_orgUserInviteTokenDataFactory, "orgUserInviteTokenDataFactory");
|
|
|
|
|
sutProvider.Create();
|
|
|
|
|
|
|
|
|
|
SetupCommonAcceptOrgUserByTokenMocks(sutProvider, user, orgUser);
|
|
|
|
|
|
|
|
|
|
// Modify the orgUser's email to be different from the user's email to simulate the mismatch
|
|
|
|
|
orgUser.Email = "mismatchedEmail@example.com";
|
|
|
|
|
|
|
|
|
|
// Must come after common mocks as they mutate the org user.
|
|
|
|
|
// Mock tokenable factory to return a token that expires in 5 days
|
|
|
|
|
_orgUserInviteTokenableFactory.CreateToken(orgUser).Returns(new OrgUserInviteTokenable(orgUser)
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = DateTime.UtcNow.Add(TimeSpan.FromDays(5))
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var newToken = CreateNewToken(orgUser);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByEmailTokenAsync(orgUser.Id, user, newToken, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("User email does not match invite.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AcceptOrgUserByOrgSsoIdAsync -----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByOrgSsoIdAsync_ValidData_AcceptsOrgUser(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
|
|
|
.GetByIdentifierAsync(org.Identifier)
|
|
|
|
|
.Returns(org);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByOrganizationAsync(org.Id, user.Id)
|
|
|
|
|
.Returns(orgUser);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var resultOrgUser = await sutProvider.Sut.AcceptOrgUserByOrgSsoIdAsync(org.Identifier, user, _userService);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
AssertValidAcceptedOrgUser(resultOrgUser, orgUser, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByOrgSsoIdAsync_InvalidOrg_ThrowsBadRequest(SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
string orgSsoIdentifier, User user)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
|
|
|
.GetByIdentifierAsync(orgSsoIdentifier)
|
|
|
|
|
.Returns((Organization)null);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByOrgSsoIdAsync(orgSsoIdentifier, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Organization invalid.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByOrgSsoIdAsync_UserNotInOrg_ThrowsBadRequest(SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
Organization org, User user)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
|
|
|
.GetByIdentifierAsync(org.Identifier)
|
|
|
|
|
.Returns(org);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByOrganizationAsync(org.Id, user.Id)
|
|
|
|
|
.Returns((OrganizationUser)null);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByOrgSsoIdAsync(org.Identifier, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("User not found within organization.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AcceptOrgUserByOrgIdAsync ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByOrgId_ValidData_AcceptsOrgUser(
|
|
|
|
|
SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
User user, Organization org, OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
SetupCommonAcceptOrgUserMocks(sutProvider, user, org, orgUser, adminUserDetails);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
|
|
|
.GetByIdAsync(org.Id)
|
|
|
|
|
.Returns(org);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByOrganizationAsync(org.Id, user.Id)
|
|
|
|
|
.Returns(orgUser);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var resultOrgUser = await sutProvider.Sut.AcceptOrgUserByOrgIdAsync(org.Id, user, _userService);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
AssertValidAcceptedOrgUser(resultOrgUser, orgUser, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByOrgId_InvalidOrg_ThrowsBadRequest(SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
Guid orgId, User user)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
|
|
|
.GetByIdAsync(orgId)
|
|
|
|
|
.Returns((Organization)null);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByOrgIdAsync(orgId, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Organization invalid.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public async Task AcceptOrgUserByOrgId_UserNotInOrg_ThrowsBadRequest(SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
Organization org, User user)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
|
|
|
.GetByIdAsync(org.Id)
|
|
|
|
|
.Returns(org);
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByOrganizationAsync(org.Id, user.Id)
|
|
|
|
|
.Returns((OrganizationUser)null);
|
|
|
|
|
|
|
|
|
|
// Act & Assert
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptOrgUserByOrgIdAsync(org.Id, user, _userService));
|
|
|
|
|
|
|
|
|
|
Assert.Equal("User not found within organization.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private helpers -------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Asserts that the given org user is in the expected state after a successful AcceptOrgUserAsync call.
|
|
|
|
|
/// For use in happy path tests.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void AssertValidAcceptedOrgUser(OrganizationUser resultOrgUser, OrganizationUser expectedOrgUser, User user)
|
|
|
|
|
{
|
|
|
|
|
Assert.NotNull(resultOrgUser);
|
|
|
|
|
Assert.Equal(OrganizationUserStatusType.Accepted, resultOrgUser.Status);
|
|
|
|
|
Assert.Equal(expectedOrgUser, resultOrgUser);
|
|
|
|
|
Assert.Equal(expectedOrgUser.Id, resultOrgUser.Id);
|
|
|
|
|
Assert.Null(resultOrgUser.Email);
|
|
|
|
|
Assert.Equal(user.Id, resultOrgUser.UserId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetupCommonAcceptOrgUserByTokenMocks(SutProvider<AcceptOrgUserCommand> sutProvider, User user, OrganizationUser orgUser)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>().OrganizationInviteExpirationHours.Returns(24);
|
|
|
|
|
user.EmailVerified = false;
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetByIdAsync(orgUser.Id)
|
|
|
|
|
.Returns(Task.FromResult(orgUser));
|
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetCountByOrganizationAsync(orgUser.OrganizationId, user.Email, true)
|
|
|
|
|
.Returns(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets up common mock behavior for the AcceptOrgUserAsync tests.
|
|
|
|
|
/// This method initializes:
|
|
|
|
|
/// - The invited user's email, status, type, and organization ID.
|
|
|
|
|
/// - Ensures the user is not part of any other organizations.
|
|
|
|
|
/// - Confirms the target organization doesn't have a single org policy.
|
|
|
|
|
/// - Ensures the user doesn't belong to an organization with a single org policy.
|
|
|
|
|
/// - Assumes the user doesn't have 2FA enabled and the organization doesn't require it.
|
|
|
|
|
/// - Provides mock data for an admin to validate email functionality.
|
|
|
|
|
/// - Returns the corresponding organization for the given org ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void SetupCommonAcceptOrgUserMocks(SutProvider<AcceptOrgUserCommand> sutProvider, User user,
|
|
|
|
|
Organization org,
|
|
|
|
|
OrganizationUser orgUser, OrganizationUserUserDetails adminUserDetails)
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
orgUser.Email = user.Email;
|
|
|
|
|
orgUser.Status = OrganizationUserStatusType.Invited;
|
|
|
|
|
orgUser.Type = OrganizationUserType.User;
|
|
|
|
|
orgUser.OrganizationId = org.Id;
|
|
|
|
|
|
|
|
|
|
// User is not part of any other orgs
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetManyByUserAsync(user.Id)
|
|
|
|
|
.Returns(
|
|
|
|
|
Task.FromResult<ICollection<OrganizationUser>>(new List<OrganizationUser>())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Org they are trying to join does not have single org policy
|
|
|
|
|
sutProvider.GetDependency<IPolicyService>()
|
|
|
|
|
.GetPoliciesApplicableToUserAsync(user.Id, PolicyType.SingleOrg, OrganizationUserStatusType.Invited)
|
|
|
|
|
.Returns(
|
|
|
|
|
Task.FromResult<ICollection<OrganizationUserPolicyDetails>>(
|
|
|
|
|
new List<OrganizationUserPolicyDetails>()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// User is not part of any organization that applies the single org policy
|
|
|
|
|
sutProvider.GetDependency<IPolicyService>()
|
|
|
|
|
.AnyPoliciesApplicableToUserAsync(user.Id, PolicyType.SingleOrg)
|
|
|
|
|
.Returns(false);
|
|
|
|
|
|
|
|
|
|
// User doesn't have 2FA enabled
|
|
|
|
|
_userService.TwoFactorIsEnabledAsync(user).Returns(false);
|
|
|
|
|
|
|
|
|
|
// Org does not require 2FA
|
|
|
|
|
sutProvider.GetDependency<IPolicyService>().GetPoliciesApplicableToUserAsync(user.Id,
|
|
|
|
|
PolicyType.TwoFactorAuthentication, OrganizationUserStatusType.Invited)
|
|
|
|
|
.Returns(Task.FromResult<ICollection<OrganizationUserPolicyDetails>>(
|
|
|
|
|
new List<OrganizationUserPolicyDetails>()));
|
|
|
|
|
|
|
|
|
|
// Provide at least 1 admin to test email functionality
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>()
|
|
|
|
|
.GetManyByMinimumRoleAsync(orgUser.OrganizationId, OrganizationUserType.Admin)
|
|
|
|
|
.Returns(Task.FromResult<IEnumerable<OrganizationUserUserDetails>>(
|
|
|
|
|
new List<OrganizationUserUserDetails>() { adminUserDetails }
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
// Return org
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>()
|
|
|
|
|
.GetByIdAsync(org.Id)
|
|
|
|
|
.Returns(Task.FromResult(org));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private string CreateOldToken(SutProvider<AcceptOrgUserCommand> sutProvider,
|
|
|
|
|
OrganizationUser organizationUser)
|
|
|
|
|
{
|
|
|
|
|
var dataProtector = sutProvider.GetDependency<IDataProtectionProvider>()
|
|
|
|
|
.CreateProtector("OrganizationServiceDataProtector");
|
|
|
|
|
|
|
|
|
|
// Token matching the format used in OrganizationService.InviteUserAsync
|
|
|
|
|
var oldToken = dataProtector.Protect(
|
|
|
|
|
$"OrganizationUserInvite {organizationUser.Id} {organizationUser.Email} {CoreHelpers.ToEpocMilliseconds(DateTime.UtcNow)}");
|
|
|
|
|
|
|
|
|
|
return oldToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string CreateNewToken(OrganizationUser orgUser)
|
|
|
|
|
{
|
|
|
|
|
var orgUserInviteTokenable = _orgUserInviteTokenableFactory.CreateToken(orgUser);
|
|
|
|
|
var protectedToken = _orgUserInviteTokenDataFactory.Protect(orgUserInviteTokenable);
|
|
|
|
|
|
|
|
|
|
return protectedToken;
|
|
|
|
|
}
|
|
|
|
|
}
|