Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Guldborg committed Dec 28, 2024
1 parent d5c5e12 commit 3d088b8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 45 deletions.
4 changes: 2 additions & 2 deletions coffeecard/CoffeeCard.Library/Services/v2/TokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public TokenService(CoffeeCardContext context, IHashService hashService)
public async Task<string> GenerateMagicLinkToken(User user)
{
var guid = Guid.NewGuid().ToString();
var magicLinkToken = new Token(_hashService.Hash(guid), TokenType.MagicLink);
var magicLinkToken = new Token(_hashService.Hash(guid), TokenType.MagicLink){User = user};
_context.Tokens.Add(magicLinkToken);

user.Tokens.Add(magicLinkToken);
await _context.SaveChangesAsync();
return guid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task Known_user_login_succeeds_returns_token()
await Context.SaveChangesAsync();

// We authenticate using the non-hashed token and let the backend hash the string for us
var response = await CoffeeCardClientV2.Account_AuthenticateAsync(tokenString);
var response = await CoffeeCardClientV2.Account_AuthenticateAsync(new TokenLoginRequest(){Token = tokenString});

Assert.NotNull(response.Jwt);
Assert.NotNull(response.RefreshToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CoffeeCard.Common.Errors;
using CoffeeCard.Library.Persistence;
using CoffeeCard.Library.Services;
using CoffeeCard.Models.DataTransferObjects.v2.Token;
using CoffeeCard.Models.DataTransferObjects.v2.User;
using CoffeeCard.Models.Entities;
using CoffeeCard.Tests.Common.Builders;
Expand Down Expand Up @@ -577,7 +578,7 @@ public async Task GenerateTokenPairRevokesTokenOnUse()
NullLogger<AccountService>.Instance);

// Act
var tokenPair = await accountService.GenerateUserLoginFromToken(tokenHash);
var tokenPair = await accountService.GenerateUserLoginFromToken(new TokenLoginRequest(){Token = tokenHash});

// Assert
Assert.True(refreshToken.Revoked);
Expand Down Expand Up @@ -619,7 +620,7 @@ public async Task GenerateTokenPairReturnsTokenPair()
NullLogger<AccountService>.Instance);

// Act
var tokenPair = await accountService.GenerateUserLoginFromToken(tokenHash);
var tokenPair = await accountService.GenerateUserLoginFromToken(new TokenLoginRequest(){Token = tokenHash});

// Assert
Assert.NotNull(tokenPair);
Expand Down
46 changes: 6 additions & 40 deletions coffeecard/CoffeeCard.Tests.Unit/Services/v2/TokenServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ public async Task GenerateMagicLink_ReturnsLinkWithValidTokenForUser()
InitialContext.Users.Add(user);
await InitialContext.SaveChangesAsync();

var tokenService = new TokenService(AssertionContext, Mock.Of<IHashService>());
var hashService = new HashService();
var tokenService = new TokenService(InitialContext, hashService);

// Act
var result = await tokenService.GenerateMagicLinkToken(user);

// Assert
var assertionUser = await AssertionContext.Users.Include(u => u.Tokens).FirstOrDefaultAsync();
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.Contains<Token>(user.Tokens, t => t.TokenHash == result);
var token = user.Tokens.First(t => t.TokenHash == result);
var hashedToken = hashService.Hash(result);
Assert.Contains<Token>(assertionUser.Tokens, t => t.TokenHash == hashedToken);
var token = assertionUser.Tokens.First(t => t.TokenHash == hashedToken);
Assert.Equal(TokenType.MagicLink, token.Type);
Assert.False(token.Revoked, "Token should not be revoked");
}
Expand Down Expand Up @@ -127,42 +130,5 @@ public async Task GetValidTokenByHashAsync_ReturnsTokenByValidHash()
Assert.False(result.Revoked);
Assert.False(Token.Expired(result.Expires));
}

[Fact(DisplayName = "GetValidTokenByHashAsync invalidates users refresh tokens if token is invalid")]
public async Task GetValidTokenByHashAsync_InvalidatesUsersRefreshTokensIfTokenIsInvalid()
{
// Arrange
var user = UserBuilder.DefaultCustomer().Build();
InitialContext.Users.Add(user);

var token = TokenBuilder.Simple().WithUser(user).WithRevoked(true).WithType(TokenType.Refresh).Build();
var refreshToken = TokenBuilder.Simple().WithUser(user).WithType(TokenType.Refresh).Build();

Token[] otherTokens =
{
new ("magicLink", TokenType.MagicLink) {User = user},
new ("reset", TokenType.ResetPassword) {User = user},
};

InitialContext.Tokens.AddRange(token, refreshToken);
InitialContext.Tokens.AddRange(otherTokens);

await InitialContext.SaveChangesAsync();

var hashService = new Mock<IHashService>();
hashService.Setup(h => h.Hash(It.IsAny<string>())).Returns(token.TokenHash);

var tokenService = new TokenService(AssertionContext, hashService.Object);

// Act & Assert
await Assert.ThrowsAsync<ApiException>(() => tokenService.GetValidTokenByHashAsync(token.TokenHash));

// Assert
Assert.True((await AssertionContext.Tokens.FirstOrDefaultAsync(t => t.Id == refreshToken.Id)).Revoked);
foreach (var otherToken in otherTokens)
{
Assert.False((await AssertionContext.Tokens.FirstOrDefaultAsync(t => t.Id == otherToken.Id)).Revoked);
}
}
}
}

0 comments on commit 3d088b8

Please sign in to comment.