-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathapplications-passwords.test.js
More file actions
177 lines (141 loc) · 5.18 KB
/
applications-passwords.test.js
File metadata and controls
177 lines (141 loc) · 5.18 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* WordPress dependencies
*/
import { test, expect } from '@wordpress/e2e-test-utils-playwright';
const TEST_APPLICATION_NAME = 'Test Application';
test.describe( 'Manage applications passwords', () => {
test.use( {
applicationPasswords: async ( { requestUtils, admin, page }, use ) => {
await use( new ApplicationPasswords( { requestUtils, admin, page } ) );
},
} );
test.beforeEach(async ( { applicationPasswords } ) => {
await applicationPasswords.delete();
} );
test('should correctly create a new application password', async ( {
page,
applicationPasswords
} ) => {
await applicationPasswords.create();
const [ app ] = await applicationPasswords.get();
expect( app['name']).toBe( TEST_APPLICATION_NAME );
const successMessage = page.getByRole( 'alert' );
await expect( successMessage ).toHaveClass( /notice-success/ );
await expect(
successMessage
).toContainText(
`Your new password for ${TEST_APPLICATION_NAME} is:`
);
await expect(
successMessage
).toContainText(
`Be sure to save this in a safe location. You will not be able to retrieve it.`
);
} );
test('should correctly create a new application password with expiration', async ( {
page,
applicationPasswords
} ) => {
const expiresDate = new Date();
expiresDate.setDate( expiresDate.getDate() + 7 );
const expiresString = expiresDate.toISOString().split( 'T' )[ 0 ];
await applicationPasswords.create( TEST_APPLICATION_NAME, expiresString );
const [ app ] = await applicationPasswords.get();
expect( app['name'] ).toBe( TEST_APPLICATION_NAME );
expect( app['expires'] ).not.toBeNull();
expect( app['expires'].startsWith( expiresString ) ).toBe( true );
const successMessage = page.getByRole( 'alert' );
await expect( successMessage ).toHaveClass( /notice-success/ );
} );
test('should correctly update an application password expiration date', async ( {
page,
applicationPasswords
} ) => {
await applicationPasswords.create();
const [ app ] = await applicationPasswords.get();
expect( app['expires'] ).toBeNull();
const editButton = page.getByRole( 'button', { name: 'Edit Expiration Date' } );
await expect( editButton ).toBeVisible();
await editButton.click();
const expiresInput = page.locator( '.edit-expires-input' );
await expect( expiresInput ).toBeVisible();
const expiresDate = new Date();
expiresDate.setDate( expiresDate.getDate() + 10 );
const expiresString = expiresDate.toISOString().split( 'T' )[ 0 ];
await expiresInput.fill( expiresString );
const saveButton = page.getByRole( 'button', { name: 'Save' } );
await saveButton.click();
await expect( page.getByRole( 'alert' ) ).toContainText( 'Application password expiration updated.' );
const [ updatedApp ] = await applicationPasswords.get();
expect( updatedApp['expires'] ).not.toBeNull();
expect( updatedApp['expires'].startsWith( expiresString ) ).toBe( true );
} );
test( 'should correctly revoke a single application password', async ( {
page,
applicationPasswords
} ) => {
await applicationPasswords.create();
const revokeButton = page.getByRole( 'button', { name: `Revoke "${ TEST_APPLICATION_NAME }"` } );
await expect( revokeButton ).toBeVisible();
// Revoke password.
page.on( 'dialog', ( dialog ) => dialog.accept() );
await revokeButton.click();
await expect(
page.getByRole( 'alert' )
).toContainText(
'Application password revoked.'
);
const response = await applicationPasswords.get();
expect( response ).toEqual([]);
} );
test( 'should correctly revoke all the application passwords', async ( {
page,
applicationPasswords
} ) => {
await applicationPasswords.create();
const revokeAllButton = page.getByRole( 'button', { name: 'Revoke all application passwords' } );
await expect( revokeAllButton ).toBeVisible();
// Confirms revoking action.
page.on( 'dialog', ( dialog ) => dialog.accept() );
await revokeAllButton.click();
await expect(
page.getByRole( 'alert' )
).toContainText(
'All application passwords revoked.'
);
const response = await applicationPasswords.get();
expect( response ).toEqual([]);
} );
} );
class ApplicationPasswords {
constructor( { requestUtils, page, admin }) {
this.requestUtils = requestUtils;
this.page = page;
this.admin = admin;
}
async create(applicationName = TEST_APPLICATION_NAME, expires = null) {
await this.admin.visitAdminPage( '/profile.php' );
const newPasswordField = this.page.getByRole( 'textbox', { name: 'New Application Password Name' } );
await expect( newPasswordField ).toBeVisible();
await newPasswordField.fill( applicationName );
if ( expires ) {
const newPasswordExpiresField = this.page.getByLabel( 'Expires on' );
await expect( newPasswordExpiresField ).toBeVisible();
await newPasswordExpiresField.fill( expires );
}
await this.page.getByRole( 'button', { name: 'Add Application Password' } ).click();
await expect( this.page.getByRole( 'alert' ) ).toBeVisible();
}
async get() {
return this.requestUtils.rest( {
method: 'GET',
path: '/wp/v2/users/me/application-passwords',
} );
}
async delete() {
await this.requestUtils.rest( {
method: 'DELETE',
path: '/wp/v2/users/me/application-passwords',
} );
}
}