Skip to content

Commit 6155083

Browse files
committed
Fixed tests broken due to removal of authentication.
Also, test regarding PowersService had not been changed to use IRepositoryFactory...
1 parent 71ccee2 commit 6155083

10 files changed

Lines changed: 188 additions & 71 deletions

File tree

Source/Backend/TrinityContinuum.Server/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
builder.Services.AddSerilog();
2626
builder.Services.Configure<ApplicationSettings>(builder.Configuration.GetSection(ApplicationSettings.SectionName));
2727

28-
28+
/*
2929
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
3030
3131
// 1. Configure Entity Framework Core with SQLite
@@ -62,7 +62,7 @@
6262
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
6363
};
6464
});
65-
65+
*/
6666
builder.Services.AddSingleton<ApiKeyAuthorizationFilter>();
6767
builder.Services.AddSingleton<IApiKeyValidator, ApiKeyValidator>();
6868

Source/Backend/TrinityContinuum.Server/appsettings.Development.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44
"Default": "Information",
55
"Microsoft.AspNetCore": "Warning"
66
}
7+
},
8+
9+
"Application": {
10+
"DataFolder": "data",
11+
"ApiKey": "your-api-key-here"
712
}
813
}

Tests/TrinityContinuum.ApiTests/AuthControllerTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*
12
using System.Net.Http.Json;
23
using FluentAssertions;
34
using TrinityContinuum.ApiTests.Infrastructure;
@@ -10,7 +11,7 @@ public class AuthControllerTests(WebAppFactory factory) : IClassFixture<WebAppFa
1011
{
1112
private readonly WebAppFactory _factory = factory ?? throw new ArgumentNullException(nameof(factory));
1213
13-
[Fact]
14+
[Fact(Skip = "Authentication has been removed for now")]
1415
public async Task RegisterNewUser_And_Login_Success_Test()
1516
{
1617
// Arrange
@@ -37,4 +38,6 @@ public async Task RegisterNewUser_And_Login_Success_Test()
3738
}
3839
}
3940
40-
public record TokenResponse(string Token);
41+
public record TokenResponse(string Token);
42+
43+
*/

Tests/TrinityContinuum.ApiTests/BasicControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task Basic_Url_Success_TestsAsync(string url)
2525
{ "data/characters/1.json", new(CharacterData.OneJson) },
2626
{ "data/characters/2.json", new(CharacterData.TwoJson) },
2727
{ "data/characters/3.json", new(CharacterData.ThreeJson) },
28-
{ "data/psi-powers.json", new("") },
28+
{ "data/psi-powers.json", new(PsiPowerData.PsiPowersJson) },
2929
};
3030

3131
var client = _factory.WithWebHostBuilder(_ => {}, files, true)

Tests/TrinityContinuum.ApiTests/CharacterControllerTests.cs

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,51 +51,53 @@ public async Task ListCharacters_Success_Test()
5151
.And.HaveCount(3);
5252
}
5353

54-
[Fact]
55-
public async Task ListCharacters_Success_With_Authorization_Test()
56-
{
57-
// Arrange
58-
var files = new Dictionary<string, MockFileData>
59-
{
60-
{ "data/characters/1.json", new(CharacterData.OneJson) },
61-
{ "data/characters/2.json", new(CharacterData.TwoJson) },
62-
{ "data/characters/3.json", new(CharacterData.ThreeJson) },
63-
};
64-
var factoryt = _factory.WithWebHostBuilder(_ => { }, files);
65-
var client = factoryt.CreateClient();
66-
67-
// Register a new user
68-
var response = await client.PostAsJsonAsync("/api/auth/register", new RegisterUserDto("test@login.org", "TestP@ssw0rd"));
69-
response.EnsureSuccessStatusCode();
70-
71-
// Login with the new user
72-
response = await client.PostAsJsonAsync("/api/auth/login", new LoginUserDto("test@login.org", "TestP@ssw0rd"));
73-
response.EnsureSuccessStatusCode();
74-
75-
// Act
76-
var token = await response.Content.ReadFromJsonAsync<TokenResponse>();
77-
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Token);
78-
79-
response = await client.GetAsync("/api/character/list");
80-
81-
// Assert
82-
response.EnsureSuccessStatusCode();
83-
}
84-
85-
[Fact]
86-
public async Task ListCharacters_Anonymouos_Fails_Test()
87-
{
88-
// Arrange
89-
var client = _factory.WithWebHostBuilder(_ => { })
90-
.CreateClient();
91-
92-
// Act
93-
var response = await client.GetAsync("/api/character/list");
94-
95-
// Assert
96-
response.Should().NotBeNull();
97-
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
98-
}
54+
/*
55+
[Fact(Skip = "Authentication has been removed for now")]
56+
public async Task ListCharacters_Success_With_Authorization_Test()
57+
{
58+
// Arrange
59+
var files = new Dictionary<string, MockFileData>
60+
{
61+
{ "data/characters/1.json", new(CharacterData.OneJson) },
62+
{ "data/characters/2.json", new(CharacterData.TwoJson) },
63+
{ "data/characters/3.json", new(CharacterData.ThreeJson) },
64+
};
65+
var factoryt = _factory.WithWebHostBuilder(_ => { }, files);
66+
var client = factoryt.CreateClient();
67+
68+
// Register a new user
69+
var response = await client.PostAsJsonAsync("/api/auth/register", new RegisterUserDto("test@login.org", "TestP@ssw0rd"));
70+
response.EnsureSuccessStatusCode();
71+
72+
// Login with the new user
73+
response = await client.PostAsJsonAsync("/api/auth/login", new LoginUserDto("test@login.org", "TestP@ssw0rd"));
74+
response.EnsureSuccessStatusCode();
75+
76+
// Act
77+
var token = await response.Content.ReadFromJsonAsync<TokenResponse>();
78+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Token);
79+
80+
response = await client.GetAsync("/api/character/list");
81+
82+
// Assert
83+
response.EnsureSuccessStatusCode();
84+
}
85+
86+
[Fact(Skip = "Authentication has been removed for now")]
87+
public async Task ListCharacters_Anonymouos_Fails_Test()
88+
{
89+
// Arrange
90+
var client = _factory.WithWebHostBuilder(_ => { })
91+
.CreateClient();
92+
93+
// Act
94+
var response = await client.GetAsync("/api/character/list");
95+
96+
// Assert
97+
response.Should().NotBeNull();
98+
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
99+
}
100+
*/
99101

100102

101103
[Fact]

Tests/TrinityContinuum.ApiTests/Infrastructure/WebAppFactory.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO.Abstractions;
22
using System.IO.Abstractions.TestingHelpers;
3+
using System.Runtime;
34
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.AspNetCore.Mvc.Testing;
@@ -40,20 +41,11 @@ async Task IAsyncLifetime.DisposeAsync()
4041
bool allowAnonymous = false)
4142
{
4243
_files = files;
43-
_allowAnonymous = allowAnonymous;
44+
_allowAnonymous = true; // allowAnonymous;
4445

4546
return base.WithWebHostBuilder(configuration);
4647
}
4748

48-
public void RemoveServiceDescriptor<TService>(IServiceCollection services)
49-
{
50-
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(TService));
51-
if (descriptor != null)
52-
{
53-
services.Remove(descriptor);
54-
}
55-
}
56-
5749
protected override void ConfigureWebHost(IWebHostBuilder builder)
5850
{
5951
builder.ConfigureServices(services =>
@@ -66,14 +58,14 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
6658
options.UseSqlite(_connection);
6759
});
6860

69-
if(_allowAnonymous)
70-
{
71-
// Remove the default authorization policy provider.
72-
RemoveServiceDescriptor<IAuthorizationPolicyProvider>(services);
61+
//if(_allowAnonymous)
62+
//{
63+
// // Remove the default authorization policy provider.
64+
// RemoveServiceDescriptor<IAuthorizationPolicyProvider>(services);
7365

74-
// Add our fake provider which allows anonymous access to all policies.
75-
services.AddSingleton<IAuthorizationPolicyProvider, FakePolicyProvider>();
76-
}
66+
// // Add our fake provider which allows anonymous access to all policies.
67+
// services.AddSingleton<IAuthorizationPolicyProvider, FakePolicyProvider>();
68+
//}
7769

7870
// Build a temporary service provider to create the database
7971
var sp = services.BuildServiceProvider();
@@ -85,12 +77,27 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
8577
var fs = new MockFileSystem(_files, scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>().ContentRootPath);
8678
services.AddSingleton<IFileSystem>(fs);
8779

88-
IOptions<ApplicationSettings> settings = Substitute.For<IOptions<ApplicationSettings>>();
89-
settings.Value.Returns(new ApplicationSettings { DataFolder = "data" });
90-
services.AddSingleton<IOptions<ApplicationSettings>>(settings);
80+
//IOptions<ApplicationSettings> settings = Substitute.For<IOptions<ApplicationSettings>>();
81+
//settings.Value.Returns(new ApplicationSettings { DataFolder = "data", ApiKey = "your-api-key-here" });
82+
//services.AddSingleton<IOptions<ApplicationSettings>>(settings);
9183

9284
});
9385

9486
builder.UseEnvironment("Development");
9587
}
88+
89+
protected override void ConfigureClient(HttpClient client)
90+
{
91+
var settings = Services.GetRequiredService<IOptions<ApplicationSettings>>().Value;
92+
client.DefaultRequestHeaders.Add("X-API-Key", settings.ApiKey);
93+
}
94+
95+
private void RemoveServiceDescriptor<TService>(IServiceCollection services)
96+
{
97+
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(TService));
98+
if (descriptor != null)
99+
{
100+
services.Remove(descriptor);
101+
}
102+
}
96103
}

Tests/TrinityContinuum.ApiTests/TrinityContinuum.ApiTests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
<Compile Include="..\AssertionEngineInitializer.cs" Link="AssertionEngineInitializer.cs" />
1212
</ItemGroup>
1313

14+
<ItemGroup>
15+
<Content Include="appsettings.Development.json">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
18+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
19+
</Content>
20+
</ItemGroup>
21+
1422
<ItemGroup>
1523
<PackageReference Include="coverlet.collector" Version="6.0.4">
1624
<PrivateAssets>all</PrivateAssets>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace TrinityContinuum.TestData;
8+
public static class PsiPowerData
9+
{
10+
public static string PsiPowersJson => """
11+
[
12+
{
13+
"Aptitude": "Biokinesis",
14+
"Mode": "Adaptation",
15+
"Name": "Resist",
16+
"Description": "The psion mitigates sources of harm coming from within her body.",
17+
"System": "Success reduces the damage rating of hazards such as toxins, irritants, and diseases within the psion's body by an amount equal to her Mode dots. If the power reduces the damage rating to zero the character can either internally neutralize the substance, or expel it intact from her body. If the damage rating of a hazard is not reduced to zero, it continues to damage the psion at the reduced rate.",
18+
"Dots": 1,
19+
"Cost": "0",
20+
"Source": "ÆON",
21+
"Page": 211
22+
},
23+
{
24+
"Aptitude": "Biokinesis",
25+
"Mode": "Adaptation",
26+
"Name": "Acclimatize",
27+
"Description": "A psion can adjust his physiology to best suit the environment.",
28+
"System": "The psion suffers no penalties from increased or decreased gravity that is less than extreme gravity and can also ignore all environmental indirect damage with the Continuous (hour) tag as well as all environmental indirect damage with the Continuous (minute) tag that does not also possess the Aggravated tag. The psion can reduce the damage rating of other forms of environmental indirect damage by one per success spent.",
29+
"Dots": 2,
30+
"Cost": "0",
31+
"Source": "ÆON",
32+
"Page": 211
33+
},
34+
{
35+
"Aptitude": "Biokinesis",
36+
"Mode": "Adaptation",
37+
"Name": "Metabolic Control",
38+
"Description": "The biokinetic controls the speed of her body's processes. While this can reduce harmful conditions, it also works to repair damage.",
39+
"System": "If successful, the character changes the rate of one aspect of her physiology. For functions that take a defined time, such as healing, holding her breath, or going without water, she multiplies (or divides) the time by Mode dots + successes. For processes not measured in time, such as Initiative, the character either adds (or subtracts) Mode dots/2 to her total.",
40+
"Dots": 3,
41+
"Cost": "1",
42+
"Source": "ÆON",
43+
"Page": 211
44+
},
45+
{
46+
"Aptitude": "Biokinesis",
47+
"Mode": "Adaptation",
48+
"Name": "Resist",
49+
"Description": "The psion mitigates sources of harm coming from within her body.",
50+
"System": "Success reduces the damage rating of hazards such as toxins, irritants, and diseases within the psion's body by an amount equal to her Mode dots. If the power reduces the damage rating to zero the character can either internally neutralize the substance, or expel it intact from her body. If the damage rating of a hazard is not reduced to zero, it continues to damage the psion at the reduced rate.",
51+
"Dots": 1,
52+
"Cost": "0",
53+
"Source": "ÆON",
54+
"Page": 211
55+
},
56+
{
57+
"Aptitude": "Biokinesis",
58+
"Mode": "Adaptation",
59+
"Name": "Acclimatize",
60+
"Description": "A psion can adjust his physiology to best suit the environment.",
61+
"System": "The psion suffers no penalties from increased or decreased gravity that is less than extreme gravity and can also ignore all environmental indirect damage with the Continuous (hour) tag as well as all environmental indirect damage with the Continuous (minute) tag that does not also possess the Aggravated tag. The psion can reduce the damage rating of other forms of environmental indirect damage by one per success spent.",
62+
"Dots": 2,
63+
"Cost": "0",
64+
"Source": "ÆON",
65+
"Page": 211
66+
},
67+
{
68+
"Aptitude": "Biokinesis",
69+
"Mode": "Adaptation",
70+
"Name": "Metabolic Control",
71+
"Description": "The biokinetic controls the speed of her body's processes. While this can reduce harmful conditions, it also works to repair damage.",
72+
"System": "If successful, the character changes the rate of one aspect of her physiology. For functions that take a defined time, such as healing, holding her breath, or going without water, she multiplies (or divides) the time by Mode dots + successes. For processes not measured in time, such as Initiative, the character either adds (or subtracts) Mode dots/2 to her total.",
73+
"Dots": 3,
74+
"Cost": "1",
75+
"Source": "ÆON",
76+
"Page": 211
77+
}
78+
]
79+
""";
80+
81+
}

Tests/TrinityContinuum.Tests/Services/PowersServiceTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class PowersServiceTests
1111
{
1212
private readonly PowersService _powersService;
1313
private readonly IDataProviderService _dataProviderService;
14+
private readonly IRepositoryFactory _factory;
1415

1516
public PowersServiceTests()
1617
{
@@ -22,8 +23,10 @@ public PowersServiceTests()
2223
.Returns(Task.FromResult(TestData));
2324
var repository = new SingleFileRepository<PsiPower>(_dataProviderService);
2425
repository.Initialize(default).GetAwaiter().GetResult();
26+
_factory = Substitute.For<IRepositoryFactory>();
27+
_factory.CreateSingleFileRepository<PsiPower>().Returns(repository);
2528

26-
_powersService = new PowersService(repository);
29+
_powersService = new PowersService(_factory);
2730

2831
}
2932

0 commit comments

Comments
 (0)