|
9 | 9 | "github.com/stretchr/testify/assert"
|
10 | 10 | "github.com/stretchr/testify/require"
|
11 | 11 | v1API "github.com/supabase/cli/pkg/api"
|
| 12 | + "github.com/supabase/cli/pkg/cast" |
12 | 13 | )
|
13 | 14 |
|
14 | 15 | func TestUpdateApi(t *testing.T) {
|
@@ -63,3 +64,155 @@ func TestUpdateApi(t *testing.T) {
|
63 | 64 | assert.True(t, gock.IsDone())
|
64 | 65 | })
|
65 | 66 | }
|
| 67 | + |
| 68 | +func TestUpdateDbConfig(t *testing.T) { |
| 69 | + server := "http://localhost" |
| 70 | + client, err := v1API.NewClientWithResponses(server) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + t.Run("updates remote DB config", func(t *testing.T) { |
| 74 | + updater := NewConfigUpdater(*client) |
| 75 | + // Setup mock server |
| 76 | + defer gock.Off() |
| 77 | + gock.New(server). |
| 78 | + Get("/v1/projects/test-project/config/database"). |
| 79 | + Reply(http.StatusOK). |
| 80 | + JSON(v1API.PostgresConfigResponse{}) |
| 81 | + gock.New(server). |
| 82 | + Put("/v1/projects/test-project/config/database"). |
| 83 | + Reply(http.StatusOK). |
| 84 | + JSON(v1API.PostgresConfigResponse{ |
| 85 | + MaxConnections: cast.Ptr(cast.UintToInt(100)), |
| 86 | + }) |
| 87 | + // Run test |
| 88 | + err := updater.UpdateDbConfig(context.Background(), "test-project", db{ |
| 89 | + Settings: settings{ |
| 90 | + MaxConnections: cast.Ptr(cast.IntToUint(100)), |
| 91 | + }, |
| 92 | + }) |
| 93 | + // Check result |
| 94 | + assert.NoError(t, err) |
| 95 | + assert.True(t, gock.IsDone()) |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("skips update if no diff in DB config", func(t *testing.T) { |
| 99 | + updater := NewConfigUpdater(*client) |
| 100 | + // Setup mock server |
| 101 | + defer gock.Off() |
| 102 | + gock.New(server). |
| 103 | + Get("/v1/projects/test-project/config/database"). |
| 104 | + Reply(http.StatusOK). |
| 105 | + JSON(v1API.PostgresConfigResponse{ |
| 106 | + MaxConnections: cast.Ptr(cast.UintToInt(100)), |
| 107 | + }) |
| 108 | + // Run test |
| 109 | + err := updater.UpdateDbConfig(context.Background(), "test-project", db{ |
| 110 | + Settings: settings{ |
| 111 | + MaxConnections: cast.Ptr(cast.IntToUint(100)), |
| 112 | + }, |
| 113 | + }) |
| 114 | + // Check result |
| 115 | + assert.NoError(t, err) |
| 116 | + assert.True(t, gock.IsDone()) |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func TestUpdateExperimentalConfig(t *testing.T) { |
| 121 | + server := "http://localhost" |
| 122 | + client, err := v1API.NewClientWithResponses(server) |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + t.Run("enables webhooks", func(t *testing.T) { |
| 126 | + updater := NewConfigUpdater(*client) |
| 127 | + // Setup mock server |
| 128 | + defer gock.Off() |
| 129 | + gock.New(server). |
| 130 | + Post("/v1/projects/test-project/database/webhooks/enable"). |
| 131 | + Reply(http.StatusOK). |
| 132 | + JSON(map[string]interface{}{}) |
| 133 | + // Run test |
| 134 | + err := updater.UpdateExperimentalConfig(context.Background(), "test-project", experimental{ |
| 135 | + Webhooks: &webhooks{ |
| 136 | + Enabled: true, |
| 137 | + }, |
| 138 | + }) |
| 139 | + // Check result |
| 140 | + assert.NoError(t, err) |
| 141 | + assert.True(t, gock.IsDone()) |
| 142 | + }) |
| 143 | + |
| 144 | + t.Run("skips update if webhooks not enabled", func(t *testing.T) { |
| 145 | + updater := NewConfigUpdater(*client) |
| 146 | + // Run test |
| 147 | + err := updater.UpdateExperimentalConfig(context.Background(), "test-project", experimental{ |
| 148 | + Webhooks: &webhooks{ |
| 149 | + Enabled: false, |
| 150 | + }, |
| 151 | + }) |
| 152 | + // Check result |
| 153 | + assert.NoError(t, err) |
| 154 | + assert.True(t, gock.IsDone()) |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +func TestUpdateRemoteConfig(t *testing.T) { |
| 159 | + server := "http://localhost" |
| 160 | + client, err := v1API.NewClientWithResponses(server) |
| 161 | + require.NoError(t, err) |
| 162 | + |
| 163 | + t.Run("updates all configs", func(t *testing.T) { |
| 164 | + updater := NewConfigUpdater(*client) |
| 165 | + // Setup mock server |
| 166 | + defer gock.Off() |
| 167 | + // API config |
| 168 | + gock.New(server). |
| 169 | + Get("/v1/projects/test-project/postgrest"). |
| 170 | + Reply(http.StatusOK). |
| 171 | + JSON(v1API.PostgrestConfigWithJWTSecretResponse{}) |
| 172 | + gock.New(server). |
| 173 | + Patch("/v1/projects/test-project/postgrest"). |
| 174 | + Reply(http.StatusOK). |
| 175 | + JSON(v1API.PostgrestConfigWithJWTSecretResponse{ |
| 176 | + DbSchema: "public", |
| 177 | + MaxRows: 1000, |
| 178 | + }) |
| 179 | + // DB config |
| 180 | + gock.New(server). |
| 181 | + Get("/v1/projects/test-project/config/database"). |
| 182 | + Reply(http.StatusOK). |
| 183 | + JSON(v1API.PostgresConfigResponse{}) |
| 184 | + gock.New(server). |
| 185 | + Put("/v1/projects/test-project/config/database"). |
| 186 | + Reply(http.StatusOK). |
| 187 | + JSON(v1API.PostgresConfigResponse{ |
| 188 | + MaxConnections: cast.Ptr(cast.UintToInt(100)), |
| 189 | + }) |
| 190 | + // Experimental config |
| 191 | + gock.New(server). |
| 192 | + Post("/v1/projects/test-project/database/webhooks/enable"). |
| 193 | + Reply(http.StatusOK). |
| 194 | + JSON(map[string]interface{}{}) |
| 195 | + // Run test |
| 196 | + err := updater.UpdateRemoteConfig(context.Background(), baseConfig{ |
| 197 | + ProjectId: "test-project", |
| 198 | + Api: api{ |
| 199 | + Enabled: true, |
| 200 | + Schemas: []string{"public", "private"}, |
| 201 | + MaxRows: 1000, |
| 202 | + }, |
| 203 | + Db: db{ |
| 204 | + Settings: settings{ |
| 205 | + MaxConnections: cast.Ptr(cast.IntToUint(100)), |
| 206 | + }, |
| 207 | + }, |
| 208 | + Experimental: experimental{ |
| 209 | + Webhooks: &webhooks{ |
| 210 | + Enabled: true, |
| 211 | + }, |
| 212 | + }, |
| 213 | + }) |
| 214 | + // Check result |
| 215 | + assert.NoError(t, err) |
| 216 | + assert.True(t, gock.IsDone()) |
| 217 | + }) |
| 218 | +} |
0 commit comments