1111import io .vertx .junit5 .VertxTestContext ;
1212import org .junit .jupiter .api .Test ;
1313import org .mockito .Mock ;
14-
14+ import java . time . Duration ;
1515import java .time .Instant ;
1616import java .util .Arrays ;
1717
@@ -111,7 +111,7 @@ void rotateSaltsNoNewSnapshot(Vertx vertx, VertxTestContext testContext) throws
111111 }
112112
113113 @ Test
114- void rotateSaltsWitnSpecificTargetDate (Vertx vertx , VertxTestContext testContext ) throws Exception {
114+ void rotateSaltsWithSpecificTargetDate (Vertx vertx , VertxTestContext testContext ) throws Exception {
115115 fakeAuth (Role .SUPER_USER );
116116 final SaltSnapshotBuilder [] snapshots = {
117117 SaltSnapshotBuilder .start ().effective (daysEarlier (5 )).expires (daysEarlier (4 )).entries (10 , daysEarlier (5 )),
@@ -133,6 +133,75 @@ void rotateSaltsWitnSpecificTargetDate(Vertx vertx, VertxTestContext testContext
133133 });
134134 }
135135
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+ verify (saltRotation ).rotateSalts (any (), eq (expectedCustomAgeThresholds ), eq (0.2 ), eq (utcTomorrow ));
157+ assertEquals (200 , response .statusCode ());
158+ testContext .completeNow ();
159+ });
160+ }
161+
162+ @ Test
163+ void rotateSaltsWithDefaultAgeThresholds (Vertx vertx , VertxTestContext testContext ) throws Exception {
164+ fakeAuth (Role .SUPER_USER );
165+
166+ when (saltRotation .isCustomAgeThresholdEnabled ()).thenReturn (false );
167+
168+ final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder .start ().effective (daysEarlier (1 )).expires (daysLater (6 )).entries (1 , daysEarlier (1 ));
169+ setSnapshots (lastSnapshot );
170+
171+ var result = SaltRotation .Result .fromSnapshot (SaltSnapshotBuilder .start ().effective (targetDate ()).expires (daysEarlier (7 )).entries (1 , targetDate ()).build ());
172+
173+ Duration [] expectedDefaultAgeThresholds = new Duration []{
174+ Duration .ofDays (30 ), Duration .ofDays (60 ), Duration .ofDays (90 ), Duration .ofDays (120 ),
175+ Duration .ofDays (150 ), Duration .ofDays (180 ), Duration .ofDays (210 ), Duration .ofDays (240 ),
176+ Duration .ofDays (270 ), Duration .ofDays (300 ), Duration .ofDays (330 ), Duration .ofDays (360 ),
177+ Duration .ofDays (390 )
178+ };
179+
180+ when (saltRotation .rotateSalts (any (), eq (expectedDefaultAgeThresholds ), eq (0.2 ), eq (utcTomorrow ))).thenReturn (result );
181+
182+ post (vertx , testContext , "api/salt/rotate?min_ages_in_seconds=50,60,70&fraction=0.2" , "" , response -> {
183+ verify (saltRotation ).rotateSalts (any (), eq (expectedDefaultAgeThresholds ), eq (0.2 ), eq (utcTomorrow ));
184+ assertEquals (200 , response .statusCode ());
185+ testContext .completeNow ();
186+ });
187+ }
188+
189+ @ Test
190+ void rotateSaltsWithCustomAgeThresholdsEnabledButMissingParameter (Vertx vertx , VertxTestContext testContext ) {
191+ fakeAuth (Role .SUPER_USER );
192+
193+ when (saltRotation .isCustomAgeThresholdEnabled ()).thenReturn (true );
194+
195+ final SaltSnapshotBuilder lastSnapshot = SaltSnapshotBuilder .start ().effective (daysEarlier (1 )).expires (daysLater (6 )).entries (1 , daysEarlier (1 ));
196+ setSnapshots (lastSnapshot );
197+
198+ post (vertx , testContext , "api/salt/rotate?fraction=0.2" , "" , response -> {
199+ verify (saltRotation , never ()).rotateSalts (any (), any (), anyDouble (), any ());
200+ assertEquals (400 , response .statusCode ());
201+ testContext .completeNow ();
202+ });
203+ }
204+
136205 private void checkSnapshotsResponse (SaltSnapshotBuilder [] expectedSnapshots , Object [] actualSnapshots ) {
137206 assertEquals (expectedSnapshots .length , actualSnapshots .length );
138207 for (int i = 0 ; i < expectedSnapshots .length ; ++i ) {
0 commit comments