loginHandler_test.go 878 B

12345678910111213141516171819202122232425262728293031323334353637
  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. func init() {
  15. response.Setup()
  16. }
  17. // TC-0012: 缺少必填字段
  18. func TestLoginHandler_MissingFields(t *testing.T) {
  19. svcCtx := svc.NewServiceContext(testutil.GetTestConfig())
  20. handler := LoginHandler(svcCtx)
  21. req := httptest.NewRequest(http.MethodPost, "/api/auth/login", strings.NewReader("{}"))
  22. req.Header.Set("Content-Type", "application/json")
  23. rr := httptest.NewRecorder()
  24. handler.ServeHTTP(rr, req)
  25. var body response.Body
  26. err := json.Unmarshal(rr.Body.Bytes(), &body)
  27. require.NoError(t, err)
  28. assert.Equal(t, 400, body.Code)
  29. assert.Contains(t, body.Msg, "username")
  30. }