aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functional/dashboard/account.spec.ts
blob: bee9d6a9ea561ec643aafaed60f0c2a70ffeb403 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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');
  });
});