package controllers_test import ( "context" "testing" "time" "gitea.badhouseplants.net/softplayer/softplayer-backend/internal/controllers" "github.com/google/uuid" "github.com/stretchr/testify/assert" ) func newTestTokensController(ctx context.Context) *controllers.TokenController { return &controllers.TokenController{ DB: newTestDbConnection(ctx), Redis: newTestRedisConnection(), } } func TestIntegrationCreateToken_Success(t *testing.T) { // Create a user for the token ctrlAccount := newTestAccountController(t.Context()) accountData := &controllers.AccountData{ Password: "qwertyu9", Email: newTestUniqueEmail("accounts"), } id, err := ctrlAccount.Create(t.Context(), accountData) assert.NoError(t, err) tokenData := &controllers.TokenData{ Name: "Test Token", UserID: id, ExpiresAt: time.Now().Add(time.Second * 5), Scopes: map[string][]string{ "Test": {"test", "test2"}, }, } ctrl := newTestTokensController(t.Context()) tokenVal, tokenID, err := ctrl.Create(t.Context(), tokenData) assert.NoError(t, err) assert.NotEmpty(t, tokenID) assert.NotEmpty(t, tokenVal) } func TestIntegrationCreateToken_UserNotExist(t *testing.T) { tokenData := &controllers.TokenData{ Name: "Test Token", UserID: uuid.NewString(), ExpiresAt: time.Now().Add(time.Second * 5), Scopes: map[string][]string{ "Test": {"test", "test2"}, }, } ctrl := newTestTokensController(t.Context()) tokenVal, tokenID, err := ctrl.Create(t.Context(), tokenData) assert.Error(t, err) assert.ErrorIs(t, err, controllers.ErrUserNotFound) assert.Empty(t, tokenID) assert.Empty(t, tokenVal) } func TestIntegrationGetToken_Success(t *testing.T) { // Create a user for the token ctrlAccount := newTestAccountController(t.Context()) accountData := &controllers.AccountData{ Password: "qwertyu9", Email: newTestUniqueEmail("accounts"), } now := time.Now() userID, err := ctrlAccount.Create(t.Context(), accountData) assert.NoError(t, err) tokenData := &controllers.TokenData{ Name: "Test Token", UserID: userID, ExpiresAt: time.Now().Add(time.Second * 5), Scopes: map[string][]string{ "Test": {"test", "test2"}, }, } ctrl := newTestTokensController(t.Context()) _, tokenID, err := ctrl.Create(t.Context(), tokenData) assert.NoError(t, err) token, err := ctrl.Get(t.Context(), tokenID, userID) assert.NoError(t, err) assert.Equal(t, now.Truncate(time.Second), token.GeneratedAt.Truncate(time.Second)) assert.Equal(t, now.Truncate(time.Second), token.CreatedAt.Truncate(time.Second)) assert.Equal(t, tokenData.Scopes, token.Scopes) assert.Equal(t, tokenData.Name, token.Name) assert.Equal(t, tokenData.ExpiresAt.Truncate(time.Second), token.ExpiresAt.Truncate(time.Second)) } func TestIntegrationGetToken_NotExists(t *testing.T) { ctrl := newTestTokensController(t.Context()) token, err := ctrl.Get(t.Context(), uuid.NewString(), uuid.NewString()) assert.Error(t, err) assert.ErrorIs(t, err, controllers.ErrTokenNotFound) assert.Empty(t, token) } func TestIntegrationVerifyTokenOwner_Success(t *testing.T) { // Create a user for the token ctrlAccount := newTestAccountController(t.Context()) accountData := &controllers.AccountData{ Password: "qwertyu9", Email: newTestUniqueEmail("accounts"), } userID, err := ctrlAccount.Create(t.Context(), accountData) assert.NoError(t, err) tokenData := &controllers.TokenData{ Name: "Test Token", UserID: userID, ExpiresAt: time.Now().Add(time.Second * 5), Scopes: map[string][]string{ "Test": {"test", "test2"}, }, } ctrl := newTestTokensController(t.Context()) _, tokenID, err := ctrl.Create(t.Context(), tokenData) assert.NoError(t, err) assert.NoError(t, ctrl.VerifyTokenOwner(t.Context(), userID, tokenID)) } func TestIntegrationVerifyTokenOwner_WrongOwner(t *testing.T) { // Create a user for the token ctrlAccount := newTestAccountController(t.Context()) accountData := &controllers.AccountData{ Password: "qwertyu9", Email: newTestUniqueEmail("accounts"), } secondAccountData := &controllers.AccountData{ Password: "qwertyu9", Email: newTestUniqueEmail("accounts"), } userID, err := ctrlAccount.Create(t.Context(), accountData) assert.NoError(t, err) secondUserID, err := ctrlAccount.Create(t.Context(), secondAccountData) assert.NoError(t, err) tokenData := &controllers.TokenData{ Name: "Test Token", UserID: userID, ExpiresAt: time.Now().Add(time.Second * 5), Scopes: map[string][]string{ "Test": {"test", "test2"}, }, } ctrl := newTestTokensController(t.Context()) _, tokenID, err := ctrl.Create(t.Context(), tokenData) assert.NoError(t, err) assert.ErrorIs(t, ctrl.VerifyTokenOwner(t.Context(), secondUserID, tokenID), controllers.ErrUserTokenMismatch) }