Skip to content

Commit

Permalink
fix: made existing tests require no changes and fixed EmailVerificati…
Browse files Browse the repository at this point in the history
…on recipe reset
  • Loading branch information
porcellus committed Dec 15, 2024
1 parent fb6e67a commit 525cb00
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 62 deletions.
1 change: 0 additions & 1 deletion bundle/emailverification.6bb70c065a3e900daf8f.js

This file was deleted.

1 change: 1 addition & 0 deletions bundle/emailverification.7cd6a51e06782defcef5.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/build/recipe/emailverification/emailVerificationClaim.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/recipe/emailverification/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion lib/build/recipe/emailverification/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/build/recipe/emailverification/recipe.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/ts/recipe/emailverification/emailVerificationClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export class EmailVerificationClaimClass extends BooleanClaim {
return false;
}

this.shouldRefreshLastCalled = currentTime;

refetchTimeOnFalseInSeconds = refetchTimeOnFalseInSeconds ?? getThresholdAwareDefaultValue(10);

if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
Expand All @@ -53,19 +51,22 @@ export class EmailVerificationClaimClass extends BooleanClaim {
const value = this.getValueFromPayload(payload, userContext);

if (value === undefined) {
this.shouldRefreshLastCalled = currentTime;
return true;
}

const lastRefetchTime = this.getLastFetchedTime(payload, userContext)!;

if (maxAgeInSeconds !== undefined) {
if (lastRefetchTime < currentTime - maxAgeInSeconds * 1000) {
this.shouldRefreshLastCalled = currentTime;
return true;
}
}

if (value === false) {
if (lastRefetchTime < currentTime - refetchTimeOnFalseInSeconds * 1000) {
this.shouldRefreshLastCalled = currentTime;
return true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/ts/recipe/emailverification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import { getNormalisedUserContext } from "../../utils";
import { EmailVerificationClaimClass } from "./emailVerificationClaim";

export default class RecipeWrapper {
static EmailVerificationClaim = Recipe.EmailVerificationClaim;
static get EmailVerificationClaim() {
return Recipe.EmailVerificationClaim;
}

static init(config?: UserInput) {
return Recipe.init(config);
Expand Down
4 changes: 3 additions & 1 deletion lib/ts/recipe/emailverification/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export default class Recipe implements RecipeModule<PreAndPostAPIHookAction, Nor
return;
}
Recipe.instance = undefined;
return;
Recipe.EmailVerificationClaim = new EmailVerificationClaimClass(
() => Recipe.getInstanceOrThrow().recipeImplementation
);
}
}

Expand Down
89 changes: 36 additions & 53 deletions test/unit/emailverificationclaim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
import jsdom from "mocha-jsdom";
import EmailVerification from "../../recipe/emailverification";
import EmailVerificationRecipe from "../../lib/build/recipe/emailverification/recipe";
import SuperTokens from "../../lib/build/supertokens";
import assert from "assert";

Expand All @@ -25,119 +26,101 @@ import assert from "assert";
* Test for checking when the shouldRefresh returning false when called multiple times in 1000ms
* is added.
*/
let delayCounter = 0;
const delay = async () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000);
});
++delayCounter;
};

const commonConfig = {
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
dateProvider: function () {
return {
getThresholdInSeconds: function () {
return 7;
},
now: function () {
return Date.now() + delayCounter * 1000;
},
};
},
recipeList: [EmailVerification.init()],
};

describe("EmailVerificationClaim test", function () {
jsdom({ url: "http://localhost.org" });

beforeEach(function () {
SuperTokens.reset();
EmailVerificationRecipe.reset();
delayCounter = 0;
});

describe("EmailVerification Claim", function () {
it("value should be refreshed if it is undefined", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshUndefined = await validator.shouldRefresh({});
assert.strictEqual(shouldRefreshUndefined, true);
});

it("value should be refreshed as per maxAgeInSeconds if it is provided", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified(10, 200);
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshValid = await validator.shouldRefresh({
"st-ev": { v: true, t: Date.now() - 199 * 1000 },
"st-ev": { v: true, t: Date.now() + delayCounter * 1000 - 199 * 1000 },
});
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshExpired = await validator.shouldRefresh({
"st-ev": { v: true, t: Date.now() - 201 * 1000 },
"st-ev": { v: true, t: Date.now() + delayCounter * 1000 - 201 * 1000 },
});
assert.strictEqual(shouldRefreshValid, false);
assert.strictEqual(shouldRefreshExpired, true);
});

it("value should be refreshed as per refetchTimeOnFalseInSeconds if it is provided", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

const validator = EmailVerification.EmailVerificationClaim.validators.isVerified(8);
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshValid = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 7 * 1000 },
"st-ev": { v: false, t: Date.now() + delayCounter * 1000 - 7 * 1000 },
});
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshExpired = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 9 * 1000 },
"st-ev": { v: false, t: Date.now() + delayCounter * 1000 - 9 * 1000 },
});
assert.strictEqual(shouldRefreshValid, false);
assert.strictEqual(shouldRefreshExpired, true);
});

it("value should be refreshed as per default the refetchTimeOnFalseInSeconds if it is not provided", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

// NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds
const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshValid = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 9 * 1000 },
"st-ev": { v: false, t: Date.now() + delayCounter * 1000 - 9 * 1000 },
});
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefreshExpired = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 11 * 1000 },
"st-ev": { v: false, t: Date.now() + delayCounter * 1000 - 11 * 1000 },
});
assert.strictEqual(shouldRefreshValid, false);
assert.strictEqual(shouldRefreshExpired, true);
});

it("shouldRefresh should return false if called before 1000ms", async function () {
SuperTokens.init({
appInfo: {
appName: "SuperTokens",
apiDomain: "api.supertokens.io",
},
recipeList: [EmailVerification.init()],
});
SuperTokens.init(commonConfig);

// NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds
const validator = EmailVerification.EmailVerificationClaim.validators.isVerified();
await delay(); // Added for correct results, since shouldRefresh is debounced
const shouldRefresh = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 11 * 1000 },
"st-ev": { v: false, t: Date.now() + delayCounter * 1000 - 11 * 1000 },
});
const shouldRefreshAgain = await validator.shouldRefresh({
"st-ev": { v: false, t: Date.now() - 11 * 1000 },
"st-ev": { v: false, t: Date.now() + delayCounter * 1000 - 11 * 1000 },
});
assert.strictEqual(shouldRefresh, true);
assert.strictEqual(shouldRefreshAgain, false);
Expand Down

0 comments on commit 525cb00

Please sign in to comment.