File size: 2,123 Bytes
1e92f2d |
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 |
import { describe, expect, test, jest } from '@jest/globals';
import nock from 'nock';
import { RestAPIClient, BEARER_TOKEN_URL } from '../rest-api-client';
import { SecretsManager } from '../secrets';
import type { Secrets } from '../secrets';
const fakeSecrets = {
calypsoOauthApplication: {
client_id: 'some_value',
client_secret: 'some_value',
},
testAccounts: {
basicUser: {
username: 'wpcomuser2',
password: 'hunter2',
primarySite: 'wpcomuser.wordpress.com/',
},
noUrlUser: {
username: 'nourluser',
password: 'password1234',
},
},
} as unknown as Secrets;
jest.spyOn( SecretsManager, 'secrets', 'get' ).mockImplementation( () => fakeSecrets );
describe( 'RestAPIClient: getBearerToken', function () {
test( 'Bearer Token is returned upon successful request', async function () {
const mockedToken = 'abcdefghijklmn';
nock( BEARER_TOKEN_URL )
.post( /.*/ )
.reply( 200, {
success: true,
data: {
bearer_token: mockedToken,
token_links: [ 'link_1', 'link_2' ],
},
} );
const restAPIClient = new RestAPIClient( {
username: 'user',
password: 'password',
} );
const bearerToken = await restAPIClient.getBearerToken();
expect( bearerToken ).toBeDefined();
expect( bearerToken ).toBe( mockedToken );
expect( typeof bearerToken ).toBe( 'string' );
} );
test.each( [
{
code: 'incorrect_password',
message: "Oops, that's not the right password. Please try again!",
},
{
code: 'invalid_username',
message:
"We don't seem to have an account with that name. Double-check the spelling and try again!",
},
] )( 'Throws error with expected code and message ($code)', async function ( { code, message } ) {
nock( BEARER_TOKEN_URL )
.post( /.*/ )
.reply( 200, {
success: false,
data: {
errors: [
{
code: code,
message: message,
},
],
},
} );
const restAPIClient = new RestAPIClient( {
username: 'fake_user',
password: 'fake_password',
} );
await expect( restAPIClient.getBearerToken() ).rejects.toThrow( `${ code }: ${ message }` );
} );
} );
|