|
| 1 | +import UserData from '../lib/user-data'; |
| 2 | +import { IdentityVerification } from '../lib/index'; |
| 3 | +import assert from 'assert'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +describe('userData', () => { |
| 7 | + it('should be able to grab the verification secret', () => { |
| 8 | + const settings = { |
| 9 | + verificationSecret: 'abc123', |
| 10 | + app_id: 'xyz789', |
| 11 | + user_id: 1 |
| 12 | + }; |
| 13 | + const userData = new UserData(settings); |
| 14 | + assert.equal(userData.getVerificationSecret(), 'abc123'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should grab the user_id as the identifier', () => { |
| 18 | + const settings = { |
| 19 | + verificationSecret: 'abc123', |
| 20 | + app_id: 'xyz789', |
| 21 | + user_id: 1, |
| 22 | + |
| 23 | + }; |
| 24 | + const userData = new UserData(settings); |
| 25 | + const userHash = sinon.spy(IdentityVerification, 'userHash'); |
| 26 | + userData.json(); |
| 27 | + userHash.restore(); |
| 28 | + sinon.assert.calledWith(userHash, { |
| 29 | + secretKey: 'abc123', |
| 30 | + identifier: '1' |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should grab the email as the identifier if no user_id', () => { |
| 35 | + const settings = { |
| 36 | + verificationSecret: 'abc123', |
| 37 | + app_id: 'xyz789', |
| 38 | + |
| 39 | + }; |
| 40 | + const userData = new UserData(settings); |
| 41 | + const userHash = sinon.spy(IdentityVerification, 'userHash'); |
| 42 | + userData.json(); |
| 43 | + userHash.restore(); |
| 44 | + sinon.assert.calledWith(userHash, { |
| 45 | + secretKey: 'abc123', |
| 46 | + |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should throw an error if there\'s no verification secret', () => { |
| 51 | + const settings = { |
| 52 | + app_id: 'xyz789', |
| 53 | + user_id: 1 |
| 54 | + }; |
| 55 | + assert.throws(() => new UserData(settings), Error); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should error if there\'s no app_id', () => { |
| 59 | + const settings = { |
| 60 | + verificationSecret: 'abc123', |
| 61 | + user_id: 1 |
| 62 | + }; |
| 63 | + assert.throws(() => new UserData(settings), Error); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return the logged out userData if no identifier', () => { |
| 67 | + const settings = { |
| 68 | + app_id: 'xyz789' |
| 69 | + }; |
| 70 | + const userData = new UserData(settings); |
| 71 | + assert.equal(JSON.stringify(userData.json()), JSON.stringify({ app_id: 'xyz789'})); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should escape bad stuff in values', () => { |
| 75 | + const settings = { |
| 76 | + verificationSecret: 'abc123', |
| 77 | + app_id: 'xyz789', |
| 78 | + email: 'jess@"<script>alert(1)</script>intercom.io' |
| 79 | + }; |
| 80 | + const userData = new UserData(settings); |
| 81 | + assert.equal(userData.json().email, 'jess@\\"<script>alert(1)</script>intercom.io'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should escape bad stuff in object keys', () => { |
| 85 | + const settings = { |
| 86 | + verificationSecret: 'abc123', |
| 87 | + app_id: 'xyz789', |
| 88 | + user_id: '1' |
| 89 | + }; |
| 90 | + settings['<script>doSomethingEvil();</script>'] = 'jess@"<script>alert(1)</script>intercom.io'; |
| 91 | + const userData = new UserData(settings); |
| 92 | + const result = userData.json(); |
| 93 | + assert.equal(result['<script>doSomethingEvil();</script>'], 'jess@\\\"<script>alert(1)</script>intercom.io'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should escape bad stuff in next object keys and values', () => { |
| 97 | + const settings = { |
| 98 | + verificationSecret: 'abc123', |
| 99 | + app_id: 'xyz789', |
| 100 | + user_id: '1' |
| 101 | + }; |
| 102 | + settings.maliciousSettings = { |
| 103 | + '<script>doSomethingEvil();</script>': 'jess@"<script>alert(1)</script>intercom.io' |
| 104 | + }; |
| 105 | + const userData = new UserData(settings); |
| 106 | + const result = userData.json(); |
| 107 | + assert.equal(result.maliciousSettings['<script>doSomethingEvil();</script>'], 'jess@\\"<script>alert(1)</script>intercom.io'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should not include the verification secret in the userData', () => { |
| 111 | + const settings = { |
| 112 | + verificationSecret: 'abc123', |
| 113 | + app_id: 'xyz789', |
| 114 | + user_id: 1, |
| 115 | + |
| 116 | + name: 'Jess OB', |
| 117 | + company: { |
| 118 | + id: 123, |
| 119 | + name: 'Intercom' |
| 120 | + } |
| 121 | + }; |
| 122 | + const userData = new UserData(settings); |
| 123 | + const result = userData.json(); |
| 124 | + assert.equal(Object.keys(result).indexOf(settings.verificationSecret), -1); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should skip setUserHash if user_hash is already defined', () => { |
| 128 | + const settings = { |
| 129 | + verificationSecret: 'abc123', |
| 130 | + app_id: 'xyz789', |
| 131 | + user_id: 1 |
| 132 | + }; |
| 133 | + const userData = new UserData(settings); |
| 134 | + const userHash = sinon.spy(IdentityVerification, 'userHash'); |
| 135 | + userData.json(); |
| 136 | + userData.json(); |
| 137 | + userHash.restore(); |
| 138 | + sinon.assert.calledOnce(userHash); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should return the userData', () => { |
| 142 | + const settings = { |
| 143 | + verificationSecret: 'abc123', |
| 144 | + app_id: 'xyz789', |
| 145 | + user_id: 1, |
| 146 | + |
| 147 | + name: 'Jess OB', |
| 148 | + company: { |
| 149 | + id: 123, |
| 150 | + name: 'Intercom' |
| 151 | + } |
| 152 | + }; |
| 153 | + const userData = new UserData(settings); |
| 154 | + assert.equal(JSON.stringify(userData.json()), JSON.stringify({ |
| 155 | + app_id: 'xyz789', |
| 156 | + user_id: 1, |
| 157 | + |
| 158 | + name: 'Jess OB', |
| 159 | + company: { |
| 160 | + id: 123, |
| 161 | + name: 'Intercom' |
| 162 | + }, |
| 163 | + user_hash: 'f02877f24c9dd37542268a28627ebaf2e07d0d114d9482abcdc20f60874b40b3' |
| 164 | + })); |
| 165 | + }); |
| 166 | +}); |
0 commit comments