-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLicensingServicesTests.cs
More file actions
146 lines (118 loc) · 5.76 KB
/
LicensingServicesTests.cs
File metadata and controls
146 lines (118 loc) · 5.76 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
using Code311.Licensing.DependencyInjection;
using Code311.Licensing.Diagnostics;
using Code311.Licensing.Models;
using Code311.Licensing.Validation;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Code311.Tests.Licensing;
public sealed class LicensingServicesTests
{
[Fact]
public async Task Validator_ShouldReturnValid_ForActiveLicense()
{
var validator = new DefaultLicenseValidator();
var license = BuildLicense(expiresUtc: DateTimeOffset.UtcNow.AddDays(30));
var result = validator.Validate(license, DateTimeOffset.UtcNow, new LicensingOptions());
Assert.True(result.IsValid);
Assert.Equal(LicenseStatusLevel.Valid, result.OverallLevel);
}
[Fact]
public void Validator_ShouldReturnInvalid_ForExpiredLicense()
{
var validator = new DefaultLicenseValidator();
var license = BuildLicense(expiresUtc: DateTimeOffset.UtcNow.AddMinutes(-1));
var result = validator.Validate(license, DateTimeOffset.UtcNow, new LicensingOptions());
Assert.False(result.IsValid);
Assert.Equal(LicenseStatusLevel.Error, result.OverallLevel);
}
[Fact]
public async Task StartupValidator_ShouldThrow_WhenStartupRequiresValidLicense()
{
var services = new ServiceCollection()
.AddCode311Licensing(options => options.RequireValidLicenseAtStartup = true)
.AddCode311InMemoryLicenseSource(null)
.BuildServiceProvider();
var startup = services.GetRequiredService<IStartupLicenseValidator>();
await Assert.ThrowsAsync<InvalidOperationException>(() => startup.ValidateAtStartupAsync());
}
[Fact]
public async Task StartupValidator_ShouldReportStatus_WhenValidationRuns()
{
var services = new ServiceCollection()
.AddCode311Licensing(options => options.RequireValidLicenseAtStartup = false)
.AddCode311InMemoryLicenseSource(BuildLicense(expiresUtc: DateTimeOffset.UtcNow.AddDays(20)))
.BuildServiceProvider();
var startup = services.GetRequiredService<IStartupLicenseValidator>();
var reporter = services.GetRequiredService<ILicensingStatusReporter>();
var result = await startup.ValidateAtStartupAsync();
Assert.True(result.IsValid);
Assert.NotNull(reporter.Current);
Assert.Equal(LicenseCheckStage.Startup, reporter.Current!.Stage);
}
[Fact]
public async Task FeatureGate_ShouldDeny_WhenFeatureMissing()
{
var services = new ServiceCollection()
.AddCode311Licensing(options => options.RequireValidLicenseAtStartup = false)
.AddCode311InMemoryLicenseSource(BuildLicense(features: new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "dashboard.basic" }))
.BuildServiceProvider();
var gate = services.GetRequiredService<ILicenseFeatureGate>();
var result = await gate.CheckFeatureAsync("dashboard.advanced");
Assert.False(result.IsAllowed);
Assert.Equal(LicenseStatusLevel.Warning, result.Level);
}
[Fact]
public async Task FeatureGate_ShouldAllow_WhenFeaturePresent()
{
var services = new ServiceCollection()
.AddCode311Licensing(options => options.RequireValidLicenseAtStartup = false)
.AddCode311InMemoryLicenseSource(BuildLicense(features: new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "dashboard.advanced" }))
.BuildServiceProvider();
var gate = services.GetRequiredService<ILicenseFeatureGate>();
var result = await gate.CheckFeatureAsync("dashboard.advanced");
Assert.True(result.IsAllowed);
Assert.Equal("dashboard.advanced", result.Feature);
}
[Fact]
public void AddCode311Licensing_ShouldRegisterCoreServices()
{
var services = new ServiceCollection();
services.AddCode311Licensing();
using var provider = services.BuildServiceProvider();
Assert.NotNull(provider.GetService<IStartupLicenseValidator>());
Assert.NotNull(provider.GetService<ILicenseFeatureGate>());
Assert.NotNull(provider.GetService<ILicenseValidator>());
Assert.NotNull(provider.GetService<ILicensingStatusReporter>());
}
[Fact]
public void UiAndTablerPackages_ShouldNotReferenceCode311Licensing()
{
var forbiddenProjectFiles = new[]
{
"src/Code311.Ui.Abstractions/Code311.Ui.Abstractions.csproj",
"src/Code311.Ui.Core/Code311.Ui.Core.csproj",
"src/Code311.Tabler.Core/Code311.Tabler.Core.csproj",
"src/Code311.Tabler.Components/Code311.Tabler.Components.csproj",
"src/Code311.Tabler.Dashboard/Code311.Tabler.Dashboard.csproj",
"src/Code311.Tabler.Widgets.DataTables/Code311.Tabler.Widgets.DataTables.csproj",
"src/Code311.Tabler.Widgets.Calendar/Code311.Tabler.Widgets.Calendar.csproj",
"src/Code311.Tabler.Widgets.Charts/Code311.Tabler.Widgets.Charts.csproj"
};
foreach (var relativePath in forbiddenProjectFiles)
{
var full = Path.Combine(AppContext.BaseDirectory, "../../../../", relativePath);
var xml = File.ReadAllText(full);
Assert.DoesNotContain("Code311.Licensing", xml, StringComparison.Ordinal);
}
}
private static Code311License BuildLicense(DateTimeOffset? expiresUtc = null, DateTimeOffset? notBeforeUtc = null, IReadOnlySet<string>? features = null)
=> new()
{
LicenseId = "lic-001",
CustomerName = "Code311 Test",
Plan = "pro",
NotBeforeUtc = notBeforeUtc,
ExpiresUtc = expiresUtc ?? DateTimeOffset.UtcNow.AddDays(30),
Features = features ?? new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "dashboard.basic" }
};
}