-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathOrganizationDeleteCommandTests.cs
More file actions
205 lines (170 loc) · 8.22 KB
/
OrganizationDeleteCommandTests.cs
File metadata and controls
205 lines (170 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System.Text.Json;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.OrganizationFeatures.Organizations;
using Bit.Core.Auth.Entities;
using Bit.Core.Auth.Enums;
using Bit.Core.Auth.Models.Data;
using Bit.Core.Auth.Repositories;
using Bit.Core.Billing;
using Bit.Core.Billing.Services;
using Bit.Core.Exceptions;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Test.AutoFixture.OrganizationFixtures;
using Bit.Core.Tools.Entities;
using Bit.Core.Tools.Enums;
using Bit.Core.Tools.Models.Data;
using Bit.Core.Tools.Repositories;
using Bit.Core.Tools.Services;
using Bit.Core.Vault.Services;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.Organizations;
[SutProviderCustomize]
public class OrganizationDeleteCommandTests
{
[Theory, PaidOrganizationCustomize, BitAutoData]
public async Task Delete_Success(Organization organization,
SutProvider<OrganizationDeleteCommand> sutProvider)
{
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
var applicationCacheService = sutProvider.GetDependency<IApplicationCacheService>();
var cipherService = sutProvider.GetDependency<ICipherService>();
await sutProvider.Sut.DeleteAsync(organization);
await cipherService.Received(1).DeleteAttachmentsForOrganizationAsync(organization.Id);
await organizationRepository.Received(1).DeleteAsync(organization);
await applicationCacheService.Received(1).DeleteOrganizationAbilityAsync(organization.Id);
}
[Theory, PaidOrganizationCustomize, BitAutoData]
public async Task Delete_Fails_KeyConnector(Organization organization, SutProvider<OrganizationDeleteCommand> sutProvider,
SsoConfig ssoConfig)
{
ssoConfig.Enabled = true;
ssoConfig.SetData(new SsoConfigurationData { MemberDecryptionType = MemberDecryptionType.KeyConnector });
var ssoConfigRepository = sutProvider.GetDependency<ISsoConfigRepository>();
var organizationRepository = sutProvider.GetDependency<IOrganizationRepository>();
var applicationCacheService = sutProvider.GetDependency<IApplicationCacheService>();
ssoConfigRepository.GetByOrganizationIdAsync(organization.Id).Returns(ssoConfig);
var exception = await Assert.ThrowsAsync<BadRequestException>(
() => sutProvider.Sut.DeleteAsync(organization));
Assert.Contains("You cannot delete an Organization that is using Key Connector.", exception.Message);
await organizationRepository.DidNotReceiveWithAnyArgs().DeleteAsync(default);
await applicationCacheService.DidNotReceiveWithAnyArgs().DeleteOrganizationAbilityAsync(default);
}
[Theory, PaidOrganizationCustomize, BitAutoData]
public async Task Delete_WhenFlagEnabled_CallsSubscriberService(
Organization organization,
SutProvider<OrganizationDeleteCommand> sutProvider)
{
organization.GatewaySubscriptionId = "sub_123";
organization.ExpirationDate = DateTime.UtcNow.AddDays(10);
sutProvider.GetDependency<IFeatureService>()
.IsEnabled(FeatureFlagKeys.PM32645_DeferPriceMigrationToRenewal)
.Returns(true);
await sutProvider.Sut.DeleteAsync(organization);
await sutProvider.GetDependency<ISubscriberService>()
.Received(1)
.CancelSubscription(organization, cancelImmediately: false);
await sutProvider.GetDependency<IStripePaymentService>()
.DidNotReceiveWithAnyArgs()
.CancelSubscriptionAsync(default, default);
}
[Theory, PaidOrganizationCustomize, BitAutoData]
public async Task Delete_WhenFlagDisabled_CallsLegacyPaymentService(
Organization organization,
SutProvider<OrganizationDeleteCommand> sutProvider)
{
organization.GatewaySubscriptionId = "sub_123";
organization.ExpirationDate = DateTime.UtcNow.AddDays(10);
sutProvider.GetDependency<IFeatureService>()
.IsEnabled(FeatureFlagKeys.PM32645_DeferPriceMigrationToRenewal)
.Returns(false);
await sutProvider.Sut.DeleteAsync(organization);
await sutProvider.GetDependency<IStripePaymentService>()
.Received(1)
.CancelSubscriptionAsync(organization, true);
await sutProvider.GetDependency<ISubscriberService>()
.DidNotReceiveWithAnyArgs()
.CancelSubscription(default, default, default);
}
[Theory, PaidOrganizationCustomize, BitAutoData]
public async Task Delete_WhenFlagEnabled_HandlesBillingException(
Organization organization,
SutProvider<OrganizationDeleteCommand> sutProvider)
{
organization.GatewaySubscriptionId = "sub_123";
organization.ExpirationDate = DateTime.UtcNow.AddDays(10);
sutProvider.GetDependency<IFeatureService>()
.IsEnabled(FeatureFlagKeys.PM32645_DeferPriceMigrationToRenewal)
.Returns(true);
var billingException = new BillingException();
sutProvider.GetDependency<ISubscriberService>()
.CancelSubscription(organization, cancelImmediately: false)
.ThrowsAsync(billingException);
await sutProvider.Sut.DeleteAsync(organization);
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).DeleteAsync(organization);
}
[Theory, PaidOrganizationCustomize, BitAutoData]
public async Task Delete_WhenFlagDisabled_HandlesBillingException(
Organization organization,
SutProvider<OrganizationDeleteCommand> sutProvider)
{
organization.GatewaySubscriptionId = "sub_123";
organization.ExpirationDate = DateTime.UtcNow.AddDays(10);
sutProvider.GetDependency<IFeatureService>()
.IsEnabled(FeatureFlagKeys.PM32645_DeferPriceMigrationToRenewal)
.Returns(false);
var billingException = new BillingException();
sutProvider.GetDependency<IStripePaymentService>()
.CancelSubscriptionAsync(organization, Arg.Any<bool>())
.ThrowsAsync(billingException);
await sutProvider.Sut.DeleteAsync(organization);
await sutProvider.GetDependency<IOrganizationRepository>().Received(1).DeleteAsync(organization);
}
[Theory, PaidOrganizationCustomize, BitAutoData]
public async Task Delete_WithFileSends_DeletesFilesBeforeDbRecords(
Organization organization,
SutProvider<OrganizationDeleteCommand> sutProvider)
{
// Ensuring that the file is deleted first avoids the following situation:
// 1. DB row is deleted successfully
// 2. File blob fails to delete
// 3. File blob still exists but with no parent Send
var fileData = new SendFileData { Id = "file1", FileName = "test.txt", Size = 100 };
var fileSend = new Send
{
Id = Guid.NewGuid(),
OrganizationId = organization.Id,
Type = SendType.File,
Data = JsonSerializer.Serialize(fileData)
};
var textSend = new Send
{
Id = Guid.NewGuid(),
OrganizationId = organization.Id,
Type = SendType.Text,
Data = "{}"
};
sutProvider.GetDependency<ISendRepository>()
.GetManyByOrganizationIdAsync(organization.Id)
.Returns(new List<Send> { fileSend, textSend });
var callOrder = new List<string>();
sutProvider.GetDependency<ISendFileStorageService>()
.DeleteFileAsync(fileSend, fileData.Id)
.Returns(Task.CompletedTask)
.AndDoes(_ => callOrder.Add("file"));
sutProvider.GetDependency<IOrganizationRepository>()
.DeleteAsync(organization)
.Returns(Task.CompletedTask)
.AndDoes(_ => callOrder.Add("db"));
await sutProvider.Sut.DeleteAsync(organization);
await sutProvider.GetDependency<ISendFileStorageService>()
.Received(1).DeleteFileAsync(fileSend, fileData.Id);
await sutProvider.GetDependency<IOrganizationRepository>()
.Received(1).DeleteAsync(organization);
Assert.Equal(new[] { "file", "db" }, callOrder);
}
}