File size: 4,411 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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: getMyAccountInformation', function () {
	const restAPIClient = new RestAPIClient( {
		username: 'fake_user',
		password: 'fake_password',
	} );
	const requestURL = restAPIClient.getRequestURL( '1.1', '/me' );

	test( 'Account information is returned on successful request', async function () {
		const testData = {
			ID: 420,
			username: 'maryJane',
			email: 'maryJane@test.com',
			primary_blog: 199,
			primary_blog_url: 'maryjane.test.com',
			language: 'en',
		};
		nock( requestURL.origin ).get( requestURL.pathname ).reply( 200, testData );

		const response = await restAPIClient.getMyAccountInformation();
		expect( response.username ).toBe( testData.username );
		expect( response.primary_blog_url ).toEqual( testData.primary_blog_url );
		expect( response.ID ).toBe( testData.ID );
		expect( response.email ).toBe( testData.email );
	} );

	test.each( [
		{
			code: 'invalid_token',
			message: 'The OAuth2 token is invalid',
		},
		{
			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( requestURL.origin )
			.get( requestURL.pathname )
			.reply( 400, { error: code, message: message } );

		await expect( restAPIClient.getMyAccountInformation() ).rejects.toThrow(
			`${ code }: ${ message }`
		);
	} );
} );

describe( 'RestAPIClient: getAllDomains', function () {
	const restAPIClient = new RestAPIClient( {
		username: 'fake_user',
		password: 'fake_password',
	} );
	const requestURL = restAPIClient.getRequestURL( '1.1', '/all-domains/' );

	test( 'Sites are returned on successful request', async function () {
		const testData = {
			domains: [
				{
					blog_id: 5420,
					domain: 'myamazinsite.test.com',
				},
				{
					blog_id: 8799,
					domain: 'myamazinsiteredux.test.com',
				},
			],
		};

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

		const response = await restAPIClient.getAllDomains();
		expect( response.domains.length ).toBe( 2 );
		expect( response.domains[ 0 ].blog_id ).toBe( 5420 );
		expect( response.domains[ 1 ].blog_id ).toBe( 8799 );
	} );

	test.each( [
		{
			code: 'invalid_token',
			message: 'The OAuth2 token is invalid',
		},
	] )( 'Throws error with expected code and message ($code)', async function ( { code, message } ) {
		nock( requestURL.origin )
			.get( requestURL.pathname )
			.reply( 400, { error: code, message: message } );

		await expect( restAPIClient.getAllDomains() ).rejects.toThrow( `${ code }: ${ message }` );
	} );
} );

describe( 'RestAPIClient: createSite', function () {
	const restAPIClient = new RestAPIClient( {
		username: 'fake_user',
		password: 'fake_password',
	} );
	const requestURL = restAPIClient.getRequestURL( '1.1', '/sites/new' );

	test( 'Site metadata is returned on successful request', async function () {
		const testResponse = {
			success: true,
			blog_details: {
				url: 'https://fakeblog.blog.com',
				blogid: '420',
				blogname: 'fake_blog_name',
				site_slug: 'fakeblog.blog.com',
			},
		};

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

		const response = await restAPIClient.createSite( {
			name: 'fake_blog_name',
			title: 'fake_blog_title',
		} );

		expect( response ).not.toHaveProperty( 'error' );
		expect( response.success ).toBe( true );
	} );
} );