|
| 1 | +import Snippet from '../lib/snippet'; |
| 2 | +import assert from 'assert'; |
| 3 | + |
| 4 | +describe('snippet', () => { |
| 5 | + it('should be able to grab the verification secret', () => { |
| 6 | + const settings = { |
| 7 | + verificationSecret: 'abc123', |
| 8 | + app_id: 'xyz789', |
| 9 | + user_id: 1 |
| 10 | + }; |
| 11 | + const snippet = new Snippet(settings); |
| 12 | + assert.equal(snippet.getVerificationSecret(), 'abc123'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should grab the user_id as the identifier', () => { |
| 16 | + const settings = { |
| 17 | + verificationSecret: 'abc123', |
| 18 | + app_id: 'xyz789', |
| 19 | + user_id: 1, |
| 20 | + email: 'peter@intercom.io' |
| 21 | + }; |
| 22 | + const snippet = new Snippet(settings); |
| 23 | + assert.equal(snippet.getIdentifier(), 1); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should grab the email as the identifier if no user_id', () => { |
| 27 | + const settings = { |
| 28 | + verificationSecret: 'abc123', |
| 29 | + app_id: 'xyz789', |
| 30 | + email: 'peter@intercom.io' |
| 31 | + }; |
| 32 | + const snippet = new Snippet(settings); |
| 33 | + assert.equal(snippet.getIdentifier(), 'peter@intercom.io'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should throw an error if there\'s no verification secret', () => { |
| 37 | + const settings = { |
| 38 | + app_id: 'xyz789', |
| 39 | + user_id: 1 |
| 40 | + }; |
| 41 | + assert.throws(() => new Snippet(settings), Error); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should error if there\'s no app_id', () => { |
| 45 | + const settings = { |
| 46 | + verificationSecret: 'abc123', |
| 47 | + user_id: 1 |
| 48 | + }; |
| 49 | + assert.throws(() => new Snippet(settings), Error); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return the logged out snippet if no identifier', () => { |
| 53 | + const settings = { |
| 54 | + app_id: 'xyz789' |
| 55 | + }; |
| 56 | + const snippet = new Snippet(settings); |
| 57 | + assert.equal(snippet.settingsToString(settings), 'app_id: "xyz789"'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should escape bad stuff', () => { |
| 61 | + const settings = { |
| 62 | + verificationSecret: 'abc123', |
| 63 | + app_id: 'xyz789', |
| 64 | + email: 'peter@"<script>alert(1)</script>intercom.io' |
| 65 | + }; |
| 66 | + const snippet = new Snippet(settings); |
| 67 | + assert.equal(snippet.settingsToString(settings), 'verificationSecret: "abc123", app_id: "xyz789", email: "peter@\\"<script>alert(1)</script>intercom.io"'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should not include the verification secret in the snippet html', () => { |
| 71 | + const settings = { |
| 72 | + verificationSecret: 'abc123', |
| 73 | + app_id: 'xyz789', |
| 74 | + user_id: 1, |
| 75 | + email: 'peter@intercom.io', |
| 76 | + name: 'Peter McKenna', |
| 77 | + company: { |
| 78 | + id: 123, |
| 79 | + name: 'Intercom' |
| 80 | + } |
| 81 | + }; |
| 82 | + const snippet = new Snippet(settings); |
| 83 | + assert.equal(snippet.create().indexOf(settings.verificationSecret), -1); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return the snippet html', () => { |
| 87 | + const settings = { |
| 88 | + verificationSecret: 'abc123', |
| 89 | + app_id: 'xyz789', |
| 90 | + user_id: 1, |
| 91 | + email: 'peter@intercom.io', |
| 92 | + name: 'Peter McKenna', |
| 93 | + company: { |
| 94 | + id: 123, |
| 95 | + name: 'Intercom' |
| 96 | + } |
| 97 | + }; |
| 98 | + const snippet = new Snippet(settings); |
| 99 | + assert.equal(snippet.create(), ` |
| 100 | +<script> |
| 101 | + window.intercomSettings = { |
| 102 | + app_id: "xyz789", user_id: 1, email: "peter@intercom.io", name: "Peter McKenna", company: { id: 123, name: "Intercom" }, user_hash: "f02877f24c9dd37542268a28627ebaf2e07d0d114d9482abcdc20f60874b40b3" |
| 103 | + }; |
| 104 | +</script> |
| 105 | +<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/xyz789';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script> |
| 106 | + `); |
| 107 | + }); |
| 108 | +}); |
0 commit comments