Files
mtusi2026lab4/internal/session/session_integration_test.go
T
2026-10-02 17:49:28 +03:00

73 lines
1.7 KiB
Go

package session_test
import (
"context"
"errors"
"os"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/suite"
"gitea.privcloud.tech/dizi/mtusi2026lab4/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)
}