Skip to content

Commit 4151877

Browse files
henrybarretogustavosbarreto
authored andcommitted
refactor(api): check if session exist before try to delete active session
1 parent 2d4d985 commit 4151877

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

api/services/session.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"net"
66

7-
"github.com/shellhub-io/shellhub/api/store"
87
"github.com/shellhub-io/shellhub/pkg/api/query"
98
"github.com/shellhub-io/shellhub/pkg/api/requests"
109
"github.com/shellhub-io/shellhub/pkg/models"
@@ -51,12 +50,12 @@ func (s *service) CreateSession(ctx context.Context, session requests.SessionCre
5150
}
5251

5352
func (s *service) DeactivateSession(ctx context.Context, uid models.UID) error {
54-
err := s.store.SessionDeleteActives(ctx, uid)
55-
if err == store.ErrNoDocuments {
53+
sess, err := s.store.SessionGet(ctx, uid)
54+
if err != nil {
5655
return NewErrSessionNotFound(uid, err)
5756
}
5857

59-
return err
58+
return s.store.SessionDeleteActives(ctx, models.UID(sess.UID))
6059
}
6160

6261
func (s *service) KeepAliveSession(ctx context.Context, uid models.UID) error {

api/services/session_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,25 +231,35 @@ func TestDeactivateSession(t *testing.T) {
231231
name: "fails when session is not found",
232232
uid: models.UID("_uid"),
233233
requiredMocks: func() {
234-
mock.On("SessionDeleteActives", ctx, models.UID("_uid")).
235-
Return(store.ErrNoDocuments).Once()
234+
mock.On("SessionGet", ctx, models.UID("_uid")).
235+
Return(nil, goerrors.New("get error")).Once()
236236
},
237-
expected: NewErrSessionNotFound("_uid", store.ErrNoDocuments),
237+
expected: NewErrSessionNotFound("_uid", goerrors.New("get error")),
238238
},
239239
{
240240
name: "fails",
241241
uid: models.UID("_uid"),
242242
requiredMocks: func() {
243+
mock.On("SessionGet", ctx, models.UID("_uid")).
244+
Return(&models.Session{
245+
UID: "_uid",
246+
}, nil).Once()
247+
243248
mock.On("SessionDeleteActives", ctx, models.UID("_uid")).
244249
Return(goerrors.New("error")).Once()
245250
},
246251
expected: goerrors.New("error"),
247252
},
248253
{
249254
name: "succeeds",
250-
uid: models.UID("uid"),
255+
uid: models.UID("_uid"),
251256
requiredMocks: func() {
252-
mock.On("SessionDeleteActives", ctx, models.UID("uid")).
257+
mock.On("SessionGet", ctx, models.UID("_uid")).
258+
Return(&models.Session{
259+
UID: "_uid",
260+
}, nil).Once()
261+
262+
mock.On("SessionDeleteActives", ctx, models.UID("_uid")).
253263
Return(nil).Once()
254264
},
255265
expected: nil,

0 commit comments

Comments
 (0)