-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a76c695
commit 8ce9540
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package session | ||
|
||
import ( | ||
"context" | ||
|
||
"ahbcc/internal/database" | ||
"ahbcc/internal/log" | ||
) | ||
|
||
// Delete deletes a session | ||
type Delete func(ctx context.Context, token string) error | ||
|
||
// MakeDelete creates a new Delete | ||
func MakeDelete(db database.Connection) Delete { | ||
const query string = ` | ||
DELETE FROM users_sessions | ||
WHERE token = $1 | ||
` | ||
|
||
return func(ctx context.Context, token string) error { | ||
_, err := db.Exec(ctx, query, token) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
return FailedToDeleteUserSession | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package session_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/jackc/pgx/v5/pgconn" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"ahbcc/cmd/api/user/session" | ||
"ahbcc/internal/database" | ||
) | ||
|
||
func TestDelete_success(t *testing.T) { | ||
mockPostgresConnection := new(database.MockPostgresConnection) | ||
mockPostgresConnection.On("Exec", mock.Anything, mock.Anything, mock.Anything).Return(pgconn.CommandTag{}, nil) | ||
|
||
deleteSession := session.MakeDelete(mockPostgresConnection) | ||
|
||
got := deleteSession(context.Background(), "token") | ||
|
||
assert.Nil(t, got) | ||
mockPostgresConnection.AssertExpectations(t) | ||
} | ||
|
||
func TestDelete_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 session")) | ||
|
||
deleteSession := session.MakeDelete(mockPostgresConnection) | ||
|
||
want := session.FailedToDeleteUserSession | ||
got := deleteSession(context.Background(), "token") | ||
|
||
assert.Equal(t, want, got) | ||
mockPostgresConnection.AssertExpectations(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters