|
4 | 4 | "bytes" |
5 | 5 | "context" |
6 | 6 | "encoding/json" |
| 7 | + "fmt" |
7 | 8 | "io" |
8 | 9 | "net/http" |
9 | 10 | "strings" |
@@ -213,6 +214,87 @@ func TestSendSMS_RateLimit(t *testing.T) { |
213 | 214 | } |
214 | 215 | } |
215 | 216 |
|
| 217 | +func TestRotateAPIKey_InvalidatesCache(t *testing.T) { |
| 218 | + ctx := context.Background() |
| 219 | + |
| 220 | + // 1) Confirm the current user API key works |
| 221 | + url := apiBaseURL + "/v1/users/me" |
| 222 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 223 | + require.NoError(t, err) |
| 224 | + req.Header.Set("x-api-key", userAPIKey) |
| 225 | + |
| 226 | + resp, err := http.DefaultClient.Do(req) |
| 227 | + require.NoError(t, err) |
| 228 | + defer resp.Body.Close() |
| 229 | + |
| 230 | + body, err := io.ReadAll(resp.Body) |
| 231 | + require.NoError(t, err) |
| 232 | + require.Equal(t, http.StatusOK, resp.StatusCode, "initial auth failed: %s", string(body)) |
| 233 | + |
| 234 | + // Parse user ID from the response |
| 235 | + var meResp struct { |
| 236 | + Data struct { |
| 237 | + ID string `json:"id"` |
| 238 | + APIKey string `json:"api_key"` |
| 239 | + } `json:"data"` |
| 240 | + } |
| 241 | + require.NoError(t, json.Unmarshal(body, &meResp)) |
| 242 | + userID := meResp.Data.ID |
| 243 | + oldAPIKey := meResp.Data.APIKey |
| 244 | + require.NotEmpty(t, userID) |
| 245 | + require.NotEmpty(t, oldAPIKey) |
| 246 | + t.Logf("user ID: %s, old API key prefix: %s...", userID, oldAPIKey[:10]) |
| 247 | + |
| 248 | + // 2) Rotate the API key |
| 249 | + rotateURL := fmt.Sprintf("%s/v1/users/%s/api-keys", apiBaseURL, userID) |
| 250 | + req, err = http.NewRequestWithContext(ctx, http.MethodDelete, rotateURL, nil) |
| 251 | + require.NoError(t, err) |
| 252 | + req.Header.Set("x-api-key", userAPIKey) |
| 253 | + |
| 254 | + resp, err = http.DefaultClient.Do(req) |
| 255 | + require.NoError(t, err) |
| 256 | + defer resp.Body.Close() |
| 257 | + |
| 258 | + body, err = io.ReadAll(resp.Body) |
| 259 | + require.NoError(t, err) |
| 260 | + require.Equal(t, http.StatusOK, resp.StatusCode, "rotate failed: %s", string(body)) |
| 261 | + |
| 262 | + // Parse new API key from rotate response |
| 263 | + var rotateResp struct { |
| 264 | + Data struct { |
| 265 | + APIKey string `json:"api_key"` |
| 266 | + } `json:"data"` |
| 267 | + } |
| 268 | + require.NoError(t, json.Unmarshal(body, &rotateResp)) |
| 269 | + newAPIKey := rotateResp.Data.APIKey |
| 270 | + require.NotEmpty(t, newAPIKey) |
| 271 | + require.NotEqual(t, oldAPIKey, newAPIKey, "API key should have changed after rotation") |
| 272 | + t.Logf("new API key prefix: %s...", newAPIKey[:10]) |
| 273 | + |
| 274 | + // 3) Old API key should immediately fail (401) — this is the bug regression check |
| 275 | + req, err = http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 276 | + require.NoError(t, err) |
| 277 | + req.Header.Set("x-api-key", oldAPIKey) |
| 278 | + |
| 279 | + resp, err = http.DefaultClient.Do(req) |
| 280 | + require.NoError(t, err) |
| 281 | + defer resp.Body.Close() |
| 282 | + assert.Equal(t, http.StatusUnauthorized, resp.StatusCode, "old API key should return 401 after rotation") |
| 283 | + |
| 284 | + // 4) New API key should work |
| 285 | + req, err = http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 286 | + require.NoError(t, err) |
| 287 | + req.Header.Set("x-api-key", newAPIKey) |
| 288 | + |
| 289 | + resp, err = http.DefaultClient.Do(req) |
| 290 | + require.NoError(t, err) |
| 291 | + defer resp.Body.Close() |
| 292 | + |
| 293 | + body, err = io.ReadAll(resp.Body) |
| 294 | + require.NoError(t, err) |
| 295 | + assert.Equal(t, http.StatusOK, resp.StatusCode, "new API key should work: %s", string(body)) |
| 296 | +} |
| 297 | + |
216 | 298 | func TestSendSMS_OutstandingFlow(t *testing.T) { |
217 | 299 | ctx := context.Background() |
218 | 300 | phone := setupPhone(ctx, t, 60) |
|
0 commit comments