Skip to content

Commit

Permalink
chore: adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoern-m committed Dec 11, 2024
1 parent 672ea46 commit 4ae3fca
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 215 deletions.
44 changes: 5 additions & 39 deletions backend/handler/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/gofrs/uuid"
"github.com/stretchr/testify/suite"
"github.com/teamhanko/hanko/backend/config"
"github.com/teamhanko/hanko/backend/crypto/jwk"
"github.com/teamhanko/hanko/backend/dto"
"github.com/teamhanko/hanko/backend/session"
"github.com/teamhanko/hanko/backend/test"
"net/http"
"net/http/httptest"
Expand All @@ -26,7 +24,7 @@ type emailSuite struct {
}

func (s *emailSuite) TestEmailHandler_New() {
emailHandler := NewEmailHandler(&config.Config{}, s.Storage, sessionManager{}, test.NewAuditLogger())
emailHandler := NewEmailHandler(&config.Config{}, s.Storage, s.GetDefaultSessionManager(), test.NewAuditLogger())
s.NotEmpty(emailHandler)
}

Expand All @@ -40,11 +38,6 @@ func (s *emailSuite) TestEmailHandler_List() {

e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
s.Require().NoError(err)
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
s.Require().NoError(err)

tests := []struct {
name string
userId uuid.UUID
Expand All @@ -64,10 +57,7 @@ func (s *emailSuite) TestEmailHandler_List() {

for _, currentTest := range tests {
s.Run(currentTest.name, func() {
token, _, err := sessionManager.GenerateJWT(currentTest.userId, nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.Require().NoError(err)
cookie := s.GenerateSessionCookie(currentTest.userId)

req := httptest.NewRequest(http.MethodGet, "/emails", nil)
req.AddCookie(cookie)
Expand Down Expand Up @@ -172,15 +162,7 @@ func (s *emailSuite) TestEmailHandler_Create() {
cfg.Email.RequireVerification = currentTest.requiresVerification
cfg.Email.Limit = currentTest.maxNumberOfAddresses
e := NewPublicRouter(&cfg, s.Storage, nil, nil)
jwkManager, err := jwk.NewDefaultManager(cfg.Secrets.Keys, s.Storage.GetJwkPersister())
s.Require().NoError(err)
sessionManager, err := session.NewManager(jwkManager, cfg)
s.Require().NoError(err)

token, _, err := sessionManager.GenerateJWT(currentTest.userId, nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.Require().NoError(err)
cookie := s.GenerateSessionCookie(currentTest.userId)

body := dto.EmailCreateRequest{
Address: currentTest.email,
Expand Down Expand Up @@ -235,19 +217,11 @@ func (s *emailSuite) TestEmailHandler_SetPrimaryEmail() {

e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
s.Require().NoError(err)
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
s.Require().NoError(err)

oldPrimaryEmailId := uuid.FromStringOrNil("51b7c175-ceb6-45ba-aae6-0092221c1b84")
newPrimaryEmailId := uuid.FromStringOrNil("8bb4c8a7-a3e6-48bb-b54f-20e3b485ab33")
userId := uuid.FromStringOrNil("b5dd5267-b462-48be-b70d-bcd6f1bbe7a5")

token, _, err := sessionManager.GenerateJWT(userId, nil)
s.NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.NoError(err)
cookie := s.GenerateSessionCookie(userId)

req := httptest.NewRequest(http.MethodPost, fmt.Sprintf("/emails/%s/set_primary", newPrimaryEmailId), nil)
req.AddCookie(cookie)
Expand Down Expand Up @@ -280,15 +254,7 @@ func (s *emailSuite) TestEmailHandler_Delete() {
e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)
userId := uuid.FromStringOrNil("b5dd5267-b462-48be-b70d-bcd6f1bbe7a5")

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
s.Require().NoError(err)
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
s.Require().NoError(err)

token, _, err := sessionManager.GenerateJWT(userId, nil)
s.NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.NoError(err)
cookie := s.GenerateSessionCookie(userId)

tests := []struct {
name string
Expand Down
17 changes: 1 addition & 16 deletions backend/handler/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/teamhanko/hanko/backend/config"
"github.com/teamhanko/hanko/backend/crypto/jwk"
"github.com/teamhanko/hanko/backend/session"
"github.com/teamhanko/hanko/backend/test"
"golang.org/x/crypto/bcrypt"
"net/http"
Expand Down Expand Up @@ -90,11 +88,7 @@ func (s *passwordSuite) TestPasswordHandler_Set_Create() {
err := s.LoadFixtures("../test/fixtures/password")
s.Require().NoError(err)

sessionManager := s.GetDefaultSessionManager()
token, _, err := sessionManager.GenerateJWT(currentTest.userId, nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.Require().NoError(err)
cookie := s.GenerateSessionCookie(currentTest.userId)

req := httptest.NewRequest(http.MethodPut, "/password", strings.NewReader(currentTest.body))
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -196,15 +190,6 @@ func (s *passwordSuite) TestPasswordHandler_Login() {
}
}

func (s *passwordSuite) GetDefaultSessionManager() session.Manager {
jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
s.Require().NoError(err)
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
s.Require().NoError(err)

return sessionManager
}

// TestMaxPasswordLength bcrypt since version 0.5.0 only accepts passwords at least 72 bytes long. This test documents this behaviour.
func TestMaxPasswordLength(t *testing.T) {
tests := []struct {
Expand Down
107 changes: 23 additions & 84 deletions backend/handler/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"github.com/gofrs/uuid"
_ "github.com/lib/pq"
"github.com/stretchr/testify/suite"
"github.com/teamhanko/hanko/backend/crypto/jwk"
"github.com/teamhanko/hanko/backend/dto"
"github.com/teamhanko/hanko/backend/persistence/models"
"github.com/teamhanko/hanko/backend/session"
"github.com/teamhanko/hanko/backend/test"
"golang.org/x/exp/slices"
"net/http"
Expand Down Expand Up @@ -249,24 +247,13 @@ func (s *userSuite) TestUserHandler_Get() {
err := s.LoadFixtures("../test/fixtures/user")
s.Require().NoError(err)

userId := "b5dd5267-b462-48be-b70d-bcd6f1bbe7a5"
userId := uuid.FromStringOrNil("b5dd5267-b462-48be-b70d-bcd6f1bbe7a5")

e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
if err != nil {
panic(fmt.Errorf("failed to create jwk manager: %w", err))
}
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
if err != nil {
panic(fmt.Errorf("failed to create session generator: %w", err))
}
token, _, err := sessionManager.GenerateJWT(uuid.FromStringOrNil(userId), nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.Require().NoError(err)
cookie := s.GenerateSessionCookie(userId)

req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/users/%s", userId), nil)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/users/%s", userId.String()), nil)
req.AddCookie(cookie)
rec := httptest.NewRecorder()

Expand All @@ -277,7 +264,7 @@ func (s *userSuite) TestUserHandler_Get() {
user := models.User{}
err := json.Unmarshal(rec.Body.Bytes(), &user)
s.NoError(err)
s.Equal(userId, user.ID.String())
s.Equal(userId.String(), user.ID.String())
s.Equal(len(user.WebauthnCredentials), 0)
}
}
Expand All @@ -289,24 +276,13 @@ func (s *userSuite) TestUserHandler_GetUserWithWebAuthnCredential() {
err := s.LoadFixtures("../test/fixtures/user_with_webauthn_credential")
s.Require().NoError(err)

userId := "b5dd5267-b462-48be-b70d-bcd6f1bbe7a5"
userId := uuid.FromStringOrNil("b5dd5267-b462-48be-b70d-bcd6f1bbe7a5")

e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
if err != nil {
panic(fmt.Errorf("failed to create jwk manager: %w", err))
}
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
if err != nil {
panic(fmt.Errorf("failed to create session generator: %w", err))
}
token, _, err := sessionManager.GenerateJWT(uuid.FromStringOrNil(userId), nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.Require().NoError(err)
cookie := s.GenerateSessionCookie(userId)

req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/users/%s", userId), nil)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/users/%s", userId.String()), nil)
rec := httptest.NewRecorder()
req.AddCookie(cookie)

Expand All @@ -317,7 +293,7 @@ func (s *userSuite) TestUserHandler_GetUserWithWebAuthnCredential() {
user := models.User{}
err := json.Unmarshal(rec.Body.Bytes(), &user)
s.Require().NoError(err)
s.Equal(userId, user.ID.String())
s.Equal(userId.String(), user.ID.String())
s.Equal(len(user.WebauthnCredentials), 1)
}
}
Expand All @@ -328,21 +304,13 @@ func (s *userSuite) TestUserHandler_Get_InvalidUserId() {
}
e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

userId := "b5dd5267-b462-48be-b70d-bcd6f1bbe7a5"

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
if err != nil {
panic(fmt.Errorf("failed to create jwk manager: %w", err))
}
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
if err != nil {
panic(fmt.Errorf("failed to create session generator: %w", err))
}
token, _, err := sessionManager.GenerateJWT(uuid.FromStringOrNil(userId), nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
err := s.LoadFixtures("../test/fixtures/user")
s.Require().NoError(err)

userId := uuid.FromStringOrNil("b5dd5267-b462-48be-b70d-bcd6f1bbe7a5")

cookie := s.GenerateSessionCookie(userId)

req := httptest.NewRequest(http.MethodGet, "/users/invalidUserId", nil)
req.Header.Set("Content-Type", "application/json")
req.AddCookie(cookie)
Expand Down Expand Up @@ -463,22 +431,11 @@ func (s *userSuite) TestUserHandler_Me() {
err := s.LoadFixtures("../test/fixtures/user_with_webauthn_credential")
s.Require().NoError(err)

userId := "b5dd5267-b462-48be-b70d-bcd6f1bbe7a5"
userId := uuid.FromStringOrNil("b5dd5267-b462-48be-b70d-bcd6f1bbe7a5")

e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
if err != nil {
panic(fmt.Errorf("failed to create jwk manager: %w", err))
}
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
if err != nil {
panic(fmt.Errorf("failed to create session generator: %w", err))
}
token, _, err := sessionManager.GenerateJWT(uuid.FromStringOrNil(userId), nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.Require().NoError(err)
cookie := s.GenerateSessionCookie(userId)

req := httptest.NewRequest(http.MethodGet, "/me", nil)
req.AddCookie(cookie)
Expand All @@ -493,30 +450,23 @@ func (s *userSuite) TestUserHandler_Me() {
}{}
err = json.Unmarshal(rec.Body.Bytes(), &response)
s.NoError(err)
s.Equal(userId, response.UserId)
s.Equal(userId.String(), response.UserId)
}
}

func (s *userSuite) TestUserHandler_Logout() {
if testing.Short() {
s.T().Skip("skipping test in short mode.")
}
userId, _ := uuid.NewV4()
e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
if err != nil {
panic(fmt.Errorf("failed to create jwk manager: %w", err))
}
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
if err != nil {
panic(fmt.Errorf("failed to create session generator: %w", err))
}
token, _, err := sessionManager.GenerateJWT(userId, nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
err := s.LoadFixtures("../test/fixtures/user")
s.Require().NoError(err)

userId := uuid.FromStringOrNil("b5dd5267-b462-48be-b70d-bcd6f1bbe7a5")
e := NewPublicRouter(&test.DefaultConfig, s.Storage, nil, nil)

cookie := s.GenerateSessionCookie(userId)

req := httptest.NewRequest(http.MethodPost, "/logout", nil)
req.AddCookie(cookie)
rec := httptest.NewRecorder()
Expand Down Expand Up @@ -545,18 +495,7 @@ func (s *userSuite) TestUserHandler_Delete() {
cfg.Account.AllowDeletion = true
e := NewPublicRouter(&cfg, s.Storage, nil, nil)

jwkManager, err := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, s.Storage.GetJwkPersister())
if err != nil {
panic(fmt.Errorf("failed to create jwk manager: %w", err))
}
sessionManager, err := session.NewManager(jwkManager, test.DefaultConfig)
if err != nil {
panic(fmt.Errorf("failed to create session generator: %w", err))
}
token, _, err := sessionManager.GenerateJWT(userId, nil)
s.Require().NoError(err)
cookie, err := sessionManager.GenerateCookie(token)
s.Require().NoError(err)
cookie := s.GenerateSessionCookie(userId)

req := httptest.NewRequest(http.MethodDelete, "/user", nil)
req.AddCookie(cookie)
Expand Down
Loading

0 comments on commit 4ae3fca

Please sign in to comment.