Skip to content

Commit d8bca0e

Browse files
vinckrkevgoaeneasr
authored
feat: add tests to django example (ory#42)
* u * fix: add makefile * fix: add ory proxy * fix: django test * chore: apply suggestions from code review Co-authored-by: Kevin Goslar <kevin.goslar@gmail.com> * fix: install poetry * fix: install python * fix: makefile * u * u * u * u * u * u * u * u * u * u * u * test: address pull request suggestions (ory#47) * chore: update repository templates [skip ci] - updated repository templates to ory/meta@32137ce * chore: update repository templates [skip ci] - updated repository templates to ory/meta@4301832 * Move files into django-ory-cloud dir * package-lock.json package.json * .gitignore package-lock.json package.json package-lock.json package.json Co-authored-by: aeneasr <3372410+aeneasr@users.noreply.github.com> * chore: removed unused test * chore: run django test in folder * chore: run django test in folder * chore: fix makefile * fix: rename workflow * fix: remove test artifacts * fix: makefile * chore: apply suggestions from code review * chore: django-ory-cloud/tests/playwright/django-ory-cloud.spec.ts Co-authored-by: Kevin Goslar <kevin.goslar@gmail.com> * u * u * u * chore: clean Makefile * fix: test out working dir only on test * chore: delete unused config * chore: shorten workflow name * u * chore: clean Makefile * chore: clean script Co-authored-by: Kevin Goslar <kevin.goslar@gmail.com> Co-authored-by: aeneasr <3372410+aeneasr@users.noreply.github.com>
1 parent 15bd385 commit d8bca0e

11 files changed

Lines changed: 534 additions & 191 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: django-ory-cloud
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
pull_request:
7+
8+
jobs:
9+
test-playwright:
10+
timeout-minutes: 10
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-node@v3
15+
with:
16+
node-version: '18'
17+
cache: 'npm'
18+
- name: Install dependencies
19+
run: npm ci
20+
- uses: 'actions/setup-python@v4'
21+
with:
22+
python-version: '3.10'
23+
- name: Install Poetry
24+
uses: snok/install-poetry@v1
25+
- name: Install Playwright
26+
run: npx playwright install --with-deps
27+
- name: Run Playwright tests
28+
working-directory: ./django-ory-cloud
29+
run: make test

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules
1+
node_modules

django-ory-cloud/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
.bin
12
*.pyc
23
*/db.sqlite3
4+
node_modules
5+
/test-results/
6+
/playwright-report/
7+
/playwright/.cache/

django-ory-cloud/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
install: node_modules poetry.lock
2+
poetry install
3+
4+
.PHONY: test
5+
test: install .bin/ory
6+
./test.sh
7+
8+
.bin/ory: Makefile
9+
curl https://raw.githubusercontent.com/ory/meta/master/install.sh | sh -s -- -d -b .bin ory v0.1.36
10+
11+
node_modules: package.json package-lock.json
12+
npm i
13+
touch node_modules

django-ory-cloud/package-lock.json

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

django-ory-cloud/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "django-ory-cloud-tests",
3+
"version": "0.0.0",
4+
"private": "true",
5+
"scripts": {
6+
"test:e2e": "playwright test",
7+
"test:debug": "playwright test --debug",
8+
"test:unit": "jest tests/jest --maxConcurrency=8 --bail 1"
9+
},
10+
"devDependencies": {
11+
"@playwright/test": "^1.24.0"
12+
}
13+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import type { PlaywrightTestConfig } from '@playwright/test'
2+
import { devices } from '@playwright/test'
3+
4+
/**
5+
* Read environment variables from file.
6+
* https://github.com/motdotla/dotenv
7+
*/
8+
// require('dotenv').config();
9+
10+
/**
11+
* See https://playwright.dev/docs/test-configuration.
12+
*/
13+
const config: PlaywrightTestConfig = {
14+
testDir: './tests',
15+
/* Maximum time one test can run for. */
16+
timeout: 30_000,
17+
expect: {
18+
/**
19+
* Maximum time expect() should wait for the condition to be met.
20+
* For example in `await expect(locator).toHaveText();`
21+
*/
22+
timeout: 1_000
23+
},
24+
/* Run tests in files in parallel */
25+
fullyParallel: true,
26+
/* Fail the build on CI if you accidentally left test.only in the source code. */
27+
forbidOnly: !!process.env.CI,
28+
/* Retry on CI only */
29+
retries: process.env.CI ? 2 : 0,
30+
/* the number of workers, undefined for parallel tests */
31+
workers: undefined,
32+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
33+
reporter: 'list',
34+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
35+
use: {
36+
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
37+
actionTimeout: 10_000,
38+
/* Base URL to use in actions like `await page.goto('/')`. */
39+
// baseURL: 'http://localhost:3000',
40+
41+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
42+
trace: 'on-first-retry'
43+
},
44+
45+
/* Configure projects for major browsers */
46+
projects: [
47+
{
48+
name: 'chromium',
49+
use: {
50+
...devices['Desktop Chrome']
51+
}
52+
}
53+
54+
/*
55+
{
56+
name: 'firefox',
57+
use: {
58+
...devices['Desktop Firefox'],
59+
},
60+
},
61+
62+
{
63+
name: 'webkit',
64+
use: {
65+
...devices['Desktop Safari'],
66+
},
67+
},
68+
*/
69+
70+
/* Test against mobile viewports. */
71+
// {
72+
// name: 'Mobile Chrome',
73+
// use: {
74+
// ...devices['Pixel 5'],
75+
// },
76+
// },
77+
// {
78+
// name: 'Mobile Safari',
79+
// use: {
80+
// ...devices['iPhone 12'],
81+
// },
82+
// },
83+
84+
/* Test against branded browsers. */
85+
// {
86+
// name: 'Microsoft Edge',
87+
// use: {
88+
// channel: 'msedge',
89+
// },
90+
// },
91+
// {
92+
// name: 'Google Chrome',
93+
// use: {
94+
// channel: 'chrome',
95+
// },
96+
// },
97+
]
98+
99+
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
100+
// outputDir: 'test-results/',
101+
102+
/* Run your local dev server before starting the tests */
103+
// webServer: {
104+
// command: 'npm run start',
105+
// port: 3000,
106+
// },
107+
}
108+
109+
export default config

django-ory-cloud/test.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -euxo pipefail
4+
5+
export ORY_SDK_URL=https://playground.projects.oryapis.com
6+
export ORY_UI_URL=https://playground.projects.oryapis.com/ui
7+
8+
(
9+
cd mysite
10+
poetry run python manage.py migrate
11+
poetry run python manage.py runserver &
12+
)
13+
.bin/ory proxy --no-jwt --port 4000 http://localhost:8000/ &
14+
15+
trap "exit" INT TERM ERR
16+
trap 'kill $(jobs -p)' EXIT
17+
18+
npx wait-on -v -t 300000 tcp:localhost:4000
19+
npm run test:e2e
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { test, expect, Page } from '@playwright/test'
2+
import { randomEmail, randomString } from './helpers'
3+
4+
const login = async (page: Page) => {
5+
const email = randomEmail()
6+
await page.fill('[name="traits.email"]', email)
7+
await page.fill('[name="password"]', randomString())
8+
await page.click('[value="password"]')
9+
return email
10+
}
11+
12+
test.describe('django-ory-cloud', () => {
13+
const url = 'http://localhost:4000/'
14+
const name = 'django'
15+
test('login and sign up', async ({ page }) => {
16+
await page.goto(url, { waitUntil: 'networkidle' })
17+
await page.locator('text=Sign in').click()
18+
await expect(page).toHaveURL(/.*\/ui\/login.*/)
19+
await page.click('[data-testid="cta-link"]')
20+
await expect(page).toHaveURL(/.*\/ui\/registration.*/)
21+
const email = await login(page)
22+
await expect(page).toHaveURL(/.*\/ui\/welcome.*/)
23+
await expect(page.locator('body')).toContainText(email)
24+
})
25+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const randomString = () =>
2+
Math.random().toString(36).substring(2, 15) +
3+
Math.random().toString(36).substring(2, 15)
4+
export const randomEmail = () => randomString() + "@" + randomString() + ".dev"

0 commit comments

Comments
 (0)