-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tests): ⚡️ Add msw and mock authentication
- Loading branch information
1 parent
d19b26e
commit b1f54b7
Showing
15 changed files
with
789 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { rest, setupWorker } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
const handlers = () => [ | ||
rest.get('http://localhost:3000/api/auth/session', (req, res, ctx) => { | ||
const authenticatedUser = JSON.parse( | ||
typeof localStorage !== 'undefined' | ||
? (localStorage.getItem('authenticatedUser') as string) | ||
: '{"id":"proUser","name":"John Smith","email":"john@smith.com","emailVerified":null,"image":"https://avatars.githubusercontent.com/u/16015833?v=4","plan":"PRO","stripeId":null}' | ||
) | ||
return res( | ||
ctx.json({ | ||
user: authenticatedUser, | ||
expires: '2022-03-13T17:02:42.317Z', | ||
}) | ||
) | ||
}), | ||
] | ||
|
||
export const enableMocks = () => { | ||
if (typeof window === 'undefined') { | ||
const server = setupServer(...handlers()) | ||
server.listen() | ||
} else { | ||
const worker = setupWorker(...handlers()) | ||
worker.start({ | ||
onUnhandledRequest: 'bypass', | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"cookies": [], | ||
"origins": [ | ||
{ | ||
"origin": "http://localhost:3000", | ||
"localStorage": [ | ||
{ | ||
"name": "authenticatedUser", | ||
"value": "{\"id\":\"freeUser\",\"name\":\"John Smith\",\"email\":\"john@smith.fr\",\"emailVerified\":null,\"image\":\"https://avatars.githubusercontent.com/u/16015833?v=4\",\"plan\":\"FREE\",\"stripeId\":null}" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,14 @@ | ||
import { chromium, FullConfig, Page } from '@playwright/test' | ||
import { existsSync } from 'fs' | ||
import { | ||
getSignedInUser, | ||
setupDatabase, | ||
teardownDatabase, | ||
} from './services/database' | ||
import { FullConfig } from '@playwright/test' | ||
import { setupDatabase, teardownDatabase } from './services/database' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
require('dotenv').config({ path: '.env' }) | ||
|
||
async function globalSetup(config: FullConfig) { | ||
const { baseURL } = config.projects[0].use | ||
if (!baseURL) throw new Error('baseURL is missing') | ||
if (!process.env.GITHUB_EMAIL || !process.env.GITHUB_PASSWORD) | ||
throw new Error( | ||
'GITHUB_EMAIL or GITHUB_PASSWORD are missing in the environment. They are required to log in.' | ||
) | ||
|
||
await teardownDatabase() | ||
|
||
const signedInUser = await getSignedInUser(process.env.GITHUB_EMAIL as string) | ||
if (!signedInUser || !existsSync('./playwright/authenticatedState.json')) { | ||
const browser = await chromium.launch() | ||
const page = await browser.newPage() | ||
await signIn(page) | ||
await page.context().storageState({ | ||
path: './playwright/authenticatedState.json', | ||
}) | ||
} | ||
|
||
await setupDatabase(process.env.GITHUB_EMAIL as string) | ||
} | ||
|
||
const signIn = async (page: Page) => { | ||
await page.goto(`${process.env.PLAYWRIGHT_BUILDER_TEST_BASE_URL}/signin`) | ||
await page.click('text=Continue with GitHub') | ||
await page.fill('input[name="login"]', process.env.GITHUB_EMAIL as string) | ||
await page.fill( | ||
'input[name="password"]', | ||
process.env.GITHUB_PASSWORD as string | ||
) | ||
await page.press('input[name="password"]', 'Enter') | ||
try { | ||
await page.locator('text=Authorize baptisteArno').click({ timeout: 3000 }) | ||
} catch { | ||
return | ||
} | ||
await page.waitForNavigation({ | ||
url: `${process.env.PLAYWRIGHT_BUILDER_TEST_BASE_URL}/typebots`, | ||
}) | ||
await setupDatabase() | ||
} | ||
|
||
export default globalSetup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"cookies": [], | ||
"origins": [ | ||
{ | ||
"origin": "http://localhost:3000", | ||
"localStorage": [ | ||
{ | ||
"name": "authenticatedUser", | ||
"value": "{\"id\":\"proUser\",\"name\":\"John Smith\",\"email\":\"john@smith.com\",\"emailVerified\":null,\"image\":\"https://avatars.githubusercontent.com/u/16015833?v=4\",\"plan\":\"PRO\",\"stripeId\":null}" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b1f54b7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
viewer-v2 – ./apps/viewer
typebot-viewer.vercel.app
viewer-v2-git-main-typebot-io.vercel.app
viewer-v2-typebot-io.vercel.app
b1f54b7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
builder-v2 – ./apps/builder
builder-v2-typebot-io.vercel.app
next.typebot.io
builder-v2-git-main-typebot-io.vercel.app
b1f54b7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
landing-page-v2 – ./apps/landing-page
landing-page-v2-typebot-io.vercel.app
landing-page-v2-jade.vercel.app
landing-page-v2-git-main-typebot-io.vercel.app