diff options
Diffstat (limited to 'tests/functional/dashboard/account.spec.ts')
-rw-r--r-- | tests/functional/dashboard/account.spec.ts | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/tests/functional/dashboard/account.spec.ts b/tests/functional/dashboard/account.spec.ts new file mode 100644 index 0000000..bee9d6a --- /dev/null +++ b/tests/functional/dashboard/account.spec.ts | |||
@@ -0,0 +1,124 @@ | |||
1 | import { test } from '@japa/runner'; | ||
2 | import User from 'App/Models/User'; | ||
3 | import UserFactory from 'Database/factories/UserFactory'; | ||
4 | |||
5 | test.group('Dashboard / Account page', () => { | ||
6 | test('redirects to /user/login when accessing /user/account as guest', async ({ | ||
7 | client, | ||
8 | }) => { | ||
9 | const response = await client.get('/user/account'); | ||
10 | |||
11 | response.assertRedirectsTo('/user/login'); // Check if it redirects to the expected URL | ||
12 | }); | ||
13 | |||
14 | test('returns a 200 opening the account route while logged in', async ({ | ||
15 | client, | ||
16 | }) => { | ||
17 | const user = await UserFactory.create(); | ||
18 | const response = await client.get('/user/account').loginAs(user); | ||
19 | |||
20 | response.assertStatus(200); | ||
21 | response.assertTextIncludes('Your Ferdium account'); | ||
22 | |||
23 | response.assertTextIncludes(user.email); | ||
24 | response.assertTextIncludes(user.username); | ||
25 | response.assertTextIncludes(user.lastname); | ||
26 | }); | ||
27 | |||
28 | test('returns a validation error for all fields when missing', async ({ | ||
29 | client, | ||
30 | }) => { | ||
31 | const user = await UserFactory.create(); | ||
32 | const response = await client.post('/user/account').loginAs(user); | ||
33 | |||
34 | response.assertTextIncludes( | ||
35 | 'value="required validation failed,required validation failed" placeholder="E-Mail"', | ||
36 | ); | ||
37 | response.assertTextIncludes( | ||
38 | 'value="required validation failed,required validation failed" placeholder="Name"', | ||
39 | ); | ||
40 | response.assertTextIncludes( | ||
41 | 'value="required validation failed,required validation failed" placeholder="Last Name"', | ||
42 | ); | ||
43 | }); | ||
44 | |||
45 | test('returns a validation error for username when there is another user with same username', async ({ | ||
46 | client, | ||
47 | }) => { | ||
48 | const user = await UserFactory.create(); | ||
49 | const existingUser = await UserFactory.create(); | ||
50 | |||
51 | const response = await client.post('/user/account').loginAs(user).form({ | ||
52 | username: existingUser.username, | ||
53 | email: user.email, | ||
54 | lastname: user.lastname, | ||
55 | }); | ||
56 | |||
57 | response.assertTextIncludes( | ||
58 | 'value="unique validation failure" placeholder="Name"', | ||
59 | ); | ||
60 | }); | ||
61 | |||
62 | test('returns a validation error for email when there is another user with same email', async ({ | ||
63 | client, | ||
64 | }) => { | ||
65 | const user = await UserFactory.create(); | ||
66 | const existingUser = await UserFactory.create(); | ||
67 | |||
68 | const response = await client.post('/user/account').loginAs(user).form({ | ||
69 | username: user.username, | ||
70 | email: existingUser.email, | ||
71 | lastname: user.lastname, | ||
72 | }); | ||
73 | |||
74 | response.assertTextIncludes( | ||
75 | 'value="unique validation failure" placeholder="E-Mail"', | ||
76 | ); | ||
77 | }); | ||
78 | |||
79 | test('updates user data and ensures the data is persisted', async ({ | ||
80 | client, | ||
81 | assert, | ||
82 | }) => { | ||
83 | const user = await UserFactory.create(); | ||
84 | const response = await client.post('/user/account').loginAs(user).form({ | ||
85 | username: 'edited-username', | ||
86 | email: 'edited-email', | ||
87 | lastname: 'edited-lastname', | ||
88 | }); | ||
89 | |||
90 | response.assertStatus(200); | ||
91 | |||
92 | // Ensure updated data is displayed on account page | ||
93 | response.assertTextIncludes('edited-username'); | ||
94 | response.assertTextIncludes('edited-email'); | ||
95 | response.assertTextIncludes('edited-lastname'); | ||
96 | |||
97 | // Ensure updated data is persisted in database | ||
98 | const updatedUser = await User.findBy('id', user.id); | ||
99 | assert.equal(updatedUser?.username, 'edited-username'); | ||
100 | assert.equal(updatedUser?.email, 'edited-email'); | ||
101 | assert.equal(updatedUser?.lastname, 'edited-lastname'); | ||
102 | }); | ||
103 | |||
104 | test('updates user password and ensures the user can still login', async ({ | ||
105 | client, | ||
106 | }) => { | ||
107 | const user = await UserFactory.create(); | ||
108 | const response = await client.post('/user/account').loginAs(user).form({ | ||
109 | username: user.username, | ||
110 | email: user.email, | ||
111 | lastname: user.lastname, | ||
112 | password: 'modified-password-account-page', | ||
113 | }); | ||
114 | |||
115 | response.assertStatus(200); | ||
116 | |||
117 | const loginResponse = await client.post('/user/login').fields({ | ||
118 | mail: user.email, | ||
119 | password: 'modified-password-account-page', | ||
120 | }); | ||
121 | |||
122 | loginResponse.assertRedirectsTo('/user/account'); | ||
123 | }); | ||
124 | }); | ||