File size: 2,812 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
/**
 * @jest-environment jsdom
 */

import { addQueryArgs } from 'calypso/lib/url';
import { requestAdminMenu, receiveAdminMenu } from 'calypso/state/admin-menu/actions';
import { http } from 'calypso/state/data-layer/wpcom-http/actions';
import { requestFetchAdminMenu, handleSuccess } from '../';

describe( 'requestFetchAdminMenu', () => {
	test( 'should create the correct http request action', () => {
		const action = requestAdminMenu( 73738 );
		const output = requestFetchAdminMenu( action );
		expect( output ).toEqual(
			http(
				{
					method: 'GET',
					path: '/sites/73738/admin-menu/?_locale=user',
					apiNamespace: 'wpcom/v2',
				},
				action
			)
		);
	} );
} );

describe( 'handlers', () => {
	const getState = () => ( {
		sites: {
			items: { 73738: { options: { admin_url: 'https://example.wordpress.com/wp-admin' } } },
		},
	} );

	test( 'should create correct success action on fetch success', () => {
		const dispatch = jest.fn();
		const menuData = {};
		const action = receiveAdminMenu( 73738, menuData );
		handleSuccess( { siteId: 73738 }, menuData )( dispatch, getState );
		expect( dispatch ).toHaveBeenCalledTimes( 1 );
		expect( dispatch ).toHaveBeenCalledWith( expect.objectContaining( action ) );
	} );

	test( 'should sanitize menu URLs', () => {
		const dispatch = jest.fn();
		const unsafeMenu = [
			{
				icon: 'dashicons-warning',
				slug: 'my-custom-menu',
				title: 'Click here',
				type: 'menu-item',
				url: 'javascript:alert("hello")',
				children: [
					{
						parent: 'my-custom-menu',
						slug: 'my-custom-menu-2',
						title: 'Or here',
						type: 'submenu-item',
						url: 'http://example.com',
					},
				],
			},
			{
				icon: 'dashicons-default',
				slug: 'my-custom-menu-3',
				title: 'Home',
				type: 'menu-item',
				url: '/home',
				children: [
					{
						parent: 'my-custom-menu-3',
						slug: 'my-custom-menu-3',
						title: 'Settings',
						type: 'submenu-item',
						url: 'https://example.wordpress.com/wp-admin/settings.php',
					},
					{
						parent: 'my-custom-menu-4',
						slug: 'my-custom-menu-4',
						title: 'Malicious Child',
						type: 'submenu-item',
						url: 'javascript:alert("not good")',
					},
				],
			},
		];
		const sanitizedMenu = [ ...unsafeMenu ];
		sanitizedMenu[ 0 ].url = '';
		sanitizedMenu[ 0 ].children[ 0 ].url = '';
		sanitizedMenu[ 1 ].children[ 0 ].url = addQueryArgs(
			{
				return: document.location.href,
			},
			sanitizedMenu[ 1 ].children[ 0 ].url
		);
		sanitizedMenu[ 1 ].children[ 1 ].url = '';
		const action = receiveAdminMenu( 73738, sanitizedMenu );

		handleSuccess( { siteId: 73738 }, unsafeMenu )( dispatch, getState );

		expect( dispatch ).toHaveBeenCalledTimes( 1 );
		expect( dispatch ).toHaveBeenCalledWith( expect.objectContaining( action ) );
	} );
} );