-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathProviderAbilityCacheTests.cs
More file actions
116 lines (98 loc) · 4.05 KB
/
ProviderAbilityCacheTests.cs
File metadata and controls
116 lines (98 loc) · 4.05 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
using System.Net;
using Bit.Api.AdminConsole.Models.Request.Providers;
using Bit.Api.IntegrationTest.Factories;
using Bit.Api.IntegrationTest.Helpers;
using Bit.Core;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Enums.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Repositories;
using Bit.Core.Services;
using NSubstitute;
using Xunit;
namespace Bit.Api.IntegrationTest.AdminConsole.Controllers;
public class ProviderAbilityCacheTests : IClassFixture<ApiApplicationFactory>, IAsyncLifetime
{
private readonly HttpClient _client;
private readonly ApiApplicationFactory _factory;
private readonly LoginHelper _loginHelper;
private Provider _provider = null!;
private string _providerAdminEmail = null!;
public ProviderAbilityCacheTests(ApiApplicationFactory factory)
{
_factory = factory;
_factory.SubstituteService<IFeatureService>(featureService =>
{
featureService
.IsEnabled(FeatureFlagKeys.ProviderAbilityExtendedCache)
.Returns(true);
});
_client = factory.CreateClient();
_loginHelper = new LoginHelper(_factory, _client);
}
public async Task InitializeAsync()
{
_providerAdminEmail = $"provider-cache-test-{Guid.NewGuid()}@example.com";
await _factory.LoginWithNewAccount(_providerAdminEmail);
var providerRepository = _factory.GetService<IProviderRepository>();
var userRepository = _factory.GetService<IUserRepository>();
var providerUserRepository = _factory.GetService<IProviderUserRepository>();
_provider = await providerRepository.CreateAsync(new Provider
{
Name = "Provider Cache Test",
BillingEmail = _providerAdminEmail,
Type = ProviderType.Reseller,
Status = ProviderStatusType.Created,
Enabled = true
});
var user = await userRepository.GetByEmailAsync(_providerAdminEmail);
await providerUserRepository.CreateAsync(new ProviderUser
{
ProviderId = _provider.Id,
UserId = user!.Id,
Type = ProviderUserType.ProviderAdmin,
Status = ProviderUserStatusType.Confirmed,
Key = Guid.NewGuid().ToString()
});
}
public Task DisposeAsync()
{
_client.Dispose();
return Task.CompletedTask;
}
[Fact]
public async Task Put_UpdatesProvider_CacheReflectsUpdatedValues()
{
// Arrange
await _loginHelper.LoginAsync(_providerAdminEmail);
var updateRequest = new ProviderUpdateRequestModel
{
Name = "Updated Provider Cache Test",
BillingEmail = _providerAdminEmail
};
// Act - update the provider via the HTTP endpoint
var response = await _client.PutAsJsonAsync($"/providers/{_provider.Id}", updateRequest);
// Assert - endpoint succeeded and cache was populated
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var cacheService = _factory.GetService<IApplicationCacheService>();
var ability = await cacheService.GetProviderAbilityAsync(_provider.Id);
Assert.NotNull(ability);
Assert.Equal(_provider.Id, ability.Id);
}
[Fact]
public async Task Delete_RemovesProvider_CacheReturnsNull()
{
// Arrange - populate the cache before deletion
var cacheService = _factory.GetService<IApplicationCacheService>();
await cacheService.UpsertProviderAbilityAsync(_provider);
var abilityBeforeDelete = await cacheService.GetProviderAbilityAsync(_provider.Id);
Assert.NotNull(abilityBeforeDelete);
// Act - delete the provider via the HTTP endpoint
await _loginHelper.LoginAsync(_providerAdminEmail);
var response = await _client.DeleteAsync($"/providers/{_provider.Id}");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var abilityAfterDelete = await cacheService.GetProviderAbilityAsync(_provider.Id);
Assert.Null(abilityAfterDelete);
}
}