|
| 1 | +package controllers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/envelope-zero/backend/v3/internal/types" |
| 10 | + "github.com/envelope-zero/backend/v3/pkg/models" |
| 11 | + "github.com/envelope-zero/backend/v3/test" |
| 12 | + "github.com/shopspring/decimal" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func (suite *TestSuiteStandard) TestCleanupV3() { |
| 17 | + _ = suite.createTestBudget(models.BudgetCreate{}) |
| 18 | + account := suite.createTestAccount(models.AccountCreate{Name: "TestCleanup"}) |
| 19 | + _ = suite.createTestCategory(models.CategoryCreate{}) |
| 20 | + envelope := suite.createTestEnvelope(models.EnvelopeCreate{}) |
| 21 | + _ = suite.createTestAllocation(models.AllocationCreate{}) |
| 22 | + _ = suite.createTestTransaction(models.TransactionCreate{Amount: decimal.NewFromFloat(17.32)}) |
| 23 | + _ = suite.createTestMonthConfig(envelope.Data.ID, types.NewMonth(time.Now().Year(), time.Now().Month()), models.MonthConfigCreate{}) |
| 24 | + _ = suite.createTestMatchRule(suite.T(), models.MatchRuleCreate{AccountID: account.Data.ID, Match: "Delete me"}) |
| 25 | + |
| 26 | + tests := []string{ |
| 27 | + "http://example.com/v3/budgets", |
| 28 | + "http://example.com/v1/accounts", |
| 29 | + "http://example.com/v1/categories", |
| 30 | + "http://example.com/v3/transactions", |
| 31 | + "http://example.com/v1/envelopes", |
| 32 | + "http://example.com/v1/allocations", |
| 33 | + "http://example.com/v1/month-configs", |
| 34 | + "http://example.com/v3/match-rules", |
| 35 | + } |
| 36 | + |
| 37 | + // Delete |
| 38 | + recorder := test.Request(suite.controller, suite.T(), http.MethodDelete, "http://example.com/v3?confirm=yes-please-delete-everything", "") |
| 39 | + assertHTTPStatus(suite.T(), &recorder, http.StatusNoContent) |
| 40 | + |
| 41 | + // Verify |
| 42 | + for _, tt := range tests { |
| 43 | + suite.T().Run(tt, func(t *testing.T) { |
| 44 | + recorder := test.Request(suite.controller, suite.T(), http.MethodGet, tt, "") |
| 45 | + assertHTTPStatus(suite.T(), &recorder, http.StatusOK) |
| 46 | + |
| 47 | + var response struct { |
| 48 | + Data []any `json:"data"` |
| 49 | + } |
| 50 | + |
| 51 | + suite.decodeResponse(&recorder, &response) |
| 52 | + assert.Len(t, response.Data, 0, "There are resources left for type %s", tt) |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (suite *TestSuiteStandard) TestCleanupV3Fails() { |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + path string |
| 61 | + }{ |
| 62 | + {"Invalid path", "confirm=2"}, |
| 63 | + {"Confirmation wrong", "confirm=invalid-confirmation"}, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + suite.T().Run(tt.name, func(t *testing.T) { |
| 68 | + recorder := test.Request(suite.controller, t, http.MethodDelete, fmt.Sprintf("http://example.com/v3?%s", tt.path), "") |
| 69 | + assertHTTPStatus(suite.T(), &recorder, http.StatusBadRequest) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func (suite *TestSuiteStandard) TestCleanupV3DBError() { |
| 75 | + suite.CloseDB() |
| 76 | + |
| 77 | + recorder := test.Request(suite.controller, suite.T(), http.MethodDelete, "http://example.com/v3?confirm=yes-please-delete-everything", "") |
| 78 | + assertHTTPStatus(suite.T(), &recorder, http.StatusInternalServerError) |
| 79 | +} |
0 commit comments