Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Qaleka committed Dec 21, 2024
1 parent 639e652 commit 84c2b95
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 179 deletions.
19 changes: 19 additions & 0 deletions microservices/auth_service/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/golang-jwt/jwt"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"time"
)

type MockJwtTokenService struct {
Expand All @@ -16,6 +17,24 @@ type MockJwtTokenService struct {
MockParseSecretGetter func(token *jwt.Token) (interface{}, error)
}

type MockRedisStore struct {
MockGet func(ctx context.Context, sessionID string) (domain.SessionData, error)
MockSet func(ctx context.Context, sessionID string, data domain.SessionData, ttl time.Duration) error
MockDelete func(ctx context.Context, sessionID string) error
}

func (m *MockRedisStore) Get(ctx context.Context, sessionID string) (domain.SessionData, error) {
return m.MockGet(ctx, sessionID)
}

func (m *MockRedisStore) Set(ctx context.Context, sessionID string, data domain.SessionData, ttl time.Duration) error {
return m.MockSet(ctx, sessionID, data, ttl)
}

func (m *MockRedisStore) Delete(ctx context.Context, sessionID string) error {
return m.MockDelete(ctx, sessionID)
}

func (m *MockJwtTokenService) Create(session_id string, tokenExpTime int64) (string, error) {
return m.MockCreate(session_id, tokenExpTime)
}
Expand Down
184 changes: 5 additions & 179 deletions microservices/auth_service/usecase/auth_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func TestRegisterUser(t *testing.T) {

mockAuthRepo := &mocks.MockAuthRepository{}
mockMinioService := &mocks.MockMinioService{}
mockRedis := &mocks.MockRedisStore{}

uc := NewAuthUseCase(mockAuthRepo, mockMinioService)
uc := NewAuthUseCase(mockAuthRepo, mockMinioService, mockRedis)
ctx := context.TODO()

// Тест-кейс 1: Успешная регистрация
Expand Down Expand Up @@ -266,7 +267,7 @@ func TestLoginUser(t *testing.T) {
}()
mockAuthRepo := &mocks.MockAuthRepository{}

uc := NewAuthUseCase(mockAuthRepo, nil)
uc := NewAuthUseCase(mockAuthRepo, nil, nil)
ctx := context.TODO()

// Тест-кейс 1: Успешный вход
Expand Down Expand Up @@ -445,184 +446,9 @@ func TestLoginUser(t *testing.T) {
})
}

func TestPutUser(t *testing.T) {
if err := logger.InitLoggers(); err != nil {
log.Fatalf("Failed to initialize loggers: %v", err)
}
defer func() {
err := logger.SyncLoggers()
if err != nil {
return
}
}()

mockAuthRepo := &mocks.MockAuthRepository{}
mockMinioService := &mocks.MockMinioService{}

uc := NewAuthUseCase(mockAuthRepo, mockMinioService)
ctx := context.TODO()

validAvatar, _ := GenerateImage("jpeg", 2000, 2000)
invalidAvatar, _ := GenerateImage("jpeg", 5000, 5000)

creds := &domain.User{
Username: "testuser",
Email: "[email protected]",
Password: "ValidPassword123",
Name: "Test User",
Avatar: "",
UUID: "valid-uuid",
}

userID := "test-uuid"

// Тест-кейс 1: Успешное обновление пользователя без аватара
t.Run("Successful Update Without Avatar", func(t *testing.T) {
mockAuthRepo.PutUserFunc = func(ctx context.Context, user *domain.User, userID string) error {
return nil
}

err := uc.PutUser(ctx, creds, userID, nil)
assert.NoError(t, err)
})

// Тест-кейс 2: Успешное обновление пользователя с аватаром
t.Run("Successful Update With Avatar", func(t *testing.T) {
mockAuthRepo.PutUserFunc = func(ctx context.Context, user *domain.User, userID string) error {
return nil
}
mockMinioService.UploadFileFunc = func(file []byte, contentType, id string) (string, error) {
return "path/to/image.jpg", nil
}

err := uc.PutUser(ctx, creds, userID, validAvatar)
assert.NoError(t, err)
assert.Equal(t, "/images/path/to/image.jpg", creds.Avatar)
})

// Тест-кейс 3: Неверные символы в данных пользователя
t.Run("Invalid Characters in User Data", func(t *testing.T) {
invalidCreds := &domain.User{
UUID: "invalid<>uuid",
Avatar: "invalid<>avatar",
}

err := uc.PutUser(ctx, invalidCreds, userID, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "input contains invalid characters")
})

// Тест-кейс 4: Превышение длины данных
t.Run("Exceeds Character Limit", func(t *testing.T) {
longString := make([]byte, 256)
for i := range longString {
longString[i] = 'a'
}

invalidCreds := &domain.User{
Username: string(longString),
}

err := uc.PutUser(ctx, invalidCreds, userID, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "input exceeds character limit")
})

// Тест-кейс 5: Неверный формат аватара
t.Run("Invalid Avatar Format", func(t *testing.T) {
mockMinioService.UploadFileFunc = func(file []byte, contentType, id string) (string, error) {
return "", nil
}

creds = &domain.User{
Username: "testuser",
Email: "[email protected]",
Password: "ValidPassword123",
Name: "Test User",
Avatar: "",
UUID: "valid-uuid",
}

err := uc.PutUser(ctx, creds, userID, invalidAvatar)
assert.Error(t, err)
assert.Contains(t, err.Error(), "image resolution exceeds maximum allowed size of 2000 x 2000")
})

// Тест-кейс 6: Ошибка загрузки аватара
t.Run("Avatar Upload Failure", func(t *testing.T) {
mockMinioService.UploadFileFunc = func(file []byte, contentType, id string) (string, error) {
return "", errors.New("upload error")
}

err := uc.PutUser(ctx, creds, userID, validAvatar)
assert.Error(t, err)
assert.Equal(t, "failed to upload file", err.Error())
})

// Тест-кейс 7: Ошибка удаления файла при откате
t.Run("Avatar Deletion Failure on Rollback", func(t *testing.T) {
mockAuthRepo.PutUserFunc = func(ctx context.Context, user *domain.User, userID string) error {
return errors.New("db error")
}
mockMinioService.UploadFileFunc = func(file []byte, contentType, id string) (string, error) {
return "path/to/image.jpg", nil
}
mockMinioService.DeleteFileFunc = func(filePath string) error {
return errors.New("deletion error")
}

err := uc.PutUser(ctx, creds, userID, validAvatar)
assert.Error(t, err)
assert.Equal(t, "failed to delete file", err.Error())
})

// Тест-кейс 8: Ошибка обновления пользователя в базе данных
t.Run("Database Update Failure", func(t *testing.T) {
mockAuthRepo.PutUserFunc = func(ctx context.Context, user *domain.User, userID string) error {
return errors.New("db error")
}
mockMinioService.UploadFileFunc = func(file []byte, contentType, id string) (string, error) {
return "path/to/image.jpg", nil
}
mockMinioService.DeleteFileFunc = func(filePath string) error {
return nil
}

creds = &domain.User{
Username: "testuser",
Email: "[email protected]",
Password: "ValidPassword123",
Name: "Test User",
Avatar: "",
UUID: "valid-uuid",
}

err := uc.PutUser(ctx, creds, userID, validAvatar)
assert.Error(t, err)
assert.Equal(t, "db error", err.Error())
})

// Тест-кейс 9: Ошибки валидации полей
t.Run("Validation Errors", func(t *testing.T) {
creds = &domain.User{
Username: "u", // Слишком короткий логин
Password: "123", // Слишком простой пароль
Email: "invalid_email", // Некорректный email
Name: "Invalid<>Name", // Некорректное имя
}

err := uc.PutUser(ctx, creds, userID, validAvatar)
assert.Error(t, err)
assert.Contains(t, err.Error(), "username")
assert.Contains(t, err.Error(), "email")
assert.Contains(t, err.Error(), "password")
assert.Contains(t, err.Error(), "name")
})
}

func TestGetAllUser(t *testing.T) {
mockAuthRepo := &mocks.MockAuthRepository{}
uc := NewAuthUseCase(mockAuthRepo, nil)
uc := NewAuthUseCase(mockAuthRepo, nil, nil)
ctx := context.TODO()

// Тест-кейс 1: Успешное получение всех пользователей
Expand Down Expand Up @@ -656,7 +482,7 @@ func TestGetUserById(t *testing.T) {
}()

mockAuthRepo := &mocks.MockAuthRepository{}
uc := NewAuthUseCase(mockAuthRepo, nil)
uc := NewAuthUseCase(mockAuthRepo, nil, nil)
ctx := context.TODO()

// Успешный тест-кейс
Expand Down

0 comments on commit 84c2b95

Please sign in to comment.