File size: 6,957 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import config from '@automattic/calypso-config';
import { type Request, type Response, type NextFunction } from 'express';
import { type Details as UserAgentDetails, parse as parseUserAgent } from 'express-useragent';
import unsupportedBrowserMiddleware from '../../middleware/unsupported-browser';

jest.mock( '@automattic/calypso-config', () =>
	Object.assign(
		jest.fn().mockImplementation( ( key ) => {
			if ( key === 'magnificent_non_en_locales' ) {
				return [ 'es' ];
			}
		} ),
		{
			isEnabled: jest.fn().mockImplementation( ( feature ) => {
				if ( feature === 'redirect-fallback-browsers' ) {
					return true;
				}
				if ( feature === 'redirect-fallback-browsers/test' ) {
					return false;
				}
				return false;
			} ),
		}
	)
);

jest.mock( 'calypso/server/lib/analytics', () => ( {
	tracks: {
		recordEvent: jest.fn(),
	},
} ) );

describe( 'unsupported-browser', () => {
	let req: Partial< Request > & { useragent: UserAgentDetails };
	let res: Partial< Response > & { redirect: jest.Mock; cookie: jest.Mock };
	let next: NextFunction;

	beforeEach( () => {
		jest.clearAllMocks();

		req = {
			useragent: {} as UserAgentDetails,
			path: '/test',
			cookies: {},
			query: {},
			originalUrl: '/test',
		};

		res = {
			redirect: jest.fn(),
			cookie: jest.fn(),
		};

		next = jest.fn();
	} );

	it( 'should call next() if feature flag is disabled', () => {
		( config.isEnabled as jest.Mock ).mockReturnValueOnce( false );

		unsupportedBrowserMiddleware()( req, res, next );

		expect( next ).toHaveBeenCalled();
		expect( res.redirect ).not.toHaveBeenCalled();
	} );

	it( 'should call next() if bypass_target_redirection cookie is true', () => {
		req.cookies.bypass_target_redirection = 'true';

		unsupportedBrowserMiddleware()( req, res, next );

		expect( next ).toHaveBeenCalled();
		expect( res.redirect ).not.toHaveBeenCalled();
	} );

	it( 'should set cookie and call next() if bypassTargetRedirection query param is true', () => {
		unsupportedBrowserMiddleware()(
			{ ...req, query: { ...req.query, bypassTargetRedirection: 'true' } },
			res,
			next
		);

		expect( res.cookie ).toHaveBeenCalledWith(
			'bypass_target_redirection',
			true,
			expect.objectContaining( {
				expires: expect.any( Date ),
				httpOnly: true,
				secure: true,
			} )
		);
		expect( next ).toHaveBeenCalled();
		expect( res.redirect ).not.toHaveBeenCalled();
	} );

	describe( 'allowed paths', () => {
		beforeEach( () => {
			// Use an unsupported browser that should normally be redirected
			req.useragent = parseUserAgent(
				'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko'
			);
		} );

		it.each( [
			{ path: '/browsehappy', allowed: true },
			{ path: '/themes', allowed: true },
			{ path: '/es/themes', allowed: true },
			{ path: '/theme/twentytwenty', allowed: true },
			{ path: '/calypso/style.css', allowed: true },
			{ path: '/themeshaper', allowed: false },
		] )( 'should not redirect for allowed paths', ( { path, allowed } ) => {
			unsupportedBrowserMiddleware()( { ...req, path }, res, next );

			expect( next ).toHaveBeenCalledTimes( allowed ? 1 : 0 );
			expect( res.redirect ).toHaveBeenCalledTimes( allowed ? 0 : 1 );
		} );
	} );

	describe( 'unsupported browsers', () => {
		test.each( [
			{
				description: 'IE 11',
				userAgent: 'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko',
			},
			{
				description: 'Edge 79',
				userAgent:
					'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43',
			},
			{
				description: 'Chrome 79',
				userAgent:
					'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
			},
			{
				description: 'Firefox 73',
				userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0',
			},
			{
				description: 'Safari 13.0',
				userAgent:
					'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15',
			},
			{
				description: 'iOS 13.3',
				userAgent:
					'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1',
			},
			{
				description: 'Opera 66',
				userAgent:
					'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 OPR/66.0.3575.31',
			},
		] )( 'should redirect $description', ( { userAgent } ) => {
			req.useragent = parseUserAgent( userAgent );

			unsupportedBrowserMiddleware()( req, res, next );

			expect( res.redirect ).toHaveBeenCalledWith( '/browsehappy?from=%2Ftest' );
			expect( next ).not.toHaveBeenCalled();
		} );
	} );

	describe( 'supported browsers', () => {
		test.each( [
			{
				description: 'Chrome 80',
				userAgent:
					'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
			},
			{
				description: 'Firefox 74',
				userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0',
			},
			{
				description: 'Safari 14.0',
				userAgent:
					'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
			},
			{
				description: 'WordPress Desktop app',
				userAgent:
					'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 WordPressDesktop/7.0.0 Electron/13.5.1',
			},
			{
				description: 'Edge 80',
				userAgent:
					'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36 Edg/80.0.361.69',
			},
			{
				description: 'Opera 67',
				userAgent:
					'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 OPR/67.0.3575.31',
			},
		] )( 'should not redirect $description', ( { userAgent } ) => {
			req.useragent = parseUserAgent( userAgent );

			unsupportedBrowserMiddleware()( req, res, next );

			expect( next ).toHaveBeenCalled();
			expect( res.redirect ).not.toHaveBeenCalled();
		} );
	} );

	it( 'should force redirect when test flag is enabled', () => {
		// Override config.isEnabled for this test to enable test mode
		( config.isEnabled as jest.Mock ).mockImplementation(
			( feature ) =>
				feature === 'redirect-fallback-browsers' || feature === 'redirect-fallback-browsers/test'
		);

		// Use a modern browser that shouldn't normally be redirected
		req.useragent = parseUserAgent(
			'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15'
		);

		unsupportedBrowserMiddleware()( req, res, next );

		expect( res.redirect ).toHaveBeenCalledWith( '/browsehappy?from=%2Ftest' );
		expect( next ).not.toHaveBeenCalled();
	} );
} );