-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5126b0
commit e57d7e3
Showing
2 changed files
with
274 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
243 changes: 243 additions & 0 deletions
243
...test/java/io/supertokens/test/emailpassword/api/GeneratePasswordResetTokenAPITest4_0.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |