@memberjunction/actions-bizapps-lms 5.6.0 → 5.8.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.
- package/.env.integration.example +28 -0
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +23 -0
- package/dist/base/base-lms.action.d.ts.map +1 -1
- package/dist/base/base-lms.action.js +10 -3
- package/dist/base/base-lms.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/attach-tags.action.js +1 -1
- package/dist/providers/learnworlds/actions/attach-tags.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/create-user.action.d.ts.map +1 -1
- package/dist/providers/learnworlds/actions/create-user.action.js +6 -5
- package/dist/providers/learnworlds/actions/create-user.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/detach-tags.action.js +1 -1
- package/dist/providers/learnworlds/actions/detach-tags.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/enroll-user.action.d.ts +1 -1
- package/dist/providers/learnworlds/actions/enroll-user.action.d.ts.map +1 -1
- package/dist/providers/learnworlds/actions/enroll-user.action.js +11 -34
- package/dist/providers/learnworlds/actions/enroll-user.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/get-course-details.action.d.ts.map +1 -1
- package/dist/providers/learnworlds/actions/get-course-details.action.js +50 -34
- package/dist/providers/learnworlds/actions/get-course-details.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/get-user-details.action.js +1 -1
- package/dist/providers/learnworlds/actions/get-user-details.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/get-user-enrollments.action.js +1 -1
- package/dist/providers/learnworlds/actions/get-user-enrollments.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/get-users.action.js +1 -1
- package/dist/providers/learnworlds/actions/get-users.action.js.map +1 -1
- package/dist/providers/learnworlds/actions/sso-login.action.js +1 -1
- package/dist/providers/learnworlds/actions/sso-login.action.js.map +1 -1
- package/dist/providers/learnworlds/learnworlds-base.action.d.ts +5 -1
- package/dist/providers/learnworlds/learnworlds-base.action.d.ts.map +1 -1
- package/dist/providers/learnworlds/learnworlds-base.action.js +42 -9
- package/dist/providers/learnworlds/learnworlds-base.action.js.map +1 -1
- package/package.json +10 -8
- package/src/__tests__/create-user.action.test.ts +12 -1
- package/src/__tests__/enroll-user.action.test.ts +7 -7
- package/src/__tests__/integration.test.ts +510 -0
- package/src/__tests__/lms.test.ts +17 -3
- package/src/__tests__/sso-login.action.test.ts +7 -7
- package/src/base/base-lms.action.ts +9 -3
- package/src/providers/learnworlds/actions/attach-tags.action.ts +1 -1
- package/src/providers/learnworlds/actions/create-user.action.ts +6 -4
- package/src/providers/learnworlds/actions/detach-tags.action.ts +1 -1
- package/src/providers/learnworlds/actions/enroll-user.action.ts +9 -39
- package/src/providers/learnworlds/actions/get-course-details.action.ts +63 -51
- package/src/providers/learnworlds/actions/get-user-details.action.ts +1 -1
- package/src/providers/learnworlds/actions/get-user-enrollments.action.ts +1 -1
- package/src/providers/learnworlds/actions/get-users.action.ts +1 -1
- package/src/providers/learnworlds/actions/sso-login.action.ts +1 -1
- package/src/providers/learnworlds/learnworlds-base.action.ts +55 -10
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LearnWorlds Integration Tests — hits the REAL LearnWorlds API.
|
|
3
|
+
*
|
|
4
|
+
* Covers ALL 17 LearnWorlds actions chained together:
|
|
5
|
+
* Read-only (no setup needed): GetCourses, GetCourseDetails, GetBundles, GetUsers
|
|
6
|
+
* Create/mutate: CreateUser, UpdateUser, AttachTags, DetachTags
|
|
7
|
+
* Enrollment: EnrollUser, GetUserEnrollments, GetUserProgress
|
|
8
|
+
* Auth: SSOLogin
|
|
9
|
+
* Details: GetUserDetails
|
|
10
|
+
* Orchestration: OnboardLearner
|
|
11
|
+
* Lookup: FindUserByEmail
|
|
12
|
+
*
|
|
13
|
+
* PREREQUISITES:
|
|
14
|
+
* 1. Copy .env.integration.example -> .env.integration and fill in real values
|
|
15
|
+
* 2. Run: npm run test:integration
|
|
16
|
+
*
|
|
17
|
+
* These tests are SKIPPED by default (the `describe.skipIf` guard).
|
|
18
|
+
* They only run when LW_INTEGRATION=true is set in the environment.
|
|
19
|
+
*/
|
|
20
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
21
|
+
import dotenv from 'dotenv';
|
|
22
|
+
import path from 'path';
|
|
23
|
+
import { fileURLToPath } from 'url';
|
|
24
|
+
|
|
25
|
+
// ---------- load .env.integration if present ----------
|
|
26
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
27
|
+
dotenv.config({ path: path.resolve(__dirname, '../../.env.integration') });
|
|
28
|
+
|
|
29
|
+
const RUN_INTEGRATION = process.env.LW_INTEGRATION === 'true';
|
|
30
|
+
|
|
31
|
+
// ---------- mock only the MJ framework plumbing (not fetch) ----------
|
|
32
|
+
vi.mock('@memberjunction/actions', () => ({
|
|
33
|
+
BaseAction: class BaseAction {
|
|
34
|
+
protected async InternalRunAction(): Promise<unknown> {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
vi.mock('@memberjunction/global', () => ({
|
|
41
|
+
RegisterClass: () => (target: unknown) => target,
|
|
42
|
+
UUIDsEqual: (a: string, b: string) => a?.toLowerCase() === b?.toLowerCase(),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
vi.mock('@memberjunction/core', () => ({
|
|
46
|
+
UserInfo: class UserInfo {},
|
|
47
|
+
Metadata: vi.fn(),
|
|
48
|
+
RunView: vi.fn().mockImplementation(() => ({
|
|
49
|
+
RunView: vi.fn().mockResolvedValue({ Success: true, Results: [] }),
|
|
50
|
+
})),
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
vi.mock('@memberjunction/core-entities', () => ({
|
|
54
|
+
MJCompanyIntegrationEntity: class MJCompanyIntegrationEntity {
|
|
55
|
+
CompanyID = '';
|
|
56
|
+
APIKey: string | null = null;
|
|
57
|
+
AccessToken: string | null = null;
|
|
58
|
+
ExternalSystemID: string | null = null;
|
|
59
|
+
CustomAttribute1: string | null = null;
|
|
60
|
+
},
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
vi.mock('@memberjunction/actions-base', () => ({
|
|
64
|
+
ActionParam: class ActionParam {
|
|
65
|
+
Name = '';
|
|
66
|
+
Value: unknown = null;
|
|
67
|
+
Type = 'Input';
|
|
68
|
+
},
|
|
69
|
+
}));
|
|
70
|
+
|
|
71
|
+
// ---------- imports (after mocks) ----------
|
|
72
|
+
import { CreateUserAction } from '../providers/learnworlds/actions/create-user.action';
|
|
73
|
+
import { EnrollUserAction } from '../providers/learnworlds/actions/enroll-user.action';
|
|
74
|
+
import { SSOLoginAction } from '../providers/learnworlds/actions/sso-login.action';
|
|
75
|
+
import { GetLearnWorldsUsersAction } from '../providers/learnworlds/actions/get-users.action';
|
|
76
|
+
import { GetLearnWorldsUserDetailsAction } from '../providers/learnworlds/actions/get-user-details.action';
|
|
77
|
+
import { GetUserEnrollmentsAction } from '../providers/learnworlds/actions/get-user-enrollments.action';
|
|
78
|
+
import { GetLearnWorldsUserProgressAction } from '../providers/learnworlds/actions/get-user-progress.action';
|
|
79
|
+
import { GetLearnWorldsCoursesAction } from '../providers/learnworlds/actions/get-courses.action';
|
|
80
|
+
import { GetLearnWorldsCourseDetailsAction } from '../providers/learnworlds/actions/get-course-details.action';
|
|
81
|
+
import { GetBundlesAction } from '../providers/learnworlds/actions/get-bundles.action';
|
|
82
|
+
import { UpdateUserAction } from '../providers/learnworlds/actions/update-user.action';
|
|
83
|
+
import { AttachTagsAction } from '../providers/learnworlds/actions/attach-tags.action';
|
|
84
|
+
import { DetachTagsAction } from '../providers/learnworlds/actions/detach-tags.action';
|
|
85
|
+
import { OnboardLearnerAction } from '../providers/learnworlds/actions/onboard-learner.action';
|
|
86
|
+
import { LearnWorldsBaseAction } from '../providers/learnworlds/learnworlds-base.action';
|
|
87
|
+
import { UserInfo } from '@memberjunction/core';
|
|
88
|
+
|
|
89
|
+
// ---------- env-driven config ----------
|
|
90
|
+
const SCHOOL_DOMAIN = process.env.LW_SCHOOL_DOMAIN || '';
|
|
91
|
+
const API_KEY = process.env.LW_API_KEY || '';
|
|
92
|
+
const CLIENT_ID = process.env.LW_CLIENT_ID || '';
|
|
93
|
+
const COMPANY_ID = process.env.LW_COMPANY_ID || '00000000-0000-0000-0000-000000000001';
|
|
94
|
+
const TEST_COURSE_ID = process.env.LW_TEST_COURSE_ID || '';
|
|
95
|
+
const TEST_EMAIL = process.env.LW_TEST_EMAIL || `mj-test-${Date.now()}@example.com`;
|
|
96
|
+
const ONBOARD_EMAIL = `mj-onboard-${Date.now()}@example.com`;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Helper: patch `getCompanyIntegration` and `getAPICredentials` so we
|
|
100
|
+
* bypass the database and use env-var credentials directly.
|
|
101
|
+
*/
|
|
102
|
+
function patchCredentials<T extends LearnWorldsBaseAction>(action: T): T {
|
|
103
|
+
const a = action as Record<string, unknown>;
|
|
104
|
+
|
|
105
|
+
a['getCompanyIntegration'] = async () => ({
|
|
106
|
+
CompanyID: COMPANY_ID,
|
|
107
|
+
APIKey: API_KEY,
|
|
108
|
+
ExternalSystemID: SCHOOL_DOMAIN,
|
|
109
|
+
AccessToken: null,
|
|
110
|
+
CustomAttribute1: null,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
a['getAPICredentials'] = async () => ({
|
|
114
|
+
apiKey: API_KEY,
|
|
115
|
+
apiSecret: undefined,
|
|
116
|
+
accessToken: undefined,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Seed the CLIENT_ID env var so buildRequestConfig resolves it
|
|
120
|
+
const provider = 'LEARNWORLDS';
|
|
121
|
+
process.env[`BIZAPPS_${provider}_${COMPANY_ID}_CLIENT_ID`] = CLIENT_ID || SCHOOL_DOMAIN;
|
|
122
|
+
|
|
123
|
+
return action;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function mockContextUser(): UserInfo {
|
|
127
|
+
return { ID: 'integration-test-user' } as unknown as UserInfo;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
131
|
+
// Integration suite — only runs when LW_INTEGRATION=true
|
|
132
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
133
|
+
describe.skipIf(!RUN_INTEGRATION)('LearnWorlds Integration Tests', () => {
|
|
134
|
+
const contextUser = mockContextUser();
|
|
135
|
+
let createdUserId: string | undefined;
|
|
136
|
+
let onboardedUserId: string | undefined;
|
|
137
|
+
|
|
138
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
139
|
+
// PHASE 1: Read-only actions (no setup needed)
|
|
140
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
141
|
+
|
|
142
|
+
describe('Phase 1: Read-only actions', () => {
|
|
143
|
+
// ─── GetCourses ──────────────────────────────────────────────────
|
|
144
|
+
it('GetCourses — should list courses', async () => {
|
|
145
|
+
const action = patchCredentials(new GetLearnWorldsCoursesAction());
|
|
146
|
+
const result = await action.GetCourses({ CompanyID: COMPANY_ID, MaxResults: 5 }, contextUser);
|
|
147
|
+
|
|
148
|
+
expect(result.Courses).toBeDefined();
|
|
149
|
+
expect(Array.isArray(result.Courses)).toBe(true);
|
|
150
|
+
console.log(` GetCourses: OK (${result.TotalCount} courses returned)`);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// ─── GetCourseDetails ────────────────────────────────────────────
|
|
154
|
+
it('GetCourseDetails — should get course details', async () => {
|
|
155
|
+
if (!TEST_COURSE_ID) {
|
|
156
|
+
console.warn(' Skipping GetCourseDetails — LW_TEST_COURSE_ID not set');
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const action = patchCredentials(new GetLearnWorldsCourseDetailsAction());
|
|
161
|
+
const result = await action.GetCourseDetails(
|
|
162
|
+
{ CompanyID: COMPANY_ID, CourseID: TEST_COURSE_ID, IncludeModules: false, IncludeInstructors: false, IncludeStats: false },
|
|
163
|
+
contextUser,
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
expect(result.CourseDetails).toBeDefined();
|
|
167
|
+
expect(result.CourseDetails.id).toBeTruthy();
|
|
168
|
+
console.log(` GetCourseDetails: OK (title: "${result.CourseDetails.title}")`);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// ─── GetBundles ──────────────────────────────────────────────────
|
|
172
|
+
it('GetBundles — should list bundles', async () => {
|
|
173
|
+
const action = patchCredentials(new GetBundlesAction());
|
|
174
|
+
const result = await action.GetBundles({ CompanyID: COMPANY_ID, MaxResults: 5 }, contextUser);
|
|
175
|
+
|
|
176
|
+
expect(result.Bundles).toBeDefined();
|
|
177
|
+
expect(Array.isArray(result.Bundles)).toBe(true);
|
|
178
|
+
console.log(` GetBundles: OK (${result.TotalCount} bundles returned)`);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// ─── GetUsers ────────────────────────────────────────────────────
|
|
182
|
+
it('GetUsers — should list users', async () => {
|
|
183
|
+
const action = patchCredentials(new GetLearnWorldsUsersAction());
|
|
184
|
+
const result = await action.GetUsers({ CompanyID: COMPANY_ID, MaxResults: 5 }, contextUser);
|
|
185
|
+
|
|
186
|
+
expect(result.Users).toBeDefined();
|
|
187
|
+
expect(Array.isArray(result.Users)).toBe(true);
|
|
188
|
+
expect(result.TotalCount).toBeGreaterThan(0);
|
|
189
|
+
console.log(` GetUsers: OK (${result.TotalCount} users returned)`);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
194
|
+
// PHASE 2: User lifecycle (create -> update -> tags -> enroll)
|
|
195
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
196
|
+
|
|
197
|
+
describe('Phase 2: User lifecycle', () => {
|
|
198
|
+
// ─── FindUserByEmail (non-existent) ──────────────────────────────
|
|
199
|
+
it('FindUserByEmail — should return null for non-existent user', async () => {
|
|
200
|
+
const action = patchCredentials(new CreateUserAction());
|
|
201
|
+
action.SetCompanyContext(COMPANY_ID);
|
|
202
|
+
|
|
203
|
+
const result = await action.FindUserByEmail('nonexistent-integration-test@example.com', contextUser);
|
|
204
|
+
expect(result).toBeNull();
|
|
205
|
+
console.log(' FindUserByEmail: OK (no match, API responded)');
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// ─── CreateUser ──────────────────────────────────────────────────
|
|
209
|
+
it('CreateUser — should create a test user', async () => {
|
|
210
|
+
const action = patchCredentials(new CreateUserAction());
|
|
211
|
+
|
|
212
|
+
const result = await action.CreateUser(
|
|
213
|
+
{
|
|
214
|
+
CompanyID: COMPANY_ID,
|
|
215
|
+
Email: TEST_EMAIL,
|
|
216
|
+
FirstName: 'MJ',
|
|
217
|
+
LastName: 'IntegrationTest',
|
|
218
|
+
Role: 'student',
|
|
219
|
+
IsActive: true,
|
|
220
|
+
SendWelcomeEmail: false,
|
|
221
|
+
},
|
|
222
|
+
contextUser,
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
expect(result.UserDetails.id).toBeTruthy();
|
|
226
|
+
expect(result.UserDetails.email).toBe(TEST_EMAIL);
|
|
227
|
+
createdUserId = result.UserDetails.id;
|
|
228
|
+
|
|
229
|
+
console.log(` CreateUser: OK (userId=${createdUserId}, username=${result.UserDetails.username})`);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// ─── UpdateUser ──────────────────────────────────────────────────
|
|
233
|
+
it('UpdateUser — should update the created user', async () => {
|
|
234
|
+
if (!createdUserId) {
|
|
235
|
+
console.warn(' Skipping UpdateUser — no userId');
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const action = patchCredentials(new UpdateUserAction());
|
|
240
|
+
|
|
241
|
+
const result = await action.UpdateUser(
|
|
242
|
+
{
|
|
243
|
+
CompanyID: COMPANY_ID,
|
|
244
|
+
UserID: createdUserId,
|
|
245
|
+
FirstName: 'MJ-Updated',
|
|
246
|
+
LastName: 'IntegrationTest-Updated',
|
|
247
|
+
},
|
|
248
|
+
contextUser,
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
expect(result.UserDetails).toBeDefined();
|
|
252
|
+
expect(result.Summary.fieldsUpdated.length).toBeGreaterThan(0);
|
|
253
|
+
console.log(` UpdateUser: OK (fields updated: ${result.Summary.fieldsUpdated.join(', ')})`);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// ─── AttachTags ──────────────────────────────────────────────────
|
|
257
|
+
it('AttachTags — should attach tags to the user', async () => {
|
|
258
|
+
if (!createdUserId) {
|
|
259
|
+
console.warn(' Skipping AttachTags — no userId');
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const action = patchCredentials(new AttachTagsAction());
|
|
264
|
+
|
|
265
|
+
const result = await action.AttachTags(
|
|
266
|
+
{
|
|
267
|
+
CompanyID: COMPANY_ID,
|
|
268
|
+
UserID: createdUserId,
|
|
269
|
+
Tags: ['mj-integration-test', 'automated'],
|
|
270
|
+
},
|
|
271
|
+
contextUser,
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
expect(result.Success).toBe(true);
|
|
275
|
+
console.log(` AttachTags: OK (tags: ${result.Tags.join(', ')})`);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// ─── DetachTags ──────────────────────────────────────────────────
|
|
279
|
+
it('DetachTags — should detach a tag from the user', async () => {
|
|
280
|
+
if (!createdUserId) {
|
|
281
|
+
console.warn(' Skipping DetachTags — no userId');
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const action = patchCredentials(new DetachTagsAction());
|
|
286
|
+
|
|
287
|
+
const result = await action.DetachTags(
|
|
288
|
+
{
|
|
289
|
+
CompanyID: COMPANY_ID,
|
|
290
|
+
UserID: createdUserId,
|
|
291
|
+
Tags: ['automated'],
|
|
292
|
+
},
|
|
293
|
+
contextUser,
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
expect(result.Success).toBe(true);
|
|
297
|
+
console.log(` DetachTags: OK (remaining tags: ${result.Tags.join(', ')})`);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// ─── EnrollUser ──────────────────────────────────────────────────
|
|
301
|
+
it('EnrollUser — should enroll the created user in a course', async () => {
|
|
302
|
+
if (!createdUserId) {
|
|
303
|
+
console.warn(' Skipping EnrollUser — no userId');
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (!TEST_COURSE_ID) {
|
|
307
|
+
console.warn(' Skipping EnrollUser — LW_TEST_COURSE_ID not set');
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const action = patchCredentials(new EnrollUserAction());
|
|
312
|
+
|
|
313
|
+
const result = await action.EnrollUser(
|
|
314
|
+
{
|
|
315
|
+
CompanyID: COMPANY_ID,
|
|
316
|
+
UserID: createdUserId,
|
|
317
|
+
CourseID: TEST_COURSE_ID,
|
|
318
|
+
ProductType: 'course',
|
|
319
|
+
Price: 0,
|
|
320
|
+
Justification: 'MJ integration test',
|
|
321
|
+
NotifyUser: false,
|
|
322
|
+
},
|
|
323
|
+
contextUser,
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
expect(result.EnrollmentDetails).toBeTruthy();
|
|
327
|
+
console.log(` EnrollUser: OK (enrollmentId=${result.EnrollmentDetails.id}, status=${result.EnrollmentDetails.status})`);
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
332
|
+
// PHASE 3: User data retrieval (needs created+enrolled user)
|
|
333
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
334
|
+
|
|
335
|
+
describe('Phase 3: User data retrieval', () => {
|
|
336
|
+
// ─── GetUserDetails ──────────────────────────────────────────────
|
|
337
|
+
it('GetUserDetails — should get full user details', async () => {
|
|
338
|
+
if (!createdUserId) {
|
|
339
|
+
console.warn(' Skipping GetUserDetails — no userId');
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const action = patchCredentials(new GetLearnWorldsUserDetailsAction());
|
|
344
|
+
|
|
345
|
+
const result = await action.GetUserDetails(
|
|
346
|
+
{ CompanyID: COMPANY_ID, UserID: createdUserId, IncludeEnrollments: true, IncludeStats: false },
|
|
347
|
+
contextUser,
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
expect(result.UserDetails).toBeDefined();
|
|
351
|
+
expect(result.UserDetails.id).toBe(createdUserId);
|
|
352
|
+
expect(result.UserDetails.email).toBe(TEST_EMAIL);
|
|
353
|
+
console.log(` GetUserDetails: OK (email=${result.UserDetails.email}, enrollments=${result.UserDetails.enrollments?.length ?? 0})`);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// ─── GetUserEnrollments ──────────────────────────────────────────
|
|
357
|
+
it('GetUserEnrollments — should list enrollments', async () => {
|
|
358
|
+
if (!createdUserId) {
|
|
359
|
+
console.warn(' Skipping GetUserEnrollments — no userId');
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const action = patchCredentials(new GetUserEnrollmentsAction());
|
|
364
|
+
|
|
365
|
+
const result = await action.GetUserEnrollments(
|
|
366
|
+
{ CompanyID: COMPANY_ID, UserID: createdUserId, IncludeCourseDetails: false },
|
|
367
|
+
contextUser,
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
expect(result.Enrollments).toBeDefined();
|
|
371
|
+
expect(Array.isArray(result.Enrollments)).toBe(true);
|
|
372
|
+
console.log(` GetUserEnrollments: OK (${result.TotalCount} enrollments)`);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// ─── GetUserProgress ─────────────────────────────────────────────
|
|
376
|
+
it('GetUserProgress — should get user progress', async () => {
|
|
377
|
+
if (!createdUserId) {
|
|
378
|
+
console.warn(' Skipping GetUserProgress — no userId');
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const action = patchCredentials(new GetLearnWorldsUserProgressAction());
|
|
383
|
+
|
|
384
|
+
const result = await action.GetUserProgress(
|
|
385
|
+
{
|
|
386
|
+
CompanyID: COMPANY_ID,
|
|
387
|
+
UserID: createdUserId,
|
|
388
|
+
...(TEST_COURSE_ID ? { CourseID: TEST_COURSE_ID } : {}),
|
|
389
|
+
},
|
|
390
|
+
contextUser,
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
expect(result.UserProgress).toBeDefined();
|
|
394
|
+
console.log(` GetUserProgress: OK (courses=${result.UserProgress.totalCourses}, overall=${result.UserProgress.overallProgressPercentage}%)`);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
// ─── FindUserByEmail (verify) ────────────────────────────────────
|
|
398
|
+
it('FindUserByEmail (verify) — should find the user we created', async () => {
|
|
399
|
+
if (!createdUserId) {
|
|
400
|
+
console.warn(' Skipping FindUserByEmail verify — no userId');
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const action = patchCredentials(new CreateUserAction());
|
|
405
|
+
action.SetCompanyContext(COMPANY_ID);
|
|
406
|
+
|
|
407
|
+
const result = await action.FindUserByEmail(TEST_EMAIL, contextUser);
|
|
408
|
+
|
|
409
|
+
expect(result).not.toBeNull();
|
|
410
|
+
expect(result!.email.toLowerCase()).toBe(TEST_EMAIL.toLowerCase());
|
|
411
|
+
console.log(` FindUserByEmail (verify): OK (found user id=${result!.id})`);
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
416
|
+
// PHASE 4: SSO Login
|
|
417
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
418
|
+
|
|
419
|
+
describe('Phase 4: SSO Login', () => {
|
|
420
|
+
it('SSOLogin — should generate an SSO URL', async () => {
|
|
421
|
+
const action = patchCredentials(new SSOLoginAction());
|
|
422
|
+
|
|
423
|
+
const result = await action.GenerateSSOUrl(
|
|
424
|
+
{ CompanyID: COMPANY_ID, Email: TEST_EMAIL },
|
|
425
|
+
contextUser,
|
|
426
|
+
);
|
|
427
|
+
|
|
428
|
+
expect(result.LoginURL).toBeTruthy();
|
|
429
|
+
expect(result.LoginURL).toContain('http');
|
|
430
|
+
console.log(` SSOLogin: OK (loginURL=${result.LoginURL})`);
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
435
|
+
// PHASE 5: Orchestration — OnboardLearner
|
|
436
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
437
|
+
|
|
438
|
+
describe('Phase 5: OnboardLearner orchestration', () => {
|
|
439
|
+
it('OnboardLearner — should create user + enroll + SSO in one call', async () => {
|
|
440
|
+
const action = patchCredentials(new OnboardLearnerAction());
|
|
441
|
+
|
|
442
|
+
// Patch credentials at the prototype level so inner action instances
|
|
443
|
+
// (CreateUserAction, EnrollUserAction, SSOLoginAction) also get them
|
|
444
|
+
const credentialMock = async () => ({
|
|
445
|
+
CompanyID: COMPANY_ID,
|
|
446
|
+
APIKey: API_KEY,
|
|
447
|
+
ExternalSystemID: SCHOOL_DOMAIN,
|
|
448
|
+
AccessToken: null,
|
|
449
|
+
CustomAttribute1: null,
|
|
450
|
+
});
|
|
451
|
+
const apiCredMock = async () => ({
|
|
452
|
+
apiKey: API_KEY,
|
|
453
|
+
apiSecret: undefined,
|
|
454
|
+
accessToken: undefined,
|
|
455
|
+
});
|
|
456
|
+
vi.spyOn(LearnWorldsBaseAction.prototype as Record<string, unknown>, 'getCompanyIntegration' as never).mockImplementation(credentialMock as never);
|
|
457
|
+
vi.spyOn(LearnWorldsBaseAction.prototype as Record<string, unknown>, 'getAPICredentials' as never).mockImplementation(apiCredMock as never);
|
|
458
|
+
|
|
459
|
+
const result = await action.OnboardLearner(
|
|
460
|
+
{
|
|
461
|
+
CompanyID: COMPANY_ID,
|
|
462
|
+
Email: ONBOARD_EMAIL,
|
|
463
|
+
FirstName: 'MJ',
|
|
464
|
+
LastName: 'OnboardTest',
|
|
465
|
+
Role: 'student',
|
|
466
|
+
...(TEST_COURSE_ID ? { CourseIDs: [TEST_COURSE_ID] } : {}),
|
|
467
|
+
SendWelcomeEmail: false,
|
|
468
|
+
},
|
|
469
|
+
contextUser,
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
expect(result.Success).toBe(true);
|
|
473
|
+
expect(result.LearnWorldsUserId).toBeTruthy();
|
|
474
|
+
expect(result.IsNewUser).toBe(true);
|
|
475
|
+
onboardedUserId = result.LearnWorldsUserId;
|
|
476
|
+
|
|
477
|
+
console.log(` OnboardLearner: OK`);
|
|
478
|
+
console.log(` userId = ${result.LearnWorldsUserId}`);
|
|
479
|
+
console.log(` isNewUser = ${result.IsNewUser}`);
|
|
480
|
+
console.log(` loginURL = ${result.LoginURL}`);
|
|
481
|
+
console.log(` enrollments = ${result.Enrollments.length} (${result.Enrollments.filter((e) => e.success).length} succeeded)`);
|
|
482
|
+
if (result.Errors.length > 0) {
|
|
483
|
+
console.log(` errors = ${result.Errors.join('; ')}`);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it('OnboardLearner — should find existing user on second call', async () => {
|
|
488
|
+
if (!onboardedUserId) {
|
|
489
|
+
console.warn(' Skipping OnboardLearner re-run — no onboardedUserId');
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const action = patchCredentials(new OnboardLearnerAction());
|
|
494
|
+
|
|
495
|
+
const result = await action.OnboardLearner(
|
|
496
|
+
{
|
|
497
|
+
CompanyID: COMPANY_ID,
|
|
498
|
+
Email: ONBOARD_EMAIL,
|
|
499
|
+
FirstName: 'MJ',
|
|
500
|
+
LastName: 'OnboardTest',
|
|
501
|
+
},
|
|
502
|
+
contextUser,
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
expect(result.IsNewUser).toBe(false);
|
|
506
|
+
expect(result.LearnWorldsUserId).toBe(onboardedUserId);
|
|
507
|
+
console.log(` OnboardLearner (re-run): OK (isNewUser=false, same userId=${result.LearnWorldsUserId})`);
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
});
|
|
@@ -158,6 +158,20 @@ describe('BaseLMSAction', () => {
|
|
|
158
158
|
const result = action['getCredentialFromEnv']('COMP1', 'MISSING_KEY');
|
|
159
159
|
expect(result).toBeUndefined();
|
|
160
160
|
});
|
|
161
|
+
|
|
162
|
+
it('should fallback to lowercase companyId', () => {
|
|
163
|
+
process.env['BIZAPPS_TESTLMS_abc-123_API_KEY'] = 'lower-key';
|
|
164
|
+
expect(action['getCredentialFromEnv']('ABC-123', 'API_KEY')).toBe('lower-key');
|
|
165
|
+
delete process.env['BIZAPPS_TESTLMS_abc-123_API_KEY'];
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('should prefer original casing over lowercase fallback', () => {
|
|
169
|
+
process.env['BIZAPPS_TESTLMS_COMP1_API_KEY'] = 'original';
|
|
170
|
+
process.env['BIZAPPS_TESTLMS_comp1_API_KEY'] = 'lower';
|
|
171
|
+
expect(action['getCredentialFromEnv']('COMP1', 'API_KEY')).toBe('original');
|
|
172
|
+
delete process.env['BIZAPPS_TESTLMS_COMP1_API_KEY'];
|
|
173
|
+
delete process.env['BIZAPPS_TESTLMS_comp1_API_KEY'];
|
|
174
|
+
});
|
|
161
175
|
});
|
|
162
176
|
|
|
163
177
|
describe('getParamValue', () => {
|
|
@@ -329,7 +343,7 @@ describe('LearnWorldsBaseAction', () => {
|
|
|
329
343
|
};
|
|
330
344
|
|
|
331
345
|
action.SetCompanyContext('comp-123');
|
|
332
|
-
vi.spyOn(action as never, '
|
|
346
|
+
vi.spyOn(action as never, 'makeLearnWorldsRequest').mockResolvedValue({ data: [mockUser], meta: { page: 1, totalPages: 1 } } as never);
|
|
333
347
|
|
|
334
348
|
const mockCtx = { ID: 'ctx-user' } as unknown as (typeof import('@memberjunction/core'))['UserInfo'];
|
|
335
349
|
const result = await action.FindUserByEmail('test@example.com', mockCtx as never);
|
|
@@ -341,7 +355,7 @@ describe('LearnWorldsBaseAction', () => {
|
|
|
341
355
|
|
|
342
356
|
it('should return null when not found', async () => {
|
|
343
357
|
action.SetCompanyContext('comp-123');
|
|
344
|
-
vi.spyOn(action as never, '
|
|
358
|
+
vi.spyOn(action as never, 'makeLearnWorldsRequest').mockResolvedValue({ data: [], meta: { page: 1, totalPages: 1 } } as never);
|
|
345
359
|
|
|
346
360
|
const mockCtx = { ID: 'ctx-user' } as unknown as (typeof import('@memberjunction/core'))['UserInfo'];
|
|
347
361
|
const result = await action.FindUserByEmail('nobody@example.com', mockCtx as never);
|
|
@@ -351,7 +365,7 @@ describe('LearnWorldsBaseAction', () => {
|
|
|
351
365
|
|
|
352
366
|
it('should re-throw on API error', async () => {
|
|
353
367
|
action.SetCompanyContext('comp-123');
|
|
354
|
-
vi.spyOn(action as never, '
|
|
368
|
+
vi.spyOn(action as never, 'makeLearnWorldsRequest').mockRejectedValue(new Error('API error') as never);
|
|
355
369
|
|
|
356
370
|
const mockCtx = { ID: 'ctx-user' } as unknown as (typeof import('@memberjunction/core'))['UserInfo'];
|
|
357
371
|
|
|
@@ -72,7 +72,7 @@ describe('SSOLoginAction', () => {
|
|
|
72
72
|
describe('GenerateSSOUrl() typed method', () => {
|
|
73
73
|
it('should generate SSO URL with email', async () => {
|
|
74
74
|
const requestSpy = vi
|
|
75
|
-
.spyOn(action as unknown as {
|
|
75
|
+
.spyOn(action as unknown as { makeLearnWorldsNonVersionedRequest: (...args: unknown[]) => Promise<unknown> }, 'makeLearnWorldsNonVersionedRequest')
|
|
76
76
|
.mockResolvedValue({
|
|
77
77
|
url: 'https://school.learnworlds.com/sso?token=abc123',
|
|
78
78
|
user_id: 'lw-user-42',
|
|
@@ -99,7 +99,7 @@ describe('SSOLoginAction', () => {
|
|
|
99
99
|
|
|
100
100
|
it('should generate SSO URL with userId', async () => {
|
|
101
101
|
const requestSpy = vi
|
|
102
|
-
.spyOn(action as unknown as {
|
|
102
|
+
.spyOn(action as unknown as { makeLearnWorldsNonVersionedRequest: (...args: unknown[]) => Promise<unknown> }, 'makeLearnWorldsNonVersionedRequest')
|
|
103
103
|
.mockResolvedValue({
|
|
104
104
|
url: 'https://school.learnworlds.com/sso?token=def456',
|
|
105
105
|
user_id: 'lw-user-99',
|
|
@@ -130,7 +130,7 @@ describe('SSOLoginAction', () => {
|
|
|
130
130
|
|
|
131
131
|
it('should include RedirectTo in the request body when provided', async () => {
|
|
132
132
|
const requestSpy = vi
|
|
133
|
-
.spyOn(action as unknown as {
|
|
133
|
+
.spyOn(action as unknown as { makeLearnWorldsNonVersionedRequest: (...args: unknown[]) => Promise<unknown> }, 'makeLearnWorldsNonVersionedRequest')
|
|
134
134
|
.mockResolvedValue({
|
|
135
135
|
url: 'https://school.learnworlds.com/sso?token=xyz',
|
|
136
136
|
});
|
|
@@ -149,7 +149,7 @@ describe('SSOLoginAction', () => {
|
|
|
149
149
|
|
|
150
150
|
it('should prefer email over userId when both are provided', async () => {
|
|
151
151
|
const requestSpy = vi
|
|
152
|
-
.spyOn(action as unknown as {
|
|
152
|
+
.spyOn(action as unknown as { makeLearnWorldsNonVersionedRequest: (...args: unknown[]) => Promise<unknown> }, 'makeLearnWorldsNonVersionedRequest')
|
|
153
153
|
.mockResolvedValue({
|
|
154
154
|
url: 'https://school.learnworlds.com/sso?token=both',
|
|
155
155
|
});
|
|
@@ -170,7 +170,7 @@ describe('SSOLoginAction', () => {
|
|
|
170
170
|
});
|
|
171
171
|
|
|
172
172
|
it('should propagate API errors', async () => {
|
|
173
|
-
vi.spyOn(action as unknown as {
|
|
173
|
+
vi.spyOn(action as unknown as { makeLearnWorldsNonVersionedRequest: (...args: unknown[]) => Promise<unknown> }, 'makeLearnWorldsNonVersionedRequest').mockRejectedValue(
|
|
174
174
|
new Error('LearnWorlds API error: 401 Unauthorized'),
|
|
175
175
|
);
|
|
176
176
|
|
|
@@ -185,7 +185,7 @@ describe('SSOLoginAction', () => {
|
|
|
185
185
|
|
|
186
186
|
describe('InternalRunAction()', () => {
|
|
187
187
|
it('should work via framework path', async () => {
|
|
188
|
-
vi.spyOn(action as unknown as {
|
|
188
|
+
vi.spyOn(action as unknown as { makeLearnWorldsNonVersionedRequest: (...args: unknown[]) => Promise<unknown> }, 'makeLearnWorldsNonVersionedRequest').mockResolvedValue({
|
|
189
189
|
url: 'https://school.learnworlds.com/sso?token=framework',
|
|
190
190
|
user_id: 'lw-user-fw',
|
|
191
191
|
});
|
|
@@ -208,7 +208,7 @@ describe('SSOLoginAction', () => {
|
|
|
208
208
|
});
|
|
209
209
|
|
|
210
210
|
it('should set output params on success', async () => {
|
|
211
|
-
vi.spyOn(action as unknown as {
|
|
211
|
+
vi.spyOn(action as unknown as { makeLearnWorldsNonVersionedRequest: (...args: unknown[]) => Promise<unknown> }, 'makeLearnWorldsNonVersionedRequest').mockResolvedValue({
|
|
212
212
|
url: 'https://school.learnworlds.com/sso?token=output',
|
|
213
213
|
user_id: 'lw-user-out',
|
|
214
214
|
});
|
|
@@ -75,7 +75,7 @@ export abstract class BaseLMSAction extends BaseAction {
|
|
|
75
75
|
const result = await rv.RunView<MJCompanyIntegrationEntity>(
|
|
76
76
|
{
|
|
77
77
|
EntityName: 'MJ: Company Integrations',
|
|
78
|
-
ExtraFilter: `CompanyID = '${companyId}' AND Integration
|
|
78
|
+
ExtraFilter: `CompanyID = '${companyId}' AND Integration = '${safeIntegrationName}'`,
|
|
79
79
|
ResultType: 'entity_object',
|
|
80
80
|
},
|
|
81
81
|
contextUser,
|
|
@@ -99,8 +99,14 @@ export abstract class BaseLMSAction extends BaseAction {
|
|
|
99
99
|
* Example: BIZAPPS_LEARNWORLDS_12345_API_KEY
|
|
100
100
|
*/
|
|
101
101
|
protected getCredentialFromEnv(companyId: string, credentialType: string): string | undefined {
|
|
102
|
-
const
|
|
103
|
-
|
|
102
|
+
const provider = this.lmsProvider.toUpperCase().replace(/\s+/g, '_');
|
|
103
|
+
const credential = credentialType.toUpperCase();
|
|
104
|
+
const envKey = `BIZAPPS_${provider}_${companyId}_${credential}`;
|
|
105
|
+
const value = process.env[envKey];
|
|
106
|
+
if (value !== undefined) return value;
|
|
107
|
+
// Fallback: try lowercase companyId (SQL Server returns uppercase UUIDs)
|
|
108
|
+
const lowerKey = `BIZAPPS_${provider}_${companyId.toLowerCase()}_${credential}`;
|
|
109
|
+
return process.env[lowerKey];
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
/**
|
|
@@ -32,7 +32,7 @@ export class AttachTagsAction extends LearnWorldsBaseAction {
|
|
|
32
32
|
|
|
33
33
|
this.validatePathSegment(params.UserID, 'UserID');
|
|
34
34
|
|
|
35
|
-
const response = await this.makeLearnWorldsRequest<LWApiTagResponse>(`users/${params.UserID}/tags`, '
|
|
35
|
+
const response = await this.makeLearnWorldsRequest<LWApiTagResponse>(`users/${params.UserID}/tags`, 'PUT', { tags: params.Tags, action: 'attach' }, contextUser);
|
|
36
36
|
|
|
37
37
|
return {
|
|
38
38
|
Success: true,
|