|
14 | 14 | import io.vertx.junit5.VertxTestContext; |
15 | 15 | import org.junit.jupiter.api.BeforeEach; |
16 | 16 | import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.params.ParameterizedTest; |
| 18 | +import org.junit.jupiter.params.provider.Arguments; |
| 19 | +import org.junit.jupiter.params.provider.MethodSource; |
17 | 20 | import org.mockito.ArgumentCaptor; |
18 | 21 |
|
19 | 22 | import java.time.Instant; |
20 | 23 | import java.util.*; |
| 24 | +import java.util.stream.Stream; |
21 | 25 |
|
22 | 26 | import static org.junit.jupiter.api.Assertions.*; |
23 | 27 | import static org.mockito.Mockito.*; |
@@ -556,6 +560,102 @@ void updateKeypairDisabledAndName(Vertx vertx, VertxTestContext testContext) thr |
556 | 560 | testContext.completeNow(); |
557 | 561 | }); |
558 | 562 | } |
| 563 | + |
| 564 | + @Test |
| 565 | + void deleteKeypairNoSubscriptionId(Vertx vertx, VertxTestContext testContext) throws Exception { |
| 566 | + fakeAuth(Role.PRIVILEGED); |
| 567 | + |
| 568 | + setKeypairs(new ArrayList<>()); |
| 569 | + |
| 570 | + JsonObject jo = new JsonObject(); |
| 571 | + |
| 572 | + post(vertx, testContext, "api/client_side_keypairs/delete", jo.encode(), response -> { |
| 573 | + assertEquals(400, response.statusCode()); |
| 574 | + assertEquals("Required parameters: subscription_id", response.bodyAsJsonObject().getString("message")); |
| 575 | + verify(keypairStoreWriter, times(0)).upload(any(), isNull()); |
| 576 | + testContext.completeNow(); |
| 577 | + }); |
| 578 | + } |
| 579 | + |
| 580 | + @Test |
| 581 | + void deleteKeypairBadSubscriptionId(Vertx vertx, VertxTestContext testContext) throws Exception { |
| 582 | + fakeAuth(Role.PRIVILEGED); |
| 583 | + |
| 584 | + Map<String, ClientSideKeypair> keypairs = new HashMap<>() {{ |
| 585 | + put( "89aZ234567", new ClientSideKeypair( "89aZ234567", pub1, priv1, 124, "[email protected]", Instant. now(), false, name1)); |
| 586 | + put( "9aZ2345678", new ClientSideKeypair( "9aZ2345678", pub2, priv2, 125, "[email protected]", Instant. now(), false, name2)); |
| 587 | + }}; |
| 588 | + setKeypairs(new ArrayList<>(keypairs.values())); |
| 589 | + |
| 590 | + JsonObject jo = new JsonObject(); |
| 591 | + jo.put("subscription_id", "bad-id"); |
| 592 | + |
| 593 | + post(vertx, testContext, "api/client_side_keypairs/delete", jo.encode(), response -> { |
| 594 | + assertEquals(404, response.statusCode()); |
| 595 | + assertEquals("Failed to find a keypair for subscription id: bad-id", response.bodyAsJsonObject().getString("message")); |
| 596 | + verify(keypairStoreWriter, times(0)).upload(any(), isNull()); |
| 597 | + testContext.completeNow(); |
| 598 | + }); |
| 599 | + } |
| 600 | + |
| 601 | + @Test |
| 602 | + void deleteKeypair(Vertx vertx, VertxTestContext testContext) throws Exception { |
| 603 | + fakeAuth(Role.PRIVILEGED); |
| 604 | + |
| 605 | + Instant time = Instant.now(); |
| 606 | + ClientSideKeypair keypairToDelete = new ClientSideKeypair( "89aZ234567", pub1, priv1, 124, "[email protected]", time, false, name1); |
| 607 | + ClientSideKeypair remainingKeypair = new ClientSideKeypair( "9aZ2345678", pub2, priv2, 124, "[email protected]", time, false, name2); |
| 608 | + |
| 609 | + setKeypairs(List.of(keypairToDelete, remainingKeypair)); |
| 610 | + setSites(new Site(124, "test", true)); |
| 611 | + |
| 612 | + JsonObject jo = new JsonObject(); |
| 613 | + jo.put("subscription_id", "89aZ234567"); |
| 614 | + |
| 615 | + post(vertx, testContext, "api/client_side_keypairs/delete", jo.encode(), response -> { |
| 616 | + assertEquals(200, response.statusCode()); |
| 617 | + assertEquals(true, response.bodyAsJsonObject().getBoolean("success")); |
| 618 | + validateKeypair(keypairToDelete, "test", response.bodyAsJsonObject().getJsonObject("deleted_keypair")); |
| 619 | + verify(keypairStoreWriter, times(1)).upload(collectionOfSize(1), isNull()); |
| 620 | + testContext.completeNow(); |
| 621 | + }); |
| 622 | + } |
| 623 | + |
| 624 | + private static Stream<Arguments> deleteRoles() { |
| 625 | + return Stream.of( |
| 626 | + Arguments.of(Role.MAINTAINER, 401, false), |
| 627 | + Arguments.of(Role.PRIVILEGED, 200, true), |
| 628 | + Arguments.of(Role.SHARING_PORTAL, 200, true) |
| 629 | + ); |
| 630 | + } |
| 631 | + |
| 632 | + @ParameterizedTest |
| 633 | + @MethodSource("deleteRoles") |
| 634 | + void deleteKeypairAuthorization(Role role, int expectedStatus, boolean shouldSucceed, Vertx vertx, VertxTestContext testContext) throws Exception { |
| 635 | + fakeAuth(role); |
| 636 | + |
| 637 | + Instant time = Instant.now(); |
| 638 | + ClientSideKeypair keypairToDelete = new ClientSideKeypair( "CC12345678", pub1, priv1, 222, "[email protected]", time, false, name1); |
| 639 | + ClientSideKeypair remainingKeypair = new ClientSideKeypair( "DD12345678", pub2, priv2, 222, "[email protected]", time, false, name2); |
| 640 | + |
| 641 | + setKeypairs(List.of(keypairToDelete, remainingKeypair)); |
| 642 | + setSites(new Site(222, "test", true)); |
| 643 | + |
| 644 | + JsonObject jo = new JsonObject().put("subscription_id", "CC12345678"); |
| 645 | + |
| 646 | + post(vertx, testContext, "api/client_side_keypairs/delete", jo.encode(), response -> { |
| 647 | + assertEquals(expectedStatus, response.statusCode()); |
| 648 | + |
| 649 | + if (shouldSucceed) { |
| 650 | + assertTrue(response.bodyAsJsonObject().getBoolean("success")); |
| 651 | + validateKeypair(keypairToDelete, "test", response.bodyAsJsonObject().getJsonObject("deleted_keypair")); |
| 652 | + verify(keypairStoreWriter, times(1)).upload(collectionOfSize(1), isNull()); |
| 653 | + } else { |
| 654 | + verify(keypairStoreWriter, times(0)).upload(any(), isNull()); |
| 655 | + } |
| 656 | + testContext.completeNow(); |
| 657 | + }); |
| 658 | + } |
559 | 659 | } |
560 | 660 |
|
561 | 661 |
|
0 commit comments