|
| 1 | +//go:build unit |
| 2 | + |
| 3 | +package admin |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/Wei-Shaw/sub2api/internal/service" |
| 15 | + "github.com/gin-gonic/gin" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +type systemHandlerUpdateServiceStub struct { |
| 20 | + performErr error |
| 21 | + updateInfo *service.UpdateInfo |
| 22 | + checkErr error |
| 23 | + checkForces []bool |
| 24 | + performCall int |
| 25 | +} |
| 26 | + |
| 27 | +func (s *systemHandlerUpdateServiceStub) CheckUpdate(_ context.Context, force bool) (*service.UpdateInfo, error) { |
| 28 | + s.checkForces = append(s.checkForces, force) |
| 29 | + return s.updateInfo, s.checkErr |
| 30 | +} |
| 31 | + |
| 32 | +func (s *systemHandlerUpdateServiceStub) PerformUpdate(context.Context) error { |
| 33 | + s.performCall++ |
| 34 | + return s.performErr |
| 35 | +} |
| 36 | + |
| 37 | +func (s *systemHandlerUpdateServiceStub) Rollback() error { |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +type systemUpdateResponseEnvelope struct { |
| 42 | + Code int `json:"code"` |
| 43 | + Message string `json:"message"` |
| 44 | + Data struct { |
| 45 | + Message string `json:"message"` |
| 46 | + AlreadyUpToDate bool `json:"already_up_to_date"` |
| 47 | + CurrentVersion string `json:"current_version"` |
| 48 | + LatestVersion string `json:"latest_version"` |
| 49 | + OperationID string `json:"operation_id"` |
| 50 | + } `json:"data"` |
| 51 | +} |
| 52 | + |
| 53 | +type systemUpdateErrorEnvelope struct { |
| 54 | + Code int `json:"code"` |
| 55 | + Message string `json:"message"` |
| 56 | +} |
| 57 | + |
| 58 | +func newSystemHandlerTestRouter(t *testing.T, updateSvc *systemHandlerUpdateServiceStub, repo *memoryIdempotencyRepoStub) *gin.Engine { |
| 59 | + t.Helper() |
| 60 | + gin.SetMode(gin.TestMode) |
| 61 | + service.SetDefaultIdempotencyCoordinator(nil) |
| 62 | + t.Cleanup(func() { |
| 63 | + service.SetDefaultIdempotencyCoordinator(nil) |
| 64 | + }) |
| 65 | + |
| 66 | + lockSvc := service.NewSystemOperationLockService(repo, service.IdempotencyConfig{ |
| 67 | + ProcessingTimeout: time.Second, |
| 68 | + SystemOperationTTL: time.Minute, |
| 69 | + }) |
| 70 | + handler := NewSystemHandler(updateSvc, lockSvc) |
| 71 | + |
| 72 | + router := gin.New() |
| 73 | + router.POST("/api/v1/admin/system/update", handler.PerformUpdate) |
| 74 | + return router |
| 75 | +} |
| 76 | + |
| 77 | +func requireSystemLockStatus(t *testing.T, repo *memoryIdempotencyRepoStub, wantStatus string) { |
| 78 | + t.Helper() |
| 79 | + repo.mu.Lock() |
| 80 | + defer repo.mu.Unlock() |
| 81 | + |
| 82 | + for _, record := range repo.data { |
| 83 | + if record.Status == wantStatus { |
| 84 | + return |
| 85 | + } |
| 86 | + } |
| 87 | + t.Fatalf("system lock status %q not found in records: %#v", wantStatus, repo.data) |
| 88 | +} |
| 89 | + |
| 90 | +func TestSystemHandlerPerformUpdateAlreadyUpToDateReturnsOK(t *testing.T) { |
| 91 | + updateSvc := &systemHandlerUpdateServiceStub{ |
| 92 | + performErr: service.ErrNoUpdateAvailable, |
| 93 | + updateInfo: &service.UpdateInfo{ |
| 94 | + CurrentVersion: "0.1.132", |
| 95 | + LatestVersion: "0.1.132", |
| 96 | + HasUpdate: false, |
| 97 | + }, |
| 98 | + } |
| 99 | + repo := newMemoryIdempotencyRepoStub() |
| 100 | + router := newSystemHandlerTestRouter(t, updateSvc, repo) |
| 101 | + |
| 102 | + rec := httptest.NewRecorder() |
| 103 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/system/update", nil) |
| 104 | + req.Header.Set("Idempotency-Key", "already-up-to-date") |
| 105 | + router.ServeHTTP(rec, req) |
| 106 | + |
| 107 | + require.Equal(t, http.StatusOK, rec.Code) |
| 108 | + require.Equal(t, 1, updateSvc.performCall) |
| 109 | + require.Equal(t, []bool{false}, updateSvc.checkForces) |
| 110 | + requireSystemLockStatus(t, repo, service.IdempotencyStatusSucceeded) |
| 111 | + |
| 112 | + var body systemUpdateResponseEnvelope |
| 113 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body)) |
| 114 | + require.Equal(t, 0, body.Code) |
| 115 | + require.Equal(t, "success", body.Message) |
| 116 | + require.Equal(t, "Already up to date", body.Data.Message) |
| 117 | + require.True(t, body.Data.AlreadyUpToDate) |
| 118 | + require.Equal(t, "0.1.132", body.Data.CurrentVersion) |
| 119 | + require.Equal(t, "0.1.132", body.Data.LatestVersion) |
| 120 | + require.NotEmpty(t, body.Data.OperationID) |
| 121 | +} |
| 122 | + |
| 123 | +func TestSystemHandlerPerformUpdateFailureStillReturnsInternalError(t *testing.T) { |
| 124 | + updateSvc := &systemHandlerUpdateServiceStub{ |
| 125 | + performErr: errors.New("download failed"), |
| 126 | + } |
| 127 | + repo := newMemoryIdempotencyRepoStub() |
| 128 | + router := newSystemHandlerTestRouter(t, updateSvc, repo) |
| 129 | + |
| 130 | + rec := httptest.NewRecorder() |
| 131 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/system/update", nil) |
| 132 | + req.Header.Set("Idempotency-Key", "real-failure") |
| 133 | + router.ServeHTTP(rec, req) |
| 134 | + |
| 135 | + require.Equal(t, http.StatusInternalServerError, rec.Code) |
| 136 | + require.Equal(t, 1, updateSvc.performCall) |
| 137 | + require.Empty(t, updateSvc.checkForces) |
| 138 | + requireSystemLockStatus(t, repo, service.IdempotencyStatusFailedRetryable) |
| 139 | + |
| 140 | + var body systemUpdateErrorEnvelope |
| 141 | + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body)) |
| 142 | + require.Equal(t, http.StatusInternalServerError, body.Code) |
| 143 | + require.Equal(t, "internal error", body.Message) |
| 144 | +} |
0 commit comments