loginHandler_test.go 923 B

1234567891011121314151617181920212223242526272829303132333435363738
  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.False(t, body.Success)
  29. assert.Equal(t, 400, body.ErrorCode)
  30. assert.Contains(t, body.ErrorMessage, "username")
  31. }