forked from mtusi2026/mtusi2026lab6
chore: import lab5 baseline
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package session_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/session"
|
||||
)
|
||||
|
||||
type SessionSuite struct {
|
||||
suite.Suite
|
||||
store *session.Store
|
||||
}
|
||||
|
||||
func TestSessionSuite(t *testing.T) {
|
||||
suite.Run(t, new(SessionSuite))
|
||||
}
|
||||
|
||||
func (s *SessionSuite) SetupSuite() {
|
||||
rawURL := strings.TrimSpace(os.Getenv("TEST_CACHE_URL"))
|
||||
if rawURL == "" {
|
||||
path := strings.TrimSpace(os.Getenv("TEST_CACHE_URL_FILE"))
|
||||
if path == "" {
|
||||
s.T().Skip("set TEST_CACHE_URL or TEST_CACHE_URL_FILE to run integration tests")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
s.Require().NoError(err)
|
||||
rawURL = strings.TrimSpace(string(data))
|
||||
}
|
||||
store, err := session.New(rawURL)
|
||||
s.Require().NoError(err)
|
||||
s.store = store
|
||||
}
|
||||
|
||||
func (s *SessionSuite) TearDownSuite() {
|
||||
if s.store != nil {
|
||||
s.Require().NoError(s.store.Close())
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SessionSuite) TestSessionLifecycle() {
|
||||
userID := uuid.NewString()
|
||||
token, expiresAt, err := s.store.Create(context.Background(), userID, time.Minute)
|
||||
s.Require().NoError(err)
|
||||
s.NotEmpty(token)
|
||||
s.True(expiresAt.After(time.Now()))
|
||||
|
||||
actualUserID, err := s.store.UserID(context.Background(), token)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(userID, actualUserID)
|
||||
s.Require().NoError(s.store.Delete(context.Background(), token))
|
||||
|
||||
_, err = s.store.UserID(context.Background(), token)
|
||||
s.ErrorIs(err, session.ErrNotFound)
|
||||
}
|
||||
|
||||
func (s *SessionSuite) TestSessionExpires() {
|
||||
token, _, err := s.store.Create(context.Background(), uuid.NewString(), time.Second)
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.Eventually(func() bool {
|
||||
_, err = s.store.UserID(context.Background(), token)
|
||||
return errors.Is(err, session.ErrNotFound)
|
||||
}, 3*time.Second, 50*time.Millisecond)
|
||||
}
|
||||
Reference in New Issue
Block a user