21
21
import org .junit .Test ;
22
22
import org .junit .rules .TestRule ;
23
23
24
+ import java .io .IOException ;
25
+
24
26
import static org .junit .Assert .*;
25
27
26
28
public class VerifyTotpDeviceAPITest {
@@ -53,6 +55,21 @@ private Exception updateDeviceRequest(TestingProcessManager.TestingProcess proce
53
55
"totp" ));
54
56
}
55
57
58
+ private void requestWithInvalidCode (TestingProcessManager .TestingProcess process , JsonObject body )
59
+ throws HttpResponseException , IOException {
60
+ JsonObject resp = HttpRequestForTesting .sendJsonPOSTRequest (
61
+ process .getProcess (),
62
+ "" ,
63
+ "http://localhost:3567/recipe/totp/device/verify" ,
64
+ body ,
65
+ 1000 ,
66
+ 1000 ,
67
+ null ,
68
+ Utils .getCdiVersionStringLatestForTests (),
69
+ "totp" );
70
+ assertEquals ("INVALID_TOTP_ERROR" , resp .get ("status" ).getAsString ());
71
+ }
72
+
56
73
private void checkFieldMissingErrorResponse (Exception ex , String fieldName ) {
57
74
assert ex instanceof HttpResponseException ;
58
75
HttpResponseException e = (HttpResponseException ) ex ;
@@ -126,7 +143,7 @@ public void testApi() throws Exception {
126
143
checkFieldMissingErrorResponse (e , "totp" );
127
144
}
128
145
129
- // Invalid userId/deviceName/skew/period
146
+ // Invalid userId/deviceName/totp
130
147
{
131
148
body .addProperty ("totp" , "" );
132
149
Exception e = updateDeviceRequest (process , body );
@@ -137,18 +154,27 @@ public void testApi() throws Exception {
137
154
checkResponseErrorContains (e , "deviceName cannot be empty" );
138
155
139
156
body .addProperty ("deviceName" , device .deviceName );
140
- e = updateDeviceRequest (process , body );
141
- checkResponseErrorContains (e , "totp must be 6 characters long" );
157
+ requestWithInvalidCode (process , body );
158
+
159
+ Thread .sleep (1100 );
142
160
143
161
// test totp of length 5:
144
162
body .addProperty ("totp" , "12345" );
145
- e = updateDeviceRequest (process , body );
146
- checkResponseErrorContains (e , "totp must be 6 characters long" );
163
+ requestWithInvalidCode (process , body );
164
+
165
+ Thread .sleep (1100 );
147
166
148
167
// test totp of length 8:
149
168
body .addProperty ("totp" , "12345678" );
150
- e = updateDeviceRequest (process , body );
151
- checkResponseErrorContains (e , "totp must be 6 characters long" );
169
+ requestWithInvalidCode (process , body );
170
+
171
+ Thread .sleep (1100 );
172
+
173
+ // test totp of length alphabets:
174
+ body .addProperty ("totp" , "abcd" );
175
+ requestWithInvalidCode (process , body );
176
+
177
+ Thread .sleep (2100 );
152
178
153
179
// but let's pass invalid code first
154
180
body .addProperty ("totp" , "123456" );
@@ -247,5 +273,4 @@ public void testApi() throws Exception {
247
273
process .kill ();
248
274
assertNotNull (process .checkOrWaitForEvent (ProcessState .PROCESS_STATE .STOPPED ));
249
275
}
250
-
251
276
}
0 commit comments