|
17 | 17 | package io.supertokens.test.session; |
18 | 18 |
|
19 | 19 | import com.google.gson.JsonObject; |
| 20 | +import com.google.gson.JsonParser; |
| 21 | + |
20 | 22 | import io.supertokens.ProcessState; |
21 | 23 | import io.supertokens.exceptions.TokenTheftDetectedException; |
22 | 24 | import io.supertokens.exceptions.TryRefreshTokenException; |
|
25 | 27 | import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException; |
26 | 28 | import io.supertokens.session.Session; |
27 | 29 | import io.supertokens.session.accessToken.AccessTokenSigningKey; |
| 30 | +import io.supertokens.session.info.SessionInfo; |
28 | 31 | import io.supertokens.session.info.SessionInformationHolder; |
29 | 32 | import io.supertokens.storageLayer.StorageLayer; |
30 | 33 | import io.supertokens.test.TestingProcessManager; |
@@ -367,4 +370,89 @@ public void checkThatExpiredSessionIsNotReturnedForUserNorCanItBeUpdated() throw |
367 | 370 | process.kill(); |
368 | 371 | assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); |
369 | 372 | } |
| 373 | + |
| 374 | + // session tests with long access and refresh token lifetimes |
| 375 | + @Test |
| 376 | + public void testCreatingSessionsWithLongAccessAndRefreshTokenLifeTimes() throws Exception { |
| 377 | + |
| 378 | + Utils.setValueInConfig("access_token_validity", "63072000"); // 2 years in seconds |
| 379 | + Utils.setValueInConfig("refresh_token_validity", "1051200"); // 2 years in minutes |
| 380 | + |
| 381 | + String[] args = { "../" }; |
| 382 | + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); |
| 383 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); |
| 384 | + |
| 385 | + SessionInformationHolder sessionInfo = Session.createNewSession(process.getProcess(), "user", new JsonObject(), |
| 386 | + new JsonObject()); |
| 387 | + long twoYearsInSeconds = 63072000; |
| 388 | + |
| 389 | + assertEquals(sessionInfo.accessToken.expiry - sessionInfo.accessToken.createdTime, twoYearsInSeconds * 1000); |
| 390 | + assertEquals(sessionInfo.refreshToken.expiry - sessionInfo.refreshToken.createdTime, twoYearsInSeconds * 1000); |
| 391 | + |
| 392 | + process.kill(); |
| 393 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); |
| 394 | + } |
| 395 | + |
| 396 | + @Test |
| 397 | + public void testCreatingSessionsWithLongAccessAndRefreshTokenLifeTimesAndRefreshingTokens() throws Exception { |
| 398 | + |
| 399 | + Utils.setValueInConfig("access_token_validity", "63072000"); // 2 years in seconds |
| 400 | + Utils.setValueInConfig("refresh_token_validity", "1051200"); // 2 years in minutes |
| 401 | + |
| 402 | + String[] args = { "../" }; |
| 403 | + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); |
| 404 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); |
| 405 | + |
| 406 | + SessionInformationHolder sessionInfo = Session.createNewSession(process.getProcess(), "user", new JsonObject(), |
| 407 | + new JsonObject()); |
| 408 | + long twoYearsInSeconds = 63072000; |
| 409 | + |
| 410 | + assertEquals(sessionInfo.accessToken.expiry - sessionInfo.accessToken.createdTime, twoYearsInSeconds * 1000); |
| 411 | + assertEquals(sessionInfo.refreshToken.expiry - sessionInfo.refreshToken.createdTime, twoYearsInSeconds * 1000); |
| 412 | + |
| 413 | + SessionInformationHolder sessionInfo2 = Session.refreshSession(process.main, sessionInfo.refreshToken.token, |
| 414 | + null, false); |
| 415 | + |
| 416 | + assertFalse(sessionInfo.accessToken.token.equals(sessionInfo2.accessToken.token)); |
| 417 | + assertFalse(sessionInfo.refreshToken.token.equals(sessionInfo2.refreshToken.token)); |
| 418 | + |
| 419 | + assertEquals(sessionInfo2.accessToken.expiry - sessionInfo2.accessToken.createdTime, twoYearsInSeconds * 1000); |
| 420 | + assertEquals(sessionInfo2.refreshToken.expiry - sessionInfo2.refreshToken.createdTime, |
| 421 | + twoYearsInSeconds * 1000); |
| 422 | + |
| 423 | + process.kill(); |
| 424 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); |
| 425 | + } |
| 426 | + |
| 427 | + @Test |
| 428 | + public void createNewSessionAndUpdateSession() throws Exception { |
| 429 | + |
| 430 | + Utils.setValueInConfig("access_token_validity", "63072000"); // 2 years in seconds |
| 431 | + Utils.setValueInConfig("refresh_token_validity", "1051200"); // 2 years in minutes |
| 432 | + String[] args = { "../" }; |
| 433 | + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); |
| 434 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); |
| 435 | + |
| 436 | + SessionInformationHolder sessionInfo = Session.createNewSession(process.getProcess(), "user", new JsonObject(), |
| 437 | + new JsonObject()); |
| 438 | + long twoYearsInSeconds = 63072000; |
| 439 | + |
| 440 | + assertEquals(sessionInfo.accessToken.expiry - sessionInfo.accessToken.createdTime, twoYearsInSeconds * 1000); |
| 441 | + assertEquals(sessionInfo.refreshToken.expiry - sessionInfo.refreshToken.createdTime, twoYearsInSeconds * 1000); |
| 442 | + JsonObject sessionData = new JsonObject(); |
| 443 | + sessionData.addProperty("test", "value"); |
| 444 | + |
| 445 | + JsonObject jwtData = new JsonObject(); |
| 446 | + jwtData.addProperty("test", "value"); |
| 447 | + |
| 448 | + Session.updateSession(process.main, sessionInfo.session.handle, sessionData, jwtData, null); |
| 449 | + |
| 450 | + io.supertokens.pluginInterface.session.SessionInfo sessionInfo2 = Session.getSession(process.main, |
| 451 | + sessionInfo.session.handle); |
| 452 | + |
| 453 | + assertEquals(sessionInfo2.expiry - sessionInfo2.timeCreated, twoYearsInSeconds * 1000); |
| 454 | + |
| 455 | + process.kill(); |
| 456 | + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); |
| 457 | + } |
370 | 458 | } |
0 commit comments