Skip to content

Commit

Permalink
improve auth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-burel committed Dec 1, 2021
1 parent 423989e commit 4708bf2
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 68 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# If token is less than 32 char you'll get "Password too short error"!
TOKEN_SECRET="this-is-a-token-secret-with-at-least-32-chars"

NEXT_PUBLIC_GRAPHQL_URI="http://localhost:3000/api/graphql"
Expand Down
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Define this secret ONLY in UNTRACKED .env.production.local or in your production environement!
# LEAVE ME EMPTY HERE!
# If secret token is less than 32 char you'll get "Password too short error"!
TOKEN_SECRET="" # LEAVE ME EMPTY HERE!

NEXT_PUBLIC_GRAPHQL_URI="http://localhost:3000/api/graphql"
Expand Down
3 changes: 2 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TOKEN_SECRET="test-secret-token"
# If token is less than 32 char you'll get "Password too short error"!
TOKEN_SECRET="test-secret-token-must-be-at-least-32-chars"
NEXT_PUBLIC_GRAPHQL_URI="http://localhost:3000/api/graphql"
MONGO_URI="mongodb://localhost:27017/vulcan-next-app"
APOLLO_SERVER_CORS_WHITELIST="http://localhost:3000"
Expand Down
2 changes: 2 additions & 0 deletions cypress/integration/e2e/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ describe("auth", () => {
`http://(?<domain>.+)${routes.account.verifyEmail.href}/(?<token>\\w+)`
);
// Then, we check for the verification email to be sent
// /!\ IF THIS FAIL CHECK THAT YOU ARE RUNNING IN TEST MODE
// yarn run dev:test (and not just yarn run dev!)
cy.task("getLastEmail", email).then((emailBody: string) => {
const verificationLinkMatch = emailBody.match(verificationLinkRegex);
cy.wrap(verificationLinkMatch).should("exist");
Expand Down
4 changes: 4 additions & 0 deletions cypress/integration/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ it("redirect back to from page after login", () => {
`${apiRoutes.account.login.href}`,
{
statusCode: 200,
headers: {
"Set-Cookie":
"token=fake-token; Max-Age=28800; Path=/; Expires=Thu, 02 Dec 2021 01:32:07 GMT; HttpOnly; SameSite=Lax",
},
body: { done: true },
}
);
Expand Down
35 changes: 26 additions & 9 deletions cypress/plugins/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
// @see https://www.cypress.io/blog/2021/05/11/testing-html-emails-using-cypress/
const ms = require("smtp-tester");

const getRecipientKey = (recipientEmailAddress) => {
if (Array.isArray(recipientEmailAddress)) {
return recipientEmailAddress.join(",");
}
return recipientEmailAddress;
};
/**
* Will start a mail server on port 7777
* @type {Cypress.PluginConfig}
Expand All @@ -12,22 +18,28 @@ module.exports = (on, config) => {
const mailServer = ms.init(port);
console.log("mail server at port %d", port);

let lastEmail = {};
// process all emails
let lastEmailPerRecipient = {};
let lastEmail = null;
mailServer.bind((addr, id, email) => {
console.log("--- email ---");
console.log(addr, id, email);
// store the body for the email adress
lastEmail[email.headers.to] = email.html || email.body;
// If there is a least of expeditors, store the list
const to = getRecipientKey(email.headers.to);
console.log("Set last email", to);
lastEmail = email.html || email.body;
lastEmailPerRecipient[to] = lastEmail;
});
on("task", {
resetEmails(email) {
resetEmails(recipientEmail) {
console.log("Reset all emails");
if (email) {
delete lastEmail[email];
if (recipientEmail) {
const key = getRecipientKey(recipientEmail);
delete lastEmailPerRecipient[key];
} else {
// reset for all users
lastEmail = {};
lastEmail = null;
lastEmailPerRecipient = {};
}
return null;
},
Expand All @@ -36,10 +48,15 @@ module.exports = (on, config) => {
* @param {*} email User's email
* @returns The email HTML body
*/
getLastEmail(email) {
getLastEmail(recipientEmail) {
console.log("\tGet last email", recipientEmail, lastEmail);
// cy.task cannot return undefined
// thus we return null as a fallback
return lastEmail[email] || null;
if (recipientEmail) {
const key = getRecipientKey(recipientEmail);
return lastEmailPerRecipient[key] || null;
}
return lastEmail || null;
},
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"@types/jest": "^25.2.3",
"auto-changelog": "^2.2.1",
"chromatic": "^5.9.2",
"cypress": "^8.3.1",
"cypress": "^9",
"dotenv-cli": "^4.0.0",
"eslint-config-next": "^11.1.0",
"eslint-plugin-cypress": "2.11.1",
Expand Down
3 changes: 2 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function VNApp({
<VulcanCurrentUserProvider
value={{
currentUser: user || null,
loading: false /* TODO: we don't get the loading information from useUser yet */,
loading:
false /* TODO: we don't get the loading information from useUser yet */,
}}
>
<VulcanComponentsProvider>
Expand Down
2 changes: 2 additions & 0 deletions src/pages/api/account/send-verification-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*
* You don't need to call it after signup, as signup will already trigger a
* first email
*
* TODO: this is not finished, it should reuse code from signup
*/
import { NextApiRequest, NextApiResponse } from "next";
import { UserConnector } from "~/models/user.server";
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/account/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default async function signup(
// we need to use it to ensure that we run all callbacks associated to the user collection
const user: Partial<UserTypeServer> = { email, password };
let fullUser: UserTypeServer;
const context = await contextFromReq((req as unknown) as Request);
const context = await contextFromReq(req as unknown as Request);
// create mutator should return the created user
// NOTE: we use the mutator and not the model connector so callbacks are applied
// like hashing the password
Expand Down
57 changes: 2 additions & 55 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@
js-yaml "3.14.1"
nyc "15.1.0"

"@cypress/request@^2.88.6", "@cypress/request@^2.88.7":
"@cypress/request@^2.88.7":
version "2.88.10"
resolved "https://registry.yarnpkg.com/@cypress/request/-/request-2.88.10.tgz#b66d76b07f860d3a4b8d7a0604d020c662752cce"
integrity sha512-Zp7F+R93N0yZyG34GutyTNr+okam7s/Fzc1+i3kcqOP8vk6OuajuE9qZJ6Rs+10/1JFtXFYMdyarnU1rZuJesg==
Expand Down Expand Up @@ -7364,7 +7364,7 @@ cyclist@^1.0.1:
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=

cypress@*:
cypress@*, cypress@^9:
version "9.1.0"
resolved "https://registry.yarnpkg.com/cypress/-/cypress-9.1.0.tgz#5d23c1b363b7d4853009c74a422a083a8ad2601c"
integrity sha512-fyXcCN51vixkPrz/vO/Qy6WL3hKYJzCQFeWofOpGOFewVVXrGfmfSOGFntXpzWBXsIwPn3wzW0HOFw51jZajNQ==
Expand Down Expand Up @@ -7411,54 +7411,6 @@ cypress@*:
url "^0.11.0"
yauzl "^2.10.0"

cypress@^8.3.1:
version "8.7.0"
resolved "https://registry.yarnpkg.com/cypress/-/cypress-8.7.0.tgz#2ee371f383d8f233d3425b6cc26ddeec2668b6da"
integrity sha512-b1bMC3VQydC6sXzBMFnSqcvwc9dTZMgcaOzT0vpSD+Gq1yFc+72JDWi55sfUK5eIeNLAtWOGy1NNb6UlhMvB+Q==
dependencies:
"@cypress/request" "^2.88.6"
"@cypress/xvfb" "^1.2.4"
"@types/node" "^14.14.31"
"@types/sinonjs__fake-timers" "^6.0.2"
"@types/sizzle" "^2.3.2"
arch "^2.2.0"
blob-util "^2.0.2"
bluebird "^3.7.2"
cachedir "^2.3.0"
chalk "^4.1.0"
check-more-types "^2.24.0"
cli-cursor "^3.1.0"
cli-table3 "~0.6.0"
commander "^5.1.0"
common-tags "^1.8.0"
dayjs "^1.10.4"
debug "^4.3.2"
enquirer "^2.3.6"
eventemitter2 "^6.4.3"
execa "4.1.0"
executable "^4.1.1"
extract-zip "2.0.1"
figures "^3.2.0"
fs-extra "^9.1.0"
getos "^3.2.1"
is-ci "^3.0.0"
is-installed-globally "~0.4.0"
lazy-ass "^1.6.0"
listr2 "^3.8.3"
lodash "^4.17.21"
log-symbols "^4.0.0"
minimist "^1.2.5"
ospath "^1.2.2"
pretty-bytes "^5.6.0"
proxy-from-env "1.0.0"
ramda "~0.27.1"
request-progress "^3.0.0"
supports-color "^8.1.1"
tmp "~0.2.1"
untildify "^4.0.0"
url "^0.11.0"
yauzl "^2.10.0"

damerau-levenshtein@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/damerau-levenshtein/-/damerau-levenshtein-1.0.7.tgz#64368003512a1a6992593741a09a9d31a836f55d"
Expand Down Expand Up @@ -14454,11 +14406,6 @@ ramda@^0.21.0:
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.21.0.tgz#a001abedb3ff61077d4ff1d577d44de77e8d0a35"
integrity sha1-oAGr7bP/YQd9T/HVd9RN536NCjU=

ramda@~0.27.1:
version "0.27.1"
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.27.1.tgz#66fc2df3ef873874ffc2da6aa8984658abacf5c9"
integrity sha512-PgIdVpn5y5Yns8vqb8FzBUEYn98V3xcPgawAkkgj0YJ0qDsnHCiNmZYfOGMgOvoB0eWFLpYbhxUR3mxfDIMvpw==

randombytes@^2.0.0, randombytes@^2.0.1, randombytes@^2.0.5, randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down

0 comments on commit 4708bf2

Please sign in to comment.