|
| 1 | +/* eslint-env jest */ |
| 2 | + |
| 3 | +const { user, Connection } = require('../lib'); |
| 4 | +const { |
| 5 | + seedDatabase, |
| 6 | + dropDatabase, |
| 7 | + generateDatabaseName, |
| 8 | + generateRandomString, |
| 9 | + ConnectionFactory, |
| 10 | +} = require('./setup-database'); |
| 11 | + |
| 12 | +describe('checkPermission()', () => { |
| 13 | + const database = generateDatabaseName(); |
| 14 | + let conn; |
| 15 | + |
| 16 | + beforeAll(seedDatabase(database)); |
| 17 | + afterAll(dropDatabase(database)); |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + conn = ConnectionFactory(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should return true for a permission the admin user has', () => |
| 24 | + user.checkPermission(conn, 'READ', 'db', database).then(res => { |
| 25 | + expect(res.status).toBe(200); |
| 26 | + expect(res.body.hasPermission).toBe(true); |
| 27 | + })); |
| 28 | + |
| 29 | + it('should return false for a permission a new user does not have', () => { |
| 30 | + const username = generateRandomString(); |
| 31 | + const password = generateRandomString(); |
| 32 | + |
| 33 | + return user |
| 34 | + .create(conn, { username, password }) |
| 35 | + .then(res => { |
| 36 | + expect(res.status).toBe(201); |
| 37 | + // Create a new connection for the unprivileged user |
| 38 | + const userConn = new Connection({ |
| 39 | + username, |
| 40 | + password, |
| 41 | + endpoint: conn.uri(), |
| 42 | + }); |
| 43 | + return user.checkPermission(userConn, 'WRITE', 'db', database); |
| 44 | + }) |
| 45 | + .then(res => { |
| 46 | + expect(res.status).toBe(200); |
| 47 | + expect(res.body.hasPermission).toBe(false); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should return true after assigning a permission to a user', () => { |
| 52 | + const username = generateRandomString(); |
| 53 | + const password = generateRandomString(); |
| 54 | + const userConn = new Connection({ |
| 55 | + username, |
| 56 | + password, |
| 57 | + endpoint: conn.uri(), |
| 58 | + }); |
| 59 | + const permission = { |
| 60 | + action: 'WRITE', |
| 61 | + resourceType: 'db', |
| 62 | + resources: [database], |
| 63 | + }; |
| 64 | + |
| 65 | + return user |
| 66 | + .create(conn, { username, password }) |
| 67 | + .then(res => { |
| 68 | + expect(res.status).toBe(201); |
| 69 | + // First verify the user does not have the permission |
| 70 | + return user.checkPermission(userConn, 'WRITE', 'db', database); |
| 71 | + }) |
| 72 | + .then(res => { |
| 73 | + expect(res.status).toBe(200); |
| 74 | + expect(res.body.hasPermission).toBe(false); |
| 75 | + // Now assign the permission |
| 76 | + return user.assignPermission(conn, username, permission); |
| 77 | + }) |
| 78 | + .then(res => { |
| 79 | + expect(res.status).toBe(201); |
| 80 | + // Verify the user now has the permission |
| 81 | + return user.checkPermission(userConn, 'WRITE', 'db', database); |
| 82 | + }) |
| 83 | + .then(res => { |
| 84 | + expect(res.status).toBe(200); |
| 85 | + expect(res.body.hasPermission).toBe(true); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments