aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functional/dashboard/account.spec.ts
blob: 25de22c93a25b7422ea73ddff2c38d9c31d78bca (plain) (blame)
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
import { test } from '@japa/runner'
import User from '#app/Models/User'
import UserFactory from '#database/factories/UserFactory'

test.group('Dashboard / Account page', () => {
  test('redirects to /user/login when accessing /user/account as guest', async ({ client }) => {
    const response = await client.get('/user/account')

    response.assertRedirectsTo('/user/login') // Check if it redirects to the expected URL
  })

  test('returns a 200 opening the account route while logged in', async ({ client }) => {
    const user = await UserFactory.create()
    const response = await client.get('/user/account').loginAs(user)

    response.assertStatus(200)
    response.assertTextIncludes('Your Ferdium account')

    response.assertTextIncludes(user.email)
    response.assertTextIncludes(user.username)
    response.assertTextIncludes(user.lastname)
  })

  test('returns a validation error for all fields when missing', async ({ client }) => {
    const user = await UserFactory.create()
    const response = await client.post('/user/account').loginAs(user)

    response.assertTextIncludes(
      'value="required validation failed,required validation failed" placeholder="E-Mail"'
    )
    response.assertTextIncludes(
      'value="required validation failed,required validation failed" placeholder="Name"'
    )
    response.assertTextIncludes(
      'value="required validation failed,required validation failed" placeholder="Last Name"'
    )
  })

  test('returns a validation error for username when there is another user with same username', async ({
    client,
  }) => {
    const user = await UserFactory.create()
    const existingUser = await UserFactory.create()

    const response = await client.post('/user/account').loginAs(user).form({
      username: existingUser.username,
      email: user.email,
      lastname: user.lastname,
    })

    response.assertTextIncludes('value="unique validation failure" placeholder="Name"')
  })

  test('returns a validation error for email when there is another user with same email', async ({
    client,
  }) => {
    const user = await UserFactory.create()
    const existingUser = await UserFactory.create()

    const response = await client.post('/user/account').loginAs(user).form({
      username: user.username,
      email: existingUser.email,
      lastname: user.lastname,
    })

    response.assertTextIncludes('value="unique validation failure" placeholder="E-Mail"')
  })

  test('updates user data and ensures the data is persisted', async ({ client, assert }) => {
    const user = await UserFactory.create()
    const response = await client.post('/user/account').loginAs(user).form({
      username: 'edited-username',
      email: 'edited-email',
      lastname: 'edited-lastname',
    })

    response.assertStatus(200)

    // Ensure updated data is displayed on account page
    response.assertTextIncludes('edited-username')
    response.assertTextIncludes('edited-email')
    response.assertTextIncludes('edited-lastname')

    // Ensure updated data is persisted in database
    const updatedUser = await User.findBy('id', user.id)
    assert.equal(updatedUser?.username, 'edited-username')
    assert.equal(updatedUser?.email, 'edited-email')
    assert.equal(updatedUser?.lastname, 'edited-lastname')
  })

  test('updates user password and ensures the user can still login', async ({ client }) => {
    const user = await UserFactory.create()
    const response = await client.post('/user/account').loginAs(user).form({
      username: user.username,
      email: user.email,
      lastname: user.lastname,
      password: 'modified-password-account-page',
    })

    response.assertStatus(200)

    const loginResponse = await client.post('/user/login').fields({
      mail: user.email,
      password: 'modified-password-account-page',
    })

    loginResponse.assertRedirectsTo('/user/account')
  })
})