Skip to content

feat: migrates tests from Node SDK #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 4.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions test/emailverification/apiInterface.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* 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.
*/
const { printPath, createCoreApplication } = require("../utils");
const assert = require("assert");
const createWebauthnUser = require("../webauthn/lib/createUser");
const { recipesMock, request } = require("../../api-mock");
const { EmailPassword, EmailVerification, supertokens, Session, AccountLinking, WebAuthn } = recipesMock;

describe(`apiInterface: ${printPath("[test/emailverification/apiInterface.test.js]")}`, function () {
describe("[verifyEmailPOST]", function () {
it("should verify the user when user signed up with emailpassword recipe", async function () {
const connectionURI = await createCoreApplication();

supertokens.init({
supertokens: {
connectionURI,
},
appInfo: {
apiDomain: "api.supertokens.io",
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [Session.init(), EmailPassword.init(), EmailVerification.init({ mode: "REQUIRED" }), AccountLinking.init()],
});

const email = `${Math.random().toString().slice(2)}@supertokens.com`;
const signUpResult = await EmailPassword.signUp("public", email, "1234");

assert.equal(signUpResult.status, "OK");
assert.equal(typeof signUpResult.user.id, "string");
assert.equal(typeof signUpResult.recipeUserId.getAsString(), "string");

const tokenResult = await EmailVerification.createEmailVerificationToken("public", signUpResult.recipeUserId, email, {});

assert.equal(tokenResult.status, "OK");
assert.equal(typeof tokenResult.token, "string");

const verifyResponse = await new Promise((resolve, reject) =>
request()
.post("/auth/user/email/verify")
.send({
token: tokenResult.token,
})
.expect(200)
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(res.text));
}
})
);

assert.equal(verifyResponse.status, "OK");

const user = await supertokens.getUser(signUpResult.user.id, {});
assert(user?.loginMethods?.find((method) => method.recipeId === "emailpassword")?.verified);
});

it("should verify the user when user signed up with webauthn recipe", async function () {
const connectionURI = await createCoreApplication();

const origin = "https://supertokens.io";
const rpId = "supertokens.io";
const rpName = "SuperTokens";
supertokens.init({
supertokens: {
connectionURI,
},
appInfo: {
apiDomain: "api.supertokens.io",
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [
Session.init(),
AccountLinking.init(),
WebAuthn.init({
// Using hard-coded values to make backend-sdk server mappings simpler
getOrigin: async () => {
return "https://supertokens.io"; // set it like this because the default value would actually use the origin and it would not match the default relying party id
},
getRelyingPartyId: async () => {
return "supertokens.io";
},
getRelyingPartyName: async () => {
return "SuperTokens";
},
}),
EmailVerification.init({ mode: "REQUIRED" }),
],
});

const { email, signUpResponse } = await createWebauthnUser(rpId, rpName, origin);

assert.equal(signUpResponse.status, "OK");
assert.equal(
typeof signUpResponse.user?.loginMethods?.find((method) => method.recipeId === "webauthn")
?.recipeUserId,
"string"
);
const recipeUserId = supertokens.convertToRecipeUserId(
signUpResponse.user?.loginMethods?.find((method) => method.recipeId === "webauthn")?.recipeUserId
);

const tokenResult = await EmailVerification.createEmailVerificationToken("public", recipeUserId, email, {});

assert.equal(tokenResult.status, "OK");
assert.equal(typeof tokenResult.token, "string");

const verifyResponse = await new Promise((resolve, reject) =>
request()
.post("/auth/user/email/verify")
.send({
token: tokenResult.token,
})
.expect(200)
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(res.text));
}
})
);

assert.equal(verifyResponse.status, "OK");

const user = await supertokens.getUser(signUpResponse.user.id, {});
assert(user?.loginMethods?.find((method) => method.recipeId === "webauthn")?.verified);
});
});
});