From 382da3d81150f81fb96c2cfd634abb67f4683db1 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Tue, 15 Oct 2024 21:39:08 +0200 Subject: [PATCH] test: add test for session delete --- .../adapter/controller/session_test.go | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/internal/howmuch/adapter/controller/session_test.go b/internal/howmuch/adapter/controller/session_test.go index 4d14d52..0a88cf8 100644 --- a/internal/howmuch/adapter/controller/session_test.go +++ b/internal/howmuch/adapter/controller/session_test.go @@ -24,7 +24,9 @@ package controller import ( "bytes" + "context" "encoding/json" + "fmt" "net/http" "testing" "time" @@ -32,12 +34,43 @@ import ( "git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller/usecasemock" "git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase" "git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno" + "git.vinchent.xyz/vinchent/howmuch/internal/pkg/middleware/authn" "git.vinchent.xyz/vinchent/howmuch/internal/pkg/test" "git.vinchent.xyz/vinchent/howmuch/internal/pkg/token" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" ) +// {{{ Test Cache + +type testCache struct { + kvMap map[string]interface{} +} + +func (c *testCache) Get(ctx context.Context, key string) (string, error) { + val, ok := c.kvMap[key] + if ok { + return val.(string), nil + } + return "", nil +} + +func (c *testCache) Set( + ctx context.Context, + key string, + value interface{}, + expiration time.Duration, +) error { + c.kvMap[key] = value + return nil +} + +func (c *testCache) Close() error { + return nil +} + +// }}} + func TestSessionCreate(t *testing.T) { tests := []struct { Name string @@ -93,3 +126,57 @@ func TestSessionCreate(t *testing.T) { }) } } + +func TestSessionDelete(t *testing.T) { + testUserUsecase := usecasemock.NewtestUserUsecase() + kvMap := make(map[string]interface{}, 1) + tc := &testCache{kvMap: kvMap} + sessionController := NewSessionController(testUserUsecase, tc) + r := gin.New() + session := r.Group("/session") + { + session.POST("/create", func(ctx *gin.Context) { sessionController.Create(ctx) }) + session.Use(authn.Authn(tc)) + session.POST("/delete", func(ctx *gin.Context) { sessionController.Delete(ctx) }) + } + + params := createParams{ + Email: "correct@correct.com", + Password: "strong password", + } + user, _ := json.Marshal(params) + res := test.PerformRequest(t, r, "POST", "/session/create", bytes.NewReader(user), + test.Header{ + Key: "content-type", + Value: "application/json", + }) + + var tk Token + _ = json.NewDecoder(res.Result().Body).Decode(&tk) + tkResp, _ := token.Parse(tk.Token) + + // Log out + res = test.PerformRequest(t, r, "POST", "/session/delete", nil, + test.Header{ + Key: "Authorization", + Value: fmt.Sprintf("Bearer %s", tkResp.Raw), + }) + + var loggedOut string + err := json.NewDecoder(res.Result().Body).Decode(&loggedOut) + assert.NoError(t, err) + assert.Equal(t, "logged out", loggedOut) + + // Try to access the handler with the old token + res = test.PerformRequest(t, r, "POST", "/session/delete", nil, + test.Header{ + Key: "Authorization", + Value: fmt.Sprintf("Bearer %s", tkResp.Raw), + }) + + var unauth errno.Errno + err = json.NewDecoder(res.Result().Body).Decode(&unauth) + assert.NoError(t, err) + unauth.HTTP = res.Result().StatusCode + assert.Equal(t, *authn.ErrLoggedOut, unauth) +}