Skip to content
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

test: add test to check that isEmailChangeAllowed checks across tenants #25

Merged
merged 1 commit into from
Jul 26, 2024
Merged
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
130 changes: 130 additions & 0 deletions test/accountlinking/helperFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,136 @@ describe(`accountlinkingTests: ${printPath("[test/accountlinking/helperFunctions

assert(response === true);
});

it("isEmailChangeAllowed returns false if it'd lead to primary user conflict on a tenant", async function () {
const connectionURI = await startST();
supertokens.init({
supertokens: {
connectionURI,
},
debug: true,
appInfo: {
apiDomain: "api.supertokens.io",
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [
Multitenancy.init(),
EmailPassword.init(),
Session.init(),
EmailVerification.init({
mode: "OPTIONAL",
}),
AccountLinking.init({
shouldDoAutomaticAccountLinking:
shouldDoAutomaticAccountLinkingOverride.automaticallyLinkIfVerified,
}),
],
});

await Multitenancy.createOrUpdateTenant("tenant1", {
firstFactors: null,
});

await Multitenancy.createOrUpdateTenant("tenant2", {
firstFactors: null,
});

let { user: conflictingUser } = await EmailPassword.signUp(
"tenant1",
"[email protected]",
"password123"
);
let createPrimaryResult = await AccountLinking.createPrimaryUser(
conflictingUser.loginMethods[0].recipeUserId
);
assert(createPrimaryResult.status === "OK");

let { user: user1 } = await EmailPassword.signUp("tenant1", "[email protected]", "password123");
assert(user1.isPrimaryUser === false);

let { user: user2 } = await EmailPassword.signUp("tenant2", "[email protected]", "password123");
assert(user2.isPrimaryUser === false);

createPrimaryResult = await AccountLinking.createPrimaryUser(user2.loginMethods[0].recipeUserId);
assert(createPrimaryResult.status === "OK");

let linkResult = await AccountLinking.linkAccounts(user1.loginMethods[0].recipeUserId, user2.id);
assert(linkResult.status === "OK");

let isAllowed = await AccountLinking.isEmailChangeAllowed(
user1.loginMethods[0].recipeUserId,
"[email protected]",
false
);

assert(!isAllowed);
});

it("isEmailChangeAllowed returns false if it'd lead to primary user conflict on another tenant", async function () {
const connectionURI = await startST();
supertokens.init({
supertokens: {
connectionURI,
},
debug: true,
appInfo: {
apiDomain: "api.supertokens.io",
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [
Multitenancy.init(),
EmailPassword.init(),
Session.init(),
EmailVerification.init({
mode: "OPTIONAL",
}),
AccountLinking.init({
shouldDoAutomaticAccountLinking:
shouldDoAutomaticAccountLinkingOverride.automaticallyLinkIfVerified,
}),
],
});

await Multitenancy.createOrUpdateTenant("tenant1", {
firstFactors: null,
});

await Multitenancy.createOrUpdateTenant("tenant2", {
firstFactors: null,
});

let { user: conflictingUser } = await EmailPassword.signUp(
"tenant2",
"[email protected]",
"password123"
);
let createPrimaryResult = await AccountLinking.createPrimaryUser(
conflictingUser.loginMethods[0].recipeUserId
);
assert(createPrimaryResult.status === "OK");

let { user: user1 } = await EmailPassword.signUp("tenant1", "[email protected]", "password123");
assert(user1.isPrimaryUser === false);

let { user: user2 } = await EmailPassword.signUp("tenant2", "[email protected]", "password123");
assert(user2.isPrimaryUser === false);

createPrimaryResult = await AccountLinking.createPrimaryUser(user1.loginMethods[0].recipeUserId);
assert(createPrimaryResult.status === "OK");

let linkResult = await AccountLinking.linkAccounts(user2.loginMethods[0].recipeUserId, user1.id);
assert.strictEqual(linkResult.status, "OK");

let isAllowed = await AccountLinking.isEmailChangeAllowed(
user2.loginMethods[0].recipeUserId,
"[email protected]",
false
);

assert(!isAllowed);
});
});

describe("isSignInAllowed tests", function () {
Expand Down