|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present, Parse, LLC |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the LICENSE file in |
| 6 | + * the root directory of this source tree. |
| 7 | + */ |
| 8 | +jest.dontMock('../../../Parse-Dashboard/Authentication.js'); |
| 9 | +jest.dontMock('bcryptjs'); |
| 10 | + |
| 11 | +const Authentication = require('../../../Parse-Dashboard/Authentication'); |
| 12 | +const apps = [{appId: 'test123'}, {appId: 'test789'}]; |
| 13 | +const unencryptedUsers = [ |
| 14 | + { |
| 15 | + user: 'parse.dashboard', |
| 16 | + pass: 'abc123' |
| 17 | + }, |
| 18 | + { |
| 19 | + user: 'parse.apps', |
| 20 | + pass: 'xyz789', |
| 21 | + apps: apps |
| 22 | + } |
| 23 | +]; |
| 24 | +const encryptedUsers = [ |
| 25 | + { |
| 26 | + user: 'parse.dashboard', |
| 27 | + pass: '$2a$08$w92YfzwkhB3WGFTBjHwZLO2tSwNIS2rX0qQER.TF8izEzWF5M.U8S' |
| 28 | + }, |
| 29 | + { |
| 30 | + user: 'parse.apps', |
| 31 | + pass: '$2a$08$B666bpJqE9v/R5KNbgfOMOjycvHzv6zWs0sGky/QuBZb4HY0M6LE2', |
| 32 | + apps: apps |
| 33 | + } |
| 34 | +] |
| 35 | + |
| 36 | +function createAuthenticationResult(isAuthenticated, appsUserHasAccessTo) { |
| 37 | + return { |
| 38 | + isAuthenticated, |
| 39 | + appsUserHasAccessTo |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +describe('Authentication', () => { |
| 44 | + it('does not authenticate with no users', () => { |
| 45 | + let authentication = new Authentication(null, false); |
| 46 | + expect(authentication.authenticate({name: 'parse.dashboard', pass: 'abc123'})) |
| 47 | + .toEqual(createAuthenticationResult(false, null)); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not authenticate with no auth', () => { |
| 51 | + let authentication = new Authentication(unencryptedUsers, false); |
| 52 | + expect(authentication.authenticate(null)) |
| 53 | + .toEqual(createAuthenticationResult(false, null)); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does not authenticate invalid user', () => { |
| 57 | + let authentication = new Authentication(unencryptedUsers, false); |
| 58 | + expect(authentication.authenticate({name: 'parse.invalid', pass: 'abc123'})) |
| 59 | + .toEqual(createAuthenticationResult(false, null)); |
| 60 | + }); |
| 61 | + |
| 62 | + it('does not authenticate valid user with invalid unencrypted password', () => { |
| 63 | + let authentication = new Authentication(unencryptedUsers, false); |
| 64 | + expect(authentication.authenticate({name: 'parse.dashboard', pass: 'xyz789'})) |
| 65 | + .toEqual(createAuthenticationResult(false, null)); |
| 66 | + }); |
| 67 | + |
| 68 | + it('authenticates valid user with valid unencrypted password', () => { |
| 69 | + let authentication = new Authentication(unencryptedUsers, false); |
| 70 | + expect(authentication.authenticate({name: 'parse.dashboard', pass: 'abc123'})) |
| 71 | + .toEqual(createAuthenticationResult(true, null)); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns apps if valid user', () => { |
| 75 | + let authentication = new Authentication(unencryptedUsers, false); |
| 76 | + expect(authentication.authenticate({name: 'parse.apps', pass: 'xyz789'})) |
| 77 | + .toEqual(createAuthenticationResult(true, apps)); |
| 78 | + }); |
| 79 | + |
| 80 | + it('authenticates valid user with valid encrypted password', () => { |
| 81 | + let authentication = new Authentication(encryptedUsers, true); |
| 82 | + expect(authentication.authenticate({name: 'parse.dashboard', pass: 'abc123'})) |
| 83 | + .toEqual(createAuthenticationResult(true, null)); |
| 84 | + }); |
| 85 | + |
| 86 | + it('does not authenticate valid user with invalid encrypted password', () => { |
| 87 | + let authentication = new Authentication(encryptedUsers, true); |
| 88 | + expect(authentication.authenticate({name: 'parse.dashboard', pass: 'xyz789'})) |
| 89 | + .toEqual(createAuthenticationResult(false, null)); |
| 90 | + }); |
| 91 | +}); |
0 commit comments