turash/bugulma/backend/internal/handler/auth_handler_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

109 lines
2.9 KiB
Go

package handler_test
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/gin-gonic/gin"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"bugulma/backend/internal/domain"
"bugulma/backend/internal/handler"
"bugulma/backend/internal/repository"
"bugulma/backend/internal/service"
"bugulma/backend/internal/testutils"
)
var _ = Describe("AuthHandler", func() {
var (
authHandler *handler.AuthHandler
userRepo domain.UserRepository
authService *service.AuthService
router *gin.Engine
db *gorm.DB
)
BeforeEach(func() {
gin.SetMode(gin.TestMode)
// Setup PostgreSQL test database using pgtestdb
db = testutils.SetupTestDBForGinkgo(GinkgoT())
userRepo = repository.NewUserRepository(db)
authService = service.NewAuthService(userRepo, "secret-key")
authHandler = handler.NewAuthHandler(authService)
router = gin.New()
router.POST("/login", authHandler.Login)
// Create a test user
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("password123"), bcrypt.DefaultCost)
user := &domain.User{
ID: "user-1",
Email: "test@example.com",
Password: string(hashedPassword),
Name: "Test User",
Role: domain.UserRoleUser,
}
err := userRepo.Create(context.Background(), user)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
// pgtestdb automatically cleans up the database after each test
})
Describe("Login", func() {
It("should return token for valid credentials", func() {
reqBody := handler.LoginRequest{
Email: "test@example.com",
Password: "password123",
}
body, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
Expect(w.Code).To(Equal(http.StatusOK))
var resp handler.LoginResponse
err := json.Unmarshal(w.Body.Bytes(), &resp)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Token).NotTo(BeEmpty())
Expect(resp.User.Email).To(Equal("test@example.com"))
})
It("should return 401 for invalid credentials", func() {
reqBody := handler.LoginRequest{
Email: "test@example.com",
Password: "wrongpassword",
}
body, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST", "/login", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
Expect(w.Code).To(Equal(http.StatusUnauthorized))
})
It("should return 400 for invalid request body", func() {
req, _ := http.NewRequest("POST", "/login", bytes.NewBufferString("invalid json"))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
Expect(w.Code).To(Equal(http.StatusBadRequest))
})
})
})