From 207ea5e86ce7555b34333a23fdbed4795519218f Mon Sep 17 00:00:00 2001 From: Lucas Belfanti Date: Tue, 14 Jan 2025 02:03:41 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Added=20LogOut=20service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/api/auth/errors.go | 1 + cmd/api/auth/logout.go | 25 +++++++++++++++++++++++++ cmd/api/auth/logout_test.go | 33 +++++++++++++++++++++++++++++++++ cmd/api/user/session/mocks.go | 7 +++++++ 4 files changed, 66 insertions(+) create mode 100644 cmd/api/auth/logout.go create mode 100644 cmd/api/auth/logout_test.go diff --git a/cmd/api/auth/errors.go b/cmd/api/auth/errors.go index dfd5967..e994e86 100644 --- a/cmd/api/auth/errors.go +++ b/cmd/api/auth/errors.go @@ -12,6 +12,7 @@ var ( FailedToSelectUserByUsername = errors.New("failed to execute select user by username") FailedToLoginDueWrongPassword = errors.New("failed to login due wrong password") FailedToCreateUserSession = errors.New("failed to create user session") + FailedToDeleteUserSession = errors.New("failed to delete user session") ) const ( diff --git a/cmd/api/auth/logout.go b/cmd/api/auth/logout.go new file mode 100644 index 0000000..3140d7e --- /dev/null +++ b/cmd/api/auth/logout.go @@ -0,0 +1,25 @@ +package auth + +import ( + "context" + + "ahbcc/cmd/api/user/session" + "ahbcc/internal/log" +) + +// LogOut Logs the user out. It deletes the current user session associated to that user by the token +type LogOut func(ctx context.Context, token string) error + +// MakeLogOut creates a new LogOut +func MakeLogOut(deleteUserSession session.Delete) LogOut { + return func(ctx context.Context, token string) error { + + err := deleteUserSession(ctx, token) + if err != nil { + log.Error(ctx, err.Error()) + return FailedToDeleteUserSession + } + + return nil + } +} diff --git a/cmd/api/auth/logout_test.go b/cmd/api/auth/logout_test.go new file mode 100644 index 0000000..098c796 --- /dev/null +++ b/cmd/api/auth/logout_test.go @@ -0,0 +1,33 @@ +package auth_test + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/assert" + + "ahbcc/cmd/api/auth" + "ahbcc/cmd/api/user/session" +) + +func TestLogOut_success(t *testing.T) { + mockDeleteUserSession := session.MockDelete(nil) + + logOut := auth.MakeLogOut(mockDeleteUserSession) + + got := logOut(context.Background(), "token") + + assert.Nil(t, got) +} + +func TestLogOut_failsWhenDeleteSessionThrowsError(t *testing.T) { + mockDeleteUserSession := session.MockDelete(errors.New("failed to delete session")) + + logOut := auth.MakeLogOut(mockDeleteUserSession) + + want := auth.FailedToDeleteUserSession + got := logOut(context.Background(), "token") + + assert.Equal(t, want, got) +} diff --git a/cmd/api/user/session/mocks.go b/cmd/api/user/session/mocks.go index 3da1a95..0f60393 100644 --- a/cmd/api/user/session/mocks.go +++ b/cmd/api/user/session/mocks.go @@ -19,6 +19,13 @@ func MockCreateToken(token string, expiresAt time.Time, err error) CreateToken { } } +// MockDelete mocks a Delete function +func MockDelete(err error) Delete { + return func(ctx context.Context, token string) error { + return err + } +} + // MockUserSessionDAO mocks a session DAO func MockUserSessionDAO() DAO { return DAO{