Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Sep 3, 2023
1 parent aab3275 commit 7524d82
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 197 deletions.
2 changes: 1 addition & 1 deletion MongoWebApiStarterTemplate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>

<PackageVersion>7.10.3</PackageVersion>
<PackageVersion>7.11.0</PackageVersion>

<PackageType>Template</PackageType>
<PackageId>MongoWebApiStarter</PackageId>
Expand Down
12 changes: 5 additions & 7 deletions Template/Source/MongoWebApiStarter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1701;1702;CA2016;CS1591;RCS1090</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.SimpleEmailV2" Version="3.7.200.27" />
<PackageReference Include="AWSSDK.SimpleEmailV2" Version="3.7.201.5" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="FastEndpoints" Version="5.15.0" />
<PackageReference Include="FastEndpoints.Security" Version="5.15.0" />
<PackageReference Include="FastEndpoints.Swagger" Version="5.15.0" />
<PackageReference Include="FastEndpoints" Version="5.17.0" />
<PackageReference Include="FastEndpoints.Security" Version="5.17.0" />
<PackageReference Include="FastEndpoints.Swagger" Version="5.17.0" />
<PackageReference Include="Flurl" Version="3.0.7" />
<PackageReference Include="Flurl.Http" Version="3.2.4" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="MlkPwgen" Version="0.3.0" />
<PackageReference Include="MongoDB.Entities" Version="22.1.0" />
<PackageReference Include="NodaTime" Version="3.1.9" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.2" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Template/Source/[Features]/Account/Login/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override async Task HandleAsync(Request r, CancellationToken ct)
Response.Token.Value = JWTBearer.CreateToken(
signingKey: Settings.Value.Auth.SigningKey,
expireAt: expiryDate,
priviledges: u =>
privileges: u =>
{
u.Permissions.AddRange(permissions.AllCodes());
u[Claim.AccountID] = acc.ID!;
Expand Down
98 changes: 98 additions & 0 deletions Template/Tests/Account/AccountTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Get = Account.Get;
using Login = Account.Login;
using Save = Account.Save;
using Verify = Account.Verify;

namespace Tests.Account;

public class AccountTests : TestClass<Fixture>
{
public AccountTests(Fixture f, ITestOutputHelper o) : base(f, o) { }

[Fact, Priority(1)]
public async Task Account_Creation()
{
var req = Fixture.SaveRequest;

var (rsp, res) = await Fixture.Client.POSTAsync<Save.Endpoint, Save.Request, Save.Response>(req);

rsp!.IsSuccessStatusCode.Should().BeTrue();
res!.EmailSent.Should().BeTrue();
res.ID.Should().NotBeNullOrEmpty();

Fixture.AccountID = res.ID;

var acc = await DB.Find<Dom.Account>().OneAsync(res.ID);

acc!.Email.Should().Be(req.EmailAddress.ToLower());
acc.IsEmailVerified.Should().BeFalse();
}

[Fact, Priority(2)]
public async Task Account_Verification()
{
var accountID = Fixture.AccountID;

var code = (await DB
.Find<Dom.Account>()
.OneAsync(accountID))!
.EmailVerificationCode;

await Fixture.Client.POSTAsync<Verify.Endpoint, Verify.Request>(new()
{
ID = accountID!,
Code = code
});

var verified = await DB
.Find<Dom.Account, bool>()
.Match(a => a.ID == accountID)
.Project(a => a.IsEmailVerified)
.ExecuteSingleAsync();

verified.Should().BeTrue();
}

[Fact, Priority(3)]
public async Task Account_Login()
{
var (rsp, res) = await Fixture.Client.POSTAsync<Login.Endpoint, Login.Request, Login.Response>(new()
{
UserName = Fixture.SaveRequest.EmailAddress,
Password = Fixture.SaveRequest.Password
});

var acc = await DB.Find<Dom.Account>().OneAsync(Fixture.AccountID);

rsp!.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
res!.FullName.Should().Be($"{acc!.Title} {acc.FirstName} {acc.LastName}");
res.PermissionSet.Should().HaveCount(7);
res.Token.Value.Should().NotBeNull();

Fixture.JwtToken = res.Token.Value;
}

[Fact, Priority(4)]
public async Task Account_Retrieve()
{
Fixture.Client.DefaultRequestHeaders.Authorization = new("Bearer", Fixture.JwtToken);

var (_, res) = await Fixture.Client.GETAsync<Get.Endpoint, Get.Response>();

var acc = await DB.Find<Dom.Account>().OneAsync(res!.AccountID);

var match =
acc!.Email == res.EmailAddress &&
acc.Title == res.Title &&
acc.FirstName == res.FirstName &&
acc.LastName == res.LastName &&
acc.Address.Street == res.Street &&
acc.Address.City == res.City &&
acc.Address.State == res.State &&
acc.Address.ZipCode == res.ZipCode &&
acc.Address.CountryCode == res.CountryCode &&
acc.Mobile == res.Mobile;

match.Should().BeTrue();
}
}
22 changes: 22 additions & 0 deletions Template/Tests/Account/Fakes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Bogus;
using Save = Account.Save;

namespace Tests.Account;

static class Fakes
{
public static Save.Request SaveRequest(this Faker f) => new()
{
City = f.Address.City(),
CountryCode = f.Address.County(),
EmailAddress = f.Internet.Email(),
FirstName = f.Name.FirstName(),
LastName = f.Name.LastName(),
Mobile = f.Phone.PhoneNumber("##########"),
Password = f.Internet.Password(10),
State = f.Address.State(),
Street = f.Address.StreetName(),
Title = f.Name.Prefix(),
ZipCode = f.Address.ZipCode(),
};
}
21 changes: 21 additions & 0 deletions Template/Tests/Account/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Save = Account.Save;

namespace Tests.Account;

public class Fixture : TestFixture<Program>
{
public Fixture(IMessageSink s) : base(s) { }

internal Save.Request SaveRequest { get; private set; } = default!;
public string AccountID { get; set; } = "";
public string JwtToken { get; set; } = "";

protected override Task SetupAsync()
{
SaveRequest = Fake.SaveRequest();
return Task.CompletedTask;
}

protected override Task TearDownAsync()
=> DB.DeleteAsync<Dom.Account>(a => a.ID == AccountID);
}
116 changes: 0 additions & 116 deletions Template/Tests/AccountTests.cs

This file was deleted.

63 changes: 0 additions & 63 deletions Template/Tests/Fixtures/AppFixture.cs

This file was deleted.

11 changes: 4 additions & 7 deletions Template/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>Tests</RootNamespace>
<RootNamespace>Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.3.0" />
<PackageReference Include="FastEndpoints.Testing" Version="5.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="Xunit.Priority" Version="1.1.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Source\MongoWebApiStarter.csproj" />
Expand Down
Loading

0 comments on commit 7524d82

Please sign in to comment.