Debian Codegraph @

examples/react-spa/e2e.spec.js

raw
1 import { test, expect } from '@playwright/test'
2
3 const MAILCATCHER_URL = 'http://localhost:1080'
4 const BASE_URL = 'http://localhost:10000'
5
6 function emailFactory () {
7 return `test-${Math.round(Math.random() * 100000000000)}@test.allauth.org`
8 }
9
10 function passwordFactory () {
11 return `pw!${new Date().valueOf()}`
12 }
13
14 function socialAccountUIDFactory () {
15 return '' + Math.round(Math.random() * 100000000)
16 }
17
18 async function clearMailcatcher (page) {
19 await page.goto(MAILCATCHER_URL)
20 async function acceptDialog (dialog) {
21 await dialog.accept()
22 page.removeListener('dialog', acceptDialog)
23 }
24 page.on('dialog', acceptDialog)
25 await page.getByTitle('Clear all messages').click()
26 }
27
28 async function getLinkFromMail (page) {
29 await page.waitForTimeout(500)
30 await page.goto(MAILCATCHER_URL + '/messages/1.plain')
31 const txt = await page.content()
32 const link = txt.match(/https?:\/\/[^\s]+/)[0]
33 return link
34 }
35
36 async function login (page, email, password) {
37 await page.goto(BASE_URL + '/account/login')
38 await page.getByLabel('Email').fill(email)
39 await page.getByLabel('Password:').fill(password)
40 await page.getByRole('button', { name: 'Login' }).click()
41 await page.waitForURL(BASE_URL + '/dashboard')
42 }
43
44 async function logout (page) {
45 // Logout
46 await page.goto(BASE_URL + '/account/logout')
47 await page.getByRole('button').click()
48 await page.waitForURL(BASE_URL)
49 }
50
51 test('complete flow', async ({ page }) => {
52 await clearMailcatcher(page)
53
54 // Dashboard redirects to login
55 await page.goto(BASE_URL + '/dashboard')
56 await page.waitForURL(BASE_URL + '/account/login?next=%2Fdashboard')
57
58 // No account yet, so signup.
59 await page.goto(BASE_URL + '/account/signup')
60 const email = emailFactory()
61 await page.getByLabel('Email').fill(email)
62 const password = passwordFactory()
63 await page.getByLabel('Password:').fill(password)
64 await page.getByLabel('Password (again)').fill(password)
65 await page.getByRole('button', { name: 'Sign Up' }).click()
66
67 // Signup requires email verification
68 await page.waitForURL(BASE_URL + '/account/verify-email')
69 const verifyLink = await getLinkFromMail(page)
70 await page.goto(verifyLink)
71 await page.getByRole('button').click()
72 await page.waitForURL(BASE_URL + '/dashboard')
73
74 // Logout
75 await logout(page)
76
77 // Password reset
78 await clearMailcatcher(page)
79 await page.goto(BASE_URL + '/account/password/reset')
80 await page.getByLabel('Email').fill(email)
81 await page.getByRole('button').click()
82 const resetLink = await getLinkFromMail(page)
83 await page.goto(resetLink)
84 const newPassword = passwordFactory()
85 await page.getByLabel('Password:').fill(newPassword)
86 await page.getByLabel('Password (again)').fill(newPassword)
87 await page.getByRole('button', { name: 'Reset' }).click()
88 await page.waitForURL(BASE_URL + '/account/login')
89
90 // Login using new password
91 await login(page, email, newPassword)
92 })
93
94 test('complete socialaccount flow', async ({ page }) => {
95 await clearMailcatcher(page)
96
97 // Dashboard redirects to login
98 await page.goto(BASE_URL + '/dashboard')
99 await page.waitForURL(BASE_URL + '/account/login?next=%2Fdashboard')
100 await page.getByRole('button', { name: 'Dummy' }).click()
101
102 // Dummy authenticate
103 await page.waitForURL(BASE_URL + '/accounts/dummy/authenticate/*')
104 const socialId = socialAccountUIDFactory()
105 await page.getByLabel('Account ID').fill(socialId)
106 await page.getByRole('button', { name: 'Login' }).click()
107
108 // Provider signup
109 await page.waitForURL(BASE_URL + '/account/provider/signup')
110 const email = emailFactory()
111 await page.getByLabel('Email').fill(email)
112 await page.getByRole('button', { name: 'Sign Up' }).click()
113
114 // Verify email
115 await page.waitForURL(BASE_URL + '/account/verify-email')
116 const verifyLink = await getLinkFromMail(page)
117 await page.goto(verifyLink)
118 await page.getByRole('button').click()
119 await page.waitForURL(BASE_URL + '/dashboard')
120
121 // Logout
122 await logout(page)
123
124 // Login, no more signup needed.
125 await page.goto(BASE_URL + '/dashboard')
126 await page.waitForURL(BASE_URL + '/account/login?next=%2Fdashboard')
127 await page.getByRole('button', { name: 'Dummy' }).click()
128
129 // Dummy authenticate
130 await page.waitForURL(BASE_URL + '/accounts/dummy/authenticate/*')
131 await page.getByLabel('Account ID').fill(socialId)
132 await page.getByRole('button', { name: 'Login' }).click()
133
134 // Set password
135 await page.waitForURL(BASE_URL + '/dashboard')
136 await page.goto(BASE_URL + '/account/password/change')
137 const newPassword = passwordFactory()
138 await page.getByLabel('Password:').fill(newPassword)
139 await page.getByLabel('Password (again)').fill(newPassword)
140 await page.getByRole('button', { name: 'Set' }).click()
141 await page.waitForURL(BASE_URL + '/dashboard')
142
143 // Login using password now
144 await logout(page)
145 await login(page, email, newPassword)
146 })