Skip to content

Commit

Permalink
fix: test user association
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Aug 30, 2023
1 parent 97eca33 commit 2031f5d
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -315,6 +316,80 @@ public static JsonObject tpSignInUp(TenantIdentifier tenantIdentifier, String th
return response.get("user").getAsJsonObject();
}

private static String generateRandomString(int length) {
StringBuilder sb = new StringBuilder(length);
final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
final Random RANDOM = new Random();
for (int i = 0; i < length; i++) {
int randomIndex = RANDOM.nextInt(ALPHABET.length());
char randomChar = ALPHABET.charAt(randomIndex);
sb.append(randomChar);
}
return sb.toString();
}

private static JsonObject createCodeWithEmail(TenantIdentifier tenantIdentifier, String email, Main main)
throws HttpResponseException, IOException {
String exampleCode = generateRandomString(6);
JsonObject createCodeRequestBody = new JsonObject();
createCodeRequestBody.addProperty("email", email);
createCodeRequestBody.addProperty("userInputCode", exampleCode);

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(main, "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/signinup/code"),
createCodeRequestBody, 1000, 1000, null,
SemVer.v3_0.get(), "passwordless");

assertEquals("OK", response.get("status").getAsString());
assertEquals(8, response.entrySet().size());

return response;
}

private static JsonObject consumeCode(TenantIdentifier tenantIdentifier, String deviceId, String preAuthSessionId,
String userInputCode, Main main)
throws HttpResponseException, IOException {
JsonObject consumeCodeRequestBody = new JsonObject();
consumeCodeRequestBody.addProperty("deviceId", deviceId);
consumeCodeRequestBody.addProperty("preAuthSessionId", preAuthSessionId);
consumeCodeRequestBody.addProperty("userInputCode", userInputCode);

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(main, "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/signinup/code/consume"),
consumeCodeRequestBody, 1000, 1000, null,
SemVer.v3_0.get(), "passwordless");
assertEquals("OK", response.get("status").getAsString());
return response.get("user").getAsJsonObject();
}

public static JsonObject plSignInUpEmail(TenantIdentifier tenantIdentifier, String email, Main main)
throws HttpResponseException, IOException {
JsonObject code = createCodeWithEmail(tenantIdentifier, email, main);
return consumeCode(tenantIdentifier, code.get("deviceId").getAsString(), code.get("preAuthSessionId").getAsString(), code.get("userInputCode").getAsString(), main);
}

private static JsonObject createCodeWithNumber(TenantIdentifier tenantIdentifier, String phoneNumber, Main main)
throws HttpResponseException, IOException {
JsonObject createCodeRequestBody = new JsonObject();
createCodeRequestBody.addProperty("phoneNumber", phoneNumber);

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(main, "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/signinup/code"),
createCodeRequestBody, 1000, 1000, null,
SemVer.v3_0.get(), "passwordless");

assertEquals("OK", response.get("status").getAsString());
assertEquals(8, response.entrySet().size());

return response;
}

public static JsonObject plSignInUpNumber(TenantIdentifier tenantIdentifier, String phoneNumber, Main main)
throws HttpResponseException, IOException {
JsonObject code = createCodeWithNumber(tenantIdentifier, phoneNumber, main);
return consumeCode(tenantIdentifier, code.get("deviceId").getAsString(), code.get("preAuthSessionId").getAsString(), code.get("userInputCode").getAsString(), main);
}

public static void addLicense(String licenseKey, Main main) throws HttpResponseException, IOException {
JsonObject licenseKeyRequest = new JsonObject();
licenseKeyRequest.addProperty("licenseKey", licenseKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,76 @@ public void testDisassociateUserWithUserIdMappingAndSession() throws Exception {
// OK
}
}

@Test
public void testThatUserWithSameEmailCannotBeAssociatedToATenantForEp() throws Exception {
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

createTenants();
JsonObject user1 = TestMultitenancyAPIHelper.epSignUp(new TenantIdentifier(null, "a1", "t1"), "[email protected]",
"password", process.getProcess());
String userId1 = user1.get("id").getAsString();

TestMultitenancyAPIHelper.epSignUp(new TenantIdentifier(null, "a1", "t2"), "[email protected]",
"password", process.getProcess());

JsonObject response = TestMultitenancyAPIHelper.associateUserToTenant(new TenantIdentifier(null, "a1", "t2"), userId1, process.getProcess());
assertEquals("EMAIL_ALREADY_EXISTS_ERROR", response.getAsJsonPrimitive("status").getAsString());
}

@Test
public void testThatUserWithSameThirdPartyInfoCannotBeAssociatedToATenantForTp() throws Exception {
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

createTenants();
JsonObject user1 = TestMultitenancyAPIHelper.tpSignInUp(new TenantIdentifier(null, "a1", "t1"), "google", "google-user", "[email protected]",
process.getProcess());
String userId1 = user1.get("id").getAsString();

TestMultitenancyAPIHelper.tpSignInUp(new TenantIdentifier(null, "a1", "t2"), "google", "google-user", "[email protected]",
process.getProcess());

JsonObject response = TestMultitenancyAPIHelper.associateUserToTenant(new TenantIdentifier(null, "a1", "t2"), userId1, process.getProcess());
assertEquals("THIRD_PARTY_USER_ALREADY_EXISTS_ERROR", response.getAsJsonPrimitive("status").getAsString());
}

@Test
public void testThatUserWithSameEmailCannotBeAssociatedToATenantForPless() throws Exception {
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

createTenants();
JsonObject user1 = TestMultitenancyAPIHelper.plSignInUpEmail(new TenantIdentifier(null, "a1", "t1"), "[email protected]",
process.getProcess());
String userId1 = user1.get("id").getAsString();

TestMultitenancyAPIHelper.plSignInUpEmail(new TenantIdentifier(null, "a1", "t2"), "[email protected]",
process.getProcess());

JsonObject response = TestMultitenancyAPIHelper.associateUserToTenant(new TenantIdentifier(null, "a1", "t2"), userId1, process.getProcess());
assertEquals("EMAIL_ALREADY_EXISTS_ERROR", response.getAsJsonPrimitive("status").getAsString());
}

@Test
public void testThatUserWithSamePhoneCannotBeAssociatedToATenantForPless() throws Exception {
if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

createTenants();
JsonObject user1 = TestMultitenancyAPIHelper.plSignInUpNumber(new TenantIdentifier(null, "a1", "t1"), "+919876543210",
process.getProcess());
String userId1 = user1.get("id").getAsString();

TestMultitenancyAPIHelper.plSignInUpNumber(new TenantIdentifier(null, "a1", "t2"), "+919876543210",
process.getProcess());

JsonObject response = TestMultitenancyAPIHelper.associateUserToTenant(new TenantIdentifier(null, "a1", "t2"), userId1, process.getProcess());
assertEquals("PHONE_NUMBER_ALREADY_EXISTS_ERROR", response.getAsJsonPrimitive("status").getAsString());
}
}

0 comments on commit 2031f5d

Please sign in to comment.