From 051afa519c3180ef9c0a62577523bafde928ec19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=B5=20=CE=9D=CE=B9=CE=93=CE=9E=CE=97=CE=9B=CF=88=CE=9A?= Date: Mon, 28 Aug 2023 13:50:58 +0530 Subject: [PATCH] ordered integration tests --- MongoWebApiStarterTemplate.csproj | 2 +- Template/Tests/AccountTests.cs | 120 ++++++++---------- .../Tests/MongoWebApiStarter.Tests.csproj | 1 + changelog.md | 3 +- 4 files changed, 53 insertions(+), 73 deletions(-) diff --git a/MongoWebApiStarterTemplate.csproj b/MongoWebApiStarterTemplate.csproj index f79bc41..70f63d2 100644 --- a/MongoWebApiStarterTemplate.csproj +++ b/MongoWebApiStarterTemplate.csproj @@ -2,7 +2,7 @@ - 7.10.0 + 7.10.1 Template MongoWebApiStarter diff --git a/Template/Tests/AccountTests.cs b/Template/Tests/AccountTests.cs index 054c9c3..033be9f 100644 --- a/Template/Tests/AccountTests.cs +++ b/Template/Tests/AccountTests.cs @@ -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().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> CreateAccount(string email) + [Fact, Priority(1)] + public async Task Account_Creation() { - return App.AccountClient.POSTAsync(new() + var (rsp, res) = await App.AccountClient.POSTAsync(new() { City = "City", CountryCode = "LKA", - EmailAddress = email, + EmailAddress = EmailAddress, FirstName = "Firstname", LastName = "Surname", Mobile = "0773469292", @@ -45,92 +36,81 @@ private Task> 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().OneAsync(UserID); - verified.Should().Be(true); + acc!.Email.Should().Be(EmailAddress); + acc.IsEmailVerified.Should().BeFalse(); } - private async Task VerifyAccount(string accountID) + [Fact, Priority(2)] + public async Task Account_Verification() { var code = (await DB .Find() - .OneAsync(accountID))! + .OneAsync(UserID))! .EmailVerificationCode; await App.AccountClient.POSTAsync(new() { - ID = accountID, + ID = UserID!, Code = code }); - return await DB + var verified = await DB .Find() - .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().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> LoginToAccount(string email) + [Fact, Priority(3)] + public async Task Account_Login() { - return App.AccountClient.POSTAsync(new() + var (rsp, res) = await App.AccountClient.POSTAsync(new() { - UserName = email, + UserName = EmailAddress, Password = "qqqqq123Q" }); - } - [Fact] - public async Task Account_Retrieve() - { - var email = $"{Guid.NewGuid()}@email.com"; + var acc = await DB.Find().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(); + var (_, res) = await App.AccountClient.GETAsync(); - var acc = await DB.Find().OneAsync(gRes!.AccountID); + var acc = await DB.Find().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(); } diff --git a/Template/Tests/MongoWebApiStarter.Tests.csproj b/Template/Tests/MongoWebApiStarter.Tests.csproj index 0ea35cf..177cd52 100644 --- a/Template/Tests/MongoWebApiStarter.Tests.csproj +++ b/Template/Tests/MongoWebApiStarter.Tests.csproj @@ -15,6 +15,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/changelog.md b/changelog.md index d4ef193..80d37d3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,2 +1 @@ -- switch testing framework to xUnit -- upgrade dependencies \ No newline at end of file +- refactor integration tests to use test ordering \ No newline at end of file