@drax/ai-back 3.26.0 → 3.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,248 @@
1
+ import Fastify, {FastifyInstance} from "fastify";
2
+ import {LoadCommonConfigFromEnv} from "@drax/common-back";
3
+ import {
4
+ LoadIdentityConfigFromEnv,
5
+ CreateOrUpdateRole,
6
+ CreateUserIfNotExist,
7
+ jwtMiddleware,
8
+ rbacMiddleware,
9
+ apiKeyMiddleware,
10
+ UserRoutes, RoleRoutes, TenantRoutes, UserApiKeyRoutes,
11
+ UserPermissions, RolePermissions, TenantPermissions, UserApiKeyPermissions,
12
+ LoadPermissions
13
+ } from "@drax/identity-back";
14
+
15
+ import adminRoleData from "./data/admin-role";
16
+ import restrictedRoleData from "./data/restricted-role";
17
+
18
+ import rootUserData from "./data/root-user";
19
+ import basicUserData from "./data/basic-user";
20
+
21
+ import {IUser, IRole} from "@drax/identity-share";
22
+ import MongoInMemory from "./MongoInMemory";
23
+ import {randomUUID} from "crypto";
24
+
25
+ class TestSetup {
26
+
27
+ private _fastifyInstance: FastifyInstance;
28
+ private _mongoInMemory: MongoInMemory;
29
+ private _rootUser: IUser;
30
+ private _basicUser: IUser;
31
+ private _adminRole: IRole;
32
+ private _restrictedRole: IRole;
33
+ private readonly dbEngine: "mongo" | "sqlite";
34
+ private readonly sqliteFile: string;
35
+
36
+ constructor(dbEngine: "mongo" | "sqlite" = "mongo", sqliteFile?: string) {
37
+ this.dbEngine = dbEngine
38
+ this.sqliteFile = sqliteFile || `/tmp/drax-ai-back-${randomUUID()}.sqlite`
39
+ }
40
+
41
+ async setup() {
42
+ this.setupEnvironmentVariables();
43
+ this.setupConfig();
44
+ this.setupPermissions();
45
+ this.setupFastifyInstance();
46
+ await this.setupMongoInMemoryAndConnect();
47
+ await this.setupRootUserAndAdminRole();
48
+ await this.setupBasicUserAndRestrictedRole();
49
+ }
50
+
51
+ setupEnvironmentVariables() {
52
+ // Define environment variables
53
+ process.env.DRAX_DB_ENGINE = this.dbEngine;
54
+ process.env.DRAX_JWT_SECRET = "xxx";
55
+ if (this.dbEngine === "sqlite") {
56
+ process.env.DRAX_SQLITE_FILE = this.sqliteFile;
57
+ }
58
+ }
59
+
60
+ setupConfig() {
61
+ LoadCommonConfigFromEnv();
62
+ LoadIdentityConfigFromEnv();
63
+ }
64
+
65
+ setupPermissions() {
66
+ //Merge All Permissions
67
+ const permissions = [
68
+ ...Object.values(UserPermissions),
69
+ ...Object.values(RolePermissions),
70
+ ...Object.values(TenantPermissions),
71
+ ...Object.values(UserApiKeyPermissions),
72
+ ]
73
+
74
+ //Load All Permissions
75
+ LoadPermissions(permissions)
76
+ }
77
+
78
+ setupFastifyInstance() {
79
+ this._fastifyInstance = Fastify()
80
+ this._fastifyInstance.setValidatorCompiler(() => () => true)
81
+ this._fastifyInstance.addHook('onRequest', jwtMiddleware)
82
+ this._fastifyInstance.addHook('onRequest', rbacMiddleware)
83
+ this._fastifyInstance.addHook('onRequest', apiKeyMiddleware)
84
+ this._fastifyInstance.register(UserRoutes)
85
+ this._fastifyInstance.register(RoleRoutes)
86
+ this._fastifyInstance.register(TenantRoutes)
87
+ this._fastifyInstance.register(UserApiKeyRoutes)
88
+ }
89
+
90
+ async setupMongoInMemoryAndConnect() {
91
+ if (this.dbEngine !== "mongo") {
92
+ return
93
+ }
94
+ this._mongoInMemory = new MongoInMemory();
95
+ await this._mongoInMemory.connect();
96
+ }
97
+
98
+ async setupRootUserAndAdminRole() {
99
+ this._adminRole = await CreateOrUpdateRole({...adminRoleData})
100
+ this._rootUser = await CreateUserIfNotExist({...rootUserData})
101
+ }
102
+
103
+ async setupBasicUserAndRestrictedRole() {
104
+ this._restrictedRole = await CreateOrUpdateRole({...restrictedRoleData})
105
+ this._basicUser = await CreateUserIfNotExist({...basicUserData})
106
+ }
107
+
108
+ async dropData() {
109
+ if (this._mongoInMemory) {
110
+ await this._mongoInMemory.dropData()
111
+ }
112
+ }
113
+
114
+ async dropAndClose() {
115
+ if (this._fastifyInstance) {
116
+ await this._fastifyInstance.close()
117
+ }
118
+ if (this._mongoInMemory) {
119
+ await this._mongoInMemory.dropAndClose()
120
+ }
121
+ }
122
+
123
+ async login(username: string, password: string): Promise<{accessToken: string}> {
124
+
125
+ const resp = await this._fastifyInstance.inject({
126
+ method: 'POST',
127
+ url: '/api/auth/login',
128
+ payload: {username: username, password: password}
129
+ });
130
+
131
+ let body = resp.json()
132
+
133
+ if(resp.statusCode === 200 && body.accessToken){
134
+ return {accessToken: body.accessToken}
135
+ }else{
136
+ throw new Error(`Failed to login. Status Code: ${resp.statusCode} body: ${resp.body}`)
137
+ }
138
+
139
+ }
140
+
141
+ async rootUserLogin(): Promise<{accessToken: string}> {
142
+
143
+ const resp = await this._fastifyInstance.inject({
144
+ method: 'POST',
145
+ url: '/api/auth/login',
146
+ payload: {
147
+ username: rootUserData.username,
148
+ password: rootUserData.password
149
+ }
150
+ });
151
+
152
+ let body = resp.json()
153
+
154
+ if(resp.statusCode === 200 && body.accessToken){
155
+ return {accessToken: body.accessToken}
156
+ }else{
157
+ throw new Error(`Failed to login. Status Code: ${resp.statusCode} body: ${resp.body}`)
158
+ }
159
+ }
160
+
161
+ async basicUserLogin(): Promise<{accessToken: string}> {
162
+
163
+ const resp = await this._fastifyInstance.inject({
164
+ method: 'POST',
165
+ url: '/api/auth/login',
166
+ payload: {
167
+ username: basicUserData.username,
168
+ password: basicUserData.password
169
+ }
170
+ });
171
+
172
+ let body = resp.json()
173
+
174
+ if(resp.statusCode === 200 && body.accessToken){
175
+ return {accessToken: body.accessToken}
176
+ }else{
177
+ throw new Error(`Failed to login. Status Code: ${resp.statusCode} body: ${resp.body}`)
178
+ }
179
+ }
180
+
181
+ async me(accessToken:string): Promise<IUser> {
182
+
183
+ const resp = await this._fastifyInstance.inject({
184
+ method: 'GET',
185
+ url: '/api/auth/me',
186
+ headers: {Authorization: `Bearer ${accessToken}`}
187
+ });
188
+
189
+ if(resp.statusCode === 200){
190
+ let user : IUser = resp.json() as IUser
191
+ return user
192
+ }else{
193
+ throw new Error(`Failed to get me. Status Code: ${resp.statusCode} body: ${resp.body}`)
194
+ }
195
+
196
+
197
+ }
198
+
199
+
200
+ get adminRoleData() {
201
+ return {...adminRoleData};
202
+ }
203
+
204
+ get restrictedRoleData() {
205
+ return {...restrictedRoleData};
206
+ }
207
+
208
+ get rootUserData() {
209
+ return {...rootUserData};
210
+ }
211
+
212
+ get basicUserData() {
213
+ return {...basicUserData};
214
+ }
215
+
216
+ get adminRole() {
217
+ return this._adminRole;
218
+ }
219
+
220
+ get restrictedRole() {
221
+ return this._restrictedRole;
222
+ }
223
+
224
+ get rootUser() {
225
+ return this._rootUser;
226
+ }
227
+
228
+ get basicUser() {
229
+ return this._basicUser;
230
+ }
231
+
232
+ get fastifyInstance() {
233
+ return this._fastifyInstance;
234
+ }
235
+
236
+ get mongoInMemory() {
237
+ return this._mongoInMemory;
238
+ }
239
+
240
+
241
+ }
242
+
243
+
244
+ export default TestSetup
245
+
246
+ export {
247
+ TestSetup
248
+ }
@@ -0,0 +1,13 @@
1
+ import {PermissionService} from "@drax/identity-back"
2
+
3
+ const adminRoleData = {
4
+ name: "Admin",
5
+ permissions: PermissionService.getPermissions(),
6
+ childRoles: [],
7
+ readonly: true
8
+ }
9
+
10
+ export default adminRoleData
11
+ export {
12
+ adminRoleData
13
+ }
@@ -0,0 +1,14 @@
1
+
2
+ const basicUser = {
3
+ active: true,
4
+ groups: [],
5
+ name: "Basic User",
6
+ username: "basicUser",
7
+ password: "Basic1234",
8
+ email: "basic@example.com",
9
+ phone: "123456789",
10
+ role: "Restricted"
11
+ };
12
+
13
+ export default basicUser
14
+ export {basicUser}
@@ -0,0 +1,10 @@
1
+
2
+ const restrictedRoleData = {
3
+ name: "Restricted",
4
+ permissions: [],
5
+ childRoles: [],
6
+ readonly: true
7
+ }
8
+
9
+ export default restrictedRoleData
10
+ export {restrictedRoleData}
@@ -0,0 +1,14 @@
1
+
2
+ const rootUser = {
3
+ active: true,
4
+ groups: [],
5
+ name: "Root",
6
+ username: "root",
7
+ password: "Root1234",
8
+ email: "root@example.com",
9
+ phone: "123456789",
10
+ role: "Admin"
11
+ };
12
+
13
+ export default rootUser
14
+ export {rootUser}