-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompanySettingsControllerWithSystemLicense.cs
More file actions
219 lines (170 loc) · 9.18 KB
/
Copy pathCompanySettingsControllerWithSystemLicense.cs
File metadata and controls
219 lines (170 loc) · 9.18 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System.Net;
using Fossa.API.FunctionalTests.Seed;
using Fossa.API.Web;
using Fossa.Bridge.Models.ApiModels;
using Fossa.Bridge.Services;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
namespace Fossa.API.FunctionalTests.ControllerApis;
public class CompanySettingsControllerWithSystemLicense : IClassFixture<CustomWebApplicationFactory<DefaultWebModule>>, IAsyncLifetime
{
private readonly CustomWebApplicationFactory<DefaultWebModule> _factory;
public CompanySettingsControllerWithSystemLicense(
CustomWebApplicationFactory<DefaultWebModule> factory)
{
_factory = factory ?? throw new ArgumentNullException(nameof(factory));
}
[Fact]
public async Task CreateCompanySettingsWithAdministratorAccessTokenAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JA1ZJAWF27S0J8Z2VJE7673Y.Tenant3.ADMIN1");
const string colorSchemeId = "new-theme";
await settingsClient.CreateCompanySettingsAsync(new CompanySettingsModificationModel(colorSchemeId), TestContext.Current.CancellationToken);
var responseModel = (await settingsClient.GetCompanySettingsAsync(TestContext.Current.CancellationToken)).Unwrap();
responseModel.ShouldNotBeNull();
responseModel.Id.ShouldBePositive();
responseModel.CompanyId.ShouldBePositive();
responseModel.ColorSchemeId.ShouldBe(colorSchemeId);
}
[Fact]
public async Task CreateCompanySettingsWithInvalidColorSchemeIdAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JA1ZJAWF27S0J8Z2VJE7673Y.Tenant4.ADMIN1");
// Test various invalid colorSchemeId formats
var invalidColorSchemeIds = new[]
{
"ab", // Too short (less than 3 characters)
"ABC", // Contains uppercase letters
"test123", // Contains numbers
"test--theme", // Multiple consecutive hyphens
"-test", // Starts with hyphen
"test-", // Ends with hyphen
"test theme", // Contains space
"test_theme", // Contains underscore
};
foreach (var invalidColorSchemeId in invalidColorSchemeIds)
{
(await settingsClient.CreateCompanySettingsAsync(
new CompanySettingsModificationModel(invalidColorSchemeId), TestContext.Current.CancellationToken)).ShouldFailWith(HttpStatusCode.UnprocessableEntity);
}
}
[Fact]
public async Task CreateCompanySettingsWithValidColorSchemeIdFormatsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
// Test various valid colorSchemeId formats
var validTestCases = new[]
{
new { Tenant = "Tenant45442385", ColorSchemeId = "abc" }, // Minimum length
new { Tenant = "Tenant45442386", ColorSchemeId = "theme" }, // Simple lowercase
new { Tenant = "Tenant45442387", ColorSchemeId = "dark-theme" }, // With hyphen in middle
new { Tenant = "Tenant45442388", ColorSchemeId = "verylongthemename" }, // Long name
};
foreach (var testCase in validTestCases)
{
accessTokenContext.SetAccessToken($"01JA1ZJAWF27S0J8Z2VJE7673Y.{testCase.Tenant}.ADMIN1");
const string companyName = "Company-1412593541";
var transport = scope.ServiceProvider.GetRequiredService<IHttpTransport>();
await transport.PostAsync<CompanyModificationModel>(
"/api/1.0/Company",
EndpointSecurity.RequireToken,
new CompanyModificationModel(companyName, "US"),
TestContext.Current.CancellationToken);
await settingsClient.CreateCompanySettingsAsync(
new CompanySettingsModificationModel(testCase.ColorSchemeId), TestContext.Current.CancellationToken);
var responseModel = (await settingsClient.GetCompanySettingsAsync(TestContext.Current.CancellationToken)).Unwrap();
responseModel.ShouldNotBeNull();
responseModel.ColorSchemeId.ShouldBe(testCase.ColorSchemeId,
$"ColorSchemeId '{testCase.ColorSchemeId}' should be valid");
}
}
[Fact]
public async Task CreateDuplicateCompanySettingsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JB0QS2K6SA4KYD8S920W7DMG.Tenant1.ADMIN1");
// Try to create company settings for a company that already has settings
(await settingsClient.CreateCompanySettingsAsync(
new CompanySettingsModificationModel("duplicate-theme"), TestContext.Current.CancellationToken)).ShouldFailWith(HttpStatusCode.Conflict);
}
[Fact]
public async Task DeleteExistingCompanySettingsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JB0RAH24ZJBA53AJF5F5MMZX.Tenant2.ADMIN1");
await settingsClient.DeleteCompanySettingsAsync(TestContext.Current.CancellationToken);
(await settingsClient.GetCompanySettingsAsync(TestContext.Current.CancellationToken)).ShouldFailWith(HttpStatusCode.NotFound);
}
[Fact]
public async Task DeleteNonExistentCompanySettingsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JA1ZJAWF27S0J8Z2VJE7673Y.Tenant1000.ADMIN1");
(await settingsClient.DeleteCompanySettingsAsync(TestContext.Current.CancellationToken)).ShouldFailWith(HttpStatusCode.NotFound);
}
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
[Fact]
public async Task GetExistingCompanySettingsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JB0QS2K6SA4KYD8S920W7DMG.Tenant1.User1");
var responseModel = (await settingsClient.GetCompanySettingsAsync(TestContext.Current.CancellationToken)).Unwrap();
responseModel.ShouldNotBeNull();
responseModel.Id.ShouldBePositive();
responseModel.CompanyId.ShouldBePositive();
}
[Fact]
public async Task GetNonExistentCompanySettingsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JA1ZJAWF27S0J8Z2VJE7673Y.Tenant1000.User1");
(await settingsClient.GetCompanySettingsAsync(TestContext.Current.CancellationToken)).ShouldFailWith(HttpStatusCode.NotFound);
}
public async ValueTask InitializeAsync()
{
await _factory.SeedSystemLicenseAsync(TestContext.Current.CancellationToken).ConfigureAwait(false);
await _factory.SeedAllEntitiesAsync(TestContext.Current.CancellationToken).ConfigureAwait(false);
}
[Fact]
public async Task UpdateExistingCompanySettingsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JB0QS2K6SA4KYD8S920W7DMG.Tenant1.ADMIN1");
const string newColorSchemeId = "updated-theme";
await settingsClient.UpdateCompanySettingsAsync(new CompanySettingsModificationModel(newColorSchemeId), TestContext.Current.CancellationToken);
// Verify the update
var responseModel = (await settingsClient.GetCompanySettingsAsync(TestContext.Current.CancellationToken)).Unwrap();
responseModel.ShouldNotBeNull();
responseModel.ColorSchemeId.ShouldBe(newColorSchemeId);
}
[Fact]
public async Task UpdateNonExistentCompanySettingsAsync()
{
using var scope = _factory.Services.CreateScope();
var settingsClient = scope.ServiceProvider.GetRequiredService<IClients>().CompanySettingsClient;
var accessTokenContext = scope.ServiceProvider.GetRequiredService<IAccessTokenContext>();
accessTokenContext.SetAccessToken("01JA1ZJAWF27S0J8Z2VJE7673Y.Tenant1000.ADMIN1");
(await settingsClient.UpdateCompanySettingsAsync(
new CompanySettingsModificationModel("new-theme"), TestContext.Current.CancellationToken)).ShouldFailWith(HttpStatusCode.NotFound);
}
}