From f0d2ea7f170645e4d32053c7acce7059a58ebcdb Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 24 Jul 2019 19:31:07 +0500 Subject: [PATCH] Move Logout() to user method --- internal/backend.go | 2 +- internal/backends/memory/backend.go | 16 ---------------- internal/backends/memory/backend_test.go | 10 ++++------ internal/backends/memory/user.go | 4 ++++ internal/handlers.go | 8 +++++--- 5 files changed, 14 insertions(+), 26 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 57652be..3946f6c 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -10,7 +10,6 @@ import ( type Backend interface { Register(username, password, device string) (user User, token string, err *models.ApiError) Login(username, password, device string) (token string, err *models.ApiError) - Logout(token string) *models.ApiError GetUserByToken(token string) (user User) Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError) } @@ -33,5 +32,6 @@ type User interface { LeaveRoom(room Room) *models.ApiError SetTopic(room Room, topic string) *models.ApiError SendMessage(room Room, text string) *models.ApiError + Logout(token string) LogoutAll() } diff --git a/internal/backends/memory/backend.go b/internal/backends/memory/backend.go index 97a5cb1..229a72b 100644 --- a/internal/backends/memory/backend.go +++ b/internal/backends/memory/backend.go @@ -69,22 +69,6 @@ func (backend *Backend) Login(username, password, device string) (token string, return token, nil } -func (backend *Backend) Logout(token string) *models.ApiError { - backend.mutex.Lock() - defer backend.mutex.Unlock() - - for _, user := range backend.data { - for userToken, _ := range user.(*User).Tokens { - if userToken == token { - delete(user.(*User).Tokens, token) - return nil - } - } - } - - return internal.NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct -} - func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) { backend.mutex.Lock() defer backend.mutex.Unlock() diff --git a/internal/backends/memory/backend_test.go b/internal/backends/memory/backend_test.go index 87e4fb3..e2adfb5 100644 --- a/internal/backends/memory/backend_test.go +++ b/internal/backends/memory/backend_test.go @@ -78,15 +78,14 @@ func TestLogout(t *testing.T) { password = "password1" ) - _, _, err := backend.Register(userName, password, "") + user, _, err := backend.Register(userName, password, "") assert.Nil(t, err) token, err := backend.Login(userName, password, "") assert.Nil(t, err) assert.NotZero(t, token) - err = backend.Logout(token) - assert.Nil(t, err) + user.Logout(token) } func TestLogoutWithWrongToken(t *testing.T) { @@ -97,13 +96,12 @@ func TestLogoutWithWrongToken(t *testing.T) { password = "password1" ) - _, _, err := backend.Register(userName, password, "") + user, _, err := backend.Register(userName, password, "") assert.Nil(t, err) token, err := backend.Login(userName, password, "") assert.Nil(t, err) assert.NotZero(t, token) - err = backend.Logout("worng token") - assert.NotNil(t, err) + user.Logout("worng token") } diff --git a/internal/backends/memory/user.go b/internal/backends/memory/user.go index aa1bc66..050fcf4 100644 --- a/internal/backends/memory/user.go +++ b/internal/backends/memory/user.go @@ -155,6 +155,10 @@ func (user *User) SendMessage(room internal.Room, text string) *models.ApiError return nil } +func (user *User) Logout(token string) { + delete(user.Tokens, token) +} + func (user *User) LogoutAll() { user.Tokens = make(map[string]Token) } diff --git a/internal/handlers.go b/internal/handlers.go index ab7ce00..95ebbec 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -88,12 +88,14 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) { return } - apiErr := currServer.Backend.Logout(token) - if apiErr != nil { - errorResponse(w, *apiErr, http.StatusBadRequest, "") // TODO: check code + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") return } + user.Logout(token) + sendJsonResponse(w, http.StatusOK, struct{}{}) }