File size: 3,584 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 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 |
import { describe, expect, test, jest } from '@jest/globals';
import nock from 'nock';
import { Roles } from '../lib';
import { RestAPIClient, BEARER_TOKEN_URL } from '../rest-api-client';
import { SecretsManager } from '../secrets';
import { AllInvitesResponse, NewInviteResponse } from '../types';
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 );
// Persist and intercept all bearer token calls in these tests.
nock( BEARER_TOKEN_URL )
.persist()
.post( /.*/ )
.reply( 200, {
success: true,
data: {
bearer_token: 'abcdefghijklmn',
token_links: [ 'link_1', 'link_2' ],
},
} );
describe( 'RestAPIClient: getInvites', function () {
const restAPIClient = new RestAPIClient( {
username: 'fake_user',
password: 'fake_password',
} );
const siteID = 4823;
const requestURL = restAPIClient.getRequestURL( '1.1', `/sites/${ siteID }/invites` );
test( '1 invite is returned', async function () {
const testData = {
invites: [
{
invite_key: 'abcdefgh',
role: 'editor',
is_pending: true,
user: {
email: 'testemail@test.com',
},
invited_by: {
ID: '5420',
login: 'test_user_88',
site_ID: siteID,
},
},
],
};
nock( requestURL.origin ).get( requestURL.pathname ).reply( 200, testData );
const response: AllInvitesResponse = await restAPIClient.getInvites( siteID );
expect( response ).toHaveLength( 1 );
} );
test( 'No invites are returned', async function () {
const testData = {
invites: [],
};
nock( requestURL.origin ).get( requestURL.pathname ).reply( 200, testData );
const response: AllInvitesResponse = await restAPIClient.getInvites( siteID );
expect( response ).toHaveLength( 0 );
} );
} );
describe( 'RestAPIClient: createInvite', function () {
const restAPIClient = new RestAPIClient( {
username: 'fake_user',
password: 'fake_password',
} );
const siteID = 4823;
const requestURL = restAPIClient.getRequestURL( '1.1', `/sites/${ siteID }/invites/new` );
test( 'Invite is successfully created', async function () {
const testEmails = [ 'test1@test.com', 'test2@test.com' ];
const role = 'Editor' as Roles;
const message = 'Test message';
const testResponse = {
sent: testEmails,
errors: [],
};
nock( requestURL.origin ).post( requestURL.pathname ).reply( 200, testResponse );
const response: NewInviteResponse = await restAPIClient.createInvite( siteID, {
email: testEmails,
role: role,
message: message,
} );
expect( response.sent ).toEqual( testEmails );
expect( response.errors.length ).toBe( 0 );
} );
test( 'Invite is rejected due to existing user email', async function () {
const testSuccessfulEmails = [ 'test1@test.com' ];
const testFailedEmails = [ 'test1@test.com' ];
const role = 'Editor' as Roles;
const testResponse = {
sent: testSuccessfulEmails,
errors: testFailedEmails,
};
nock( requestURL.origin ).post( requestURL.pathname ).reply( 200, testResponse );
await expect( () =>
restAPIClient.createInvite( siteID, {
email: testSuccessfulEmails.concat( testFailedEmails ),
role: role,
} )
).rejects.toThrow();
} );
} );
|