2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Text.Json;
|
2021-07-01 14:31:05 +02:00
|
|
|
|
using Bit.Core.Context;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2021-01-12 17:02:39 +01:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
using Bit.Core.Models.Business;
|
2019-02-19 22:27:05 +01:00
|
|
|
|
using Bit.Core.Models.Data;
|
2022-05-10 23:12:09 +02:00
|
|
|
|
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
2019-02-19 22:27:05 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
2021-01-12 17:02:39 +01:00
|
|
|
|
using Bit.Core.Settings;
|
|
|
|
|
using Bit.Core.Test.AutoFixture.OrganizationFixtures;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
using Bit.Core.Test.AutoFixture.OrganizationUserFixtures;
|
Postgres & MySql Support For Self-Hosted Installations (#1386)
* EF Database Support Init (#1221)
* scaffolding for ef support
* deleted old postgres repos
* added tables to oncreate
* updated all the things to .NET 5
* Addition to #1221: Migrated DockerFiles from dotnet/3.1 to 5.0 (#1223)
* Migrated DockerFiles from dotnet/3.1 to 5.0
* Migrated SSO/Dockerfile from dotnet 3.1 to 5.0
Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
* EFDatabaseSupport: Updated links and description in README.md and SETUP.md (#1232)
* Updated requirements in README.md
* Updated link to documentation of app-secrets
* upgraded dotnet version to 5.0
* Ef database support implementation examples (#1265)
* mostly finished testing the user repo
* finished testing user repo
* finished org, user, ssoconfig, and ssouser ef implementations
* removed unused prop
* fixed a sql file
* fixed a spacing issue
* fixed a spacing issue
* removed extra database creation
* refactoring
* MsSql => SqlServer
* refactoring
* code review fixes
* build fix
* code review
* continued attempts to fix the the build
* skipped another test
* finished all create test
* initial pass at several repos
* continued building out repos
* initial pass at several repos
* initial pass at device repo
* initial pass at collection repo
* initial run of all Entity Framework implementations
* signup, signin, create/edit ciphers works
* sync working
* all web vault pages seem to load with 100% 200s
* bulkcopy, folders, and favorites
* group and collection management
* sso, groups, emergency access, send
* get basic creates matching on all repos
* got everything building again post merge
* removed some IDE config files
* cleanup
* no more notimplemented methods in the cipher repo
* no more not implementeds everywhere
* cleaned up schema/navigation properties and fixed tests
* removed a sql comment that was written in c# style
* fixed build issues from merge
* removed unsupported db providers
* formatting
* code review refactors
* naming cleanup for queries
* added provider methods
* cipher repo cleanup
* implemented several missing procedures from the EF implementation surround account revision dates, keys, and storage
* fixed the build
* added a null check
* consolidated some cipher repo methods
* formatting fix
* cleaned up indentation of queries
* removed .idea file
* generated postgres migrations
* added mysql migrations
* formatting
* Bug Fixes & Formatting
* Formatting
* fixed a bug with bulk import when using MySql
* code review fixes
* fixed the build
* implemented new methods
* formatting
* fixed the build
* cleaned up select statements in ef queries
* formatting
* formatting
* formatting
Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
2021-07-08 18:35:48 +02:00
|
|
|
|
using Bit.Core.Test.AutoFixture.PolicyFixtures;
|
Families for Enterprise (#1714)
* Create common test infrastructure project
* Add helpers to further type PlanTypes
* Enable testing of ASP.net MVC controllers
Controller properties have all kinds of validations in the background.
In general, we don't user properties on our Controllers, so the easiest
way to allow for Autofixture-based testing of our Controllers is to just
omit setting all properties on them.
* Workaround for broken MemberAutoDataAttribute
https://github.com/AutoFixture/AutoFixture/pull/1164 shows that only
the first test case is pulled for this attribute.
This is a workaround that populates the provided parameters, left to
right, using AutoFixture to populate any remaining.
* WIP: Organization sponsorship flow
* Add Attribute to use the Bit Autodata dependency chain
BitAutoDataAttribute is used to mark a Theory as autopopulating
parameters.
Extract common attribute methods to to a helper class. Cannot
inherit a common base, since both require inheriting from different
Xunit base classes to work.
* WIP: scaffolding for families for enterprise sponsorship flow
* Fix broken tests
* Create sponsorship offer (#1688)
* Initial db work (#1687)
* Add organization sponsorship databases to all providers
* Generalize create and update for database, specialize in code
* Add PlanSponsorshipType to db model
* Write valid json for test entries
* Initial scaffolding of emails (#1686)
* Initial scaffolding of emails
* Work on adding models for FamilyForEnterprise emails
* Switch verbage
* Put preliminary copy in emails
* Skip test
* Families for enterprise/stripe integrations (#1699)
* Add PlanSponsorshipType to static store
* Add sponsorship type to token and creates sponsorship
* PascalCase properties
* Require sponsorship for remove
* Create subscription sponsorship helper class
* Handle Sponsored subscription changes
* Add sponsorship id to subscription metadata
* Make sponsoring references nullable
This state indicates that a sponsorship has lapsed, but was not able to
be reverted for billing reasons
* WIP: Validate and remove subscriptions
* Update sponsorships on organization and org user delete
* Add friendly name to organization sponsorship
* Add sponsorship available boolean to orgDetails
* Add sponsorship service to DI
* Use userId to find org users
* Send f4e offer email
* Simplify names of f4e mail messages
* Fix Stripe org default tax rates
* Universal sponsorship redeem api
* Populate user in current context
* Add product type to organization details
* Use upgrade path to change sponsorship
Sponsorships need to be annual to match the GB add-on charge rate
* Use organization and auth to find organization sponsorship
* Add resend sponsorship offer api endpoint
* Fix double email send
* Fix sponsorship upgrade options
* Add is sponsored item to subscription response
* Add sponsorship validation to upcoming invoice webhook
* Add sponsorship validation to upcoming invoice webhook
* Fix organization delete sponsorship hooks
* Test org sponsorship service
* Fix sproc
* Create common test infrastructure project
* Add helpers to further type PlanTypes
* Enable testing of ASP.net MVC controllers
Controller properties have all kinds of validations in the background.
In general, we don't user properties on our Controllers, so the easiest
way to allow for Autofixture-based testing of our Controllers is to just
omit setting all properties on them.
* Workaround for broken MemberAutoDataAttribute
https://github.com/AutoFixture/AutoFixture/pull/1164 shows that only
the first test case is pulled for this attribute.
This is a workaround that populates the provided parameters, left to
right, using AutoFixture to populate any remaining.
* WIP: Organization sponsorship flow
* Add Attribute to use the Bit Autodata dependency chain
BitAutoDataAttribute is used to mark a Theory as autopopulating
parameters.
Extract common attribute methods to to a helper class. Cannot
inherit a common base, since both require inheriting from different
Xunit base classes to work.
* WIP: scaffolding for families for enterprise sponsorship flow
* Fix broken tests
* Create sponsorship offer (#1688)
* Initial db work (#1687)
* Add organization sponsorship databases to all providers
* Generalize create and update for database, specialize in code
* Add PlanSponsorshipType to db model
* Write valid json for test entries
* Initial scaffolding of emails (#1686)
* Initial scaffolding of emails
* Work on adding models for FamilyForEnterprise emails
* Switch verbage
* Put preliminary copy in emails
* Skip test
* Families for enterprise/stripe integrations (#1699)
* Add PlanSponsorshipType to static store
* Add sponsorship type to token and creates sponsorship
* PascalCase properties
* Require sponsorship for remove
* Create subscription sponsorship helper class
* Handle Sponsored subscription changes
* Add sponsorship id to subscription metadata
* Make sponsoring references nullable
This state indicates that a sponsorship has lapsed, but was not able to
be reverted for billing reasons
* WIP: Validate and remove subscriptions
* Update sponsorships on organization and org user delete
* Add friendly name to organization sponsorship
* Add sponsorship available boolean to orgDetails
* Add sponsorship service to DI
* Use userId to find org users
* Send f4e offer email
* Simplify names of f4e mail messages
* Fix Stripe org default tax rates
* Universal sponsorship redeem api
* Populate user in current context
* Add product type to organization details
* Use upgrade path to change sponsorship
Sponsorships need to be annual to match the GB add-on charge rate
* Use organization and auth to find organization sponsorship
* Add resend sponsorship offer api endpoint
* Fix double email send
* Fix sponsorship upgrade options
* Add is sponsored item to subscription response
* Add sponsorship validation to upcoming invoice webhook
* Add sponsorship validation to upcoming invoice webhook
* Fix organization delete sponsorship hooks
* Test org sponsorship service
* Fix sproc
* Fix build error
* Update emails
* Fix tests
* Skip local test
* Add newline
* Fix stripe subscription update
* Finish emails
* Skip test
* Fix unit tests
* Remove unused variable
* Fix unit tests
* Switch to handlebars ifs
* Remove ending email
* Remove reconfirmation template
* Switch naming convention
* Switch naming convention
* Fix migration
* Update copy and links
* Switch to using Guid in the method
* Remove unneeded css styles
* Add sql files to Sql.sqlproj
* Removed old comments
* Made name more verbose
* Fix SQL error
* Move unit tests to service
* Fix sp
* Revert "Move unit tests to service"
This reverts commit 1185bf3ec8ca36ccd75717ed2463adf8885159a6.
* Do repository validation in service layer
* Fix tests
* Fix merge conflicts and remove TODO
* Remove unneeded models
* Fix spacing and formatting
* Switch Org -> Organization
* Remove single use variables
* Switch method name
* Fix Controller
* Switch to obfuscating email
* Fix unit tests
Co-authored-by: Justin Baur <admin@justinbaur.com>
2021-11-19 23:25:06 +01:00
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
2019-02-19 22:27:05 +01:00
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Organization = Bit.Core.Entities.Organization;
|
|
|
|
|
using OrganizationUser = Bit.Core.Entities.OrganizationUser;
|
|
|
|
|
using Policy = Bit.Core.Entities.Policy;
|
2019-02-19 22:27:05 +01:00
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Services;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[SutProviderCustomize]
|
2019-02-19 22:27:05 +01:00
|
|
|
|
public class OrganizationServiceTests
|
|
|
|
|
{
|
2021-05-17 16:43:02 +02:00
|
|
|
|
// [Fact]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, PaidOrganizationCustomize, BitAutoData]
|
2021-05-17 16:43:02 +02:00
|
|
|
|
public async Task OrgImportCreateNewUsers(SutProvider<OrganizationService> sutProvider, Guid userId,
|
|
|
|
|
Organization org, List<OrganizationUserUserDetails> existingUsers, List<ImportedOrganizationUser> newUsers)
|
2021-01-12 17:02:39 +01:00
|
|
|
|
{
|
|
|
|
|
org.UseDirectory = true;
|
|
|
|
|
org.Seats = 10;
|
|
|
|
|
newUsers.Add(new ImportedOrganizationUser
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-01-12 17:02:39 +01:00
|
|
|
|
Email = existingUsers.First().Email,
|
|
|
|
|
ExternalId = existingUsers.First().ExternalId
|
|
|
|
|
});
|
|
|
|
|
var expectedNewUsersCount = newUsers.Count - 1;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
existingUsers.First().Type = OrganizationUserType.Owner;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyDetailsByOrganizationAsync(org.Id)
|
|
|
|
|
.Returns(existingUsers);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetCountByOrganizationIdAsync(org.Id)
|
|
|
|
|
.Returns(existingUsers.Count);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyByOrganizationAsync(org.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(existingUsers.Select(u => new OrganizationUser { Status = OrganizationUserStatusType.Confirmed, Type = OrganizationUserType.Owner, Id = u.Id }).ToList());
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageUsers(org.Id).Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
await sutProvider.Sut.ImportAsync(org.Id, userId, null, newUsers, null, false);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.UpsertAsync(default);
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().Received(1)
|
|
|
|
|
.UpsertManyAsync(Arg.Is<IEnumerable<OrganizationUser>>(users => users.Count() == 0));
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
2021-09-23 12:36:08 +02:00
|
|
|
|
.CreateAsync(default);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
// Create new users
|
2022-07-13 15:21:28 +02:00
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().Received(1)
|
|
|
|
|
.CreateManyAsync(Arg.Is<IEnumerable<OrganizationUser>>(users => users.Count() == expectedNewUsersCount));
|
|
|
|
|
await sutProvider.GetDependency<IMailService>().Received(1)
|
|
|
|
|
.BulkSendOrganizationInviteEmailAsync(org.Name,
|
|
|
|
|
Arg.Is<IEnumerable<(OrganizationUser, ExpiringToken)>>(messages => messages.Count() == expectedNewUsersCount));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-07-13 15:21:28 +02:00
|
|
|
|
// Send events
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received(1)
|
|
|
|
|
.LogOrganizationUserEventsAsync(Arg.Is<IEnumerable<(OrganizationUser, EventType, DateTime?)>>(events =>
|
|
|
|
|
events.Count() == expectedNewUsersCount));
|
|
|
|
|
await sutProvider.GetDependency<IReferenceEventService>().Received(1)
|
|
|
|
|
.RaiseEventAsync(Arg.Is<ReferenceEvent>(referenceEvent =>
|
|
|
|
|
referenceEvent.Type == ReferenceEventType.InvitedUsers && referenceEvent.Id == org.Id &&
|
|
|
|
|
referenceEvent.Users == expectedNewUsersCount));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, PaidOrganizationCustomize, BitAutoData]
|
2021-07-08 17:05:32 +02:00
|
|
|
|
public async Task OrgImportCreateNewUsersAndMarryExistingUser(SutProvider<OrganizationService> sutProvider,
|
|
|
|
|
Guid userId, Organization org, List<OrganizationUserUserDetails> existingUsers,
|
2021-05-17 16:43:02 +02:00
|
|
|
|
List<ImportedOrganizationUser> newUsers)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-05-17 16:43:02 +02:00
|
|
|
|
org.UseDirectory = true;
|
2021-08-17 15:37:00 +02:00
|
|
|
|
org.Seats = newUsers.Count + existingUsers.Count + 1;
|
2021-05-17 16:43:02 +02:00
|
|
|
|
var reInvitedUser = existingUsers.First();
|
|
|
|
|
reInvitedUser.ExternalId = null;
|
|
|
|
|
newUsers.Add(new ImportedOrganizationUser
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-05-17 16:43:02 +02:00
|
|
|
|
Email = reInvitedUser.Email,
|
|
|
|
|
ExternalId = reInvitedUser.Email,
|
2022-08-29 21:53:48 +02:00
|
|
|
|
});
|
2021-05-17 16:43:02 +02:00
|
|
|
|
var expectedNewUsersCount = newUsers.Count - 1;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-07-08 17:05:32 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(org.Id).Returns(org);
|
2021-05-17 16:43:02 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyDetailsByOrganizationAsync(org.Id)
|
2021-07-08 17:05:32 +02:00
|
|
|
|
.Returns(existingUsers);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetCountByOrganizationIdAsync(org.Id)
|
|
|
|
|
.Returns(existingUsers.Count);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetByIdAsync(reInvitedUser.Id)
|
2021-05-17 16:43:02 +02:00
|
|
|
|
.Returns(new OrganizationUser { Id = reInvitedUser.Id });
|
2021-07-08 17:05:32 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationUserRepository>().GetManyByOrganizationAsync(org.Id, OrganizationUserType.Owner)
|
2021-09-23 12:36:08 +02:00
|
|
|
|
.Returns(existingUsers.Select(u => new OrganizationUser { Status = OrganizationUserStatusType.Confirmed, Type = OrganizationUserType.Owner, Id = u.Id }).ToList());
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
currentContext.ManageUsers(org.Id).Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
await sutProvider.Sut.ImportAsync(org.Id, userId, null, newUsers, null, false);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.UpsertAsync(default);
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
|
|
|
|
.CreateAsync(default);
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs()
|
2021-05-17 16:43:02 +02:00
|
|
|
|
.CreateAsync(default, default);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
// Upserted existing user
|
2021-05-17 16:43:02 +02:00
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().Received(1)
|
|
|
|
|
.UpsertManyAsync(Arg.Is<IEnumerable<OrganizationUser>>(users => users.Count() == 1));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
// Created and invited new users
|
2021-05-17 16:43:02 +02:00
|
|
|
|
await sutProvider.GetDependency<IOrganizationUserRepository>().Received(1)
|
2021-09-23 12:36:08 +02:00
|
|
|
|
.CreateManyAsync(Arg.Is<IEnumerable<OrganizationUser>>(users => users.Count() == expectedNewUsersCount));
|
2021-07-08 17:05:32 +02:00
|
|
|
|
await sutProvider.GetDependency<IMailService>().Received(1)
|
|
|
|
|
.BulkSendOrganizationInviteEmailAsync(org.Name,
|
|
|
|
|
Arg.Is<IEnumerable<(OrganizationUser, ExpiringToken)>>(messages => messages.Count() == expectedNewUsersCount));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-07-08 17:05:32 +02:00
|
|
|
|
// Sent events
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received(1)
|
|
|
|
|
.LogOrganizationUserEventsAsync(Arg.Is<IEnumerable<(OrganizationUser, EventType, DateTime?)>>(events =>
|
|
|
|
|
events.Where(e => e.Item2 == EventType.OrganizationUser_Invited).Count() == expectedNewUsersCount));
|
2021-05-17 16:43:02 +02:00
|
|
|
|
await sutProvider.GetDependency<IReferenceEventService>().Received(1)
|
2021-07-08 17:05:32 +02:00
|
|
|
|
.RaiseEventAsync(Arg.Is<ReferenceEvent>(referenceEvent =>
|
|
|
|
|
referenceEvent.Type == ReferenceEventType.InvitedUsers && referenceEvent.Id == org.Id &&
|
2021-05-17 16:43:02 +02:00
|
|
|
|
referenceEvent.Users == expectedNewUsersCount));
|
2021-07-08 17:05:32 +02:00
|
|
|
|
}
|
2021-01-12 17:02:39 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-12 04:36:23 +02:00
|
|
|
|
public async Task UpgradePlan_OrganizationIsNull_Throws(Guid organizationId, OrganizationUpgrade upgrade,
|
2021-01-12 17:02:39 +01:00
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organizationId).Returns(Task.FromResult<Organization>(null));
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<NotFoundException>(
|
|
|
|
|
() => sutProvider.Sut.UpgradePlanAsync(organizationId, upgrade));
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2021-01-12 17:02:39 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-01-12 17:02:39 +01:00
|
|
|
|
public async Task UpgradePlan_GatewayCustomIdIsNull_Throws(Organization organization, OrganizationUpgrade upgrade,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-09-23 12:36:08 +02:00
|
|
|
|
organization.GatewayCustomerId = string.Empty;
|
2021-01-12 17:02:39 +01:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.UpgradePlanAsync(organization.Id, upgrade));
|
|
|
|
|
Assert.Contains("no payment method", exception.Message);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2021-01-12 17:02:39 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-01-12 17:02:39 +01:00
|
|
|
|
public async Task UpgradePlan_AlreadyInPlan_Throws(Organization organization, OrganizationUpgrade upgrade,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-01-12 17:02:39 +01:00
|
|
|
|
upgrade.Plan = organization.PlanType;
|
2021-11-09 16:37:32 +01:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2021-01-12 17:02:39 +01:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
2021-09-23 12:36:08 +02:00
|
|
|
|
() => sutProvider.Sut.UpgradePlanAsync(organization.Id, upgrade));
|
2021-01-12 17:02:39 +01:00
|
|
|
|
Assert.Contains("already on this plan", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, PaidOrganizationCustomize(CheckedPlanType = PlanType.Free), BitAutoData]
|
2021-05-12 04:36:23 +02:00
|
|
|
|
public async Task UpgradePlan_UpgradeFromPaidPlan_Throws(Organization organization, OrganizationUpgrade upgrade,
|
2021-01-12 17:02:39 +01:00
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.UpgradePlanAsync(organization.Id, upgrade));
|
|
|
|
|
Assert.Contains("can only upgrade", exception.Message);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2021-01-12 17:02:39 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[FreeOrganizationUpgradeCustomize, BitAutoData]
|
2021-01-12 17:02:39 +01:00
|
|
|
|
public async Task UpgradePlan_Passes(Organization organization, OrganizationUpgrade upgrade,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-01-12 17:02:39 +01:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-01 14:31:05 +02:00
|
|
|
|
await sutProvider.Sut.UpgradePlanAsync(organization.Id, upgrade);
|
|
|
|
|
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).ReplaceAsync(organization);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2021-01-12 17:02:39 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[Theory]
|
2022-11-09 13:13:29 +01:00
|
|
|
|
[OrganizationInviteCustomize(InviteeUserType = OrganizationUserType.User,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Owner), BitAutoData]
|
2021-01-12 17:02:39 +01:00
|
|
|
|
public async Task InviteUser_NoEmails_Throws(Organization organization, OrganizationUser invitor,
|
|
|
|
|
OrganizationUserInvite invite, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-01-12 17:02:39 +01:00
|
|
|
|
invite.Emails = null;
|
2022-11-09 13:13:29 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageUsers(organization.Id).Returns(true);
|
2021-01-12 17:02:39 +01:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(
|
2021-09-23 12:36:08 +02:00
|
|
|
|
() => sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) }));
|
2021-01-12 17:02:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 04:36:23 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize, BitAutoData]
|
2021-05-12 04:36:23 +02:00
|
|
|
|
public async Task InviteUser_DuplicateEmails_PassesWithoutDuplicates(Organization organization, OrganizationUser invitor,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser owner,
|
2021-01-12 17:02:39 +01:00
|
|
|
|
OrganizationUserInvite invite, SutProvider<OrganizationService> sutProvider)
|
2021-05-12 04:36:23 +02:00
|
|
|
|
{
|
|
|
|
|
invite.Emails = invite.Emails.Append(invite.Emails.First());
|
2021-01-12 17:02:39 +01:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2021-11-09 16:37:32 +01:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageUsers(organization.Id).Returns(true);
|
2021-01-12 17:02:39 +01:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-07-01 14:31:05 +02:00
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organization.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { owner });
|
2021-01-12 17:02:39 +01:00
|
|
|
|
|
2021-05-12 04:36:23 +02:00
|
|
|
|
await sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) });
|
2021-01-13 21:14:28 +01:00
|
|
|
|
|
2021-12-14 17:29:31 +01:00
|
|
|
|
await sutProvider.GetDependency<IMailService>().Received(1)
|
|
|
|
|
.BulkSendOrganizationInviteEmailAsync(organization.Name,
|
2021-05-12 04:36:23 +02:00
|
|
|
|
Arg.Is<IEnumerable<(OrganizationUser, ExpiringToken)>>(v => v.Count() == invite.Emails.Distinct().Count()));
|
2021-01-12 17:02:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.Admin,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Owner
|
|
|
|
|
), BitAutoData]
|
2021-01-12 17:02:39 +01:00
|
|
|
|
public async Task InviteUser_NoOwner_Throws(Organization organization, OrganizationUser invitor,
|
|
|
|
|
OrganizationUserInvite invite, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageUsers(organization.Id).Returns(true);
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) }));
|
|
|
|
|
Assert.Contains("Organization must have at least one confirmed owner.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.Owner,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Admin
|
|
|
|
|
), BitAutoData]
|
2021-07-08 17:05:32 +02:00
|
|
|
|
public async Task InviteUser_NonOwnerConfiguringOwner_Throws(Organization organization, OrganizationUserInvite invite,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-01-12 17:02:39 +01:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-01 14:31:05 +02:00
|
|
|
|
currentContext.OrganizationAdmin(organization.Id).Returns(true);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) }));
|
|
|
|
|
Assert.Contains("only an owner", exception.Message.ToLowerInvariant());
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
2022-08-29 20:53:16 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.Custom,
|
|
|
|
|
InvitorUserType = OrganizationUserType.User
|
|
|
|
|
), BitAutoData]
|
2021-05-12 04:36:23 +02:00
|
|
|
|
public async Task InviteUser_NonAdminConfiguringAdmin_Throws(Organization organization, OrganizationUserInvite invite,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-12-06 10:50:08 +01:00
|
|
|
|
organization.UseCustomPermissions = true;
|
|
|
|
|
|
2021-01-13 21:14:28 +01:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
currentContext.OrganizationUser(organization.Id).Returns(true);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) }));
|
|
|
|
|
Assert.Contains("only owners and admins", exception.Message.ToLowerInvariant());
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2022-12-06 10:50:08 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.Custom,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Admin
|
|
|
|
|
), BitAutoData]
|
|
|
|
|
public async Task InviteUser_WithCustomType_WhenUseCustomPermissionsIsFalse_Throws(Organization organization, OrganizationUserInvite invite,
|
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
organization.UseCustomPermissions = false;
|
|
|
|
|
|
|
|
|
|
invite.Permissions = null;
|
|
|
|
|
invitor.Status = OrganizationUserStatusType.Confirmed;
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organization.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { invitor });
|
|
|
|
|
currentContext.OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) }));
|
|
|
|
|
Assert.Contains("to enable custom permissions", exception.Message.ToLowerInvariant());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.Custom,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Admin
|
|
|
|
|
), BitAutoData]
|
|
|
|
|
public async Task InviteUser_WithCustomType_WhenUseCustomPermissionsIsTrue_Passes(Organization organization, OrganizationUserInvite invite,
|
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
organization.UseCustomPermissions = true;
|
|
|
|
|
|
|
|
|
|
invite.Permissions = null;
|
|
|
|
|
invitor.Status = OrganizationUserStatusType.Confirmed;
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organization.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { invitor });
|
|
|
|
|
currentContext.OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Manager)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.User)]
|
|
|
|
|
public async Task InviteUser_WithNonCustomType_WhenUseCustomPermissionsIsFalse_Passes(OrganizationUserType inviteUserType, Organization organization, OrganizationUserInvite invite,
|
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
organization.UseCustomPermissions = false;
|
|
|
|
|
|
|
|
|
|
invite.Type = inviteUserType;
|
|
|
|
|
invite.Permissions = null;
|
|
|
|
|
invitor.Status = OrganizationUserStatusType.Confirmed;
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organization.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { invitor });
|
|
|
|
|
currentContext.OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) });
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.Manager,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Custom
|
|
|
|
|
), BitAutoData]
|
2021-08-10 18:16:10 +02:00
|
|
|
|
public async Task InviteUser_CustomUserWithoutManageUsersConfiguringUser_Throws(Organization organization, OrganizationUserInvite invite,
|
2022-07-13 15:21:28 +02:00
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2021-05-12 04:36:23 +02:00
|
|
|
|
invitor.Permissions = JsonSerializer.Serialize(new Permissions() { ManageUsers = false },
|
2021-01-12 17:02:39 +01:00
|
|
|
|
new JsonSerializerOptions
|
2021-09-23 12:36:08 +02:00
|
|
|
|
{
|
2021-01-12 17:02:39 +01:00
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
});
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-01 14:31:05 +02:00
|
|
|
|
currentContext.OrganizationCustom(organization.Id).Returns(true);
|
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(false);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) }));
|
2021-05-12 04:36:23 +02:00
|
|
|
|
Assert.Contains("account does not have permission", exception.Message.ToLowerInvariant());
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.Admin,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Custom
|
|
|
|
|
), BitAutoData]
|
2021-01-12 17:02:39 +01:00
|
|
|
|
public async Task InviteUser_CustomUserConfiguringAdmin_Throws(Organization organization, OrganizationUserInvite invite,
|
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
invitor.Permissions = JsonSerializer.Serialize(new Permissions() { ManageUsers = true },
|
|
|
|
|
new JsonSerializerOptions
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-01-12 17:02:39 +01:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-01 14:31:05 +02:00
|
|
|
|
currentContext.OrganizationCustom(organization.Id).Returns(true);
|
2021-01-12 17:02:39 +01:00
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(true);
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) }));
|
|
|
|
|
Assert.Contains("can not manage admins", exception.Message.ToLowerInvariant());
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.User,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Owner
|
|
|
|
|
), BitAutoData]
|
2021-08-10 18:16:10 +02:00
|
|
|
|
public async Task InviteUser_NoPermissionsObject_Passes(Organization organization, OrganizationUserInvite invite,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
OrganizationUser invitor, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-01-13 21:14:28 +01:00
|
|
|
|
invite.Permissions = null;
|
2021-07-08 17:05:32 +02:00
|
|
|
|
invitor.Status = OrganizationUserStatusType.Confirmed;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-08 17:05:32 +02:00
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organization.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { invitor });
|
2021-05-25 19:23:47 +02:00
|
|
|
|
currentContext.OrganizationOwner(organization.Id).Returns(true);
|
2021-05-17 10:10:44 +02:00
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
await sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, new (OrganizationUserInvite, string)[] { (invite, null) });
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.User,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Custom
|
|
|
|
|
), BitAutoData]
|
2021-05-17 10:10:44 +02:00
|
|
|
|
public async Task InviteUser_Passes(Organization organization, IEnumerable<(OrganizationUserInvite invite, string externalId)> invites,
|
2021-07-08 17:05:32 +02:00
|
|
|
|
OrganizationUser invitor,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser owner,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
invitor.Permissions = JsonSerializer.Serialize(new Permissions() { ManageUsers = true },
|
|
|
|
|
new JsonSerializerOptions
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
});
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organization.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { owner });
|
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(true);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
await sutProvider.Sut.InviteUsersAsync(organization.Id, invitor.UserId, invites);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
await sutProvider.GetDependency<IMailService>().Received(1)
|
|
|
|
|
.BulkSendOrganizationInviteEmailAsync(organization.Name,
|
|
|
|
|
Arg.Is<IEnumerable<(OrganizationUser, ExpiringToken)>>(v => v.Count() == invites.SelectMany(i => i.invite.Emails).Count()));
|
2022-11-09 13:13:29 +01:00
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received(1).LogOrganizationUserEventsAsync(Arg.Any<IEnumerable<(OrganizationUser, EventType, DateTime?)>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[OrganizationInviteCustomize(
|
|
|
|
|
InviteeUserType = OrganizationUserType.User,
|
|
|
|
|
InvitorUserType = OrganizationUserType.Custom
|
|
|
|
|
), BitAutoData]
|
|
|
|
|
public async Task InviteUser_WithEventSystemUser_Passes(Organization organization, EventSystemUser eventSystemUser, IEnumerable<(OrganizationUserInvite invite, string externalId)> invites,
|
|
|
|
|
OrganizationUser invitor,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser owner,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
invitor.Permissions = JsonSerializer.Serialize(new Permissions() { ManageUsers = true },
|
|
|
|
|
new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organization.Id, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { owner });
|
|
|
|
|
currentContext.ManageUsers(organization.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.InviteUsersAsync(organization.Id, eventSystemUser, invites);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IMailService>().Received(1)
|
|
|
|
|
.BulkSendOrganizationInviteEmailAsync(organization.Name,
|
|
|
|
|
Arg.Is<IEnumerable<(OrganizationUser, ExpiringToken)>>(v => v.Count() == invites.SelectMany(i => i.invite.Emails).Count()));
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received(1).LogOrganizationUserEventsAsync(Arg.Any<IEnumerable<(OrganizationUser, EventType, EventSystemUser, DateTime?)>>());
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public async Task SaveUser_NoUserId_Throws(OrganizationUser user, Guid? savingUserId,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
IEnumerable<SelectionReadOnly> collections, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-01-12 17:02:39 +01:00
|
|
|
|
user.Id = default(Guid);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.SaveUserAsync(user, savingUserId, collections));
|
|
|
|
|
Assert.Contains("invite the user first", exception.Message.ToLowerInvariant());
|
2021-05-17 10:10:44 +02:00
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public async Task SaveUser_NoChangeToData_Throws(OrganizationUser user, Guid? savingUserId,
|
|
|
|
|
IEnumerable<SelectionReadOnly> collections, SutProvider<OrganizationService> sutProvider)
|
2021-05-17 10:10:44 +02:00
|
|
|
|
{
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-01-12 17:02:39 +01:00
|
|
|
|
organizationUserRepository.GetByIdAsync(user.Id).Returns(user);
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
2021-05-17 10:10:44 +02:00
|
|
|
|
() => sutProvider.Sut.SaveUserAsync(user, savingUserId, collections));
|
2021-01-12 17:02:39 +01:00
|
|
|
|
Assert.Contains("make changes before saving", exception.Message.ToLowerInvariant());
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public async Task SaveUser_Passes(
|
2022-12-06 10:50:08 +01:00
|
|
|
|
Organization organization,
|
|
|
|
|
OrganizationUser oldUserData,
|
|
|
|
|
OrganizationUser newUserData,
|
|
|
|
|
IEnumerable<SelectionReadOnly> collections,
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser savingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
|
|
|
|
|
newUserData.Id = oldUserData.Id;
|
|
|
|
|
newUserData.UserId = oldUserData.UserId;
|
|
|
|
|
newUserData.OrganizationId = savingUser.OrganizationId = oldUserData.OrganizationId = organization.Id;
|
|
|
|
|
organizationUserRepository.GetByIdAsync(oldUserData.Id).Returns(oldUserData);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(savingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new List<OrganizationUser> { savingUser });
|
|
|
|
|
currentContext.OrganizationOwner(savingUser.OrganizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.SaveUserAsync(newUserData, savingUser.UserId, collections);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task SaveUser_WithCustomType_WhenUseCustomPermissionsIsFalse_Throws(
|
|
|
|
|
Organization organization,
|
|
|
|
|
OrganizationUser oldUserData,
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Custom)] OrganizationUser newUserData,
|
|
|
|
|
IEnumerable<SelectionReadOnly> collections,
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser savingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
organization.UseCustomPermissions = false;
|
|
|
|
|
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
|
|
|
|
|
newUserData.Id = oldUserData.Id;
|
|
|
|
|
newUserData.UserId = oldUserData.UserId;
|
|
|
|
|
newUserData.OrganizationId = savingUser.OrganizationId = oldUserData.OrganizationId = organization.Id;
|
|
|
|
|
organizationUserRepository.GetByIdAsync(oldUserData.Id).Returns(oldUserData);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(savingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new List<OrganizationUser> { savingUser });
|
|
|
|
|
currentContext.OrganizationOwner(savingUser.OrganizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.SaveUserAsync(newUserData, savingUser.UserId, collections));
|
|
|
|
|
Assert.Contains("to enable custom permissions", exception.Message.ToLowerInvariant());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Manager)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.User)]
|
|
|
|
|
public async Task SaveUser_WithNonCustomType_WhenUseCustomPermissionsIsFalse_Passes(
|
|
|
|
|
OrganizationUserType newUserType,
|
|
|
|
|
Organization organization,
|
2021-05-25 19:23:47 +02:00
|
|
|
|
OrganizationUser oldUserData,
|
|
|
|
|
OrganizationUser newUserData,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
IEnumerable<SelectionReadOnly> collections,
|
2021-05-25 19:23:47 +02:00
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser savingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-12-06 10:50:08 +01:00
|
|
|
|
organization.UseCustomPermissions = false;
|
|
|
|
|
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-12-06 10:50:08 +01:00
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
|
|
|
|
|
newUserData.Id = oldUserData.Id;
|
|
|
|
|
newUserData.UserId = oldUserData.UserId;
|
|
|
|
|
newUserData.OrganizationId = savingUser.OrganizationId = oldUserData.OrganizationId = organization.Id;
|
|
|
|
|
newUserData.Type = newUserType;
|
|
|
|
|
organizationUserRepository.GetByIdAsync(oldUserData.Id).Returns(oldUserData);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(savingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new List<OrganizationUser> { savingUser });
|
|
|
|
|
currentContext.OrganizationOwner(savingUser.OrganizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.SaveUserAsync(newUserData, savingUser.UserId, collections);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task SaveUser_WithCustomType_WhenUseCustomPermissionsIsTrue_Passes(
|
|
|
|
|
Organization organization,
|
|
|
|
|
OrganizationUser oldUserData,
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Custom)] OrganizationUser newUserData,
|
|
|
|
|
IEnumerable<SelectionReadOnly> collections,
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser savingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
organization.UseCustomPermissions = true;
|
|
|
|
|
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationRepository.GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
newUserData.Id = oldUserData.Id;
|
|
|
|
|
newUserData.UserId = oldUserData.UserId;
|
2022-12-06 10:50:08 +01:00
|
|
|
|
newUserData.OrganizationId = savingUser.OrganizationId = oldUserData.OrganizationId = organization.Id;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetByIdAsync(oldUserData.Id).Returns(oldUserData);
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(savingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new List<OrganizationUser> { savingUser });
|
2021-05-25 19:23:47 +02:00
|
|
|
|
currentContext.OrganizationOwner(savingUser.OrganizationId).Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
await sutProvider.Sut.SaveUserAsync(newUserData, savingUser.UserId, collections);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-05-17 10:10:44 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-17 10:10:44 +02:00
|
|
|
|
public async Task DeleteUser_InvalidUser(OrganizationUser organizationUser, OrganizationUser deletingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
|
|
|
|
|
organizationUserRepository.GetByIdAsync(organizationUser.Id).Returns(organizationUser);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.DeleteUserAsync(Guid.NewGuid(), organizationUser.Id, deletingUser.UserId));
|
|
|
|
|
Assert.Contains("User not valid.", exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-05-25 19:23:47 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public async Task DeleteUser_RemoveYourself(OrganizationUser deletingUser, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
|
|
|
|
|
organizationUserRepository.GetByIdAsync(deletingUser.Id).Returns(deletingUser);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.DeleteUserAsync(deletingUser.OrganizationId, deletingUser.Id, deletingUser.UserId));
|
|
|
|
|
Assert.Contains("You cannot remove yourself.", exception.Message);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2021-05-25 19:23:47 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public async Task DeleteUser_NonOwnerRemoveOwner(
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser organizationUser,
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Admin)] OrganizationUser deletingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-07-01 14:31:05 +02:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2021-05-25 19:23:47 +02:00
|
|
|
|
|
|
|
|
|
organizationUser.OrganizationId = deletingUser.OrganizationId;
|
|
|
|
|
organizationUserRepository.GetByIdAsync(organizationUser.Id).Returns(organizationUser);
|
|
|
|
|
currentContext.OrganizationAdmin(deletingUser.OrganizationId).Returns(true);
|
2021-08-10 18:16:10 +02:00
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.DeleteUserAsync(deletingUser.OrganizationId, organizationUser.Id, deletingUser.UserId));
|
2021-05-25 19:23:47 +02:00
|
|
|
|
Assert.Contains("Only owners can delete other owners.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-08-10 18:16:10 +02:00
|
|
|
|
public async Task DeleteUser_LastOwner(
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser organizationUser,
|
|
|
|
|
OrganizationUser deletingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organizationUser.OrganizationId = deletingUser.OrganizationId;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetByIdAsync(organizationUser.Id).Returns(organizationUser);
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(deletingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { organizationUser });
|
2021-08-10 18:16:10 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.DeleteUserAsync(deletingUser.OrganizationId, organizationUser.Id, null));
|
|
|
|
|
Assert.Contains("Organization must have at least one confirmed owner.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public async Task DeleteUser_Success(
|
|
|
|
|
OrganizationUser organizationUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser deletingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUser.OrganizationId = deletingUser.OrganizationId;
|
|
|
|
|
organizationUserRepository.GetByIdAsync(organizationUser.Id).Returns(organizationUser);
|
|
|
|
|
organizationUserRepository.GetByIdAsync(deletingUser.Id).Returns(deletingUser);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(deletingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { deletingUser, organizationUser });
|
|
|
|
|
currentContext.OrganizationOwner(deletingUser.OrganizationId).Returns(true);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
await sutProvider.Sut.DeleteUserAsync(deletingUser.OrganizationId, organizationUser.Id, deletingUser.UserId);
|
2022-11-09 13:13:29 +01:00
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received(1).LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Removed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task DeleteUser_WithEventSystemUser_Success(
|
|
|
|
|
OrganizationUser organizationUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser deletingUser, EventSystemUser eventSystemUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
|
|
|
|
|
organizationUser.OrganizationId = deletingUser.OrganizationId;
|
|
|
|
|
organizationUserRepository.GetByIdAsync(organizationUser.Id).Returns(organizationUser);
|
|
|
|
|
organizationUserRepository.GetByIdAsync(deletingUser.Id).Returns(deletingUser);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(deletingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { deletingUser, organizationUser });
|
|
|
|
|
currentContext.OrganizationOwner(deletingUser.OrganizationId).Returns(true);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.DeleteUserAsync(deletingUser.OrganizationId, organizationUser.Id, eventSystemUser);
|
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received(1).LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Removed, eventSystemUser);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-07-07 17:08:18 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-17 10:10:44 +02:00
|
|
|
|
public async Task DeleteUsers_FilterInvalid(OrganizationUser organizationUser, OrganizationUser deletingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var organizationUsers = new[] { organizationUser };
|
|
|
|
|
var organizationUserIds = organizationUsers.Select(u => u.Id);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(organizationUsers);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.DeleteUsersAsync(deletingUser.OrganizationId, organizationUserIds, deletingUser.UserId));
|
|
|
|
|
Assert.Contains("Users invalid.", exception.Message);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2021-07-07 17:08:18 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-07-07 17:08:18 +02:00
|
|
|
|
public async Task DeleteUsers_RemoveYourself(
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser orgUser,
|
|
|
|
|
OrganizationUser deletingUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var organizationUsers = new[] { deletingUser };
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var organizationUserIds = organizationUsers.Select(u => u.Id);
|
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(organizationUsers);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(default, default).ReturnsForAnyArgs(new[] { orgUser });
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var result = await sutProvider.Sut.DeleteUsersAsync(deletingUser.OrganizationId, organizationUserIds, deletingUser.UserId);
|
|
|
|
|
Assert.Contains("You cannot remove yourself.", result[0].Item2);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-07-07 17:08:18 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-07-07 17:08:18 +02:00
|
|
|
|
public async Task DeleteUsers_NonOwnerRemoveOwner(
|
2021-05-25 19:23:47 +02:00
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Admin)] OrganizationUser deletingUser,
|
2021-07-07 17:08:18 +02:00
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser orgUser1,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed)] OrganizationUser orgUser2,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
|
|
|
|
|
orgUser1.OrganizationId = orgUser2.OrganizationId = deletingUser.OrganizationId;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUsers = new[] { orgUser1 };
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var organizationUserIds = organizationUsers.Select(u => u.Id);
|
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(organizationUsers);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(default, default).ReturnsForAnyArgs(new[] { orgUser2 });
|
|
|
|
|
|
2021-08-10 18:16:10 +02:00
|
|
|
|
var result = await sutProvider.Sut.DeleteUsersAsync(deletingUser.OrganizationId, organizationUserIds, deletingUser.UserId);
|
2021-07-07 17:08:18 +02:00
|
|
|
|
Assert.Contains("Only owners can delete other owners.", result[0].Item2);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-08-10 18:16:10 +02:00
|
|
|
|
public async Task DeleteUsers_LastOwner(
|
2021-07-07 17:08:18 +02:00
|
|
|
|
[OrganizationUser(status: OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser orgUser,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2021-05-25 19:23:47 +02:00
|
|
|
|
{
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
var organizationUsers = new[] { orgUser };
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUserIds = organizationUsers.Select(u => u.Id);
|
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(organizationUsers);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(orgUser.OrganizationId, OrganizationUserType.Owner).Returns(organizationUsers);
|
2021-07-07 17:08:18 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
2021-08-10 18:16:10 +02:00
|
|
|
|
() => sutProvider.Sut.DeleteUsersAsync(orgUser.OrganizationId, organizationUserIds, null));
|
2021-05-25 19:23:47 +02:00
|
|
|
|
Assert.Contains("Organization must have at least one confirmed owner.", exception.Message);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2021-07-07 17:08:18 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-07-07 17:08:18 +02:00
|
|
|
|
public async Task DeleteUsers_Success(
|
2021-05-25 19:23:47 +02:00
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser deletingUser,
|
|
|
|
|
[OrganizationUser(type: OrganizationUserType.Owner)] OrganizationUser orgUser1, OrganizationUser orgUser2,
|
2021-07-07 17:08:18 +02:00
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
orgUser1.OrganizationId = orgUser2.OrganizationId = deletingUser.OrganizationId;
|
|
|
|
|
var organizationUsers = new[] { orgUser1, orgUser2 };
|
|
|
|
|
var organizationUserIds = organizationUsers.Select(u => u.Id);
|
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(organizationUsers);
|
|
|
|
|
organizationUserRepository.GetByIdAsync(deletingUser.Id).Returns(deletingUser);
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(deletingUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { deletingUser, orgUser1 });
|
2021-07-07 17:08:18 +02:00
|
|
|
|
currentContext.OrganizationOwner(deletingUser.OrganizationId).Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-07-07 17:08:18 +02:00
|
|
|
|
await sutProvider.Sut.DeleteUsersAsync(deletingUser.OrganizationId, organizationUserIds, deletingUser.UserId);
|
|
|
|
|
}
|
2021-09-23 12:36:08 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-09-23 12:36:08 +02:00
|
|
|
|
public async Task ConfirmUser_InvalidStatus(OrganizationUser confirmingUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Invited)] OrganizationUser orgUser, string key,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-08-10 18:16:10 +02:00
|
|
|
|
var userService = Substitute.For<IUserService>();
|
2021-09-23 12:36:08 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetByIdAsync(orgUser.Id).Returns(orgUser);
|
2021-09-23 12:36:08 +02:00
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.ConfirmUserAsync(orgUser.OrganizationId, orgUser.Id, key, confirmingUser.Id, userService));
|
|
|
|
|
Assert.Contains("User not valid.", exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-09-23 12:36:08 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-09-23 12:36:08 +02:00
|
|
|
|
public async Task ConfirmUser_WrongOrganization(OrganizationUser confirmingUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, string key,
|
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-09-23 12:36:08 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
|
|
|
|
|
|
|
|
|
organizationUserRepository.GetByIdAsync(orgUser.Id).Returns(orgUser);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.ConfirmUserAsync(confirmingUser.OrganizationId, orgUser.Id, key, confirmingUser.Id, userService));
|
|
|
|
|
Assert.Contains("User not valid.", exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-09-23 12:36:08 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[BitAutoData(OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Owner)]
|
2021-08-10 18:16:10 +02:00
|
|
|
|
public async Task ConfirmUserToFree_AlreadyFreeAdminOrOwner_Throws(OrganizationUserType userType, Organization org, OrganizationUser confirmingUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, User user,
|
2021-05-25 19:23:47 +02:00
|
|
|
|
string key, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
org.PlanType = PlanType.Free;
|
|
|
|
|
orgUser.OrganizationId = confirmingUser.OrganizationId = org.Id;
|
|
|
|
|
orgUser.UserId = user.Id;
|
2021-08-10 18:16:10 +02:00
|
|
|
|
orgUser.Type = userType;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { orgUser });
|
|
|
|
|
organizationUserRepository.GetCountByFreeOrganizationAdminUserAsync(orgUser.UserId.Value).Returns(1);
|
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
|
|
|
|
userRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { user });
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-10-25 17:19:37 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.ConfirmUserAsync(orgUser.OrganizationId, orgUser.Id, key, confirmingUser.Id, userService));
|
|
|
|
|
Assert.Contains("User can only be an admin of one free organization.", exception.Message);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2021-09-23 12:36:08 +02:00
|
|
|
|
|
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[BitAutoData(PlanType.Custom, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.Custom, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseAnnually, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseAnnually, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseAnnually2019, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseAnnually2019, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseMonthly, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseMonthly, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseMonthly2019, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.EnterpriseMonthly2019, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.FamiliesAnnually, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.FamiliesAnnually, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.FamiliesAnnually2019, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.FamiliesAnnually2019, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsAnnually, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsAnnually, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsAnnually2019, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsAnnually2019, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsMonthly, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsMonthly, OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsMonthly2019, OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(PlanType.TeamsMonthly2019, OrganizationUserType.Owner)]
|
2021-09-23 12:36:08 +02:00
|
|
|
|
public async Task ConfirmUserToNonFree_AlreadyFreeAdminOrOwner_DoesNotThrow(PlanType planType, OrganizationUserType orgUserType, Organization org, OrganizationUser confirmingUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, User user,
|
|
|
|
|
string key, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2021-09-23 12:36:08 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-08-10 18:16:10 +02:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-09-23 12:36:08 +02:00
|
|
|
|
var userService = Substitute.For<IUserService>();
|
2021-08-10 18:16:10 +02:00
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
org.PlanType = planType;
|
2021-08-10 18:16:10 +02:00
|
|
|
|
orgUser.OrganizationId = confirmingUser.OrganizationId = org.Id;
|
2021-09-23 12:36:08 +02:00
|
|
|
|
orgUser.UserId = user.Id;
|
2021-08-10 18:16:10 +02:00
|
|
|
|
orgUser.Type = orgUserType;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { orgUser });
|
2021-08-10 18:16:10 +02:00
|
|
|
|
organizationUserRepository.GetCountByFreeOrganizationAdminUserAsync(orgUser.UserId.Value).Returns(1);
|
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
2021-09-23 12:36:08 +02:00
|
|
|
|
userRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { user });
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
await sutProvider.Sut.ConfirmUserAsync(orgUser.OrganizationId, orgUser.Id, key, confirmingUser.Id, userService);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received(1).LogOrganizationUserEventAsync(orgUser, EventType.OrganizationUser_Confirmed);
|
2021-08-10 18:16:10 +02:00
|
|
|
|
await sutProvider.GetDependency<IMailService>().Received(1).SendOrganizationConfirmedEmailAsync(org.Name, user.Email);
|
|
|
|
|
await organizationUserRepository.Received(1).ReplaceManyAsync(Arg.Is<List<OrganizationUser>>(users => users.Contains(orgUser) && users.Count == 1));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-09-23 12:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public async Task ConfirmUser_SingleOrgPolicy(Organization org, OrganizationUser confirmingUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, User user,
|
|
|
|
|
OrganizationUser orgUserAnotherOrg, [Policy(PolicyType.SingleOrg)] Policy singleOrgPolicy,
|
|
|
|
|
string key, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-07-07 17:08:18 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-09-23 12:36:08 +02:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var policyRepository = sutProvider.GetDependency<IPolicyRepository>();
|
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
org.PlanType = PlanType.EnterpriseAnnually;
|
|
|
|
|
orgUser.Status = OrganizationUserStatusType.Accepted;
|
|
|
|
|
orgUser.OrganizationId = confirmingUser.OrganizationId = org.Id;
|
|
|
|
|
orgUser.UserId = orgUserAnotherOrg.UserId = user.Id;
|
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { orgUser });
|
|
|
|
|
organizationUserRepository.GetManyByManyUsersAsync(default).ReturnsForAnyArgs(new[] { orgUserAnotherOrg });
|
2021-09-23 12:36:08 +02:00
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
userRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { user });
|
|
|
|
|
policyRepository.GetManyByOrganizationIdAsync(org.Id).Returns(new[] { singleOrgPolicy });
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.ConfirmUserAsync(orgUser.OrganizationId, orgUser.Id, key, confirmingUser.Id, userService));
|
2021-07-07 17:08:18 +02:00
|
|
|
|
Assert.Contains("User is a member of another organization.", exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-11-09 16:37:32 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-11-09 16:37:32 +01:00
|
|
|
|
public async Task ConfirmUser_TwoFactorPolicy(Organization org, OrganizationUser confirmingUser,
|
2021-05-25 19:23:47 +02:00
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, User user,
|
|
|
|
|
OrganizationUser orgUserAnotherOrg, [Policy(PolicyType.TwoFactorAuthentication)] Policy twoFactorPolicy,
|
2021-11-09 16:37:32 +01:00
|
|
|
|
string key, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-09-23 12:36:08 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var policyRepository = sutProvider.GetDependency<IPolicyRepository>();
|
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
org.PlanType = PlanType.EnterpriseAnnually;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
orgUser.OrganizationId = confirmingUser.OrganizationId = org.Id;
|
2021-11-09 16:37:32 +01:00
|
|
|
|
orgUser.UserId = orgUserAnotherOrg.UserId = user.Id;
|
2021-09-23 12:36:08 +02:00
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { orgUser });
|
2021-05-25 19:23:47 +02:00
|
|
|
|
organizationUserRepository.GetManyByManyUsersAsync(default).ReturnsForAnyArgs(new[] { orgUserAnotherOrg });
|
2021-09-23 12:36:08 +02:00
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
userRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { user });
|
|
|
|
|
policyRepository.GetManyByOrganizationIdAsync(org.Id).Returns(new[] { twoFactorPolicy });
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.ConfirmUserAsync(orgUser.OrganizationId, orgUser.Id, key, confirmingUser.Id, userService));
|
2021-05-25 19:23:47 +02:00
|
|
|
|
Assert.Contains("User does not have two-step login enabled.", exception.Message);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2021-11-09 16:37:32 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-11-09 16:37:32 +01:00
|
|
|
|
public async Task ConfirmUser_Success(Organization org, OrganizationUser confirmingUser,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser, User user,
|
2021-09-23 12:36:08 +02:00
|
|
|
|
[Policy(PolicyType.TwoFactorAuthentication)] Policy twoFactorPolicy,
|
2021-11-09 16:37:32 +01:00
|
|
|
|
[Policy(PolicyType.SingleOrg)] Policy singleOrgPolicy, string key, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var policyRepository = sutProvider.GetDependency<IPolicyRepository>();
|
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
org.PlanType = PlanType.EnterpriseAnnually;
|
|
|
|
|
orgUser.OrganizationId = confirmingUser.OrganizationId = org.Id;
|
|
|
|
|
orgUser.UserId = user.Id;
|
2021-11-09 16:37:32 +01:00
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { orgUser });
|
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
userRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { user });
|
|
|
|
|
policyRepository.GetManyByOrganizationIdAsync(org.Id).Returns(new[] { twoFactorPolicy, singleOrgPolicy });
|
|
|
|
|
userService.TwoFactorIsEnabledAsync(user).Returns(true);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-07-07 17:08:18 +02:00
|
|
|
|
await sutProvider.Sut.ConfirmUserAsync(orgUser.OrganizationId, orgUser.Id, key, confirmingUser.Id, userService);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-11-09 16:37:32 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-11-09 16:37:32 +01:00
|
|
|
|
public async Task ConfirmUsers_Success(Organization org,
|
2021-05-25 19:23:47 +02:00
|
|
|
|
OrganizationUser confirmingUser,
|
2021-11-09 16:37:32 +01:00
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser1,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser2,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Accepted)] OrganizationUser orgUser3,
|
2021-05-25 19:23:47 +02:00
|
|
|
|
OrganizationUser anotherOrgUser, User user1, User user2, User user3,
|
2021-11-09 16:37:32 +01:00
|
|
|
|
[Policy(PolicyType.TwoFactorAuthentication)] Policy twoFactorPolicy,
|
|
|
|
|
[Policy(PolicyType.SingleOrg)] Policy singleOrgPolicy, string key, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var policyRepository = sutProvider.GetDependency<IPolicyRepository>();
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var userService = Substitute.For<IUserService>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
org.PlanType = PlanType.EnterpriseAnnually;
|
|
|
|
|
orgUser1.OrganizationId = orgUser2.OrganizationId = orgUser3.OrganizationId = confirmingUser.OrganizationId = org.Id;
|
2021-11-09 16:37:32 +01:00
|
|
|
|
orgUser1.UserId = user1.Id;
|
2021-05-25 19:23:47 +02:00
|
|
|
|
orgUser2.UserId = user2.Id;
|
|
|
|
|
orgUser3.UserId = user3.Id;
|
|
|
|
|
anotherOrgUser.UserId = user3.Id;
|
|
|
|
|
var orgUsers = new[] { orgUser1, orgUser2, orgUser3 };
|
|
|
|
|
organizationUserRepository.GetManyAsync(default).ReturnsForAnyArgs(orgUsers);
|
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
|
|
|
|
userRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { user1, user2, user3 });
|
|
|
|
|
policyRepository.GetManyByOrganizationIdAsync(org.Id).Returns(new[] { twoFactorPolicy, singleOrgPolicy });
|
2021-11-09 16:37:32 +01:00
|
|
|
|
userService.TwoFactorIsEnabledAsync(user1).Returns(true);
|
2021-05-25 19:23:47 +02:00
|
|
|
|
userService.TwoFactorIsEnabledAsync(user2).Returns(false);
|
|
|
|
|
userService.TwoFactorIsEnabledAsync(user3).Returns(true);
|
|
|
|
|
organizationUserRepository.GetManyByManyUsersAsync(default)
|
|
|
|
|
.ReturnsForAnyArgs(new[] { orgUser1, orgUser2, orgUser3, anotherOrgUser });
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var keys = orgUsers.ToDictionary(ou => ou.Id, _ => key);
|
|
|
|
|
var result = await sutProvider.Sut.ConfirmUsersAsync(confirmingUser.OrganizationId, keys, confirmingUser.Id, userService);
|
|
|
|
|
Assert.Contains("", result[0].Item2);
|
|
|
|
|
Assert.Contains("User does not have two-step login enabled.", result[1].Item2);
|
|
|
|
|
Assert.Contains("User is a member of another organization.", result[2].Item2);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-11-09 16:37:32 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-11-09 16:37:32 +01:00
|
|
|
|
public async Task UpdateOrganizationKeysAsync_WithoutManageResetPassword_Throws(Guid orgId, string publicKey,
|
|
|
|
|
string privateKey, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.ManageResetPassword(orgId).Returns(false);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
await Assert.ThrowsAsync<UnauthorizedAccessException>(
|
|
|
|
|
() => sutProvider.Sut.UpdateOrganizationKeysAsync(orgId, publicKey, privateKey));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-11-09 16:37:32 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-11-09 16:37:32 +01:00
|
|
|
|
public async Task UpdateOrganizationKeysAsync_KeysAlreadySet_Throws(Organization org, string publicKey,
|
|
|
|
|
string privateKey, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
|
|
|
|
currentContext.ManageResetPassword(org.Id).Returns(true);
|
|
|
|
|
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.UpdateOrganizationKeysAsync(org.Id, publicKey, privateKey));
|
|
|
|
|
Assert.Contains("Organization Keys already exist", exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-11-09 16:37:32 +01:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-09-23 12:36:08 +02:00
|
|
|
|
public async Task UpdateOrganizationKeysAsync_KeysAlreadySet_Success(Organization org, string publicKey,
|
|
|
|
|
string privateKey, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-09-23 12:36:08 +02:00
|
|
|
|
org.PublicKey = null;
|
|
|
|
|
org.PrivateKey = null;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
var currentContext = sutProvider.GetDependency<ICurrentContext>();
|
2021-10-25 17:19:37 +02:00
|
|
|
|
currentContext.ManageResetPassword(org.Id).Returns(true);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
organizationRepository.GetByIdAsync(org.Id).Returns(org);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-05-17 16:43:02 +02:00
|
|
|
|
await sutProvider.Sut.UpdateOrganizationKeysAsync(org.Id, publicKey, privateKey);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Theory]
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[PaidOrganizationCustomize(CheckedPlanType = PlanType.EnterpriseAnnually)]
|
|
|
|
|
[BitAutoData("Cannot set max seat autoscaling below seat count", 1, 0, 2)]
|
|
|
|
|
[BitAutoData("Cannot set max seat autoscaling below seat count", 4, -1, 6)]
|
|
|
|
|
public async Task Enterprise_UpdateSubscription_BadInputThrows(string expectedMessage,
|
|
|
|
|
int? maxAutoscaleSeats, int seatAdjustment, int? currentSeats, Organization organization, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
=> await UpdateSubscription_BadInputThrows(expectedMessage, maxAutoscaleSeats, seatAdjustment, currentSeats, organization, sutProvider);
|
|
|
|
|
[Theory]
|
|
|
|
|
[FreeOrganizationCustomize]
|
|
|
|
|
[BitAutoData("Your plan does not allow seat autoscaling", 10, 0, null)]
|
|
|
|
|
public async Task Free_UpdateSubscription_BadInputThrows(string expectedMessage,
|
|
|
|
|
int? maxAutoscaleSeats, int seatAdjustment, int? currentSeats, Organization organization, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
=> await UpdateSubscription_BadInputThrows(expectedMessage, maxAutoscaleSeats, seatAdjustment, currentSeats, organization, sutProvider);
|
|
|
|
|
|
|
|
|
|
private async Task UpdateSubscription_BadInputThrows(string expectedMessage,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
int? maxAutoscaleSeats, int seatAdjustment, int? currentSeats, Organization organization, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organization.Seats = currentSeats;
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.UpdateSubscription(organization.Id,
|
|
|
|
|
seatAdjustment, maxAutoscaleSeats));
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
Assert.Contains(expectedMessage, exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2021-05-17 10:10:44 +02:00
|
|
|
|
public async Task UpdateSubscription_NoOrganization_Throws(Guid organizationId, SutProvider<OrganizationService> sutProvider)
|
2021-11-17 11:46:35 +01:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organizationId).Returns((Organization)null);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-01-12 17:02:39 +01:00
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.UpdateSubscription(organizationId, 0, null));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, PaidOrganizationCustomize]
|
|
|
|
|
[BitAutoData(0, 100, null, true, "")]
|
|
|
|
|
[BitAutoData(0, 100, 100, true, "")]
|
|
|
|
|
[BitAutoData(0, null, 100, true, "")]
|
|
|
|
|
[BitAutoData(1, 100, null, true, "")]
|
2022-09-23 06:30:39 +02:00
|
|
|
|
[BitAutoData(1, 100, 100, false, "Seat limit has been reached")]
|
2021-05-25 19:23:47 +02:00
|
|
|
|
public void CanScale(int seatsToAdd, int? currentSeats, int? maxAutoscaleSeats,
|
|
|
|
|
bool expectedResult, string expectedFailureMessage, Organization organization,
|
2021-05-17 10:10:44 +02:00
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-05-17 10:10:44 +02:00
|
|
|
|
organization.Seats = currentSeats;
|
|
|
|
|
organization.MaxAutoscaleSeats = maxAutoscaleSeats;
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageUsers(organization.Id).Returns(true);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var (result, failureMessage) = sutProvider.Sut.CanScale(organization, seatsToAdd);
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
if (expectedFailureMessage == string.Empty)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
Assert.Empty(failureMessage);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
2022-08-29 22:06:55 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-09-23 12:36:08 +02:00
|
|
|
|
Assert.Contains(expectedFailureMessage, failureMessage);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2021-09-23 12:36:08 +02:00
|
|
|
|
Assert.Equal(expectedResult, result);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, PaidOrganizationCustomize, BitAutoData]
|
2022-06-24 16:39:34 +02:00
|
|
|
|
public void CanScale_FailsOnSelfHosted(Organization organization,
|
2021-09-23 12:36:08 +02:00
|
|
|
|
SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
sutProvider.GetDependency<IGlobalSettings>().SelfHosted.Returns(true);
|
2021-10-25 17:19:37 +02:00
|
|
|
|
var (result, failureMessage) = sutProvider.Sut.CanScale(organization, 10);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-09-23 12:36:08 +02:00
|
|
|
|
Assert.False(result);
|
|
|
|
|
Assert.Contains("Cannot autoscale on self-hosted instance", failureMessage);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, PaidOrganizationCustomize, BitAutoData]
|
2021-01-12 17:02:39 +01:00
|
|
|
|
public async Task Delete_Success(Organization organization, SutProvider<OrganizationService> sutProvider)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var applicationCacheService = sutProvider.GetDependency<IApplicationCacheService>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
await sutProvider.Sut.DeleteAsync(organization);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
await organizationRepository.Received().DeleteAsync(organization);
|
|
|
|
|
await applicationCacheService.Received().DeleteOrganizationAbilityAsync(organization.Id);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 15:38:35 +02:00
|
|
|
|
[Theory, PaidOrganizationCustomize, BitAutoData]
|
2021-05-17 10:10:44 +02:00
|
|
|
|
public async Task Delete_Fails_KeyConnector(Organization organization, SutProvider<OrganizationService> sutProvider,
|
2021-11-09 16:37:32 +01:00
|
|
|
|
SsoConfig ssoConfig)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2021-11-09 16:37:32 +01:00
|
|
|
|
ssoConfig.Enabled = true;
|
2021-11-17 11:46:35 +01:00
|
|
|
|
ssoConfig.SetData(new SsoConfigurationData { KeyConnectorEnabled = true });
|
2021-11-09 16:37:32 +01:00
|
|
|
|
var ssoConfigRepository = sutProvider.GetDependency<ISsoConfigRepository>();
|
|
|
|
|
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
|
|
|
|
|
var applicationCacheService = sutProvider.GetDependency<IApplicationCacheService>();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-17 10:10:44 +02:00
|
|
|
|
ssoConfigRepository.GetByOrganizationIdAsync(organization.Id).Returns(ssoConfig);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-05-25 19:23:47 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.DeleteAsync(organization));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
Assert.Contains("You cannot delete an Organization that is using Key Connector.", exception.Message);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2021-11-09 16:37:32 +01:00
|
|
|
|
await organizationRepository.DidNotReceiveWithAnyArgs().DeleteAsync(default);
|
|
|
|
|
await applicationCacheService.DidNotReceiveWithAnyArgs().DeleteOrganizationAbilityAsync(default);
|
2019-02-19 22:27:05 +01:00
|
|
|
|
}
|
2022-11-09 13:13:29 +01:00
|
|
|
|
|
|
|
|
|
private void RestoreRevokeUser_Setup(Organization organization, OrganizationUser owner, OrganizationUser organizationUser, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organizationUser.OrganizationId).Returns(organization);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().OrganizationOwner(organization.Id).Returns(true);
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ManageUsers(organization.Id).Returns(true);
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
organizationUserRepository.GetManyByOrganizationAsync(organizationUser.OrganizationId, OrganizationUserType.Owner)
|
|
|
|
|
.Returns(new[] { owner });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task RevokeUser_Success(Organization organization, [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser owner,
|
|
|
|
|
[OrganizationUser] OrganizationUser organizationUser, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
RestoreRevokeUser_Setup(organization, owner, organizationUser, sutProvider);
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var eventService = sutProvider.GetDependency<IEventService>();
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.RevokeUserAsync(organizationUser, owner.Id);
|
|
|
|
|
|
|
|
|
|
await organizationUserRepository.Received().RevokeAsync(organizationUser.Id);
|
|
|
|
|
await eventService.Received()
|
|
|
|
|
.LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Revoked);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task RevokeUser_WithEventSystemUser_Success(Organization organization, [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser owner,
|
|
|
|
|
[OrganizationUser] OrganizationUser organizationUser, EventSystemUser eventSystemUser, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
RestoreRevokeUser_Setup(organization, owner, organizationUser, sutProvider);
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var eventService = sutProvider.GetDependency<IEventService>();
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.RevokeUserAsync(organizationUser, eventSystemUser);
|
|
|
|
|
|
|
|
|
|
await organizationUserRepository.Received().RevokeAsync(organizationUser.Id);
|
|
|
|
|
await eventService.Received()
|
|
|
|
|
.LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Revoked, eventSystemUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task RestoreUser_Success(Organization organization, [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser owner,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Revoked)] OrganizationUser organizationUser, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
RestoreRevokeUser_Setup(organization, owner, organizationUser, sutProvider);
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var eventService = sutProvider.GetDependency<IEventService>();
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.RestoreUserAsync(organizationUser, owner.Id, userService);
|
|
|
|
|
|
|
|
|
|
await organizationUserRepository.Received().RestoreAsync(organizationUser.Id, OrganizationUserStatusType.Invited);
|
|
|
|
|
await eventService.Received()
|
|
|
|
|
.LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Restored);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task RestoreUser_WithEventSystemUser_Success(Organization organization, [OrganizationUser(OrganizationUserStatusType.Confirmed, OrganizationUserType.Owner)] OrganizationUser owner,
|
|
|
|
|
[OrganizationUser(OrganizationUserStatusType.Revoked)] OrganizationUser organizationUser, EventSystemUser eventSystemUser, SutProvider<OrganizationService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
RestoreRevokeUser_Setup(organization, owner, organizationUser, sutProvider);
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
|
|
|
|
var organizationUserRepository = sutProvider.GetDependency<IOrganizationUserRepository>();
|
|
|
|
|
var eventService = sutProvider.GetDependency<IEventService>();
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.RestoreUserAsync(organizationUser, eventSystemUser, userService);
|
|
|
|
|
|
|
|
|
|
await organizationUserRepository.Received().RestoreAsync(organizationUser.Id, OrganizationUserStatusType.Invited);
|
|
|
|
|
await eventService.Received()
|
|
|
|
|
.LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Restored, eventSystemUser);
|
|
|
|
|
}
|
2019-02-19 22:27:05 +01:00
|
|
|
|
}
|