Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Aug 3, 2023
1 parent b5126b0 commit e57d7e3
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
import io.supertokens.test.httpRequest.HttpRequestForTesting;
import io.supertokens.thirdparty.ThirdParty;
import io.supertokens.utils.SemVer;
import org.junit.AfterClass;
import org.junit.Before;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void beforeEach() {
// Check for bad input (missing fields)
@Test
public void testBadInput() throws Exception {
String[] args = { "../" };
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
Expand Down Expand Up @@ -106,7 +107,7 @@ public void testBadInput() throws Exception {
// Check good input works
@Test
public void testGoodInput() throws Exception {
String[] args = { "../" };
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
Expand Down Expand Up @@ -139,7 +140,7 @@ public void testGoodInput() throws Exception {
// Failure condition: passing a valid userId will cause the test to fail
@Test
public void testForAllTypesOfOutput() throws Exception {
String[] args = { "../" };
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));
Expand All @@ -161,4 +162,31 @@ public void testForAllTypesOfOutput() throws Exception {
process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testUnknownUserWithUserIdFromNonEp() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

ThirdParty.SignInUpResponse res = ThirdParty.signInUp(process.main, "google", "ug", "t@example.com");

JsonObject requestBody = new JsonObject();
requestBody.addProperty("userId", res.user.id);

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v2_7.get(), "emailpassword");

assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
assertEquals(response.entrySet().size(), 1);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*
* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.test.emailpassword.api;

import com.google.gson.JsonObject;
import io.supertokens.ProcessState;
import io.supertokens.emailpassword.EmailPassword;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
import io.supertokens.test.httpRequest.HttpRequestForTesting;
import io.supertokens.thirdparty.ThirdParty;
import io.supertokens.useridmapping.UserIdMapping;
import io.supertokens.utils.SemVer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

import static org.junit.Assert.*;

public class GeneratePasswordResetTokenAPITest4_0 {

@Rule
public TestRule watchman = Utils.getOnFailure();

@AfterClass
public static void afterTesting() {
Utils.afterTesting();
}

@Before
public void beforeEach() {
Utils.reset();
}

// Check for bad input (missing fields)
@Test
public void testBadInput() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

{
try {
HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", null, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");
throw new Exception("Should not come here");
} catch (io.supertokens.test.httpRequest.HttpResponseException e) {
assertTrue(e.statusCode == 400
&& e.getMessage().equals("Http error. Status Code: 400. Message: Invalid Json Input"));
}
}

{
JsonObject requestBody = new JsonObject();
try {
HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");
throw new Exception("Should not come here");
} catch (io.supertokens.test.httpRequest.HttpResponseException e) {
assertTrue(e.statusCode == 400 && e.getMessage().equals(
"Http error. Status Code: 400. Message: Field name 'userId' is invalid in " + "JSON input"));

}
}

{
JsonObject requestBody = new JsonObject();
requestBody.add("userId", null);
try {
HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");
throw new Exception("Should not come here");
} catch (io.supertokens.test.httpRequest.HttpResponseException e) {
assertTrue(e.statusCode == 400 && e.getMessage().equals(
"Http error. Status Code: 400. Message: Field name 'userId' is invalid in JSON input"));
}
}

{
AuthRecipeUserInfo user = EmailPassword.signUp(process.main, "a@a.com", "p1234");
JsonObject requestBody = new JsonObject();
requestBody.addProperty("userId", user.id);
try {
HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");
throw new Exception("Should not come here");
} catch (io.supertokens.test.httpRequest.HttpResponseException e) {
assertTrue(e.statusCode == 400 && e.getMessage().equals(
"Http error. Status Code: 400. Message: Field name 'email' is invalid in JSON input"));
}
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

// Check good input works
@Test
public void testGoodInput() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

JsonObject signUpResponse = Utils.signUpRequest_2_4(process, "random@gmail.com", "validPass123");
assertEquals(signUpResponse.get("status").getAsString(), "OK");
assertEquals(signUpResponse.entrySet().size(), 2);
String userId = signUpResponse.getAsJsonObject("user").get("id").getAsString();

JsonObject requestBody = new JsonObject();
requestBody.addProperty("userId", userId);
requestBody.addProperty("email", "random@gmail.com");

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");

assertEquals(response.get("status").getAsString(), "OK");
assertNotNull(response.get("token"));
assertEquals(response.entrySet().size(), 2);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testGoodInputWithUserIdMapping() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user = EmailPassword.signUp(process.main, "a@a.com", "p1234");
UserIdMapping.createUserIdMapping(process.main, user.id, "e1", null, false);

JsonObject requestBody = new JsonObject();
requestBody.addProperty("userId", "e1");
requestBody.addProperty("email", "random@gmail.com");

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");

assertEquals(response.get("status").getAsString(), "OK");
assertNotNull(response.get("token"));
assertEquals(response.entrySet().size(), 2);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

// Check for all types of output
// Failure condition: passing a valid userId will cause the test to fail
@Test
public void testForAllTypesOfOutput() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

JsonObject requestBody = new JsonObject();
requestBody.addProperty("userId", "randomUserId");

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");

assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
assertEquals(response.entrySet().size(), 1);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testUnknownUserWithUserIdFromNonEp() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

ThirdParty.SignInUpResponse res = ThirdParty.signInUp(process.main, "google", "ug", "t@example.com");

JsonObject requestBody = new JsonObject();
requestBody.addProperty("userId", res.user.id);
requestBody.addProperty("email", res.user.loginMethods[0].email);

JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user/password/reset/token", requestBody, 1000, 1000, null,
SemVer.v4_0.get(), "emailpassword");

assertEquals(response.get("status").getAsString(), "OK");
assertNotNull(response.get("token"));
assertEquals(response.entrySet().size(), 2);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}

0 comments on commit e57d7e3

Please sign in to comment.