account_upstream_billing_probe_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package admin
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/Wei-Shaw/sub2api/internal/service"
  9. "github.com/gin-gonic/gin"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func setupUpstreamBillingProbeRouter() *gin.Engine {
  13. gin.SetMode(gin.TestMode)
  14. handler := NewAccountHandler(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
  15. handler.SetUpstreamBillingProbeService(service.NewUpstreamBillingProbeService(nil, nil, nil))
  16. router := gin.New()
  17. router.GET("/admin/accounts/upstream-billing-probe/settings", handler.GetUpstreamBillingProbeSettings)
  18. router.POST("/admin/accounts/upstream-billing-probe/batch", handler.ProbeUpstreamBillingBatch)
  19. router.PUT("/admin/accounts/:id/upstream-billing-probe", handler.SetUpstreamBillingProbeEnabled)
  20. return router
  21. }
  22. func TestAccountHandlerGetUpstreamBillingProbeSettingsReturnsDefaults(t *testing.T) {
  23. router := setupUpstreamBillingProbeRouter()
  24. recorder := httptest.NewRecorder()
  25. router.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/admin/accounts/upstream-billing-probe/settings", nil))
  26. require.Equal(t, http.StatusOK, recorder.Code)
  27. var response struct {
  28. Data service.UpstreamBillingProbeSettings `json:"data"`
  29. }
  30. require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &response))
  31. require.True(t, response.Data.Enabled)
  32. require.Equal(t, 30, response.Data.IntervalMinutes)
  33. }
  34. func TestAccountHandlerProbeUpstreamBillingBatchValidatesIDs(t *testing.T) {
  35. router := setupUpstreamBillingProbeRouter()
  36. for _, body := range []string{`{"account_ids":[]}`, `{"account_ids":[0]}`} {
  37. recorder := httptest.NewRecorder()
  38. request := httptest.NewRequest(http.MethodPost, "/admin/accounts/upstream-billing-probe/batch", bytes.NewBufferString(body))
  39. request.Header.Set("Content-Type", "application/json")
  40. router.ServeHTTP(recorder, request)
  41. require.Equal(t, http.StatusBadRequest, recorder.Code)
  42. }
  43. }
  44. func TestAccountHandlerSetUpstreamBillingProbeEnabledRejectsInvalidID(t *testing.T) {
  45. router := setupUpstreamBillingProbeRouter()
  46. recorder := httptest.NewRecorder()
  47. request := httptest.NewRequest(http.MethodPut, "/admin/accounts/not-an-id/upstream-billing-probe", bytes.NewBufferString(`{"enabled":true}`))
  48. request.Header.Set("Content-Type", "application/json")
  49. router.ServeHTTP(recorder, request)
  50. require.Equal(t, http.StatusBadRequest, recorder.Code)
  51. }
  52. func TestAccountHandlerSetUpstreamBillingProbeEnabledRequiresValue(t *testing.T) {
  53. router := setupUpstreamBillingProbeRouter()
  54. recorder := httptest.NewRecorder()
  55. request := httptest.NewRequest(http.MethodPut, "/admin/accounts/1/upstream-billing-probe", bytes.NewBufferString(`{}`))
  56. request.Header.Set("Content-Type", "application/json")
  57. router.ServeHTTP(recorder, request)
  58. require.Equal(t, http.StatusBadRequest, recorder.Code)
  59. }