test: add test for session delete

This commit is contained in:
Muyao CHEN 2024-10-15 21:39:08 +02:00
parent 322b441c70
commit 382da3d811

View File

@ -24,7 +24,9 @@ package controller
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"testing" "testing"
"time" "time"
@ -32,12 +34,43 @@ import (
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller/usecasemock" "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/howmuch/usecase/usecase"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno" "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/test"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/token" "git.vinchent.xyz/vinchent/howmuch/internal/pkg/token"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert" "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) { func TestSessionCreate(t *testing.T) {
tests := []struct { tests := []struct {
Name string 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)
}