|
11 | 11 | import io.vertx.junit5.VertxTestContext; |
12 | 12 | import org.junit.jupiter.api.Test; |
13 | 13 | import org.mockito.Mock; |
14 | | - |
| 14 | +import java.time.Duration; |
15 | 15 | import java.time.Instant; |
16 | 16 | import java.util.Arrays; |
17 | 17 |
|
@@ -133,6 +133,72 @@ void rotateSaltsWitnSpecificTargetDate(Vertx vertx, VertxTestContext testContext |
133 | 133 | }); |
134 | 134 | } |
135 | 135 |
|
| 136 | + @Test |
| 137 | + void rotateSaltsWithCustomAgeThresholdsEnabled(Vertx vertx, VertxTestContext testContext) throws Exception { |
| 138 | + fakeAuth(Role.SUPER_USER); |
| 139 | + |
| 140 | + when(saltRotation.isCustomAgeThresholdEnabled()).thenReturn(true); |
| 141 | + |
| 142 | + final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder.start().effective(daysEarlier(1)).expires(daysLater(6)).entries(1, daysEarlier(1)); |
| 143 | + setSnapshots(lastSnapshot); |
| 144 | + |
| 145 | + var result = SaltRotation.Result.fromSnapshot(SaltSnapshotBuilder.start().effective(targetDate()).expires(daysEarlier(7)).entries(1, targetDate()).build()); |
| 146 | + |
| 147 | + Duration[] expectedCustomAgeThresholds = new Duration[]{ |
| 148 | + Duration.ofSeconds(50), |
| 149 | + Duration.ofSeconds(60), |
| 150 | + Duration.ofSeconds(70) |
| 151 | + }; |
| 152 | + |
| 153 | + when(saltRotation.rotateSalts(any(), eq(expectedCustomAgeThresholds), eq(0.2), eq(utcTomorrow))).thenReturn(result); |
| 154 | + |
| 155 | + post(vertx, testContext, "api/salt/rotate?min_ages_in_seconds=50,60,70&fraction=0.2", "", response -> { |
| 156 | + assertEquals(200, response.statusCode()); |
| 157 | + testContext.completeNow(); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void rotateSaltsWithCustomAgeThresholdsDisabled(Vertx vertx, VertxTestContext testContext) throws Exception { |
| 163 | + fakeAuth(Role.SUPER_USER); |
| 164 | + |
| 165 | + when(saltRotation.isCustomAgeThresholdEnabled()).thenReturn(false); |
| 166 | + |
| 167 | + final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder.start().effective(daysEarlier(1)).expires(daysLater(6)).entries(1, daysEarlier(1)); |
| 168 | + setSnapshots(lastSnapshot); |
| 169 | + |
| 170 | + var result = SaltRotation.Result.fromSnapshot(SaltSnapshotBuilder.start().effective(targetDate()).expires(daysEarlier(7)).entries(1, targetDate()).build()); |
| 171 | + |
| 172 | + Duration[] expectedDefaultAgeThresholds = new Duration[]{ |
| 173 | + Duration.ofDays(30), Duration.ofDays(60), Duration.ofDays(90), Duration.ofDays(120), |
| 174 | + Duration.ofDays(150), Duration.ofDays(180), Duration.ofDays(210), Duration.ofDays(240), |
| 175 | + Duration.ofDays(270), Duration.ofDays(300), Duration.ofDays(330), Duration.ofDays(360), |
| 176 | + Duration.ofDays(390) |
| 177 | + }; |
| 178 | + |
| 179 | + when(saltRotation.rotateSalts(any(), eq(expectedDefaultAgeThresholds), eq(0.2), eq(utcTomorrow))).thenReturn(result); |
| 180 | + |
| 181 | + post(vertx, testContext, "api/salt/rotate?min_ages_in_seconds=50,60,70&fraction=0.2", "", response -> { |
| 182 | + assertEquals(200, response.statusCode()); |
| 183 | + testContext.completeNow(); |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void rotateSaltsWithCustomAgeThresholdsEnabledButMissingParameter(Vertx vertx, VertxTestContext testContext) throws Exception { |
| 189 | + fakeAuth(Role.SUPER_USER); |
| 190 | + |
| 191 | + when(saltRotation.isCustomAgeThresholdEnabled()).thenReturn(true); |
| 192 | + |
| 193 | + final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder.start().effective(daysEarlier(1)).expires(daysLater(6)).entries(1, daysEarlier(1)); |
| 194 | + setSnapshots(lastSnapshot); |
| 195 | + |
| 196 | + post(vertx, testContext, "api/salt/rotate?fraction=0.2", "", response -> { |
| 197 | + assertEquals(400, response.statusCode()); |
| 198 | + testContext.completeNow(); |
| 199 | + }); |
| 200 | + } |
| 201 | + |
136 | 202 | private void checkSnapshotsResponse(SaltSnapshotBuilder[] expectedSnapshots, Object[] actualSnapshots) { |
137 | 203 | assertEquals(expectedSnapshots.length, actualSnapshots.length); |
138 | 204 | for (int i = 0; i < expectedSnapshots.length; ++i) { |
|
0 commit comments