|
| 1 | +import { |
| 2 | + ContextTestAcl, |
| 3 | + UserProfileAcl, |
| 4 | + UserRole, |
| 5 | + UsersAcl, |
| 6 | +} from '@nestjs-json-api/microorm-database/entity'; |
| 7 | +import { JsonSdkPromise } from '@klerick/json-api-nestjs-sdk'; |
| 8 | +import { AxiosError } from 'axios'; |
| 9 | + |
| 10 | +import { creatSdk } from '../utils/run-application'; |
| 11 | +import { AbilityBuilder, CheckFieldAndInclude } from '../utils/acl/acl'; |
| 12 | + |
| 13 | +describe('ACL getOne:', () => { |
| 14 | + let contextTestAcl = new ContextTestAcl(); |
| 15 | + let usersAcl: UsersAcl[]; |
| 16 | + contextTestAcl.aclRules = { rules: [] }; |
| 17 | + contextTestAcl.context = {}; |
| 18 | + let jsonSdk: JsonSdkPromise; |
| 19 | + let publicUser: UsersAcl; |
| 20 | + let notPublicUser: UsersAcl; |
| 21 | + beforeEach(async () => { |
| 22 | + jsonSdk = creatSdk(); |
| 23 | + contextTestAcl = await jsonSdk.jonApiSdkService.postOne(contextTestAcl); |
| 24 | + usersAcl = await jsonSdk.jonApiSdkService.getAll(UsersAcl, { |
| 25 | + include: ['profile'], |
| 26 | + }); |
| 27 | + publicUser = usersAcl.find((i) => i.profile.isPublic) as UsersAcl; |
| 28 | + notPublicUser = usersAcl.find( |
| 29 | + (i) => !i.profile.isPublic && i.login !== 'bob' |
| 30 | + ) as UsersAcl; |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + await jsonSdk.jonApiSdkService.deleteOne(contextTestAcl); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('Without conditional: admin', () => { |
| 38 | + beforeEach(async () => { |
| 39 | + const adminUser = usersAcl.find((user) => user.login === 'admin'); |
| 40 | + if (!adminUser) throw new Error('Daphne user not found'); |
| 41 | + contextTestAcl.context = { currentUser: adminUser }; |
| 42 | + |
| 43 | + contextTestAcl.aclRules.rules = new AbilityBuilder( |
| 44 | + CheckFieldAndInclude |
| 45 | + ).permissionsFor(UserRole.admin).rules as any; |
| 46 | + await jsonSdk.jonApiSdkService.patchOne(contextTestAcl); |
| 47 | + }); |
| 48 | + |
| 49 | + it('get one profile', async () => { |
| 50 | + await jsonSdk.jonApiSdkService.getOne(UsersAcl, usersAcl[0].id); |
| 51 | + }); |
| 52 | + |
| 53 | + it('get one users with profile', async () => { |
| 54 | + await jsonSdk.jonApiSdkService.getOne(UsersAcl, usersAcl[0].id, { |
| 55 | + include: ['profile'], |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('Without conditional but with fields: moderator', () => { |
| 61 | + beforeEach(async () => { |
| 62 | + const moderatorUser = usersAcl.find((user) => user.login === 'moderator'); |
| 63 | + if (!moderatorUser) throw new Error('Sheila user not found'); |
| 64 | + contextTestAcl.context = { currentUser: moderatorUser }; |
| 65 | + |
| 66 | + contextTestAcl.aclRules.rules = new AbilityBuilder( |
| 67 | + CheckFieldAndInclude |
| 68 | + ).permissionsFor(UserRole.moderator).rules as any; |
| 69 | + await jsonSdk.jonApiSdkService.patchOne(contextTestAcl); |
| 70 | + }); |
| 71 | + |
| 72 | + it('get one profile', async () => { |
| 73 | + const item = await jsonSdk.jonApiSdkService.getOne( |
| 74 | + UserProfileAcl, |
| 75 | + usersAcl[0].id |
| 76 | + ); |
| 77 | + expect(item.role).toBeUndefined(); |
| 78 | + expect(item.salary).toBeUndefined(); |
| 79 | + expect(item.role).toBeUndefined(); |
| 80 | + expect(item.firstName).toBeDefined(); |
| 81 | + expect(item.lastName).toBeDefined(); |
| 82 | + expect(item.avatar).toBeDefined(); |
| 83 | + expect(item.phone).toBeDefined(); |
| 84 | + expect(item.createdAt).toBeDefined(); |
| 85 | + expect(item.updatedAt).toBeDefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('get one users with profile', async () => { |
| 89 | + const item = await jsonSdk.jonApiSdkService.getOne( |
| 90 | + UsersAcl, |
| 91 | + usersAcl[0].id, |
| 92 | + { |
| 93 | + include: ['profile'], |
| 94 | + } |
| 95 | + ); |
| 96 | + expect(item.profile.salary).toBeUndefined(); |
| 97 | + expect(item.profile.role).toBeDefined(); |
| 98 | + expect(item.login).toBeDefined(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('With conditional: user', () => { |
| 103 | + let bobUser: UsersAcl; |
| 104 | + beforeEach(async () => { |
| 105 | + const posibleBobUser = usersAcl.find((user) => user.login === 'bob'); |
| 106 | + if (!posibleBobUser) throw new Error('bob user not found'); |
| 107 | + bobUser = posibleBobUser; |
| 108 | + contextTestAcl.context = { currentUser: bobUser }; |
| 109 | + contextTestAcl.aclRules.rules = new AbilityBuilder( |
| 110 | + CheckFieldAndInclude |
| 111 | + ).permissionsFor(UserRole.user).rules as any; |
| 112 | + await jsonSdk.jonApiSdkService.patchOne(contextTestAcl); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should be able to get owner profile', async () => { |
| 116 | + const item = await jsonSdk.jonApiSdkService.getOne( |
| 117 | + UserProfileAcl, |
| 118 | + bobUser.profile.id |
| 119 | + ); |
| 120 | + expect(item.salary).toBeUndefined(); |
| 121 | + expect(item.isPublic).toBeUndefined(); |
| 122 | + expect(item.role).toBeUndefined(); |
| 123 | + expect(item.createdAt).toBeUndefined(); |
| 124 | + expect(item.updatedAt).toBeUndefined(); |
| 125 | + expect(item.phone).toBeDefined(); |
| 126 | + expect(item.firstName).toBeDefined(); |
| 127 | + expect(item.lastName).toBeDefined(); |
| 128 | + expect(item.avatar).toBeDefined(); |
| 129 | + expect(item.bio).toBeDefined(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should be able to get public profile', async () => { |
| 133 | + const item = await jsonSdk.jonApiSdkService.getOne( |
| 134 | + UserProfileAcl, |
| 135 | + publicUser.profile.id |
| 136 | + ); |
| 137 | + expect(item.salary).toBeUndefined(); |
| 138 | + expect(item.isPublic).toBeUndefined(); |
| 139 | + expect(item.role).toBeUndefined(); |
| 140 | + expect(item.createdAt).toBeUndefined(); |
| 141 | + expect(item.updatedAt).toBeUndefined(); |
| 142 | + expect(item.phone).toBeUndefined(); |
| 143 | + expect(item.firstName).toBeDefined(); |
| 144 | + expect(item.lastName).toBeDefined(); |
| 145 | + expect(item.avatar).toBeDefined(); |
| 146 | + expect(item.bio).toBeDefined(); |
| 147 | + }); |
| 148 | + it('should be not found to get not public profile', async () => { |
| 149 | + try { |
| 150 | + await jsonSdk.jonApiSdkService.getOne( |
| 151 | + UserProfileAcl, |
| 152 | + notPublicUser.profile.id |
| 153 | + ); |
| 154 | + assert.fail('should be not found'); |
| 155 | + } catch (e) { |
| 156 | + expect(e).toBeInstanceOf(AxiosError); |
| 157 | + expect((e as AxiosError).response?.status).toBe(404); |
| 158 | + } |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments