Skip to content

Commit

Permalink
refactor: move FindSessionByUserID to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joel@joellee.org committed Oct 10, 2022
1 parent 6a3691c commit 3b56457
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 0 additions & 11 deletions models/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,6 @@ func FindSessionById(tx *storage.Connection, id uuid.UUID) (*Session, error) {
return session, nil
}

func FindSessionByUserID(tx *storage.Connection, userId uuid.UUID) (*Session, error) {
session := &Session{}
if err := tx.Eager().Q().Where("user_id = ?", userId).Order("created_at asc").First(session); err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, SessionNotFoundError{}
}
return nil, errors.Wrap(err, "error finding session")
}
return session, nil
}

func updateFactorAssociatedSessions(tx *storage.Connection, userID, factorID uuid.UUID, aal string) error {
return tx.RawQuery("UPDATE "+(&pop.Model{Value: Session{}}).TableName()+" set aal = ?, factor_id = ? WHERE user_id = ? AND factor_id = ?", aal, uuid.Nil, userID, factorID).Exec()
}
Expand Down
13 changes: 13 additions & 0 deletions models/sessions_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package models

import (
"database/sql"
"github.com/gofrs/uuid"
"github.com/netlify/gotrue/conf"
"github.com/netlify/gotrue/storage"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"time"
Expand All @@ -25,6 +27,17 @@ func (ts *SessionsTestSuite) SetupTest() {
require.NoError(ts.T(), err)
}

func FindSessionByUserID(tx *storage.Connection, userId uuid.UUID) (*Session, error) {
session := &Session{}
if err := tx.Eager().Q().Where("user_id = ?", userId).Order("created_at asc").First(session); err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, SessionNotFoundError{}
}
return nil, errors.Wrap(err, "error finding session")
}
return session, nil
}

func (ts *SessionsTestSuite) TestCalculateAALAndAMR() {
totalDistinctClaims := 2
u, err := FindUserByEmailAndAudience(ts.db, "test@example.com", ts.Config.JWT.Aud)
Expand Down

0 comments on commit 3b56457

Please sign in to comment.