Skip to content

Commit

Permalink
integrate tokens into testing
Browse files Browse the repository at this point in the history
  • Loading branch information
connerdouglass committed Aug 12, 2023
1 parent 61106d9 commit c8431ef
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 43 deletions.
3 changes: 2 additions & 1 deletion authenticate_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ func TestCreateAuthentication(t *testing.T) {
t.Run("creates authentication successfully", func(t *testing.T) {
w, credentials, tokener := setupMocks(tc, tc.AuthenticationChallenge)
credentials.On("GetCredentials", ctx, tc.User).Return([]webauthn.Credential{testCred}, nil).Once()
tokener.On("CreateToken", tcChallenge, tc.User).Return("token", nil).Once()
tokener.On("CreateToken", tcChallenge, tc.User).Return(tc.Authentication.Token, nil).Once()

challenge, err := w.CreateAuthentication(ctx, tc.User)
require.NotNil(t, challenge, "challenge should not be nil")
require.Nil(t, err, "error should be nil")

require.Equal(t, tc.Authentication.Token, challenge.Token, "token should match")
require.Equal(t, testutil.Encode(tcChallenge[:]), challenge.Challenge, "challenge should match")
require.Equal(t, tc.RelyingParty.ID, challenge.RPID, "relying party should match")
require.Equal(t, 1, len(challenge.AllowCredentials), "allow credentials should match")
Expand Down
5 changes: 3 additions & 2 deletions authenticate_verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestVerifyAuthentication(t *testing.T) {
t.Run(tc.Name, func(t *testing.T) {
t.Run("challenge token is invalid", func(t *testing.T) {
w, credentials, tokener := setupMocks(tc, tc.AuthenticationChallenge)
tokener.On("VerifyToken", mock.Anything, tcChallenge, tc.User).Return(errors.New("invalid token")).Once()
tokener.On("VerifyToken", tc.Authentication.Token, tcChallenge, tc.User).Return(errors.New("invalid token")).Once()

result, err := w.VerifyAuthentication(ctx, tc.User, &tc.Authentication)
require.Nil(t, result, "result should be nil")
Expand All @@ -45,12 +45,13 @@ func TestVerifyAuthentication(t *testing.T) {
// Seed the store with a valid credential
credential := seedMockWithCredential(t, tc, w, credentials, tokener)

tokener.On("VerifyToken", mock.Anything, tcChallenge, tc.User).Return(nil).Once()
tokener.On("VerifyToken", tc.Authentication.Token, tcChallenge, tc.User).Return(nil).Once()
credentials.On("GetCredential", mock.Anything, tc.User, mock.Anything).Return(&credential, nil).Once()

result, err := w.VerifyAuthentication(ctx, tc.User, &tc.Authentication)
require.Nil(t, err, "error should be nil")
require.NotNil(t, result, "result should not be nil")
require.Equal(t, credential, result.Credential, "credential should match")

credentials.AssertExpectations(t)
tokener.AssertExpectations(t)
Expand Down
Loading

0 comments on commit c8431ef

Please sign in to comment.