adminLoginHandler_test.go 915 B

12345678910111213141516171819202122232425262728293031323334
  1. package pub
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. "perms-system-server/internal/response"
  9. "perms-system-server/internal/svc"
  10. "perms-system-server/internal/testutil"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. // TC-0023: 缺少必填字段(adminLogin)
  15. func TestAdminLoginHandler_MissingFields(t *testing.T) {
  16. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  17. handler := AdminLoginHandler(svcCtx)
  18. req := httptest.NewRequest(http.MethodPost, "/api/auth/adminLogin", strings.NewReader("{}"))
  19. req.Header.Set("Content-Type", "application/json")
  20. rr := httptest.NewRecorder()
  21. handler.ServeHTTP(rr, req)
  22. var body response.Body
  23. err := json.Unmarshal(rr.Body.Bytes(), &body)
  24. require.NoError(t, err)
  25. assert.False(t, body.Success)
  26. assert.Equal(t, 400, body.ErrorCode)
  27. assert.Contains(t, body.ErrorMessage, "username")
  28. }