add service
Some checks failed
Deploy Smart Search Backend / deploy (push) Failing after 1m54s

This commit is contained in:
vallyenfail
2026-01-17 20:41:37 +03:00
parent 635acd13ac
commit e2968722ed
70 changed files with 7542 additions and 463 deletions

View File

@@ -9,12 +9,17 @@ import (
"github.com/gojuno/minimock/v3"
"github.com/stretchr/testify/suite"
"smart-search-back/internal/mocks"
"smart-search-back/internal/model"
"smart-search-back/internal/service"
"smart-search-back/pkg/crypto"
apperrors "smart-search-back/pkg/errors"
"smart-search-back/pkg/jwt"
"git.techease.ru/Smart-search/smart-search-back/internal/mocks"
"git.techease.ru/Smart-search/smart-search-back/internal/model"
"git.techease.ru/Smart-search/smart-search-back/internal/service"
"git.techease.ru/Smart-search/smart-search-back/pkg/crypto"
apperrors "git.techease.ru/Smart-search/smart-search-back/pkg/errors"
"git.techease.ru/Smart-search/smart-search-back/pkg/jwt"
)
const (
testJWTSecret = "test-jwt-secret-key"
testCryptoSecret = "test-crypto-secret-key"
)
type Suite struct {
@@ -24,6 +29,7 @@ type Suite struct {
authService service.AuthService
userRepo *mocks.UserRepositoryMock
sessionRepo *mocks.SessionRepositoryMock
crypto *crypto.Crypto
}
func newSuite(ctx context.Context) *Suite {
@@ -46,15 +52,17 @@ func (s *Suite) SetupTest() {
s.userRepo = mocks.NewUserRepositoryMock(ctrl)
s.sessionRepo = mocks.NewSessionRepositoryMock(ctrl)
s.crypto = crypto.NewCrypto(testCryptoSecret)
s.authService = service.NewAuthService(s.userRepo, s.sessionRepo)
s.authService = service.NewAuthService(s.userRepo, s.sessionRepo, testJWTSecret, testCryptoSecret)
}
func createTestUser(password string) *model.User {
c := crypto.NewCrypto(testCryptoSecret)
return &model.User{
ID: 1,
Email: "test@example.com",
EmailHash: crypto.EmailHash("test@example.com"),
EmailHash: c.EmailHash("test@example.com"),
PasswordHash: crypto.PasswordHash(password),
CreatedAt: time.Now(),
}
@@ -180,7 +188,7 @@ func (s *Suite) TestAuthService_Login_EmailWithSpacesAndCase() {
password := "testpassword"
normalizedEmail := "test@example.com"
user := createTestUser(password)
user.EmailHash = crypto.EmailHash(normalizedEmail)
user.EmailHash = s.crypto.EmailHash(normalizedEmail)
s.userRepo.FindByEmailHashMock.Return(user, nil)
s.sessionRepo.CreateMock.Return(nil)
@@ -328,12 +336,12 @@ func (s *Suite) TestAuthService_Refresh_UserIDZero() {
}
func (s *Suite) TestAuthService_Validate_Success() {
s.T().Parallel()
userID := 1
accessToken, err := jwt.GenerateAccessToken(userID)
accessToken, err := jwt.GenerateAccessToken(userID, testJWTSecret)
s.NoError(err)
s.sessionRepo.IsAccessTokenValidMock.Return(true, nil)
validatedUserID, validateErr := s.authService.Validate(s.ctx, accessToken)
s.NoError(validateErr)
@@ -370,7 +378,7 @@ func (s *Suite) TestAuthService_Validate_RefreshTokenInsteadOfAccess() {
s.T().Parallel()
userID := 1
refreshToken, err := jwt.GenerateRefreshToken(userID)
refreshToken, err := jwt.GenerateRefreshToken(userID, testJWTSecret)
s.NoError(err)
validatedUserID, validateErr := s.authService.Validate(s.ctx, refreshToken)
@@ -385,11 +393,11 @@ func (s *Suite) TestAuthService_Validate_RefreshTokenInsteadOfAccess() {
}
func (s *Suite) TestAuthService_Validate_UserIDZero() {
s.T().Parallel()
accessToken, err := jwt.GenerateAccessToken(0)
accessToken, err := jwt.GenerateAccessToken(0, testJWTSecret)
s.NoError(err)
s.sessionRepo.IsAccessTokenValidMock.Return(true, nil)
validatedUserID, validateErr := s.authService.Validate(s.ctx, accessToken)
s.NoError(validateErr)
@@ -412,18 +420,18 @@ func (s *Suite) TestAuthService_Validate_InvalidSignature() {
}
func (s *Suite) TestAuthService_Logout_Success() {
s.sessionRepo.RevokeMock.Return(nil)
s.sessionRepo.RevokeByAccessTokenMock.Return(nil)
err := s.authService.Logout(s.ctx, "test-refresh-token")
err := s.authService.Logout(s.ctx, "test-access-token")
s.NoError(err)
}
func (s *Suite) TestAuthService_Logout_DatabaseError() {
dbErr := apperrors.NewInternalError(apperrors.DatabaseError, "failed to revoke session", nil)
s.sessionRepo.RevokeMock.Return(dbErr)
s.sessionRepo.RevokeByAccessTokenMock.Return(dbErr)
err := s.authService.Logout(s.ctx, "test-refresh-token")
err := s.authService.Logout(s.ctx, "test-access-token")
s.Error(err)
@@ -433,7 +441,7 @@ func (s *Suite) TestAuthService_Logout_DatabaseError() {
}
func (s *Suite) TestAuthService_Logout_EmptyToken() {
s.sessionRepo.RevokeMock.Return(nil)
s.sessionRepo.RevokeByAccessTokenMock.Return(nil)
err := s.authService.Logout(s.ctx, "")
@@ -441,7 +449,7 @@ func (s *Suite) TestAuthService_Logout_EmptyToken() {
}
func (s *Suite) TestAuthService_Logout_NonExistentToken() {
s.sessionRepo.RevokeMock.Return(nil)
s.sessionRepo.RevokeByAccessTokenMock.Return(nil)
err := s.authService.Logout(s.ctx, "non-existent-token")