Skip to content

Commit

Permalink
Fix tests for new constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Guldborg authored and duckth committed Nov 12, 2024
1 parent a5dc402 commit 625f332
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task RecoverUserGivenValidTokenReturnsTrue()

// Act
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings);
var token = new Token("valid");
var token = new Token("valid", TokenType.ResetPassword);
var userTokens = new List<Token> { token };
var programme = new Programme { FullName = "fullName", ShortName = "shortName" };

Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task RecoverUserGivenValidTokenUpdatesPasswordAndResetsUsersTokens(

// Act
await using var context = new CoffeeCardContext(builder.Options, databaseSettings, environmentSettings);
var token = new Token("valid");
var token = new Token("valid", TokenType.ResetPassword);
var userTokens = new List<Token> { token };
var programme = new Programme { FullName = "fullName", ShortName = "shortName" };

Expand Down
4 changes: 2 additions & 2 deletions coffeecard/CoffeeCard.Tests.Unit/Services/TokenServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task ValidateTokenGivenValidTokenReturnsTrue()
var tokenService = new TokenService(_identity, claimsUtility);

var token = tokenService.GenerateToken(claims);
var userTokens = new List<Token> { new Token(token) };
var userTokens = new List<Token> { new Token(token, TokenType.MagicLink) };
var user = GenerateTestUser(tokens: userTokens);
await context.AddAsync(user);
await context.SaveChangesAsync();
Expand Down Expand Up @@ -151,7 +151,7 @@ public async Task ValidateTokenGivenWelformedExpiredTokenReturnsFalse()

var token = new JwtSecurityTokenHandler().WriteToken(jwt);

var userTokens = new List<Token> { new Token(token) };
var userTokens = new List<Token> { new Token(token, TokenType.MagicLink) };
var user = GenerateTestUser(tokens: userTokens);
await context.AddAsync(user);
await context.SaveChangesAsync();
Expand Down
127 changes: 99 additions & 28 deletions coffeecard/CoffeeCard.Tests.Unit/Services/v2/AccountServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ public async Task GetAccountByClaimsReturnsUserClaimWithEmail()
context.Users.Add(expected);
await context.SaveChangesAsync();
// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
new Mock<IEmailService>().Object, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);
result = await accountService.GetAccountByClaimsAsync(claims);

// Assert
Expand All @@ -73,8 +78,13 @@ public async Task GetAccountByClaimsThrowsApiExceptionGivenInvalidClaim(IEnumera
context.Users.Add(validUser);
await context.SaveChangesAsync();
// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
new Mock<IEmailService>().Object, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

// Assert
await Assert.ThrowsAsync<ApiException>(async () => await accountService.GetAccountByClaimsAsync(claims));
Expand Down Expand Up @@ -108,7 +118,7 @@ public async Task RegisterAccountReturnsUserOnValidInput(String name, String ema

// Using same context across all valid users to test creation of multiple users
using var context = CreateTestCoffeeCardContextWithName(nameof(RegisterAccountReturnsUserOnValidInput));
var emailServiceMock = new Mock<IEmailService>();
var emailServiceMock = new Mock<Library.Services.IEmailService>();
emailServiceMock.Setup(e => e.SendRegistrationVerificationEmailAsync(It.IsAny<User>(), It.IsAny<String>())).Returns(Task.CompletedTask);
var emailService = emailServiceMock.Object;

Expand All @@ -120,8 +130,14 @@ public async Task RegisterAccountReturnsUserOnValidInput(String name, String ema
context.Programmes.Add(programme);
await context.SaveChangesAsync();
// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
emailService, hashService);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
emailService,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
hashService);

result = await accountService.RegisterAccountAsync(name, email, password, programmeId);

// Assert
Expand All @@ -148,8 +164,13 @@ public async Task RegisterAccountThrowsApiExceptionWithStatus409OnExistingEmail(
hashservice.Setup(h => h.Hash("pass")).Returns("");

// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
new Mock<IEmailService>().Object, hashservice.Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
hashservice.Object);

// Assert
// Register the first user
Expand All @@ -166,8 +187,13 @@ public async Task RegisterAccountThrowsApiExceptionWithStatus400WhenGivenInvalid
// Arrange
using var context = CreateTestCoffeeCardContextWithName(nameof(RegisterAccountThrowsApiExceptionWithStatus400WhenGivenInvalidProgrammeId));
// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
new Mock<IEmailService>().Object, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

// Assert
var exception = await Assert.ThrowsAsync<ApiException>(
Expand Down Expand Up @@ -197,7 +223,7 @@ public async Task RegisterAccountSendsVerificationEmailOnlyValidInput()
};

using var context = CreateTestCoffeeCardContextWithName(nameof(RegisterAccountSendsVerificationEmailOnlyValidInput));
var emailServiceMock = new Mock<IEmailService>();
var emailServiceMock = new Mock<Library.Services.IEmailService>();
emailServiceMock.Setup(e => e.SendRegistrationVerificationEmailAsync(It.IsAny<User>(), It.IsAny<String>())).Returns(Task.CompletedTask);
var emailService = emailServiceMock.Object;

Expand All @@ -209,8 +235,14 @@ public async Task RegisterAccountSendsVerificationEmailOnlyValidInput()
context.Programmes.Add(programme);
await context.SaveChangesAsync();
// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
emailService, hashService);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
emailService,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
hashService);

await accountService.RegisterAccountAsync("name", "email", "password", 1);

// Assert
Expand Down Expand Up @@ -275,8 +307,14 @@ public async Task UpdateAccountUpdatesAllNonNullProperties(String name, String e
var hashService = hashServiceMock.Object;

// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
new Mock<IEmailService>().Object, hashService);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
hashService);

var result = await accountService.UpdateAccountAsync(user, updateUserRequest);

// Assert
Expand Down Expand Up @@ -322,8 +360,13 @@ public async Task UpdateAccountThrowsApiExceptionOnInvalidProgrammeId()
await context.SaveChangesAsync();

// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
new Mock<IEmailService>().Object, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

// Assert
var exception = await Assert.ThrowsAsync<ApiException>(async () => await accountService.UpdateAccountAsync(user, updateUserRequest));
Expand All @@ -346,13 +389,18 @@ public async Task RequestAnonymizationSendsEmail()
context.Users.Add(user);
await context.SaveChangesAsync();

var emailServiceMock = new Mock<IEmailService>();
var emailServiceMock = new Mock<Library.Services.IEmailService>();
emailServiceMock.Setup(e => e.SendVerificationEmailForDeleteAccount(It.IsAny<User>(), It.IsAny<String>())).Returns(Task.CompletedTask);
var emailService = emailServiceMock.Object;

// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object,
emailService, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
emailService,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

await accountService.RequestAnonymizationAsync(user);
// Assert
Expand Down Expand Up @@ -389,12 +437,17 @@ public async Task AnonymizeAccountRemovesIdentifyableInformationFromUser()
context.Users.Add(user);
await context.SaveChangesAsync();

var tokenServiceMock = new Mock<ITokenService>();
var tokenServiceMock = new Mock<Library.Services.ITokenService>();
tokenServiceMock.Setup(e => e.ValidateVerificationTokenAndGetEmail("test")).Returns(userEmail);

// Act
var accountService = new Library.Services.v2.AccountService(context, tokenServiceMock.Object,
new Mock<IEmailService>().Object, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
tokenServiceMock.Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

await accountService.AnonymizeAccountAsync("test");
var result = await context.Users.Where(u => u.Id == user.Id).FirstAsync();
Expand Down Expand Up @@ -431,8 +484,14 @@ public async Task ResendVerificationEmailWhenAccountIsNotVerified()
await context.SaveChangesAsync();

// Act
var emailService = new Mock<IEmailService>();
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object, emailService.Object, new Mock<IHashService>().Object);
var emailService = new Mock<Library.Services.IEmailService>();
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
emailService.Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

await accountService.ResendAccountVerificationEmail(new ResendAccountVerificationEmailRequest
{
Expand Down Expand Up @@ -465,7 +524,13 @@ public async Task ResendVerificationEmailThrowsConflictExceptionWhenAccountIsAlr
await context.SaveChangesAsync();

// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object, new Mock<IEmailService>().Object, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

await Assert.ThrowsAsync<ConflictException>(async () => await accountService.ResendAccountVerificationEmail(new ResendAccountVerificationEmailRequest
{
Expand All @@ -480,7 +545,13 @@ public async Task ResendVerificationEmailThrowsEntityNotFoundExceptionWhenEmailD
await using var context = CreateTestCoffeeCardContextWithName(nameof(ResendVerificationEmailThrowsEntityNotFoundExceptionWhenEmailDoesnotExist));

// Act
var accountService = new Library.Services.v2.AccountService(context, new Mock<ITokenService>().Object, new Mock<IEmailService>().Object, new Mock<IHashService>().Object);
var accountService = new Library.Services.v2.AccountService(
context,
new Mock<Library.Services.ITokenService>().Object,
new Mock<Library.Services.IEmailService>().Object,
new Mock<Library.Services.v2.IEmailService>().Object,
new Mock<Library.Services.v2.ITokenService>().Object,
new Mock<IHashService>().Object);

await Assert.ThrowsAsync<EntityNotFoundException>(async () => await accountService.ResendAccountVerificationEmail(new ResendAccountVerificationEmailRequest
{
Expand Down

0 comments on commit 625f332

Please sign in to comment.