mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
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)
291 lines
8.3 KiB
Go
291 lines
8.3 KiB
Go
package handler_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
"bugulma/backend/internal/testutils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
"gorm.io/gorm"
|
|
|
|
"bugulma/backend/internal/domain"
|
|
"bugulma/backend/internal/handler"
|
|
"bugulma/backend/internal/repository"
|
|
"bugulma/backend/internal/service"
|
|
)
|
|
|
|
var _ = Describe("SiteHandler", func() {
|
|
var (
|
|
siteHandler *handler.SiteHandler
|
|
siteService *service.SiteService
|
|
siteRepo domain.SiteRepository
|
|
organizationRepo domain.OrganizationRepository
|
|
router *gin.Engine
|
|
db *gorm.DB
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
// Setup PostgreSQL test database using pgtestdb
|
|
db = testutils.SetupTestDBForGinkgo(GinkgoT())
|
|
|
|
siteRepo = repository.NewSiteRepository(db)
|
|
organizationRepo = repository.NewOrganizationRepository(db)
|
|
siteService = service.NewSiteService(siteRepo)
|
|
siteHandler = handler.NewSiteHandler(siteService)
|
|
|
|
router = gin.New()
|
|
router.POST("/sites", siteHandler.Create)
|
|
router.GET("/sites/:id", siteHandler.GetByID)
|
|
router.GET("/organizations/:organizationId/sites", siteHandler.GetByOrganization)
|
|
router.GET("/sites/nearby", siteHandler.GetNearby)
|
|
router.GET("/sites", siteHandler.GetAll)
|
|
router.PUT("/sites/:id", siteHandler.Update)
|
|
router.DELETE("/sites/:id", siteHandler.Delete)
|
|
})
|
|
|
|
AfterEach(func() {
|
|
// pgtestdb automatically cleans up the database after each test
|
|
})
|
|
|
|
Describe("Create", func() {
|
|
It("should create a site", func() {
|
|
// Create organization first (required for foreign key constraint)
|
|
org := &domain.Organization{
|
|
ID: "org-1",
|
|
Name: "Test Organization",
|
|
}
|
|
Expect(organizationRepo.Create(context.TODO(), org)).To(Succeed())
|
|
|
|
reqBody := handler.CreateSiteRequest{
|
|
Name: "Test Site",
|
|
Latitude: 52.5200,
|
|
Longitude: 13.4050,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
body, _ := json.Marshal(reqBody)
|
|
req, _ := http.NewRequest("POST", "/sites", bytes.NewBuffer(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
Expect(w.Code).To(Equal(http.StatusCreated))
|
|
|
|
var resp domain.Site
|
|
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(resp.Name).To(Equal("Test Site"))
|
|
Expect(resp.ID).NotTo(BeEmpty())
|
|
})
|
|
})
|
|
|
|
Describe("GetByID", func() {
|
|
It("should return site by ID", func() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Org 1"}
|
|
Expect(organizationRepo.Create(context.TODO(), org)).To(Succeed())
|
|
|
|
site := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Existing Site",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
Expect(siteRepo.Create(context.TODO(), site)).To(Succeed())
|
|
|
|
req, _ := http.NewRequest("GET", "/sites/site-1", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
|
|
var resp domain.Site
|
|
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(resp.Name).To(Equal("Existing Site"))
|
|
})
|
|
})
|
|
|
|
Describe("GetNearby", func() {
|
|
It("should return nearby sites", func() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Org 1"}
|
|
Expect(organizationRepo.Create(context.TODO(), org)).To(Succeed())
|
|
|
|
site1 := &domain.Site{
|
|
ID: "site-1",
|
|
Latitude: 52.5200,
|
|
Longitude: 13.4050,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
Expect(siteRepo.Create(context.TODO(), site1)).To(Succeed())
|
|
|
|
req, _ := http.NewRequest("GET", "/sites/nearby?lat=52.5200&lng=13.4050&radius=10", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
|
|
var resp []domain.Site
|
|
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(resp).To(HaveLen(1))
|
|
})
|
|
})
|
|
|
|
Describe("GetByOrganization", func() {
|
|
It("should return sites for an organization", func() {
|
|
// Create organizations first
|
|
org1 := &domain.Organization{ID: "org-1", Name: "Org 1"}
|
|
org2 := &domain.Organization{ID: "org-2", Name: "Org 2"}
|
|
Expect(organizationRepo.Create(context.TODO(), org1)).To(Succeed())
|
|
Expect(organizationRepo.Create(context.TODO(), org2)).To(Succeed())
|
|
|
|
site1 := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Site 1",
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
site2 := &domain.Site{
|
|
ID: "site-2",
|
|
Name: "Site 2",
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
site3 := &domain.Site{
|
|
ID: "site-3",
|
|
Name: "Site 3",
|
|
OwnerOrganizationID: "org-2",
|
|
}
|
|
Expect(siteRepo.Create(context.TODO(), site1)).To(Succeed())
|
|
Expect(siteRepo.Create(context.TODO(), site2)).To(Succeed())
|
|
Expect(siteRepo.Create(context.TODO(), site3)).To(Succeed())
|
|
|
|
req, _ := http.NewRequest("GET", "/organizations/org-1/sites", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
|
|
var resp []domain.Site
|
|
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(resp).To(HaveLen(2))
|
|
})
|
|
})
|
|
|
|
Describe("GetAll", func() {
|
|
It("should return all sites", func() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Org 1"}
|
|
Expect(organizationRepo.Create(context.TODO(), org)).To(Succeed())
|
|
|
|
site1 := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Site 1",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
site2 := &domain.Site{
|
|
ID: "site-2",
|
|
Name: "Site 2",
|
|
Latitude: 52.53,
|
|
Longitude: 13.406,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
Expect(siteRepo.Create(context.TODO(), site1)).To(Succeed())
|
|
Expect(siteRepo.Create(context.TODO(), site2)).To(Succeed())
|
|
|
|
req, _ := http.NewRequest("GET", "/sites", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
|
|
var resp []domain.Site
|
|
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(resp).To(HaveLen(2))
|
|
})
|
|
})
|
|
|
|
Describe("Update", func() {
|
|
It("should update a site", func() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Org 1"}
|
|
Expect(organizationRepo.Create(context.TODO(), org)).To(Succeed())
|
|
|
|
site := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Original Site",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
Expect(siteRepo.Create(context.TODO(), site)).To(Succeed())
|
|
|
|
updateReq := handler.CreateSiteRequest{
|
|
Name: "Updated Site",
|
|
Latitude: 52.5200,
|
|
Longitude: 13.4050,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
jsonData, _ := json.Marshal(updateReq)
|
|
|
|
req, _ := http.NewRequest("PUT", "/sites/site-1", bytes.NewBuffer(jsonData))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
|
|
// Verify update
|
|
updated, err := siteRepo.GetByID(context.Background(), "site-1")
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(updated.Name).To(Equal("Updated Site"))
|
|
})
|
|
})
|
|
|
|
Describe("Delete", func() {
|
|
It("should delete a site", func() {
|
|
// Create organization first
|
|
org := &domain.Organization{ID: "org-1", Name: "Org 1"}
|
|
Expect(organizationRepo.Create(context.TODO(), org)).To(Succeed())
|
|
|
|
site := &domain.Site{
|
|
ID: "site-1",
|
|
Name: "Site to Delete",
|
|
Latitude: 52.52,
|
|
Longitude: 13.405,
|
|
OwnerOrganizationID: "org-1",
|
|
}
|
|
Expect(siteRepo.Create(context.TODO(), site)).To(Succeed())
|
|
|
|
req, _ := http.NewRequest("DELETE", "/sites/site-1", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
Expect(w.Code).To(Equal(http.StatusNoContent))
|
|
|
|
// Verify it's gone
|
|
_, err := siteRepo.GetByID(context.Background(), "site-1")
|
|
Expect(err).To(HaveOccurred())
|
|
})
|
|
})
|
|
})
|