richie-education 3.3.1-dev4 → 3.3.1-dev6

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.
@@ -84,6 +84,22 @@ describe('Keycloak API', () => {
84
84
  });
85
85
 
86
86
  describe('user.me', () => {
87
+ it('returns null when init returns false (not authenticated)', async () => {
88
+ mockKeycloakInit.mockResolvedValueOnce(false);
89
+ const api = API(authConfig);
90
+ const response = await api.user.me();
91
+ expect(response).toBeNull();
92
+ expect(mockKeycloakUpdateToken).not.toHaveBeenCalled();
93
+ });
94
+
95
+ it('returns null when init rejects', async () => {
96
+ mockKeycloakInit.mockRejectedValueOnce(new Error('Init failed'));
97
+ const api = API(authConfig);
98
+ const response = await api.user.me();
99
+ expect(response).toBeNull();
100
+ expect(mockKeycloakUpdateToken).not.toHaveBeenCalled();
101
+ });
102
+
87
103
  it('returns null when updateToken fails', async () => {
88
104
  mockKeycloakUpdateToken.mockRejectedValueOnce(new Error('Token refresh failed'));
89
105
  const response = await keycloakApi.user.me();
@@ -12,7 +12,7 @@ const API = (APIConf: AuthenticationBackend): { user: APIAuthentication } => {
12
12
  realm: APIConf.realm!,
13
13
  clientId: APIConf.client_id!,
14
14
  });
15
- keycloak.init({
15
+ const initPromise = keycloak.init({
16
16
  checkLoginIframe: false,
17
17
  flow: 'standard',
18
18
  onLoad: 'check-sso',
@@ -39,6 +39,15 @@ const API = (APIConf: AuthenticationBackend): { user: APIAuthentication } => {
39
39
  user: {
40
40
  accessToken: () => sessionStorage.getItem(RICHIE_USER_TOKEN),
41
41
  me: async () => {
42
+ let isAuthenticated: boolean;
43
+ try {
44
+ isAuthenticated = await initPromise;
45
+ } catch (error) {
46
+ handle(error);
47
+ return null;
48
+ }
49
+ if (!isAuthenticated) return null;
50
+
42
51
  try {
43
52
  await keycloak.updateToken(30);
44
53
  } catch (error) {
@@ -7,6 +7,7 @@ import { handle } from 'utils/errors/handle';
7
7
  import { OpenEdxApiProfile } from 'types/openEdx';
8
8
  import { checkStatus } from 'api/utils';
9
9
  import { OpenEdxFullNameFormValues } from 'components/OpenEdxFullNameForm';
10
+ import { location } from 'utils/indirection/window';
10
11
  import OpenEdxHawthornApiInterface from './openedx-hawthorn';
11
12
 
12
13
  /**
@@ -29,6 +30,7 @@ const API = (APIConf: AuthenticationBackend | LMSBackend): APILms => {
29
30
  const APIOptions = {
30
31
  routes: {
31
32
  user: {
33
+ login: `${APIConf.endpoint}/keycloak-login`,
32
34
  me: `${APIConf.endpoint}/api/v1.0/user/me`,
33
35
  account: `${APIConf.endpoint}/api/user/v1/accounts/:username`,
34
36
  preferences: `${APIConf.endpoint}/api/user/v1/preferences/:username`,
@@ -41,6 +43,10 @@ const API = (APIConf: AuthenticationBackend | LMSBackend): APILms => {
41
43
  ...ApiInterface,
42
44
  user: {
43
45
  ...ApiInterface.user,
46
+ login: () => {
47
+ const next = encodeURIComponent(location.href);
48
+ location.assign(`${APIOptions.routes.user.login}?next=${next}`);
49
+ },
44
50
  accessToken: () => {
45
51
  return sessionStorage.getItem(RICHIE_USER_TOKEN);
46
52
  },
@@ -19,7 +19,7 @@ import CourseWishButton from '.';
19
19
 
20
20
  jest.mock('utils/indirection/window', () => ({
21
21
  location: {
22
- pathname: '/tests/CourseAddToWishlist/',
22
+ href: 'https://example.com/tests/CourseAddToWishlist/',
23
23
  assign: jest.fn(() => true),
24
24
  },
25
25
  }));
@@ -61,7 +61,7 @@ describe('CourseWishButton', () => {
61
61
  await userEvent.click($logMeButton);
62
62
 
63
63
  expect(location.assign).toHaveBeenCalledWith(
64
- `https://authentication.test/login?next=richie/tests/CourseAddToWishlist/`,
64
+ `https://authentication.test/keycloak-login?next=https%3A%2F%2Fexample.com%2Ftests%2FCourseAddToWishlist%2F`,
65
65
  );
66
66
  });
67
67
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "richie-education",
3
- "version": "3.3.1-dev4",
3
+ "version": "3.3.1-dev6",
4
4
  "description": "A CMS to build learning portals for Open Education",
5
5
  "main": "sandbox/manage.py",
6
6
  "scripts": {