File size: 2,249 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
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 );

// 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: createPost', function () {
	const restAPIClient = new RestAPIClient( {
		username: 'fake_user',
		password: 'fake_password',
	} );
	const siteID = 2789;
	const requestURL = restAPIClient.getRequestURL( '1.1', `/sites/${ siteID }/posts/new` );

	test( 'Post can be created with only title parameter', async function () {
		const title = 'test';
		const testData = {
			title: title,
			URL: `https://faketestsite.com/2022/07/26/${ title }`,
		};

		nock( requestURL.origin ).post( requestURL.pathname ).reply( 200, testData );

		const response = await restAPIClient.createPost( siteID, { title: title } );

		expect( response.URL ).toBe( 'https://faketestsite.com/2022/07/26/test' );
		expect( response.title ).toBe( title );
	} );

	test( 'Post can be created with required and optional parameters', async function () {
		const title = 'test';
		const testData = {
			title: title,
			URL: `https://faketestsite.com/2022/07/26/${ title }`,
		};

		nock( requestURL.origin ).post( requestURL.pathname ).reply( 200, testData );

		const response = await restAPIClient.createPost( siteID, {
			title: title,
			date: new Date(),
			content: 'test content',
		} );

		expect( response.URL ).toBe( 'https://faketestsite.com/2022/07/26/test' );
		expect( response.title ).toBe( title );
	} );
} );