-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathauth.spec.ts
138 lines (113 loc) · 4.7 KB
/
auth.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { describe, expect, it } from 'vitest'
import { createPage, setup } from '@nuxt/test-utils/e2e'
import { createResolver } from '@nuxt/kit'
const { resolve } = createResolver(import.meta.url)
await setup({
rootDir: resolve('../playground'),
build: false,
server: true,
browser: true,
dev: true,
browserOptions: {
type: 'chromium',
launch: {
headless: true,
},
},
})
describe('auth/multi-tenancy', async () => {
it('should create a default tenant token if no tenant is specified', async () => {
const page = await createPage('/authentication')
// 1. Sign out, clear tenant to start clean
await page.getByTestId('sign-out').click()
await page.getByTestId('tenant').clear()
// 2. Ensure test account exists
const signupResponse = page.waitForResponse((r) =>
r.url().includes('accounts:signUp')
)
await page.getByTestId('email-signup').fill('[email protected]')
await page.getByTestId('password-signup').fill('testtest')
await page.getByTestId('submit-signup').click()
await signupResponse
// 3. Log in with test account, check tenant
// Call to sign in is 'accounts:signInWithPassword', but we need __session call to get user info
const signinResponse = page.waitForResponse((r) =>
r.url().includes('/api/__session')
)
await page.getByTestId('email-signin').fill('[email protected]')
await page.getByTestId('password-signin').fill('testtest')
await page.getByTestId('submit-signin').click()
await signinResponse
// 4. Assert user does in fact not have a tenant id
const userData = await page.getByTestId('user-data-client').textContent()
expect(userData).toBeTruthy()
if (!userData) return
const user = JSON.parse(userData)
expect(user.tenantId).toBeUndefined()
})
it('should create token with tenantId if tenant name is specified', async () => {
const page = await createPage('/authentication')
const tenantName = 'tenant A'
// 1. Sign out, clear tenant to start clean
await page.getByTestId('sign-out').click()
await page.getByTestId('tenant').clear()
await page.getByTestId('tenant').fill(tenantName)
// 2. Ensure test account exists
const signupResponse = page.waitForResponse((r) =>
r.url().includes('accounts:signUp')
)
await page.getByTestId('email-signup').fill('[email protected]')
await page.getByTestId('password-signup').fill('testtest')
await page.getByTestId('submit-signup').click()
await signupResponse
// 3. Log in with test account, check tenant
// Call to sign in is 'accounts:signInWithPassword', but we need __session call to get user info
const signinResponse = page.waitForResponse((r) =>
r.url().includes('/api/__session')
)
await page.getByTestId('email-signin').fill('[email protected]')
await page.getByTestId('password-signin').fill('testtest')
await page.getByTestId('submit-signin').click()
await signinResponse
// 4. Assert user does in fact not have a tenant id
const userData = await page.getByTestId('user-data-client').textContent()
expect(userData).toBeTruthy()
if (!userData) return
const user = JSON.parse(userData)
expect(user.tenantId).toEqual(tenantName)
})
it('should return tenantId in server render', async () => {
const page = await createPage('/authentication')
const tenantName = 'tenant A'
// 1. Sign out, clear tenant to start clean
await page.getByTestId('sign-out').click()
await page.getByTestId('tenant').clear()
await page.getByTestId('tenant').fill(tenantName)
// 2. Ensure test account exists
const signupResponse = page.waitForResponse((r) =>
r.url().includes('accounts:signUp')
)
await page.getByTestId('email-signup').fill('[email protected]')
await page.getByTestId('password-signup').fill('testtest')
await page.getByTestId('submit-signup').click()
await signupResponse
// 3. Log in with test account, check tenant
// Call to sign in is 'accounts:signInWithPassword', but we need __session call to get user info
const signinResponse = page.waitForResponse((r) =>
r.url().includes('/api/__session')
)
await page.getByTestId('email-signin').fill('[email protected]')
await page.getByTestId('password-signin').fill('testtest')
await page.getByTestId('submit-signin').click()
await signinResponse
// 4. Reload the page to trigger server render
await page.reload({ waitUntil: 'domcontentloaded' })
const serverUserData = await page
.getByTestId('user-data-server')
.textContent()
expect(serverUserData).toBeTruthy()
if (!serverUserData) return
const serverUser = JSON.parse(serverUserData)
expect(serverUser.tenantId).toEqual(tenantName)
})
})