2021-06-03 18:58:29 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
using Bit.CommCore.Services;
|
|
|
|
|
using Bit.CommCore.Test.AutoFixture.ProviderUserFixtures;
|
2021-08-05 16:39:05 +02:00
|
|
|
|
using Bit.Core.Context;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
using Bit.Core.Entities.Provider;
|
2021-06-03 18:58:29 +02:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Enums.Provider;
|
|
|
|
|
using Bit.Core.Exceptions;
|
2021-07-21 19:40:38 +02:00
|
|
|
|
using Bit.Core.Models.Business;
|
2021-06-03 18:58:29 +02:00
|
|
|
|
using Bit.Core.Models.Business.Provider;
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Core.Utilities;
|
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;
|
2021-06-03 18:58:29 +02:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using NSubstitute;
|
2021-07-21 19:40:38 +02:00
|
|
|
|
using NSubstitute.ReturnsExtensions;
|
2021-06-03 18:58:29 +02:00
|
|
|
|
using Xunit;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using ProviderUser = Bit.Core.Entities.Provider.ProviderUser;
|
2021-06-03 18:58:29 +02:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
namespace Bit.CommCore.Test.Services
|
2021-06-03 18:58:29 +02:00
|
|
|
|
{
|
|
|
|
|
public class ProviderServiceTests
|
|
|
|
|
{
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task CreateAsync_UserIdIsInvalid_Throws(SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.CreateAsync(default));
|
|
|
|
|
Assert.Contains("Invalid owner.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task CreateAsync_Success(User user, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
|
|
|
|
userRepository.GetByEmailAsync(user.Email).Returns(user);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
await sutProvider.Sut.CreateAsync(user.Email);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
await sutProvider.GetDependency<IProviderRepository>().ReceivedWithAnyArgs().CreateAsync(default);
|
|
|
|
|
await sutProvider.GetDependency<IMailService>().ReceivedWithAnyArgs().SendProviderSetupInviteEmailAsync(default, default, default);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task CompleteSetupAsync_UserIdIsInvalid_Throws(SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.CompleteSetupAsync(default, default, default, default));
|
|
|
|
|
Assert.Contains("Invalid owner.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task CompleteSetupAsync_TokenIsInvalid_Throws(User user, Provider provider,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var userService = sutProvider.GetDependency<IUserService>();
|
|
|
|
|
userService.GetUserByIdAsync(user.Id).Returns(user);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.CompleteSetupAsync(provider, user.Id, default, default));
|
|
|
|
|
Assert.Contains("Invalid token.", exception.Message);
|
|
|
|
|
}
|
2021-06-30 09:35:26 +02:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
2021-06-30 09:35:26 +02:00
|
|
|
|
public async Task CompleteSetupAsync_Success(User user, Provider provider, string key,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Confirmed, ProviderUserType.ProviderAdmin)] ProviderUser providerUser,
|
2021-06-03 18:58:29 +02:00
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
2021-06-30 09:35:26 +02:00
|
|
|
|
providerUser.ProviderId = provider.Id;
|
|
|
|
|
providerUser.UserId = user.Id;
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var userService = sutProvider.GetDependency<IUserService>();
|
|
|
|
|
userService.GetUserByIdAsync(user.Id).Returns(user);
|
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetByProviderUserAsync(provider.Id, user.Id).Returns(providerUser);
|
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var dataProtectionProvider = DataProtectionProvider.Create("ApplicationName");
|
|
|
|
|
var protector = dataProtectionProvider.CreateProtector("ProviderServiceDataProtector");
|
|
|
|
|
sutProvider.GetDependency<IDataProtectionProvider>().CreateProtector("ProviderServiceDataProtector")
|
|
|
|
|
.Returns(protector);
|
|
|
|
|
sutProvider.Create();
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-30 09:35:26 +02:00
|
|
|
|
var token = protector.Protect($"ProviderSetupInvite {provider.Id} {user.Email} {CoreHelpers.ToEpocMilliseconds(DateTime.UtcNow)}");
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.CompleteSetupAsync(provider, user.Id, token, key);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IProviderRepository>().Received().UpsertAsync(provider);
|
|
|
|
|
await sutProvider.GetDependency<IProviderUserRepository>().Received()
|
2021-06-30 09:35:26 +02:00
|
|
|
|
.ReplaceAsync(Arg.Is<ProviderUser>(pu => pu.UserId == user.Id && pu.ProviderId == provider.Id && pu.Key == key));
|
2021-06-03 18:58:29 +02:00
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task UpdateAsync_ProviderIdIsInvalid_Throws(Provider provider, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
provider.Id = default;
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-09-27 19:34:55 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<ArgumentException>(
|
2021-06-03 18:58:29 +02:00
|
|
|
|
() => sutProvider.Sut.UpdateAsync(provider));
|
|
|
|
|
Assert.Contains("Cannot create provider this way.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task UpdateAsync_Success(Provider provider, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
await sutProvider.Sut.UpdateAsync(provider);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
2021-08-05 16:39:05 +02:00
|
|
|
|
public async Task InviteUserAsync_ProviderIdIsInvalid_Throws(ProviderUserInvite<string> invite, SutProvider<ProviderService> sutProvider)
|
2021-09-27 19:34:55 +02:00
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(invite.ProviderId).Returns(true);
|
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.InviteUserAsync(invite));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task InviteUserAsync_InvalidPermissions_Throws(ProviderUserInvite<string> invite, SutProvider<ProviderService> sutProvider)
|
2021-09-27 19:34:55 +02:00
|
|
|
|
{
|
2021-08-05 16:39:05 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(invite.ProviderId).Returns(false);
|
2021-09-27 19:34:55 +02:00
|
|
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => sutProvider.Sut.InviteUserAsync(invite));
|
2021-06-03 18:58:29 +02:00
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
2021-08-05 16:39:05 +02:00
|
|
|
|
public async Task InviteUserAsync_EmailsInvalid_Throws(Provider provider, ProviderUserInvite<string> providerUserInvite,
|
2021-06-03 18:58:29 +02:00
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
2021-08-05 16:39:05 +02:00
|
|
|
|
providerRepository.GetByIdAsync(providerUserInvite.ProviderId).Returns(provider);
|
2021-09-27 19:34:55 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(providerUserInvite.ProviderId).Returns(true);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
providerUserInvite.UserIdentifiers = null;
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.InviteUserAsync(providerUserInvite));
|
2021-06-03 18:58:29 +02:00
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
2021-08-05 16:39:05 +02:00
|
|
|
|
public async Task InviteUserAsync_AlreadyInvited(Provider provider, ProviderUserInvite<string> providerUserInvite,
|
2021-06-03 18:58:29 +02:00
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
2021-08-05 16:39:05 +02:00
|
|
|
|
providerRepository.GetByIdAsync(providerUserInvite.ProviderId).Returns(provider);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetCountByProviderAsync(default, default, default).ReturnsForAnyArgs(1);
|
2021-09-27 19:34:55 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(providerUserInvite.ProviderId).Returns(true);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
var result = await sutProvider.Sut.InviteUserAsync(providerUserInvite);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
Assert.Empty(result);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
2021-08-05 16:39:05 +02:00
|
|
|
|
public async Task InviteUserAsync_Success(Provider provider, ProviderUserInvite<string> providerUserInvite,
|
2021-06-03 18:58:29 +02:00
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
2021-08-05 16:39:05 +02:00
|
|
|
|
providerRepository.GetByIdAsync(providerUserInvite.ProviderId).Returns(provider);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetCountByProviderAsync(default, default, default).ReturnsForAnyArgs(0);
|
2021-09-27 19:34:55 +02:00
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(providerUserInvite.ProviderId).Returns(true);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
var result = await sutProvider.Sut.InviteUserAsync(providerUserInvite);
|
|
|
|
|
Assert.Equal(providerUserInvite.UserIdentifiers.Count(), result.Count);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
Assert.True(result.TrueForAll(pu => pu.Status == ProviderUserStatusType.Invited), "Status must be invited");
|
2021-08-05 16:39:05 +02:00
|
|
|
|
Assert.True(result.TrueForAll(pu => pu.ProviderId == providerUserInvite.ProviderId), "Provider Id must be correct");
|
2021-06-03 18:58:29 +02:00
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task ResendInviteUserAsync_InvalidPermissions_Throws(ProviderUserInvite<Guid> invite, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(invite.ProviderId).Returns(false);
|
|
|
|
|
await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.ResendInvitesAsync(invite));
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task ResendInvitesAsync_Errors(Provider provider,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Invited)] ProviderUser pu1,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Accepted)] ProviderUser pu2,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Confirmed)] ProviderUser pu3,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Invited)] ProviderUser pu4,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerUsers = new[] { pu1, pu2, pu3, pu4 };
|
|
|
|
|
pu1.ProviderId = pu2.ProviderId = pu3.ProviderId = provider.Id;
|
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
var invite = new ProviderUserInvite<Guid>
|
|
|
|
|
{
|
|
|
|
|
UserIdentifiers = providerUsers.Select(pu => pu.Id),
|
|
|
|
|
ProviderId = provider.Id
|
|
|
|
|
};
|
2021-09-27 19:34:55 +02:00
|
|
|
|
|
|
|
|
|
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
|
|
|
|
providerRepository.GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetManyAsync(default).ReturnsForAnyArgs(providerUsers.ToList());
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(invite.ProviderId).Returns(true);
|
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
var result = await sutProvider.Sut.ResendInvitesAsync(invite);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
Assert.Equal("", result[0].Item2);
|
|
|
|
|
Assert.Equal("User invalid.", result[1].Item2);
|
|
|
|
|
Assert.Equal("User invalid.", result[2].Item2);
|
|
|
|
|
Assert.Equal("User invalid.", result[3].Item2);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task ResendInvitesAsync_Success(Provider provider, IEnumerable<ProviderUser> providerUsers,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
foreach (var providerUser in providerUsers)
|
|
|
|
|
{
|
|
|
|
|
providerUser.ProviderId = provider.Id;
|
|
|
|
|
providerUser.Status = ProviderUserStatusType.Invited;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
var invite = new ProviderUserInvite<Guid>
|
|
|
|
|
{
|
|
|
|
|
UserIdentifiers = providerUsers.Select(pu => pu.Id),
|
|
|
|
|
ProviderId = provider.Id
|
|
|
|
|
};
|
2021-09-27 19:34:55 +02:00
|
|
|
|
|
|
|
|
|
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
|
|
|
|
providerRepository.GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetManyAsync(default).ReturnsForAnyArgs(providerUsers.ToList());
|
|
|
|
|
sutProvider.GetDependency<ICurrentContext>().ProviderManageUsers(invite.ProviderId).Returns(true);
|
|
|
|
|
|
2021-08-05 16:39:05 +02:00
|
|
|
|
var result = await sutProvider.Sut.ResendInvitesAsync(invite);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
Assert.True(result.All(r => r.Item2 == ""));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task AcceptUserAsync_UserIsInvalid_Throws(ProviderUser providerUser, User user,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptUserAsync(default, default, default));
|
|
|
|
|
Assert.Equal("User invalid.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task AcceptUserAsync_AlreadyAccepted_Throws(
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Accepted)] ProviderUser providerUser, User user,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetByIdAsync(providerUser.Id).Returns(providerUser);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptUserAsync(providerUser.Id, user, default));
|
|
|
|
|
Assert.Equal("Already accepted.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task AcceptUserAsync_TokenIsInvalid_Throws(
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Invited)] ProviderUser providerUser, User user,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetByIdAsync(providerUser.Id).Returns(providerUser);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptUserAsync(providerUser.Id, user, default));
|
|
|
|
|
Assert.Equal("Invalid token.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task AcceptUserAsync_WrongEmail_Throws(
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Invited)] ProviderUser providerUser, User user,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetByIdAsync(providerUser.Id).Returns(providerUser);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var dataProtectionProvider = DataProtectionProvider.Create("ApplicationName");
|
|
|
|
|
var protector = dataProtectionProvider.CreateProtector("ProviderServiceDataProtector");
|
|
|
|
|
sutProvider.GetDependency<IDataProtectionProvider>().CreateProtector("ProviderServiceDataProtector")
|
|
|
|
|
.Returns(protector);
|
|
|
|
|
sutProvider.Create();
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var token = protector.Protect($"ProviderUserInvite {providerUser.Id} {user.Email} {CoreHelpers.ToEpocMilliseconds(DateTime.UtcNow)}");
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AcceptUserAsync(providerUser.Id, user, token));
|
|
|
|
|
Assert.Equal("User email does not match invite.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task AcceptUserAsync_Success(
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Invited)] ProviderUser providerUser, User user,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetByIdAsync(providerUser.Id).Returns(providerUser);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var dataProtectionProvider = DataProtectionProvider.Create("ApplicationName");
|
|
|
|
|
var protector = dataProtectionProvider.CreateProtector("ProviderServiceDataProtector");
|
|
|
|
|
sutProvider.GetDependency<IDataProtectionProvider>().CreateProtector("ProviderServiceDataProtector")
|
|
|
|
|
.Returns(protector);
|
|
|
|
|
sutProvider.Create();
|
|
|
|
|
|
|
|
|
|
providerUser.Email = user.Email;
|
|
|
|
|
var token = protector.Protect($"ProviderUserInvite {providerUser.Id} {user.Email} {CoreHelpers.ToEpocMilliseconds(DateTime.UtcNow)}");
|
|
|
|
|
|
|
|
|
|
var pu = await sutProvider.Sut.AcceptUserAsync(providerUser.Id, user, token);
|
|
|
|
|
Assert.Null(pu.Email);
|
|
|
|
|
Assert.Equal(ProviderUserStatusType.Accepted, pu.Status);
|
|
|
|
|
Assert.Equal(user.Id, pu.UserId);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task ConfirmUsersAsync_NoValid(
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Invited)] ProviderUser pu1,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Accepted)] ProviderUser pu2,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Confirmed)] ProviderUser pu3,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
pu1.ProviderId = pu3.ProviderId;
|
|
|
|
|
var providerUsers = new[] { pu1, pu2, pu3 };
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetManyAsync(default).ReturnsForAnyArgs(providerUsers);
|
|
|
|
|
|
|
|
|
|
var dict = providerUsers.ToDictionary(pu => pu.Id, _ => "key");
|
|
|
|
|
var result = await sutProvider.Sut.ConfirmUsersAsync(pu1.ProviderId, dict, default);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
Assert.Empty(result);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task ConfirmUsersAsync_Success(
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Invited)] ProviderUser pu1, User u1,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Accepted)] ProviderUser pu2, User u2,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Confirmed)] ProviderUser pu3, User u3,
|
|
|
|
|
Provider provider, User user, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
pu1.ProviderId = pu2.ProviderId = pu3.ProviderId = provider.Id;
|
|
|
|
|
pu1.UserId = u1.Id;
|
|
|
|
|
pu2.UserId = u2.Id;
|
|
|
|
|
pu3.UserId = u3.Id;
|
|
|
|
|
var providerUsers = new[] { pu1, pu2, pu3 };
|
|
|
|
|
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetManyAsync(default).ReturnsForAnyArgs(providerUsers);
|
|
|
|
|
var providerRepository = sutProvider.GetDependency<IProviderRepository>();
|
|
|
|
|
providerRepository.GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
var userRepository = sutProvider.GetDependency<IUserRepository>();
|
|
|
|
|
userRepository.GetManyAsync(default).ReturnsForAnyArgs(new[] { u1, u2, u3 });
|
|
|
|
|
|
|
|
|
|
var dict = providerUsers.ToDictionary(pu => pu.Id, _ => "key");
|
|
|
|
|
var result = await sutProvider.Sut.ConfirmUsersAsync(pu1.ProviderId, dict, user.Id);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
Assert.Equal("Invalid user.", result[0].Item2);
|
|
|
|
|
Assert.Equal("", result[1].Item2);
|
|
|
|
|
Assert.Equal("Invalid user.", result[2].Item2);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task SaveUserAsync_UserIdIsInvalid_Throws(ProviderUser providerUser,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
providerUser.Id = default;
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.SaveUserAsync(providerUser, default));
|
|
|
|
|
Assert.Equal("Invite the user first.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task SaveUserAsync_Success(
|
|
|
|
|
[ProviderUser(type: ProviderUserType.ProviderAdmin)] ProviderUser providerUser, User savingUser,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetByIdAsync(providerUser.Id).Returns(providerUser);
|
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.SaveUserAsync(providerUser, savingUser.Id);
|
|
|
|
|
await providerUserRepository.Received().ReplaceAsync(providerUser);
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received()
|
|
|
|
|
.LogProviderUserEventAsync(providerUser, EventType.ProviderUser_Updated, null);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task DeleteUsersAsync_NoRemainingOwner_Throws(Provider provider, User deletingUser,
|
|
|
|
|
ICollection<ProviderUser> providerUsers, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var userIds = providerUsers.Select(pu => pu.Id);
|
|
|
|
|
|
|
|
|
|
providerUsers.First().UserId = deletingUser.Id;
|
|
|
|
|
foreach (var providerUser in providerUsers)
|
|
|
|
|
{
|
|
|
|
|
providerUser.ProviderId = provider.Id;
|
|
|
|
|
}
|
|
|
|
|
providerUsers.Last().ProviderId = default;
|
2021-07-15 16:37:16 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetManyAsync(default).ReturnsForAnyArgs(providerUsers);
|
|
|
|
|
providerUserRepository.GetManyByProviderAsync(default, default).ReturnsForAnyArgs(new ProviderUser[] { });
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.DeleteUsersAsync(provider.Id, userIds, deletingUser.Id));
|
|
|
|
|
Assert.Equal("Provider must have at least one confirmed ProviderAdmin.", exception.Message);
|
|
|
|
|
}
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task DeleteUsersAsync_Success(Provider provider, User deletingUser, ICollection<ProviderUser> providerUsers,
|
|
|
|
|
[ProviderUser(ProviderUserStatusType.Confirmed, ProviderUserType.ProviderAdmin)] ProviderUser remainingOwner,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var userIds = providerUsers.Select(pu => pu.Id);
|
|
|
|
|
|
|
|
|
|
providerUsers.First().UserId = deletingUser.Id;
|
|
|
|
|
foreach (var providerUser in providerUsers)
|
|
|
|
|
{
|
|
|
|
|
providerUser.ProviderId = provider.Id;
|
|
|
|
|
}
|
|
|
|
|
providerUsers.Last().ProviderId = default;
|
2021-07-15 16:37:16 +02:00
|
|
|
|
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var providerUserRepository = sutProvider.GetDependency<IProviderUserRepository>();
|
|
|
|
|
providerUserRepository.GetManyAsync(default).ReturnsForAnyArgs(providerUsers);
|
|
|
|
|
providerUserRepository.GetManyByProviderAsync(default, default).ReturnsForAnyArgs(new[] { remainingOwner });
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
var result = await sutProvider.Sut.DeleteUsersAsync(provider.Id, userIds, deletingUser.Id);
|
2021-12-16 15:35:09 +01:00
|
|
|
|
|
2021-06-03 18:58:29 +02:00
|
|
|
|
Assert.NotEmpty(result);
|
|
|
|
|
Assert.Equal("You cannot remove yourself.", result[0].Item2);
|
|
|
|
|
Assert.Equal("", result[1].Item2);
|
|
|
|
|
Assert.Equal("Invalid user.", result[2].Item2);
|
|
|
|
|
}
|
2021-07-21 19:40:38 +02:00
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task AddOrganization_OrganizationAlreadyBelongsToAProvider_Throws(Provider provider,
|
|
|
|
|
Organization organization, ProviderOrganization po, User user, string key,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
po.OrganizationId = organization.Id;
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
sutProvider.GetDependency<IProviderOrganizationRepository>().GetByOrganizationId(organization.Id)
|
|
|
|
|
.Returns(po);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
|
|
|
|
() => sutProvider.Sut.AddOrganization(provider.Id, organization.Id, user.Id, key));
|
|
|
|
|
Assert.Equal("Organization already belongs to a provider.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task AddOrganization_Success(Provider provider, Organization organization, User user, string key,
|
|
|
|
|
SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
2021-09-27 19:34:55 +02:00
|
|
|
|
organization.PlanType = PlanType.EnterpriseAnnually;
|
|
|
|
|
|
2021-07-21 19:40:38 +02:00
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
var providerOrganizationRepository = sutProvider.GetDependency<IProviderOrganizationRepository>();
|
|
|
|
|
providerOrganizationRepository.GetByOrganizationId(organization.Id).ReturnsNull();
|
2021-09-27 19:34:55 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationRepository>().GetByIdAsync(organization.Id).Returns(organization);
|
2021-07-21 19:40:38 +02:00
|
|
|
|
|
|
|
|
|
await sutProvider.Sut.AddOrganization(provider.Id, organization.Id, user.Id, key);
|
|
|
|
|
|
|
|
|
|
await providerOrganizationRepository.ReceivedWithAnyArgs().CreateAsync(default);
|
|
|
|
|
await sutProvider.GetDependency<IEventService>()
|
|
|
|
|
.Received().LogProviderOrganizationEventAsync(Arg.Any<ProviderOrganization>(),
|
|
|
|
|
EventType.ProviderOrganization_Added);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task CreateOrganizationAsync_Success(Provider provider, OrganizationSignup organizationSignup,
|
2021-07-30 15:10:58 +02:00
|
|
|
|
Organization organization, string clientOwnerEmail, User user, SutProvider<ProviderService> sutProvider)
|
2021-07-21 19:40:38 +02:00
|
|
|
|
{
|
2021-09-27 19:34:55 +02:00
|
|
|
|
organizationSignup.Plan = PlanType.EnterpriseAnnually;
|
|
|
|
|
|
2021-07-21 19:40:38 +02:00
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
var providerOrganizationRepository = sutProvider.GetDependency<IProviderOrganizationRepository>();
|
|
|
|
|
sutProvider.GetDependency<IOrganizationService>().SignUpAsync(organizationSignup, true)
|
|
|
|
|
.Returns(Tuple.Create(organization, null as OrganizationUser));
|
|
|
|
|
|
|
|
|
|
var providerOrganization =
|
2021-07-30 15:10:58 +02:00
|
|
|
|
await sutProvider.Sut.CreateOrganizationAsync(provider.Id, organizationSignup, clientOwnerEmail, user);
|
2021-07-21 19:40:38 +02:00
|
|
|
|
|
|
|
|
|
await providerOrganizationRepository.ReceivedWithAnyArgs().CreateAsync(default);
|
|
|
|
|
await sutProvider.GetDependency<IEventService>()
|
|
|
|
|
.Received().LogProviderOrganizationEventAsync(providerOrganization,
|
|
|
|
|
EventType.ProviderOrganization_Created);
|
2021-07-30 15:10:58 +02:00
|
|
|
|
await sutProvider.GetDependency<IOrganizationService>()
|
2021-09-23 12:36:08 +02:00
|
|
|
|
.Received().InviteUsersAsync(organization.Id, user.Id, Arg.Is<IEnumerable<(OrganizationUserInvite, string)>>(
|
|
|
|
|
t => t.Count() == 1 &&
|
|
|
|
|
t.First().Item1.Emails.Count() == 1 &&
|
|
|
|
|
t.First().Item1.Emails.First() == clientOwnerEmail &&
|
|
|
|
|
t.First().Item1.Type == OrganizationUserType.Owner &&
|
|
|
|
|
t.First().Item1.AccessAll &&
|
|
|
|
|
t.First().Item2 == null));
|
2021-07-21 19:40:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task RemoveOrganization_ProviderOrganizationIsInvalid_Throws(Provider provider,
|
|
|
|
|
ProviderOrganization providerOrganization, User user, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
sutProvider.GetDependency<IProviderOrganizationRepository>().GetByIdAsync(providerOrganization.Id)
|
|
|
|
|
.ReturnsNull();
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
2021-08-05 14:50:41 +02:00
|
|
|
|
() => sutProvider.Sut.RemoveOrganizationAsync(provider.Id, providerOrganization.Id, user.Id));
|
2021-07-21 19:40:38 +02:00
|
|
|
|
Assert.Equal("Invalid organization.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task RemoveOrganization_ProviderOrganizationBelongsToWrongProvider_Throws(Provider provider,
|
|
|
|
|
ProviderOrganization providerOrganization, User user, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
sutProvider.GetDependency<IProviderOrganizationRepository>().GetByIdAsync(providerOrganization.Id)
|
|
|
|
|
.Returns(providerOrganization);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
2021-08-05 14:50:41 +02:00
|
|
|
|
() => sutProvider.Sut.RemoveOrganizationAsync(provider.Id, providerOrganization.Id, user.Id));
|
2021-07-21 19:40:38 +02:00
|
|
|
|
Assert.Equal("Invalid organization.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task RemoveOrganization_HasNoOwners_Throws(Provider provider,
|
|
|
|
|
ProviderOrganization providerOrganization, User user, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
providerOrganization.ProviderId = provider.Id;
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
sutProvider.GetDependency<IProviderOrganizationRepository>().GetByIdAsync(providerOrganization.Id)
|
|
|
|
|
.Returns(providerOrganization);
|
2021-08-05 14:50:41 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationService>().HasConfirmedOwnersExceptAsync(default, default, default)
|
2021-07-21 19:40:38 +02:00
|
|
|
|
.ReturnsForAnyArgs(false);
|
|
|
|
|
|
|
|
|
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
2021-08-05 14:50:41 +02:00
|
|
|
|
() => sutProvider.Sut.RemoveOrganizationAsync(provider.Id, providerOrganization.Id, user.Id));
|
2021-07-21 19:40:38 +02:00
|
|
|
|
Assert.Equal("Organization needs to have at least one confirmed owner.", exception.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, CustomAutoData(typeof(SutProviderCustomization))]
|
|
|
|
|
public async Task RemoveOrganization_Success(Provider provider,
|
|
|
|
|
ProviderOrganization providerOrganization, User user, SutProvider<ProviderService> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
providerOrganization.ProviderId = provider.Id;
|
|
|
|
|
sutProvider.GetDependency<IProviderRepository>().GetByIdAsync(provider.Id).Returns(provider);
|
|
|
|
|
var providerOrganizationRepository = sutProvider.GetDependency<IProviderOrganizationRepository>();
|
|
|
|
|
providerOrganizationRepository.GetByIdAsync(providerOrganization.Id).Returns(providerOrganization);
|
2021-08-05 14:50:41 +02:00
|
|
|
|
sutProvider.GetDependency<IOrganizationService>().HasConfirmedOwnersExceptAsync(default, default, default)
|
2021-07-21 19:40:38 +02:00
|
|
|
|
.ReturnsForAnyArgs(true);
|
|
|
|
|
|
2021-08-05 14:50:41 +02:00
|
|
|
|
await sutProvider.Sut.RemoveOrganizationAsync(provider.Id, providerOrganization.Id, user.Id);
|
2021-07-21 19:40:38 +02:00
|
|
|
|
await providerOrganizationRepository.Received().DeleteAsync(providerOrganization);
|
|
|
|
|
await sutProvider.GetDependency<IEventService>().Received()
|
|
|
|
|
.LogProviderOrganizationEventAsync(providerOrganization, EventType.ProviderOrganization_Removed);
|
|
|
|
|
}
|
2021-06-03 18:58:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|