|
| 1 | +import createLoopback from '~/test/utils/create-loopback'; |
| 2 | +import {email} from '~/server/initial-data/maintenance-account'; |
| 3 | +import createAdmin from '~/server/boot/create-admin'; |
| 4 | + |
| 5 | +async function prepareLoopback(undoBoot = false) { |
| 6 | + const server = await createLoopback(); |
| 7 | + const Account = server.models.Account; |
| 8 | + const Role = server.models.Role; |
| 9 | + const RoleMapping = server.models.RoleMapping; |
| 10 | + if (undoBoot) { |
| 11 | + let info = await Account.deleteAll({email}); |
| 12 | + if (info.count < 1) throw Error('account not deleted'); |
| 13 | + info = await Role.deleteAll({name: 'admin'}); |
| 14 | + if (info.count < 1) throw Error('role not deleted'); |
| 15 | + } |
| 16 | + |
| 17 | + return {server, Account, Role, RoleMapping}; |
| 18 | +} |
| 19 | + |
| 20 | +describe('boot create-admin', () => { |
| 21 | + it('adds admin, role and roleMapping', async() => { |
| 22 | + const {server, RoleMapping} = await prepareLoopback(true); |
| 23 | + const result = await createAdmin(server); |
| 24 | + |
| 25 | + expect(result).not.toBe(null); |
| 26 | + expect(result.account).toMatchObject({email}); |
| 27 | + expect(result.role).toMatchObject({name: 'admin'}); |
| 28 | + expect(result.principal).toMatchObject({ |
| 29 | + principalType: RoleMapping.USER, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('do not add account if already exists', async() => { |
| 34 | + const {server} = await prepareLoopback(); |
| 35 | + const result = await createAdmin(server); |
| 36 | + expect(result).toBe(null); |
| 37 | + }); |
| 38 | + |
| 39 | + it('do not add role if already exists', async() => { |
| 40 | + const {server, Role} = await prepareLoopback(true); |
| 41 | + const created = await Role.create({name: 'admin'}); |
| 42 | + const result = await createAdmin(server); |
| 43 | + |
| 44 | + expect(result).not.toBe(null); |
| 45 | + expect(result.role).toMatchObject(created); |
| 46 | + }); |
| 47 | +}); |
0 commit comments