Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
117 lines
3.7 KiB
Plaintext
117 lines
3.7 KiB
Plaintext
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
)
|
|
|
|
func setupContactRouter(handler *ContactHandler) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
router.GET("/api/v1/accounts/:id/contacts", handler.List)
|
|
router.GET("/api/v1/accounts/:id/contacts/:contact_id", handler.Get)
|
|
router.POST("/api/v1/accounts/:id/contacts", handler.Create)
|
|
router.PUT("/api/v1/accounts/:id/contacts/:contact_id", handler.Update)
|
|
router.DELETE("/api/v1/accounts/:id/contacts/:contact_id", handler.Delete)
|
|
router.GET("/api/v1/accounts/:id/contacts/search", handler.Search)
|
|
return router
|
|
}
|
|
|
|
func TestContactListBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
|
|
router := setupContactRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
var resp map[string]interface{}
|
|
json.Unmarshal(w.Body.Bytes(), &resp)
|
|
assert.Contains(t, resp, "error")
|
|
}
|
|
|
|
func TestContactGetBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
|
|
router := setupContactRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts/1", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactGetBadContactID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
|
|
router := setupContactRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/1/contacts/abc", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactCreateBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
|
|
router := setupContactRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/contacts", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactUpdateBadIDs(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
|
|
router := setupContactRouter(handler)
|
|
|
|
// Bad account ID
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", "/api/v1/accounts/abc/contacts/1", nil)
|
|
router.ServeHTTP(w, req)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
// Bad contact ID
|
|
w2 := httptest.NewRecorder()
|
|
req2, _ := http.NewRequest("PUT", "/api/v1/accounts/1/contacts/abc", nil)
|
|
router.ServeHTTP(w2, req2)
|
|
assert.Equal(t, http.StatusBadRequest, w2.Code)
|
|
}
|
|
|
|
func TestContactDeleteBadIDs(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
|
|
router := setupContactRouter(handler)
|
|
|
|
// Bad account ID
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/abc/contacts/1", nil)
|
|
router.ServeHTTP(w, req)
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
// Bad contact ID
|
|
w2 := httptest.NewRecorder()
|
|
req2, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/contacts/abc", nil)
|
|
router.ServeHTTP(w2, req2)
|
|
assert.Equal(t, http.StatusBadRequest, w2.Code)
|
|
}
|
|
|
|
func TestContactSearchBadAccountID(t *testing.T) {
|
|
handler := NewContactHandler(&service.ContactService{}, &service.ContactInboxService{})
|
|
router := setupContactRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/contacts/search?q=test", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
} |