Skip to content

Commit 2161cdb

Browse files
committed
refactor(json-api-server-e2e): add ACL test specs, update setup/teardown process, and enhance Vite test configurations
1 parent 78e7f88 commit 2161cdb

27 files changed

+2060
-165
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import {
2+
ContextTestAcl,
3+
UserRole,
4+
UsersAcl,
5+
UserProfileAcl,
6+
} from '@nestjs-json-api/microorm-database/entity';
7+
import { JsonSdkPromise } from '@klerick/json-api-nestjs-sdk';
8+
9+
import { creatSdk } from '../utils/run-application';
10+
import { AbilityBuilder, CheckFieldAndInclude } from '../utils/acl/acl';
11+
12+
13+
describe('ACL getAll:', () => {
14+
let contextTestAcl = new ContextTestAcl();
15+
let usersAcl: UsersAcl[];
16+
contextTestAcl.aclRules = { rules: [] };
17+
contextTestAcl.context = {};
18+
let jsonSdk: JsonSdkPromise;
19+
beforeEach(async () => {
20+
jsonSdk = creatSdk();
21+
contextTestAcl = await jsonSdk.jonApiSdkService.postOne(contextTestAcl);
22+
usersAcl = await jsonSdk.jonApiSdkService.getAll(UsersAcl, {
23+
include: ['profile'],
24+
});
25+
});
26+
27+
afterEach(async () => {
28+
await jsonSdk.jonApiSdkService.deleteOne(contextTestAcl);
29+
});
30+
31+
describe('Without conditional: admin', () => {
32+
beforeEach(async () => {
33+
const adminUser = usersAcl.find((user) => user.login === 'admin');
34+
if (!adminUser) throw new Error('Daphne user not found');
35+
contextTestAcl.context = { currentUser: adminUser };
36+
37+
contextTestAcl.aclRules.rules = new AbilityBuilder(CheckFieldAndInclude).permissionsFor(UserRole.admin).rules as any;
38+
await jsonSdk.jonApiSdkService.patchOne(contextTestAcl);
39+
});
40+
41+
it('get all profile', async () => {
42+
await jsonSdk.jonApiSdkService.getAll(UserProfileAcl)
43+
})
44+
45+
it('get all users with profile', async () => {
46+
await jsonSdk.jonApiSdkService.getAll(UsersAcl, {
47+
include: ['profile'],
48+
});
49+
});
50+
})
51+
52+
describe('Without conditional but with fields: moderator', () => {
53+
beforeEach(async () => {
54+
const moderatorUser = usersAcl.find((user) => user.login === 'moderator');
55+
if (!moderatorUser) throw new Error('Sheila user not found');
56+
contextTestAcl.context = { currentUser: moderatorUser };
57+
58+
contextTestAcl.aclRules.rules = new AbilityBuilder(CheckFieldAndInclude).permissionsFor(UserRole.moderator).rules as any;
59+
await jsonSdk.jonApiSdkService.patchOne(contextTestAcl);
60+
});
61+
62+
it('get all profile', async () => {
63+
const result = await jsonSdk.jonApiSdkService.getAll(UserProfileAcl)
64+
65+
for (const item of result) {
66+
expect(item.role).toBeUndefined()
67+
expect(item.salary).toBeUndefined()
68+
expect(item.role).toBeUndefined()
69+
expect(item.firstName).toBeDefined()
70+
expect(item.lastName).toBeDefined()
71+
expect(item.avatar).toBeDefined()
72+
expect(item.phone).toBeDefined()
73+
expect(item.createdAt).toBeDefined()
74+
expect(item.updatedAt).toBeDefined()
75+
}
76+
})
77+
78+
it('get all users with profile', async () => {
79+
const result = await jsonSdk.jonApiSdkService.getAll(UsersAcl, {
80+
include: ['profile'],
81+
});
82+
for (const item of result) {
83+
expect(item.profile.salary).toBeUndefined()
84+
expect(item.profile.role).toBeDefined()
85+
expect(item.login).toBeDefined()
86+
}
87+
});
88+
})
89+
90+
describe('With conditional: user', () => {
91+
let countPublicProfile: UserProfileAcl[];
92+
beforeEach(async () => {
93+
countPublicProfile = await jsonSdk.jonApiSdkService.getAll(UserProfileAcl, {
94+
filter: {
95+
target: {
96+
isPublic: {eq: 'true'}
97+
},
98+
}
99+
})
100+
const bobUser = usersAcl.find((user) => user.login === 'bob');
101+
if (!bobUser) throw new Error('bob user not found');
102+
contextTestAcl.context = { currentUser: bobUser };
103+
contextTestAcl.aclRules.rules = new AbilityBuilder(
104+
CheckFieldAndInclude
105+
).permissionsFor(UserRole.user).rules as any;
106+
await jsonSdk.jonApiSdkService.patchOne(contextTestAcl);
107+
});
108+
109+
it('should be able to get allow profile', async () => {
110+
111+
const result = await jsonSdk.jonApiSdkService.getAll(UserProfileAcl);
112+
expect(result.length).toBe(countPublicProfile.length + 1)
113+
for (const item of result) {
114+
expect(item.salary).toBeUndefined()
115+
expect(item.isPublic).toBeUndefined()
116+
expect(item.role).toBeUndefined()
117+
expect(item.createdAt).toBeUndefined()
118+
expect(item.updatedAt).toBeUndefined()
119+
120+
if ((contextTestAcl.context.currentUser as UsersAcl).profile.id === item.id) {
121+
expect(item.phone).toBeDefined()
122+
} else {
123+
expect(item.phone).toBeUndefined()
124+
}
125+
126+
expect(item.firstName).toBeDefined()
127+
expect(item.lastName).toBeDefined()
128+
expect(item.avatar).toBeDefined()
129+
expect(item.bio).toBeDefined()
130+
131+
}
132+
})
133+
});
134+
});
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)