Skip to content

Commit

Permalink
⚡️ Added DeleteUserExpiredSessions function
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbelfanti committed Jan 14, 2025
1 parent 4780989 commit 69d9105
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
27 changes: 25 additions & 2 deletions cmd/api/user/session/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
"ahbcc/internal/log"
)

// Delete deletes a session
type Delete func(ctx context.Context, token string) error
type (
// Delete deletes a session, seeking it by its token
Delete func(ctx context.Context, token string) error

// DeleteUserExpiredSessions deletes the expired sessions of a given user
DeleteUserExpiredSessions func(ctx context.Context, userID int) error
)

// MakeDelete creates a new Delete
func MakeDelete(db database.Connection) Delete {
Expand All @@ -27,3 +32,21 @@ func MakeDelete(db database.Connection) Delete {
return nil
}
}

// MakeDeleteUserExpiredSessions creates a new DeleteUserExpiredSessions
func MakeDeleteUserExpiredSessions(db database.Connection) DeleteUserExpiredSessions {
const query string = `
DELETE FROM users_sessions
WHERE user_id = $1
`

return func(ctx context.Context, userID int) error {
_, err := db.Exec(ctx, query, userID)
if err != nil {
log.Error(ctx, err.Error())
return FailedToDeleteExpiredUserSessions
}

return nil
}
}
25 changes: 25 additions & 0 deletions cmd/api/user/session/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,28 @@ func TestDelete_failsWhenDeleteOperationThrowsError(t *testing.T) {
assert.Equal(t, want, got)
mockPostgresConnection.AssertExpectations(t)
}

func TestDeleteExpiredSessions_success(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPostgresConnection.On("Exec", mock.Anything, mock.Anything, mock.Anything).Return(pgconn.CommandTag{}, nil)

deleteUserExpiredSessions := session.MakeDeleteUserExpiredSessions(mockPostgresConnection)

got := deleteUserExpiredSessions(context.Background(), 1234)

assert.Nil(t, got)
mockPostgresConnection.AssertExpectations(t)
}

func TestDeleteExpiredSessions_failsWhenDeleteOperationThrowsError(t *testing.T) {
mockPostgresConnection := new(database.MockPostgresConnection)
mockPostgresConnection.On("Exec", mock.Anything, mock.Anything, mock.Anything).Return(pgconn.CommandTag{}, errors.New("failed to delete user expired sessions"))

deleteUserExpiredSessions := session.MakeDeleteUserExpiredSessions(mockPostgresConnection)

want := session.FailedToDeleteExpiredUserSessions
got := deleteUserExpiredSessions(context.Background(), 1234)

assert.Equal(t, want, got)
mockPostgresConnection.AssertExpectations(t)
}
7 changes: 4 additions & 3 deletions cmd/api/user/session/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package session
import "errors"

var (
FailedToInsertUserSession = errors.New("failed to insert user session")
FailedToCreatUserSessionToken = errors.New("failed to create user session token")
FailedToDeleteUserSession = errors.New("failed to delete user session")
FailedToInsertUserSession = errors.New("failed to insert user session")
FailedToCreatUserSessionToken = errors.New("failed to create user session token")
FailedToDeleteUserSession = errors.New("failed to delete user session")
FailedToDeleteExpiredUserSessions = errors.New("failed to delete expired user sessions")
)

0 comments on commit 69d9105

Please sign in to comment.