Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 28, 2023
1 parent 6be5b8c commit 0139624
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/test/java/io/supertokens/test/totp/TOTPRecipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.supertokens.pluginInterface.totp.TOTPUsedCode;
import io.supertokens.pluginInterface.totp.exception.DeviceAlreadyExistsException;
import io.supertokens.pluginInterface.totp.exception.UnknownDeviceException;
import io.supertokens.pluginInterface.totp.exception.UnknownTotpUserIdException;
import io.supertokens.pluginInterface.totp.sqlStorage.TOTPSQLStorage;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
Expand Down Expand Up @@ -146,6 +147,10 @@ public void createDeviceTest() throws Exception {
TOTPDevice device = Totp.registerDevice(main, "user", "device1", 1, 30);
assert !Objects.equals(device.secretKey, "");

// Verify device
String validTotp = TOTPRecipeTest.generateTotpCode(main, device);
Totp.verifyDevice(main, "user", "device1", validTotp);

// Create same device again (should fail)
assertThrows(DeviceAlreadyExistsException.class,
() -> Totp.registerDevice(main, "user", "device1", 1, 30));
Expand All @@ -167,7 +172,7 @@ public void createDeviceAndVerifyCodeTest() throws Exception {
Totp.verifyDevice(main, "user", device.deviceName, generateTotpCode(main, device, -1));

// Try login with non-existent user:
assertThrows(InvalidTotpException.class,
assertThrows(UnknownTotpUserIdException.class,
() -> Totp.verifyCode(main, "non-existent-user", "any-code"));

// {Code: [INVALID, VALID]} * {Devices: [verified, unverfied]}
Expand Down Expand Up @@ -555,4 +560,15 @@ public void deleteExpiredTokensCronIntervalTest() throws Exception {
assert DeleteExpiredTotpTokens.getInstance(main).getIntervalTimeSeconds() == 60 * 60;
}

@Test
public void testRegisterDeviceWithSameNameAsAnUnverifiedDevice() throws Exception {
TestSetupResult result = defaultInit();
if (result == null) {
return;
}
Main main = result.process.getProcess();

Totp.registerDevice(main, "user", "device1", 1, 30);
Totp.registerDevice(main, "user", "device1", 1, 30);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.supertokens.test.httpRequest.HttpResponseException;
import io.supertokens.test.totp.TOTPRecipeTest;
import io.supertokens.test.totp.TotpLicenseTest;
import io.supertokens.totp.Totp;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -141,6 +142,7 @@ public void testApi() throws Exception {
assert res.get("deviceName").getAsString().equals("d1");

// try again with same device name:
// This should replace the previous device
JsonObject res2 = HttpRequestForTesting.sendJsonPOSTRequest(
process.getProcess(),
"",
Expand All @@ -151,8 +153,30 @@ public void testApi() throws Exception {
null,
Utils.getCdiVersionStringLatestForTests(),
"totp");
assert res2.get("status").getAsString().equals("OK");
assert res.get("deviceName").getAsString().equals("d1");

// verify d1
{
TOTPDevice device = Totp.getDevices(process.getProcess(), "user-id" )[0];
String validTotp = TOTPRecipeTest.generateTotpCode(process.getProcess(), device);
Totp.verifyDevice(process.getProcess(), "user-id", "d1", validTotp);
}

// try again with same device name:
res2 = HttpRequestForTesting.sendJsonPOSTRequest(
process.getProcess(),
"",
"http://localhost:3567/recipe/totp/device",
body,
1000,
1000,
null,
Utils.getCdiVersionStringLatestForTests(),
"totp");
assert res2.get("status").getAsString().equals("DEVICE_ALREADY_EXISTS_ERROR");

assert res.get("deviceName").getAsString().equals("d1");

// try without passing deviceName:
body.remove("deviceName");
JsonObject res3 = HttpRequestForTesting.sendJsonPOSTRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.supertokens.test.httpRequest.HttpResponseException;
import io.supertokens.test.totp.TOTPRecipeTest;
import io.supertokens.thirdparty.InvalidProviderConfigException;
import io.supertokens.totp.Totp;
import io.supertokens.utils.SemVer;
import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -249,9 +250,12 @@ public void testDevicesWorkAppWide() throws Exception {
int userCount = 1;
for (TenantIdentifier tenant1 : tenants) {
createDevice(tenant1, "user" + userCount);
TOTPDevice device = Totp.getDevices(t1.withStorage(StorageLayer.getStorage(tenant1, process.getProcess())).toAppIdentifierWithStorage(), "user" + userCount)[0];
String validTotp = TOTPRecipeTest.generateTotpCode(process.getProcess(), device);
verifyDevice(tenant1, "user" + userCount, validTotp);

for (TenantIdentifier tenant2 : tenants) {
createDeviceAlreadyExists(tenant2, "user1");
createDeviceAlreadyExists(tenant2, "user" + userCount);
}

userCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void testApi() throws Exception {
assert res3.get("retryAfterMs") != null;

// wait for cooldown to end (1s)
Thread.sleep(1000);
Thread.sleep(1200);

// should pass now on valid code
String validTotp = generateTotpCode(process.getProcess(), device);
Expand Down Expand Up @@ -248,7 +248,7 @@ public void testApi() throws Exception {
null,
Utils.getCdiVersionStringLatestForTests(),
"totp");
assert res5.get("status").getAsString().equals("INVALID_TOTP_ERROR");
assert res5.get("status").getAsString().equals("UNKNOWN_USER_ID_ERROR");
}

process.kill();
Expand Down

0 comments on commit 0139624

Please sign in to comment.