turash/bugulma/backend/internal/service/auth_service_test.go
Damir Mukimov 000eab4740
Major repository reorganization and missing backend endpoints implementation
Repository Structure:
- Move files from cluttered root directory into organized structure
- Create archive/ for archived data and scraper results
- Create bugulma/ for the complete application (frontend + backend)
- Create data/ for sample datasets and reference materials
- Create docs/ for comprehensive documentation structure
- Create scripts/ for utility scripts and API tools

Backend Implementation:
- Implement 3 missing backend endpoints identified in gap analysis:
  * GET /api/v1/organizations/{id}/matching/direct - Direct symbiosis matches
  * GET /api/v1/users/me/organizations - User organizations
  * POST /api/v1/proposals/{id}/status - Update proposal status
- Add complete proposal domain model, repository, and service layers
- Create database migration for proposals table
- Fix CLI server command registration issue

API Documentation:
- Add comprehensive proposals.md API documentation
- Update README.md with Users and Proposals API sections
- Document all request/response formats, error codes, and business rules

Code Quality:
- Follow existing Go backend architecture patterns
- Add proper error handling and validation
- Match frontend expected response schemas
- Maintain clean separation of concerns (handler -> service -> repository)
2025-11-25 06:01:16 +01:00

126 lines
3.8 KiB
Go

package service_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"golang.org/x/crypto/bcrypt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"bugulma/backend/internal/domain"
"bugulma/backend/internal/repository"
"bugulma/backend/internal/service"
)
type AuthServiceTestSuite struct {
suite.Suite
db *gorm.DB
userRepo domain.UserRepository
authSvc *service.AuthService
}
func (suite *AuthServiceTestSuite) SetupTest() {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
suite.Require().NoError(err)
// Migrate
err = db.AutoMigrate(&domain.User{})
suite.Require().NoError(err)
suite.db = db
suite.userRepo = repository.NewUserRepository(db)
suite.authSvc = service.NewAuthService(suite.userRepo, "test-secret-key")
}
func (suite *AuthServiceTestSuite) TearDownTest() {
sqlDB, _ := suite.db.DB()
sqlDB.Close()
}
func (suite *AuthServiceTestSuite) TestLoginSuccess() {
// Create a test user
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("password123"), bcrypt.DefaultCost)
user := &domain.User{
ID: "user-1",
Email: "test@example.com",
Name: "Test User",
Password: string(hashedPassword),
Role: domain.UserRoleUser,
}
err := suite.userRepo.Create(context.Background(), user)
suite.Require().NoError(err)
// Test login
token, returnedUser, err := suite.authSvc.Login(context.Background(), "test@example.com", "password123")
assert.NoError(suite.T(), err)
assert.NotEmpty(suite.T(), token)
assert.Equal(suite.T(), user.ID, returnedUser.ID)
assert.Equal(suite.T(), user.Email, returnedUser.Email)
}
func (suite *AuthServiceTestSuite) TestLoginInvalidEmail() {
token, user, err := suite.authSvc.Login(context.Background(), "nonexistent@example.com", "password123")
assert.Error(suite.T(), err)
assert.Empty(suite.T(), token)
assert.Nil(suite.T(), user)
assert.Equal(suite.T(), "invalid credentials", err.Error())
}
func (suite *AuthServiceTestSuite) TestLoginInvalidPassword() {
// Create a test user
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("password123"), bcrypt.DefaultCost)
user := &domain.User{
ID: "user-1",
Email: "test@example.com",
Name: "Test User",
Password: string(hashedPassword),
Role: domain.UserRoleUser,
}
err := suite.userRepo.Create(context.Background(), user)
suite.Require().NoError(err)
// Test login with wrong password
token, returnedUser, err := suite.authSvc.Login(context.Background(), "test@example.com", "wrongpassword")
assert.Error(suite.T(), err)
assert.Empty(suite.T(), token)
assert.Nil(suite.T(), returnedUser)
assert.Equal(suite.T(), "invalid credentials", err.Error())
}
func (suite *AuthServiceTestSuite) TestValidateTokenSuccess() {
// Create a test user
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("password123"), bcrypt.DefaultCost)
user := &domain.User{
ID: "user-1",
Email: "test@example.com",
Name: "Test User",
Password: string(hashedPassword),
Role: domain.UserRoleUser,
}
err := suite.userRepo.Create(context.Background(), user)
suite.Require().NoError(err)
// Login to get token
token, _, err := suite.authSvc.Login(context.Background(), "test@example.com", "password123")
suite.Require().NoError(err)
// Validate token
returnedUser, err := suite.authSvc.ValidateToken(context.Background(), token)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), user.ID, returnedUser.ID)
assert.Equal(suite.T(), user.Email, returnedUser.Email)
}
func (suite *AuthServiceTestSuite) TestValidateTokenInvalid() {
returnedUser, err := suite.authSvc.ValidateToken(context.Background(), "invalid-token")
assert.Error(suite.T(), err)
assert.Nil(suite.T(), returnedUser)
}
func TestAuthServiceTestSuite(t *testing.T) {
suite.Run(t, new(AuthServiceTestSuite))
}