From 64111cc5341cbaba6a78008e912207e612fcdb1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mih=C3=A1ly=20Lengyel?= Date: Fri, 29 Sep 2023 17:45:03 +0200 Subject: [PATCH] feat: add edge function testing (#704) * feat: add netlify.toml to next example * docs(examples): add postinstall script to install root deps in CI deploys * docs(examples): update appinfo to point to the netlify deployment if defined * docs(examples): update appinfo to point to the netlify deployment * docs(examples): update appinfo to point to the netlify deployment * test: update example tests * chore: prettier * test: add edge function test * test: make sure the nextjs test call refresh to check edge compatibility --- .github/workflows/test-edge-function.yml | 28 +++++++++++++++++++ CHANGELOG.md | 2 +- .../aws/with-emailpassword/test/basic.test.js | 3 +- .../with-emailpassword/test/basic.test.js | 3 +- .../with-emailpassword/test/basic.test.js | 3 +- .../with-emailpassword/test/basic.test.js | 3 +- .../koa/with-emailpassword/test/basic.test.js | 3 +- .../with-emailpassword/test/basic.test.js | 3 +- .../with-emailpassword/test/basic.test.js | 3 +- .../next/with-emailpassword/config/appInfo.ts | 3 +- examples/next/with-emailpassword/netlify.toml | 4 +++ .../next/with-emailpassword/package-lock.json | 1 + examples/next/with-emailpassword/package.json | 1 + .../with-emailpassword/test/basic.test.js | 28 +++++++++++++++++-- updateLibInExample | 3 ++ 15 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/test-edge-function.yml create mode 100644 examples/next/with-emailpassword/netlify.toml create mode 100755 updateLibInExample diff --git a/.github/workflows/test-edge-function.yml b/.github/workflows/test-edge-function.yml new file mode 100644 index 000000000..60de821a6 --- /dev/null +++ b/.github/workflows/test-edge-function.yml @@ -0,0 +1,28 @@ +name: "Test edge function compatibility" +on: push +jobs: + test: + runs-on: ubuntu-latest + env: + NETLIFY_AUTH_TOKEN: ${{ secrets.netlify_auth_token }} + defaults: + run: + working-directory: examples/next/with-emailpassword + steps: + - uses: actions/checkout@v2 + - run: echo $GITHUB_REF_NAME + - run: npm install git+https://github.com:supertokens/supertokens-node.git#$GITHUB_SHA + - run: npm install + - run: npm install mocha@6.1.4 jsdom-global@3.0.2 puppeteer@^11.0.0 isomorphic-fetch@^3.0.0 + - run: | + (netlify deploy --json --auth=$NETLIFY_AUTH_TOKEN > deployInfo.json) && ( \ + (echo "=========== Test attempt 1 ===========" && npx mocha --no-config --timeout 80000 test/**/*.test.js) || \ + (echo "=========== Test attempt 2 ===========" && npx mocha --no-config --timeout 80000 test/**/*.test.js) || \ + (echo "=========== Test attempt 3 ===========" && npx mocha --no-config --timeout 80000 test/**/*.test.js) \ + ) + - name: The job has failed + if: ${{ failure() }} + uses: actions/upload-artifact@v3 + with: + name: screenshots + path: ./**/*screenshot.jpeg diff --git a/CHANGELOG.md b/CHANGELOG.md index b7a64b3fe..3d5819529 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) -## [16.0.1] - 2023-09-25 +## [16.2.1] - 2023-09-25 ### Changes diff --git a/examples/aws/with-emailpassword/test/basic.test.js b/examples/aws/with-emailpassword/test/basic.test.js index 34f06c029..31bb96004 100644 --- a/examples/aws/with-emailpassword/test/basic.test.js +++ b/examples/aws/with-emailpassword/test/basic.test.js @@ -86,7 +86,8 @@ describe("SuperTokens Example Basic tests", function () { ]); await submitForm(page); await page.waitForNavigation(); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".sessionButton"); let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); diff --git a/examples/express/with-emailpassword/test/basic.test.js b/examples/express/with-emailpassword/test/basic.test.js index b40f68c48..bc33830fb 100644 --- a/examples/express/with-emailpassword/test/basic.test.js +++ b/examples/express/with-emailpassword/test/basic.test.js @@ -79,7 +79,8 @@ describe("SuperTokens Example Basic tests", function () { { name: "password", value: testPW }, ]); await submitForm(page); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".sessionButton"); let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); diff --git a/examples/fastify/with-emailpassword/test/basic.test.js b/examples/fastify/with-emailpassword/test/basic.test.js index b40f68c48..bc33830fb 100644 --- a/examples/fastify/with-emailpassword/test/basic.test.js +++ b/examples/fastify/with-emailpassword/test/basic.test.js @@ -79,7 +79,8 @@ describe("SuperTokens Example Basic tests", function () { { name: "password", value: testPW }, ]); await submitForm(page); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".sessionButton"); let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); diff --git a/examples/hapi/with-emailpassword/test/basic.test.js b/examples/hapi/with-emailpassword/test/basic.test.js index 010816c0e..5a4fc5f87 100644 --- a/examples/hapi/with-emailpassword/test/basic.test.js +++ b/examples/hapi/with-emailpassword/test/basic.test.js @@ -80,7 +80,8 @@ describe("SuperTokens Example Basic tests", function () { ]); await submitForm(page); await page.waitForNavigation(); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".sessionButton"); let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); diff --git a/examples/koa/with-emailpassword/test/basic.test.js b/examples/koa/with-emailpassword/test/basic.test.js index b40f68c48..bc33830fb 100644 --- a/examples/koa/with-emailpassword/test/basic.test.js +++ b/examples/koa/with-emailpassword/test/basic.test.js @@ -79,7 +79,8 @@ describe("SuperTokens Example Basic tests", function () { { name: "password", value: testPW }, ]); await submitForm(page); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".sessionButton"); let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); diff --git a/examples/loopback/with-emailpassword/test/basic.test.js b/examples/loopback/with-emailpassword/test/basic.test.js index 010816c0e..5a4fc5f87 100644 --- a/examples/loopback/with-emailpassword/test/basic.test.js +++ b/examples/loopback/with-emailpassword/test/basic.test.js @@ -80,7 +80,8 @@ describe("SuperTokens Example Basic tests", function () { ]); await submitForm(page); await page.waitForNavigation(); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".sessionButton"); let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); diff --git a/examples/nest/with-emailpassword/test/basic.test.js b/examples/nest/with-emailpassword/test/basic.test.js index 010816c0e..5a4fc5f87 100644 --- a/examples/nest/with-emailpassword/test/basic.test.js +++ b/examples/nest/with-emailpassword/test/basic.test.js @@ -80,7 +80,8 @@ describe("SuperTokens Example Basic tests", function () { ]); await submitForm(page); await page.waitForNavigation(); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".sessionButton"); let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); diff --git a/examples/next/with-emailpassword/config/appInfo.ts b/examples/next/with-emailpassword/config/appInfo.ts index e148fa890..a7ea82b4c 100644 --- a/examples/next/with-emailpassword/config/appInfo.ts +++ b/examples/next/with-emailpassword/config/appInfo.ts @@ -2,7 +2,8 @@ const port = process.env.APP_PORT || 3000; const apiBasePath = "/api/auth/"; -export const websiteDomain = process.env.APP_URL || process.env.NEXT_PUBLIC_APP_URL || `http://localhost:${port}`; +export const websiteDomain = + process.env.APP_URL || process.env.NEXT_PUBLIC_APP_URL || process.env.URL || `http://localhost:${port}`; export const appInfo = { appName: "SuperTokens Demo App", diff --git a/examples/next/with-emailpassword/netlify.toml b/examples/next/with-emailpassword/netlify.toml new file mode 100644 index 000000000..00649df24 --- /dev/null +++ b/examples/next/with-emailpassword/netlify.toml @@ -0,0 +1,4 @@ +[build] + command = "NEXT_PUBLIC_APP_URL=$DEPLOY_URL npm run build" + functions = "netlify/functions" + publish = ".next" diff --git a/examples/next/with-emailpassword/package-lock.json b/examples/next/with-emailpassword/package-lock.json index e702c675b..8a5aa6b80 100644 --- a/examples/next/with-emailpassword/package-lock.json +++ b/examples/next/with-emailpassword/package-lock.json @@ -4,6 +4,7 @@ "requires": true, "packages": { "": { + "hasInstallScript": true, "dependencies": { "next": "latest", "react": "^18.2.0", diff --git a/examples/next/with-emailpassword/package.json b/examples/next/with-emailpassword/package.json index 7ea83a771..462e4d850 100644 --- a/examples/next/with-emailpassword/package.json +++ b/examples/next/with-emailpassword/package.json @@ -1,6 +1,7 @@ { "private": true, "scripts": { + "postinstall": "../../../updateLibInExample", "dev": "next dev", "build": "next build", "start": "next start" diff --git a/examples/next/with-emailpassword/test/basic.test.js b/examples/next/with-emailpassword/test/basic.test.js index 25087c794..3708671c8 100644 --- a/examples/next/with-emailpassword/test/basic.test.js +++ b/examples/next/with-emailpassword/test/basic.test.js @@ -34,8 +34,19 @@ const EmailPassword = require("supertokens-node/recipe/emailpassword"); // Run the tests in a DOM environment. require("jsdom-global")(); -const apiDomain = "http://localhost:3001"; -const websiteDomain = "http://localhost:3000"; +let deployInfo; + +if (process.env.TEST_DEPLOYED_VERSION) { + deployInfo = require("../deployInfo.json"); + + if (!deployInfo.deploy_url) { + throw new Error("Deployment failed or json error. " + JSON.stringify(deployInfo)); + } +} + +const apiDomain = deployInfo?.deploy_url ?? "http://localhost:3000"; +const websiteDomain = deployInfo?.deploy_url ?? "http://localhost:3000"; + SuperTokensNode.init({ supertokens: { // We are running these tests without running a local ST instance @@ -80,8 +91,19 @@ describe("SuperTokens Example Basic tests", function () { ]); await submitForm(page); await page.waitForNavigation(); - const user = await EmailPassword.getUserByEmail("public", email); + const userList = await SuperTokensNode.listUsersByAccountInfo("public", { email }); + const user = userList[0]; const callApiBtn = await page.waitForSelector(".ProtectedHome_sessionButton__ihFAK"); + + // we save the cookies.. + let originalCookies = (await page._client.send("Network.getAllCookies")).cookies; + + // we set the old cookies with invalid access token + originalCookies = originalCookies.map((c) => + c.name === "sAccessToken" || c.name === "st-access-token" ? { ...c, value: "broken" } : c + ); + await page.setCookie(...originalCookies); + let setAlertContent; let alertContent = new Promise((res) => (setAlertContent = res)); page.on("dialog", async (dialog) => { diff --git a/updateLibInExample b/updateLibInExample new file mode 100755 index 000000000..5e1d0acdf --- /dev/null +++ b/updateLibInExample @@ -0,0 +1,3 @@ +if ! [[ -z "${COMMIT_REF}" ]]; then + npm i git+$REPOSITORY_URL#$COMMIT_REF +fi