/
personal-setting-password.spec.ts
119 lines (97 loc) · 3.43 KB
/
personal-setting-password.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
// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
import type { ExtendedRenderResult } from '#tests/support/components/renderComponent.ts'
import { visitView } from '#tests/support/components/visitView.ts'
import { mockAccount } from '#tests/support/mock-account.ts'
import { mockApplicationConfig } from '#tests/support/mock-applicationConfig.ts'
import { mockAccountChangePasswordMutation } from '../graphql/mutations/accountChangePassword.mocks.ts'
const changePassword = async (
view: ExtendedRenderResult,
currentPassword: string,
newPassword: string,
newPasswordConfirm?: string,
) => {
await view.events.type(
await view.findByLabelText('Current password'),
currentPassword,
)
await view.events.type(
await view.findByLabelText('New password'),
newPassword,
)
await view.events.type(
await view.findByLabelText('Confirm new password'),
newPasswordConfirm || newPassword,
)
await view.events.click(view.getByRole('button', { name: 'Change Password' }))
}
describe('password personal settings', () => {
beforeEach(() => {
mockAccount({
firstname: 'John',
lastname: 'Doe',
})
mockApplicationConfig({
user_show_password_login: true,
})
})
it('redirects to the error page when password login is disabled', async () => {
mockApplicationConfig({
user_show_password_login: false,
})
const view = await visitView('/personal-setting/password')
await vi.waitFor(() => {
expect(view, 'correctly redirects to error page').toHaveCurrentUrl(
'/error',
)
})
})
it('shows the form to change the password', async () => {
const view = await visitView('/personal-setting/password')
expect(view.getByText('Current password')).toBeInTheDocument()
expect(view.getByText('New password')).toBeInTheDocument()
expect(view.getByText('Confirm new password')).toBeInTheDocument()
expect(
view.getByRole('button', { name: 'Change Password' }),
).toBeInTheDocument()
})
it('shows an error message when e.g. current password is incorrect', async () => {
mockAccountChangePasswordMutation({
accountChangePassword: {
success: false,
errors: [
{
message: 'The current password you provided is incorrect.',
field: 'current_password',
},
],
},
})
const view = await visitView('/personal-setting/password')
await changePassword(view, 'wrong-password', 'new-password')
expect(
await view.findByText('The current password you provided is incorrect.'),
).toBeInTheDocument()
})
it('shows an error message when new password and confirmation do not match', async () => {
const view = await visitView('/personal-setting/password')
await changePassword(view, 'old-password', 'new-password', 'wrong-password')
expect(
await view.findByText(
"This field doesn't correspond to the expected value.",
),
).toBeInTheDocument()
})
it('shows a success message when password was changed successfully', async () => {
mockAccountChangePasswordMutation({
accountChangePassword: {
success: true,
errors: null,
},
})
const view = await visitView('/personal-setting/password')
await changePassword(view, 'old-password', 'new-password')
expect(
await view.findByText('Password changed successfully.'),
).toBeInTheDocument()
})
})