| 1 |
import fs from 'fs'; |
| 2 |
import { type APIRequestContext } from '@playwright/test'; |
| 3 |
import crypto from 'crypto'; |
| 4 |
import { Image, Post, WpPage, User } from '../types/types'; |
| 5 |
|
| 6 |
export default class ApiRequests { |
| 7 |
private readonly nonce: string; |
| 8 |
private readonly baseUrl: string; |
| 9 |
constructor( baseUrl: string, nonce: string ) { |
| 10 |
this.nonce = nonce; |
| 11 |
this.baseUrl = baseUrl; |
| 12 |
} |
| 13 |
|
| 14 |
public async create( request: APIRequestContext, entity: string, data: Post ) { |
| 15 |
const response = await request.post( `${ this.baseUrl }/index.php`, { |
| 16 |
params: { rest_route: `/wp/v2/${ entity }` }, |
| 17 |
headers: { |
| 18 |
'X-WP-Nonce': this.nonce, |
| 19 |
}, |
| 20 |
multipart: data, |
| 21 |
} ); |
| 22 |
|
| 23 |
if ( ! response.ok() ) { |
| 24 |
throw new Error( ` |
| 25 |
Failed to create a ${ entity }: ${ response.status() }. |
| 26 |
${ await response.text() } |
| 27 |
${ response.url() } |
| 28 |
TEST_PARALLEL_INDEX: ${ process.env.TEST_PARALLEL_INDEX } |
| 29 |
NONCE: ${ this.nonce } |
| 30 |
` ); |
| 31 |
} |
| 32 |
const { id } = await response.json(); |
| 33 |
|
| 34 |
return id; |
| 35 |
} |
| 36 |
|
| 37 |
public async createMedia( request: APIRequestContext, image: Image ) { |
| 38 |
const imagePath = image.filePath; |
| 39 |
const response = await request.post( `${ this.baseUrl }/index.php`, { |
| 40 |
|
| 41 |
params: { rest_route: '/wp/v2/media' }, |
| 42 |
headers: { |
| 43 |
'X-WP-Nonce': this.nonce, |
| 44 |
}, |
| 45 |
multipart: { |
| 46 |
file: fs.createReadStream( imagePath ), |
| 47 |
title: image.title, |
| 48 |
status: 'publish', |
| 49 |
description: image.description, |
| 50 |
altText: image.altText, |
| 51 |
caption: image.caption, |
| 52 |
}, |
| 53 |
} ); |
| 54 |
|
| 55 |
if ( ! response.ok() ) { |
| 56 |
throw new Error( ` |
| 57 |
Failed to create default media: ${ response.status() }. |
| 58 |
${ await response.text() } |
| 59 |
` ); |
| 60 |
} |
| 61 |
|
| 62 |
const { id } = await response.json(); |
| 63 |
|
| 64 |
return id; |
| 65 |
} |
| 66 |
|
| 67 |
public async deleteMedia( request: APIRequestContext, ids: string[] ) { |
| 68 |
const requests = []; |
| 69 |
|
| 70 |
for ( const id in ids ) { |
| 71 |
requests.push( request.delete( `${ this.baseUrl }/index.php`, { |
| 72 |
headers: { |
| 73 |
'X-WP-Nonce': this.nonce, |
| 74 |
}, |
| 75 |
params: { |
| 76 |
rest_route: `/wp/v2/media/${ ids[ id ] }`, |
| 77 |
force: 1, |
| 78 |
}, |
| 79 |
} ) ); |
| 80 |
} |
| 81 |
|
| 82 |
await Promise.all( requests ); |
| 83 |
} |
| 84 |
|
| 85 |
public async cleanUpTestPages( request: APIRequestContext, shouldDeleteAllPages = false ) { |
| 86 |
const pagesPublished = await this.getPages( request ), |
| 87 |
pagesDraft = await this.getPages( request, 'draft' ), |
| 88 |
pages = [ ...pagesPublished, ...pagesDraft ]; |
| 89 |
|
| 90 |
const pageIds = pages |
| 91 |
.filter( ( page: WpPage ) => shouldDeleteAllPages || page.title.rendered.includes( 'Playwright Test Page' ) ) |
| 92 |
.map( ( page: WpPage ) => page.id ); |
| 93 |
|
| 94 |
for ( const id of pageIds ) { |
| 95 |
await this.deletePage( request, id ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public async installPlugin( request: APIRequestContext, slug: string, active: boolean ) { |
| 100 |
const response = await request.post( `${ this.baseUrl }/index.php`, { |
| 101 |
params: { |
| 102 |
rest_route: `/wp/v2/plugins`, |
| 103 |
slug, |
| 104 |
status: active ? 'active' : 'inactive', |
| 105 |
}, |
| 106 |
headers: { |
| 107 |
'X-WP-Nonce': this.nonce, |
| 108 |
}, |
| 109 |
} ); |
| 110 |
|
| 111 |
if ( ! response.ok() ) { |
| 112 |
throw new Error( ` |
| 113 |
Failed to install a plugin: ${ response ? response.status() : '<no status>' }. |
| 114 |
${ response ? await response.text() : '<no response>' } |
| 115 |
slug: ${ slug } |
| 116 |
` ); |
| 117 |
} |
| 118 |
const { plugin } = await response.json(); |
| 119 |
|
| 120 |
return plugin; |
| 121 |
} |
| 122 |
|
| 123 |
public async deactivatePlugin( request: APIRequestContext, slug: string ) { |
| 124 |
const response = await request.post( `${ this.baseUrl }/index.php`, { |
| 125 |
params: { |
| 126 |
rest_route: `/wp/v2/plugins/${ slug }`, |
| 127 |
status: 'inactive', |
| 128 |
}, |
| 129 |
headers: { |
| 130 |
'X-WP-Nonce': this.nonce, |
| 131 |
}, |
| 132 |
} ); |
| 133 |
if ( ! response.ok() ) { |
| 134 |
throw new Error( ` |
| 135 |
Failed to deactivate a plugin: ${ response ? response.status() : '<no status>' }. |
| 136 |
${ response ? await response.text() : '<no response>' } |
| 137 |
slug: ${ slug } |
| 138 |
` ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public async activatePlugin( request: APIRequestContext, slug: string ) { |
| 143 |
const response = await request.post( `${ this.baseUrl }/index.php`, { |
| 144 |
params: { |
| 145 |
rest_route: `/wp/v2/plugins/${ slug }`, |
| 146 |
status: 'active', |
| 147 |
}, |
| 148 |
headers: { |
| 149 |
'X-WP-Nonce': this.nonce, |
| 150 |
}, |
| 151 |
} ); |
| 152 |
if ( ! response.ok() ) { |
| 153 |
throw new Error( ` |
| 154 |
Failed to activate a plugin: ${ response ? response.status() : '<no status>' }. |
| 155 |
${ response ? await response.text() : '<no response>' } |
| 156 |
slug: ${ slug } |
| 157 |
` ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public async deletePlugin( request: APIRequestContext, slug: string ) { |
| 162 |
const response = await this._delete( request, 'plugins', slug ); |
| 163 |
|
| 164 |
if ( ! response.ok() ) { |
| 165 |
throw new Error( ` |
| 166 |
Failed to delete a plugin: ${ response ? response.status() : '<no status>' }. |
| 167 |
${ response ? await response.text() : '<no response>' } |
| 168 |
slug: ${ slug } |
| 169 |
` ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public async getTheme( request: APIRequestContext, status?: 'active' | 'inactive' ) { |
| 174 |
return await this.get( request, 'themes', status ); |
| 175 |
} |
| 176 |
|
| 177 |
public async customGet( request: APIRequestContext, restRoute: string, multipart? ) { |
| 178 |
const response = await request.get( `${ this.baseUrl }/${ restRoute }`, { |
| 179 |
headers: { |
| 180 |
'X-WP-Nonce': this.nonce, |
| 181 |
}, |
| 182 |
multipart, |
| 183 |
} ); |
| 184 |
|
| 185 |
if ( ! response.ok() ) { |
| 186 |
throw new Error( ` |
| 187 |
Failed to get from ${ restRoute }: ${ response.status() }. |
| 188 |
${ this.baseUrl } |
| 189 |
` ); |
| 190 |
} |
| 191 |
|
| 192 |
return await response.json(); |
| 193 |
} |
| 194 |
|
| 195 |
public async customPut( request: APIRequestContext, restRoute: string, data ) { |
| 196 |
const response = await request.put( `${ this.baseUrl }/${ restRoute }`, { |
| 197 |
headers: { |
| 198 |
'X-WP-Nonce': this.nonce, |
| 199 |
}, |
| 200 |
data, |
| 201 |
} ); |
| 202 |
|
| 203 |
if ( ! response.ok() ) { |
| 204 |
throw new Error( ` |
| 205 |
Failed to put to ${ restRoute }: ${ response.status() }. |
| 206 |
${ await response.text() } |
| 207 |
` ); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
private async get( request: APIRequestContext, entity: string, status: string = 'publish' ) { |
| 212 |
const response = await request.get( `${ this.baseUrl }/index.php`, { |
| 213 |
params: { |
| 214 |
rest_route: `/wp/v2/${ entity }`, |
| 215 |
status, |
| 216 |
}, |
| 217 |
headers: { |
| 218 |
'X-WP-Nonce': this.nonce, |
| 219 |
}, |
| 220 |
} ); |
| 221 |
|
| 222 |
if ( ! response.ok() ) { |
| 223 |
throw new Error( ` |
| 224 |
Failed to get a ${ entity }: ${ response.status() }. |
| 225 |
${ await response.text() } |
| 226 |
` ); |
| 227 |
} |
| 228 |
|
| 229 |
return await response.json(); |
| 230 |
} |
| 231 |
|
| 232 |
private async getPages( request: APIRequestContext, status: string = 'publish' ) { |
| 233 |
return await this.get( request, 'pages', status ); |
| 234 |
} |
| 235 |
|
| 236 |
private async deletePage( request: APIRequestContext, pageId: string ) { |
| 237 |
await this._delete( request, 'pages', pageId ); |
| 238 |
} |
| 239 |
|
| 240 |
public async deleteUser( request: APIRequestContext, userId: string ) { |
| 241 |
const response = await request.delete( `${ this.baseUrl }/index.php`, { |
| 242 |
headers: { |
| 243 |
'X-WP-Nonce': this.nonce, |
| 244 |
}, |
| 245 |
params: { |
| 246 |
rest_route: `/wp/v2/users/${ userId }`, |
| 247 |
force: true, |
| 248 |
reassign: '-1', |
| 249 |
}, |
| 250 |
} ); |
| 251 |
|
| 252 |
if ( ! response.ok() ) { |
| 253 |
throw new Error( ` |
| 254 |
Failed to delete a user with id: ${ userId }: ${ response.status() }. |
| 255 |
${ await response.text() } |
| 256 |
` ); |
| 257 |
} |
| 258 |
|
| 259 |
return await response.json(); |
| 260 |
} |
| 261 |
|
| 262 |
private async _delete( request: APIRequestContext, entity: string, id: string ) { |
| 263 |
const response = await request.delete( `${ this.baseUrl }/index.php`, { |
| 264 |
params: { |
| 265 |
rest_route: `/wp/v2/${ entity }/${ id }`, |
| 266 |
}, |
| 267 |
headers: { |
| 268 |
'X-WP-Nonce': this.nonce, |
| 269 |
}, |
| 270 |
} ); |
| 271 |
|
| 272 |
if ( ! response.ok() ) { |
| 273 |
throw new Error( ` |
| 274 |
Failed to delete a ${ entity } with id '${ id }': ${ response.status() }. |
| 275 |
${ await response.text() } |
| 276 |
` ); |
| 277 |
} |
| 278 |
|
| 279 |
return response; |
| 280 |
} |
| 281 |
|
| 282 |
public async createNewUser( request: APIRequestContext, user: User ) { |
| 283 |
const randomNumber = crypto.randomInt( 0, 1000 ); |
| 284 |
const username = `${ user.username }${ randomNumber }`, |
| 285 |
email = user.email || username + '@example.com', |
| 286 |
password = user.password || 'password', |
| 287 |
roles = user.roles; |
| 288 |
|
| 289 |
const response = await request.post( `${ this.baseUrl }/index.php`, { |
| 290 |
|
| 291 |
params: { rest_route: '/wp/v2/users' }, |
| 292 |
headers: { |
| 293 |
'X-WP-Nonce': this.nonce, |
| 294 |
}, |
| 295 |
multipart: { |
| 296 |
username, |
| 297 |
email, |
| 298 |
password, |
| 299 |
roles: roles.join( ',' ), |
| 300 |
}, |
| 301 |
} ); |
| 302 |
|
| 303 |
if ( ! response.ok() ) { |
| 304 |
throw new Error( ` |
| 305 |
Failed to create new user: ${ response.status() }. |
| 306 |
${ await response.text() } |
| 307 |
` ); |
| 308 |
} |
| 309 |
|
| 310 |
const { id } = await response.json(); |
| 311 |
|
| 312 |
return { id, username, password }; |
| 313 |
} |
| 314 |
} |
| 315 |
|