Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niftyvictor committed Feb 14, 2025
1 parent 1c350be commit ade3611
Show file tree
Hide file tree
Showing 7 changed files with 721 additions and 129 deletions.
8 changes: 4 additions & 4 deletions test/webauthn/apis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe(`apisFunctions: ${printPath("[test/webauthn/apis.test.js]")}`, function

describe("[registerOptionsPOST]", function () {
it("test registerOptions with default values", async function () {
await initST(false);
await initST({ origin: null, rpId: null, rpName: null });

// run test if current CDI version >= 2.11
// todo update this to crrect version
Expand Down Expand Up @@ -188,7 +188,7 @@ describe(`apisFunctions: ${printPath("[test/webauthn/apis.test.js]")}`, function

describe("[signInOptionsPOST]", function () {
it("test signInOptions with default values", async function () {
await initST(false);
await initST({ origin: null, rpId: null, rpName: null });

// run test if current CDI version >= 2.11
// todo update this to crrect version
Expand Down Expand Up @@ -881,7 +881,7 @@ describe(`apisFunctions: ${printPath("[test/webauthn/apis.test.js]")}`, function
}
})
);
assert.equal(recoverAccountResponse.status, "INVALID_GENERATED_OPTIONS_ERROR");
assert.equal(recoverAccountResponse.status, "INVALID_OPTIONS_ERROR");
});

it("should return the correct error if the register options are wrong", async function () {
Expand Down Expand Up @@ -946,7 +946,7 @@ describe(`apisFunctions: ${printPath("[test/webauthn/apis.test.js]")}`, function
}
})
);
assert.equal(recoverAccountResponse.status, "INVALID_GENERATED_OPTIONS_ERROR");
assert.equal(recoverAccountResponse.status, "INVALID_OPTIONS_ERROR");
});
});
});
31 changes: 31 additions & 0 deletions test/webauthn/lib/createRegisterOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const request = require("supertest");
const express = require("express");

let { middleware, errorHandler } = require("../../../framework/express");

const createRegisterOptions = async (email = `${Math.random().toString().slice(2)}@supertokens.com`) => {
const app = express();
app.use(middleware());
app.use(errorHandler());

let registerOptionsResponse = await new Promise((resolve, reject) =>
request(app)
.post("/auth/webauthn/options/register")
.send({
email,
})
.expect(200)
.end((err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(JSON.parse(res.text));
}
})
);

return registerOptionsResponse;
};

module.exports = createRegisterOptions;
29 changes: 29 additions & 0 deletions test/webauthn/lib/createSignInOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const request = require("supertest");
const express = require("express");

let { middleware, errorHandler } = require("../../../framework/express");

const createSignInOptions = async () => {
const app = express();
app.use(middleware());
app.use(errorHandler());

let signInOptionsResponse = await new Promise((resolve, reject) =>
request(app)
.post("/auth/webauthn/options/signin")
.send({})
.expect(200)
.end((err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(JSON.parse(res.text));
}
})
);

return signInOptionsResponse;
};

module.exports = createSignInOptions;
29 changes: 9 additions & 20 deletions test/webauthn/lib/createUser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const request = require("supertest");
const express = require("express");

const createRegisterOptions = require("./createRegisterOptions");
const createSignInOptions = require("./createSignInOptions");

let { middleware, errorHandler } = require("../../../framework/express");

const getWebauthnLib = require("./getWebAuthnLib");
Expand All @@ -11,25 +14,11 @@ const createUser = async (rpId, rpName, origin) => {
app.use(errorHandler());

const email = `${Math.random().toString().slice(2)}@supertokens.com`;
let registerOptionsResponse = await new Promise((resolve, reject) =>
request(app)
.post("/auth/webauthn/options/register")
.send({
email,
})
.expect(200)
.end((err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(JSON.parse(res.text));
}
})
);
const registerOptionsResponse = await createRegisterOptions(email);
const signInOptionsResponse = await createSignInOptions();

const { createCredential } = await getWebauthnLib();
const credential = createCredential(registerOptionsResponse, {
const { createAndAssertCredential } = await getWebauthnLib();
const credential = createAndAssertCredential(registerOptionsResponse, signInOptionsResponse, {
rpId,
rpName,
origin,
Expand All @@ -41,7 +30,7 @@ const createUser = async (rpId, rpName, origin) => {
request(app)
.post("/auth/webauthn/signup")
.send({
credential,
credential: credential.attestation,
webauthnGeneratedOptionsId: registerOptionsResponse.webauthnGeneratedOptionsId,
shouldTryLinkingWithSessionUser: false,
})
Expand All @@ -56,7 +45,7 @@ const createUser = async (rpId, rpName, origin) => {
})
);

return { email, signUpResponse, registerOptionsResponse, credential };
return { email, signUpResponse, registerOptionsResponse, signInOptionsResponse, credential };
};

module.exports = createUser;
5 changes: 3 additions & 2 deletions test/webauthn/lib/getWebAuthnRecipe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let SuperTokens = require("../../../lib/build/supertokens").default;
const WebAuthnRecipe = require("../../../lib/build/recipe/webauthn/recipe").default;

const getWebAuthnRecipe = () => {
return SuperTokens.getInstanceOrThrowError().recipeModules.find((rm) => rm.getRecipeId() === "webauthn");
const recipe = WebAuthnRecipe.getInstanceOrThrowError();
return recipe;
};

module.exports = getWebAuthnRecipe;
86 changes: 63 additions & 23 deletions test/webauthn/lib/initST.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,70 @@ let STExpress = require("../../../");
let Session = require("../../../recipe/session");
let WebAuthn = require("../../../recipe/webauthn");

const origin = "https://supertokens.io";
const rpId = "supertokens.io";
const rpName = "SuperTokens";
const _origin = "https://supertokens.io";
const _rpId = "supertokens.io";
const _rpName = "SuperTokens";

const initST = async (override = true) => {
const initST = async ({
origin = _origin,
rpId = _rpId,
rpName = _rpName,
signInTimeout = 10000,
registerTimeout = 10000,
} = {}) => {
const connectionURI = await startST();

const config = {
override: {
functions: (originalImplementation) => {
return {
...originalImplementation,
...(signInTimeout
? {
signInOptions: async (input) => {
return originalImplementation.signInOptions({
...input,
timeout: signInTimeout,
});
},
}
: {}),
...(registerTimeout
? {
registerOptions: async (input) => {
return originalImplementation.registerOptions({
...input,
timeout: registerTimeout,
});
},
}
: {}),
};
},
},
...(origin
? {
getOrigin: async () => {
return origin;
},
}
: {}),
...(rpId
? {
getRelyingPartyId: async () => {
return rpId;
},
}
: {}),
...(rpName
? {
getRelyingPartyName: async () => {
return rpName;
},
}
: {}),
};

STExpress.init({
supertokens: {
connectionURI,
Expand All @@ -21,29 +78,12 @@ const initST = async (override = true) => {
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [
Session.init(),
WebAuthn.init(
override
? {
getOrigin: async () => {
return origin;
},
getRelyingPartyId: async () => {
return rpId;
},
getRelyingPartyName: async () => {
return rpName;
},
}
: {}
),
],
recipeList: [Session.init(), WebAuthn.init(config)],
});

// run test if current CDI version >= 2.11
// todo update this to crrect version
assert(await isCDIVersionCompatible("2.11"));
};

module.exports = { initST, origin, rpId, rpName };
module.exports = { initST, origin: _origin, rpId: _rpId, rpName: _rpName };
Loading

0 comments on commit ade3611

Please sign in to comment.