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.
244 lines
7.8 KiB
Go
244 lines
7.8 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/gochat/gochat/internal/service"
|
|
)
|
|
|
|
// setupContactEdgeRouter creates a test router for contact handler edge-case tests.
|
|
|
|
|
|
|
|
func setupContactEdgeRouter(handler *ContactHandler) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
router := gin.New()
|
|
router.Use(gin.Recovery(), mockAuthMiddleware())
|
|
router.POST("/api/v1/accounts/:id/contacts", handler.Create)
|
|
router.PUT("/api/v1/accounts/:id/contacts/:contact_id", handler.Update)
|
|
router.GET("/api/v1/accounts/:id/contacts", handler.List)
|
|
router.GET("/api/v1/accounts/:id/contacts/search", handler.Search)
|
|
router.DELETE("/api/v1/accounts/:id/contacts/:contact_id", handler.Delete)
|
|
return router
|
|
}
|
|
|
|
func newContactEdgeHandler() *ContactHandler {
|
|
return NewContactHandler(&service.ContactService{}, &service.ContactInboxService{}, &service.ContactMergeService{}, &service.ContactNoteService{})
|
|
}
|
|
|
|
// ===========================
|
|
// Create Contact edge cases
|
|
// ===========================
|
|
|
|
func TestContactCreate_EmptyBody(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/contacts", nil)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
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 TestContactCreate_InvalidJSON(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/contacts", bytes.NewBufferString(`{malformed`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
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 TestContactCreate_MissingRequiredName(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
// name is required (validate:"required,min=1")
|
|
w := httptest.NewRecorder()
|
|
body := `{"email":"test@example.com"}`
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/contacts", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
// ShouldBindJSON succeeds (valid JSON syntax), but service.Create will fail due to validation
|
|
// With nil service, it panics → 500 from Recovery middleware
|
|
assert.NotEqual(t, http.StatusOK, w.Code)
|
|
assert.NotEqual(t, http.StatusCreated, w.Code)
|
|
}
|
|
|
|
func TestContactCreate_InvalidEmail(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
// email has validate:"omitempty,email"; "not-an-email" is invalid
|
|
w := httptest.NewRecorder()
|
|
body := `{"name":"John","email":"not-an-email"}`
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/1/contacts", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
// ShouldBindJSON succeeds, but service.Create rejects via validation → nil service panics → 500
|
|
assert.NotEqual(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
func TestContactCreate_InvalidAccountID(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/contacts", bytes.NewBufferString(`{"name":"John"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactCreate_WrongMethod(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
// Create is POST-only; sending GET returns 404 (no route for GET /contacts at same path level as POST)
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PATCH", "/api/v1/accounts/1/contacts", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.True(t, w.Code == http.StatusNotFound || w.Code == http.StatusBadRequest)
|
|
}
|
|
|
|
// ===========================
|
|
// Update Contact edge cases
|
|
// ===========================
|
|
|
|
func TestContactUpdate_EmptyBody(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", "/api/v1/accounts/1/contacts/1", nil)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactUpdate_InvalidJSON(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", "/api/v1/accounts/1/contacts/1", bytes.NewBufferString(`{bad json`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactUpdate_InvalidAccountID(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", "/api/v1/accounts/notanid/contacts/1", bytes.NewBufferString(`{"name":"Jane"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactUpdate_InvalidContactID(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", "/api/v1/accounts/1/contacts/xyz", bytes.NewBufferString(`{"name":"Jane"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func TestContactUpdate_InvalidEmail(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
// email has validate:"omitempty,email"; "bad-email" is invalid
|
|
w := httptest.NewRecorder()
|
|
body := `{"email":"bad-email"}`
|
|
req, _ := http.NewRequest("PUT", "/api/v1/accounts/1/contacts/1", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
router.ServeHTTP(w, req)
|
|
|
|
// ShouldBindJSON succeeds but service rejects → nil service panics → 500
|
|
assert.NotEqual(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
// ===========================
|
|
// Search Contact edge cases
|
|
// ===========================
|
|
|
|
func TestContactSearch_InvalidAccountID(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(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)
|
|
}
|
|
|
|
func TestContactSearch_WrongMethod(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
// Search is GET-only; DELETE should return 404
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/contacts/search?q=test", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.True(t, w.Code == http.StatusNotFound || w.Code == http.StatusBadRequest)
|
|
}
|
|
|
|
// ===========================
|
|
// Delete Contact edge cases
|
|
// ===========================
|
|
|
|
func TestContactDelete_InvalidAccountID(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
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)
|
|
}
|
|
|
|
func TestContactDelete_InvalidContactID(t *testing.T) {
|
|
handler := newContactEdgeHandler()
|
|
router := setupContactEdgeRouter(handler)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", "/api/v1/accounts/1/contacts/notanumber", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
} |