Skip to content

Commit

Permalink
chore: adjust tests
Browse files Browse the repository at this point in the history
wip

chore: resolve import cycle
  • Loading branch information
bjoern-m committed Dec 12, 2024
1 parent 672ea46 commit cba11d0
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 222 deletions.
17 changes: 7 additions & 10 deletions backend/handler/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@ import (
"github.com/teamhanko/hanko/backend/dto"
"github.com/teamhanko/hanko/backend/persistence"
"github.com/teamhanko/hanko/backend/persistence/models"
"github.com/teamhanko/hanko/backend/session"
"github.com/teamhanko/hanko/backend/webhooks/events"
"github.com/teamhanko/hanko/backend/webhooks/utils"
"net/http"
"strings"
)

type EmailHandler struct {
persister persistence.Persister
cfg *config.Config
sessionManager session.Manager
auditLogger auditlog.Logger
persister persistence.Persister
cfg *config.Config
auditLogger auditlog.Logger
}

func NewEmailHandler(cfg *config.Config, persister persistence.Persister, sessionManager session.Manager, auditLogger auditlog.Logger) *EmailHandler {
func NewEmailHandler(cfg *config.Config, persister persistence.Persister, auditLogger auditlog.Logger) *EmailHandler {
return &EmailHandler{
persister: persister,
cfg: cfg,
sessionManager: sessionManager,
auditLogger: auditLogger,
persister: persister,
cfg: cfg,
auditLogger: auditLogger,
}
}

Expand Down
43 changes: 6 additions & 37 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, 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,9 +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)
cookie, err := generateSessionCookie(s.Storage, currentTest.userId)
s.Require().NoError(err)

req := httptest.NewRequest(http.MethodGet, "/emails", nil)
Expand Down Expand Up @@ -172,14 +163,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)
cookie, err := generateSessionCookie(s.Storage, currentTest.userId)
s.Require().NoError(err)

body := dto.EmailCreateRequest{
Expand Down Expand Up @@ -235,19 +219,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, err := generateSessionCookie(s.Storage, userId)
s.Require().NoError(err)

req := httptest.NewRequest(http.MethodPost, fmt.Sprintf("/emails/%s/set_primary", newPrimaryEmailId), nil)
req.AddCookie(cookie)
Expand Down Expand Up @@ -280,16 +256,9 @@ 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)
cookie, err := generateSessionCookie(s.Storage, userId)
s.Require().NoError(err)

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

tests := []struct {
name string
emailId uuid.UUID
Expand Down
16 changes: 1 addition & 15 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,10 +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)
cookie, err := generateSessionCookie(s.Storage, currentTest.userId)
s.Require().NoError(err)

req := httptest.NewRequest(http.MethodPut, "/password", strings.NewReader(currentTest.body))
Expand Down Expand Up @@ -196,15 +191,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
2 changes: 1 addition & 1 deletion backend/handler/public_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func NewPublicRouter(cfg *config.Config, persister persistence.Persister, promet
wellKnown.GET("/jwks.json", wellKnownHandler.GetPublicKeys)
wellKnown.GET("/config", wellKnownHandler.GetConfig)

emailHandler := NewEmailHandler(cfg, persister, sessionManager, auditLogger)
emailHandler := NewEmailHandler(cfg, persister, auditLogger)

if cfg.Passkey.Enabled {
webauthnHandler, err := NewWebauthnHandler(cfg, persister, sessionManager, auditLogger, authenticatorMetadata)
Expand Down
40 changes: 40 additions & 0 deletions backend/handler/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handler

import (
"github.com/gofrs/uuid"
"github.com/teamhanko/hanko/backend/crypto/jwk"
"github.com/teamhanko/hanko/backend/persistence"
"github.com/teamhanko/hanko/backend/persistence/models"
"github.com/teamhanko/hanko/backend/session"
"github.com/teamhanko/hanko/backend/test"
"net/http"
"time"
)

func getDefaultSessionManager(storage persistence.Persister) session.Manager {
jwkManager, _ := jwk.NewDefaultManager(test.DefaultConfig.Secrets.Keys, storage.GetJwkPersister())
sessionManager, _ := session.NewManager(jwkManager, test.DefaultConfig)
return sessionManager
}

func generateSessionCookie(storage persistence.Persister, userId uuid.UUID) (*http.Cookie, error) {
manager := getDefaultSessionManager(storage)
token, rawToken, err := manager.GenerateJWT(userId, nil)
if err != nil {
return nil, err
}
sessionID, _ := rawToken.Get("session_id")
_ = storage.GetSessionPersister().Create(models.Session{
ID: uuid.FromStringOrNil(sessionID.(string)),
UserID: userId,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
ExpiresAt: nil,
LastUsed: time.Now(),
})
cookie, err := manager.GenerateCookie(token)
if err != nil {
return nil, err
}
return cookie, nil
}
Loading

0 comments on commit cba11d0

Please sign in to comment.