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

Commit

Permalink
ordered integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Aug 28, 2023
1 parent 0defc97 commit 051afa5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 73 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.0</PackageVersion>
<PackageVersion>7.10.1</PackageVersion>

<PackageType>Template</PackageType>
<PackageId>MongoWebApiStarter</PackageId>
Expand Down
120 changes: 50 additions & 70 deletions Template/Tests/AccountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,30 @@
using MongoDB.Entities;
using System.Net.Http.Headers;
using Xunit;
using Xunit.Priority;
using Login = Account.Login;
using Save = Account.Save;
using Verify = Account.Verify;

namespace Tests;

[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
public class AccountTests : TestBase
{
public AccountTests(AppFixture fixture) : base(fixture) { }

[Fact]
public async Task Account_Creation()
{
var email = $"{Guid.NewGuid()}@email.com";
var (rsp, res) = await CreateAccount(email);

rsp?.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
res?.EmailSent.Should().BeTrue();
res?.ID.Should().NotBeNullOrEmpty();

var acc = await DB.Find<Dom.Account>().OneAsync(res?.ID);
private static readonly string EmailAddress = $"{Guid.NewGuid()}@email.com";
private static string? UserID;
private static string? JwtToken;

acc!.Email.Should().Be(email);
acc.IsEmailVerified.Should().BeFalse();
}

private Task<TestResult<Save.Response>> CreateAccount(string email)
[Fact, Priority(1)]
public async Task Account_Creation()
{
return App.AccountClient.POSTAsync<Save.Endpoint, Save.Request, Save.Response>(new()
var (rsp, res) = await App.AccountClient.POSTAsync<Save.Endpoint, Save.Request, Save.Response>(new()
{
City = "City",
CountryCode = "LKA",
EmailAddress = email,
EmailAddress = EmailAddress,
FirstName = "Firstname",
LastName = "Surname",
Mobile = "0773469292",
Expand All @@ -45,92 +36,81 @@ private Task<TestResult<Save.Response>> CreateAccount(string email)
Title = "Mr.",
ZipCode = "10100",
});
}

[Fact]
public async Task Account_Verification()
{
var email = $"{Guid.NewGuid()}@email.com";
rsp!.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
res!.EmailSent.Should().BeTrue();
res.ID.Should().NotBeNullOrEmpty();

var (_, res) = await CreateAccount(email);
UserID = res.ID;

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

verified.Should().Be(true);
acc!.Email.Should().Be(EmailAddress);
acc.IsEmailVerified.Should().BeFalse();
}

private async Task<bool> VerifyAccount(string accountID)
[Fact, Priority(2)]
public async Task Account_Verification()
{
var code = (await DB
.Find<Dom.Account>()
.OneAsync(accountID))!
.OneAsync(UserID))!
.EmailVerificationCode;

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

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

[Fact]
public async Task Account_Login()
{
var email = $"{Guid.NewGuid()}@email.com";

var (_, res) = await CreateAccount(email);
await VerifyAccount(res!.ID);

var (acRsp, acRes) = await LoginToAccount(email);

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

acRsp?.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
acRes?.FullName.Should().Be($"{acc.Title} {acc.FirstName} {acc.LastName}");
verified.Should().Be(true);
}

private Task<TestResult<Login.Response>> LoginToAccount(string email)
[Fact, Priority(3)]
public async Task Account_Login()
{
return App.AccountClient.POSTAsync<Login.Endpoint, Login.Request, Login.Response>(new()
var (rsp, res) = await App.AccountClient.POSTAsync<Login.Endpoint, Login.Request, Login.Response>(new()
{
UserName = email,
UserName = EmailAddress,
Password = "qqqqq123Q"
});
}

[Fact]
public async Task Account_Retrieve()
{
var email = $"{Guid.NewGuid()}@email.com";
var acc = await DB.Find<Dom.Account>().OneAsync(UserID);

var (_, res) = await CreateAccount(email);
await VerifyAccount(res!.ID);
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();

var (_, acRes) = await LoginToAccount(email);
JwtToken = res.Token.Value;
}

App.AccountClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acRes!.Token.Value);
[Fact, Priority(4)]
public async Task Account_Retrieve()
{
App.AccountClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", JwtToken);

var (_, gRes) = await App.AccountClient.GETAsync<Account.Get.Endpoint, Account.Get.Response>();
var (_, res) = await App.AccountClient.GETAsync<Account.Get.Endpoint, Account.Get.Response>();

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

var match =
acc!.Email == gRes.EmailAddress &&
acc.Title == gRes.Title &&
acc.FirstName == gRes.FirstName &&
acc.LastName == gRes.LastName &&
acc.Address.Street == gRes.Street &&
acc.Address.City == gRes.City &&
acc.Address.State == gRes.State &&
acc.Address.ZipCode == gRes.ZipCode &&
acc.Address.CountryCode == gRes.CountryCode &&
acc.Mobile == gRes.Mobile;
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();
}
Expand Down
1 change: 1 addition & 0 deletions Template/Tests/MongoWebApiStarter.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.3.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="Xunit.Priority" Version="1.1.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
3 changes: 1 addition & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
- switch testing framework to xUnit
- upgrade dependencies
- refactor integration tests to use test ordering

0 comments on commit 051afa5

Please sign in to comment.