-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve merchant-id email validation (#95)
- Loading branch information
Showing
2 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,6 +235,75 @@ test('should unpack a valid sdk meta bundle with multiple components', () => { | |
} | ||
}); | ||
|
||
test('should unpack a valid sdk meta bundle with multiple merchant-id email addresses', () => { | ||
const emails = [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'test@[email protected]' | ||
]; | ||
|
||
const sdkUrl = `https://www.paypal.com/sdk/js?client-id=foo&merchant-id=${ emails.map(anEmail => encodeURIComponent(anEmail)).join(',') }`; | ||
|
||
const { getSDKLoader } = unpackSDKMeta(Buffer.from(JSON.stringify({ | ||
url: sdkUrl | ||
})).toString('base64')); | ||
|
||
const $ = cheerio.load(getSDKLoader()); | ||
const src = $('script').attr('src'); | ||
|
||
if (src !== sdkUrl) { | ||
throw new Error(`Expected script url to be ${ sdkUrl } - got ${ src }`); | ||
} | ||
}); | ||
|
||
test('should error out from invalid merchant-id email addresses', () => { | ||
const emails = [ | ||
'@', | ||
'@io', | ||
'@test.com', | ||
'name@', | ||
'no_at_sign' | ||
]; | ||
|
||
emails.forEach(email => { | ||
const sdkUrl = `https://www.paypal.com/sdk/js?client-id=foo&merchant-id=${ email }`; | ||
let error; | ||
|
||
try { | ||
unpackSDKMeta(Buffer.from(JSON.stringify({ | ||
url: sdkUrl | ||
})).toString('base64')); | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
if (!error) { | ||
throw new Error(`Expected error to be thrown for ${ sdkUrl }`); | ||
} | ||
}); | ||
}); | ||
|
||
test('should error from very long merchant-id email addresses', () => { | ||
const longEmail = `${ 'a-very-long-email'.repeat(20) }@a-very-long-domain.com`; | ||
const sdkUrl = `https://www.paypal.com/sdk/js?client-id=foo&merchant-id=${ longEmail }`; | ||
let error; | ||
|
||
try { | ||
unpackSDKMeta(Buffer.from(JSON.stringify({ | ||
url: sdkUrl | ||
})).toString('base64')); | ||
} catch (err) { | ||
error = err; | ||
} | ||
|
||
if (!error) { | ||
throw new Error(`Expected error to be thrown for ${ sdkUrl }`); | ||
} | ||
}); | ||
|
||
test('should construct a valid script url with multiple merchant ids', () => { | ||
|
||
const sdkUrl = 'https://www.paypal.com/sdk/js?client-id=foo'; | ||
|