|
4 | 4 | "bytes" |
5 | 5 | "context" |
6 | 6 | "errors" |
| 7 | + "fmt" |
7 | 8 | "io" |
8 | 9 | "net/http" |
9 | 10 | "net/http/httptest" |
@@ -336,7 +337,83 @@ func Test_ServeHttp(t *testing.T) { |
336 | 337 | assert.Equal(t, res.Code, tt.expectedCode) |
337 | 338 | }) |
338 | 339 | } |
| 340 | +} |
339 | 341 |
|
| 342 | +func Test_ServeHttpUIDAndKey(t *testing.T) { |
| 343 | + skupperObjects := []runtime.Object{ |
| 344 | + &v2alpha1.AccessGrant{ |
| 345 | + ObjectMeta: metav1.ObjectMeta{ |
| 346 | + Name: "good", |
| 347 | + Namespace: "test", |
| 348 | + UID: "0bde3bc8-a4a2-404a-bfbe-44fdf7bf3231", |
| 349 | + }, |
| 350 | + Spec: v2alpha1.AccessGrantSpec{ |
| 351 | + RedemptionsAllowed: 3, |
| 352 | + Code: "supersecret", |
| 353 | + }, |
| 354 | + Status: v2alpha1.AccessGrantStatus{ |
| 355 | + Code: "supersecret", |
| 356 | + ExpirationTime: time.Date(2124, time.January, 0, 0, 0, 0, 0, time.UTC).Format(time.RFC3339), |
| 357 | + }, |
| 358 | + }, |
| 359 | + &v2alpha1.AccessGrant{ |
| 360 | + ObjectMeta: metav1.ObjectMeta{ |
| 361 | + Name: "not-bad", |
| 362 | + Namespace: "test", |
| 363 | + UID: "0bde3bc8-a4a2-404a-bfbe-44fdf7bf3232", |
| 364 | + }, |
| 365 | + Spec: v2alpha1.AccessGrantSpec{ |
| 366 | + RedemptionsAllowed: 3, |
| 367 | + Code: "supersecret", |
| 368 | + }, |
| 369 | + Status: v2alpha1.AccessGrantStatus{ |
| 370 | + Code: "supersecret", |
| 371 | + ExpirationTime: time.Date(2124, time.January, 0, 0, 0, 0, 0, time.UTC).Format(time.RFC3339), |
| 372 | + }, |
| 373 | + }, |
| 374 | + } |
| 375 | + scenarioKeys := []struct { |
| 376 | + valid []string |
| 377 | + invalid []string |
| 378 | + allowKeyRedeem bool |
| 379 | + }{ |
| 380 | + { |
| 381 | + valid: []string{"0bde3bc8-a4a2-404a-bfbe-44fdf7bf3231", "0bde3bc8-a4a2-404a-bfbe-44fdf7bf3232"}, |
| 382 | + invalid: []string{"test/good", "test/not-bad", "test/invalid", "really-invalid"}, |
| 383 | + allowKeyRedeem: false, |
| 384 | + }, |
| 385 | + { |
| 386 | + valid: []string{"0bde3bc8-a4a2-404a-bfbe-44fdf7bf3231", "0bde3bc8-a4a2-404a-bfbe-44fdf7bf3232", "test/good", "test/not-bad"}, |
| 387 | + invalid: []string{"test/invalid", "really-invalid"}, |
| 388 | + allowKeyRedeem: true, |
| 389 | + }, |
| 390 | + } |
| 391 | + client, err := fake.NewFakeClient("test", nil, skupperObjects, "") |
| 392 | + if err != nil { |
| 393 | + t.Error(err) |
| 394 | + } |
| 395 | + registry := newGrants(client, dummyGenerator, "https", "") |
| 396 | + for _, obj := range skupperObjects { |
| 397 | + grant := obj.(*v2alpha1.AccessGrant) |
| 398 | + assert.Assert(t, registry.checkGrant(grant.Namespace+"/"+grant.Name, grant)) |
| 399 | + } |
| 400 | + for _, scenario := range scenarioKeys { |
| 401 | + registry.keyRedeem = scenario.allowKeyRedeem |
| 402 | + for _, key := range scenario.valid { |
| 403 | + t.Run("redeem_valid_key_"+key, func(t *testing.T) {}) |
| 404 | + req := httptest.NewRequest(http.MethodPost, fmt.Sprintf("/%s", key), bytes.NewBufferString("supersecret")) |
| 405 | + res := httptest.NewRecorder() |
| 406 | + registry.ServeHTTP(res, req) |
| 407 | + assert.Equal(t, res.Code, http.StatusOK) |
| 408 | + } |
| 409 | + for _, key := range scenario.invalid { |
| 410 | + t.Run("redeem_invalid_key_"+key, func(t *testing.T) {}) |
| 411 | + req := httptest.NewRequest(http.MethodPost, fmt.Sprintf("/%s", key), bytes.NewBufferString("supersecret")) |
| 412 | + res := httptest.NewRecorder() |
| 413 | + registry.ServeHTTP(res, req) |
| 414 | + assert.Equal(t, res.Code, http.StatusNotFound) |
| 415 | + } |
| 416 | + } |
340 | 417 | } |
341 | 418 |
|
342 | 419 | type CheckGrantTestInvocation struct { |
|
0 commit comments