richie-education 2.25.0-b2.dev34 → 2.25.0-b2.dev35

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.
@@ -36,7 +36,15 @@ describe('Dummy API', () => {
36
36
 
37
37
  describe('user', () => {
38
38
  it('simulates that authenticated user is admin', async () => {
39
- const response = await BaseAPI.user.me();
39
+ // Not logged-in.
40
+ let response = await BaseAPI.user.me();
41
+ expect(response).toBeNull();
42
+
43
+ // Log-in.
44
+ await BaseAPI.user.login();
45
+
46
+ // Is logged-in.
47
+ response = await BaseAPI.user.me();
40
48
  expect(response?.username).toBe('admin');
41
49
  expect(response?.access_token).toBeDefined();
42
50
  });
@@ -38,6 +38,8 @@ const JOANIE_DEV_DEMO_USER_JWT_TOKENS = {
38
38
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzMzOTI4MjE0LCJpYXQiOjE3MDIzOTIyMTQsImp0aSI6ImNkZjAyMGM4ODdjOTQxYzU5ZmExN2FkZGExNjNjMDIzIiwiZW1haWwiOiJqZWFuLWJhcHRpc3RlLnBlbnJhdGgrc3R1ZGVudF91c2VyQGZ1bi1tb29jLmZyIiwibGFuZ3VhZ2UiOiJmci1mciIsInVzZXJuYW1lIjoic3R1ZGVudF91c2VyIiwiZnVsbF9uYW1lIjoiXHUwMGM5dHVkaWFudCJ9.JMdnC2VXwq2VbNPrIYxj8PEq0oJJ4LZZT_ywWyE1lBM',
39
39
  };
40
40
 
41
+ export const RICHIE_DUMMY_IS_LOGGED_IN = 'RICHIE_DUMMY_IS_LOGGED_IN';
42
+
41
43
  function getUserInfo(username: keyof typeof JOANIE_DEV_DEMO_USER_JWT_TOKENS): Maybe<User> {
42
44
  const accessToken = JOANIE_DEV_DEMO_USER_JWT_TOKENS[username];
43
45
  const JWTPayload: JWTPayload = JSON.parse(base64Decode(accessToken.split('.')[1]));
@@ -71,11 +73,19 @@ const API = (APIConf: LMSBackend | AuthenticationBackend): APILms => {
71
73
  "username": "admin",
72
74
  }
73
75
  */
76
+ if (!localStorage.getItem(RICHIE_DUMMY_IS_LOGGED_IN)) {
77
+ return null;
78
+ }
74
79
  return getUserInfo(CURRENT_JOANIE_DEV_DEMO_USER) || null;
75
80
  },
76
- login: () => location.reload(),
81
+ login: () => {
82
+ localStorage.setItem(RICHIE_DUMMY_IS_LOGGED_IN, 'true');
83
+ location.reload();
84
+ },
77
85
  register: () => location.reload(),
78
- logout: async () => undefined,
86
+ logout: async () => {
87
+ localStorage.removeItem(RICHIE_DUMMY_IS_LOGGED_IN);
88
+ },
79
89
  accessToken: () => sessionStorage.getItem(RICHIE_USER_TOKEN),
80
90
  },
81
91
  enrollment: {
@@ -47,29 +47,21 @@ const BaseSessionProvider = ({ children }: PropsWithChildren<any>) => {
47
47
  AuthenticationApi!.register();
48
48
  }, [queryClient]);
49
49
 
50
- const invalidate = useCallback(() => {
51
- /*
52
- Invalidate all queries except 'user' as we can set it to null manually
53
- after logout to avoid extra requests
54
- */
50
+ const destroy = useCallback(async () => {
51
+ await AuthenticationApi!.logout();
52
+ sessionStorage.removeItem(REACT_QUERY_SETTINGS.cacheStorage.key);
55
53
  queryClient.removeQueries({
56
54
  predicate: (query: any) =>
57
55
  query.options.queryKey.includes('user') && query.options.queryKey.length > 1,
58
56
  });
59
57
  queryClient.setQueryData(['user'], null);
60
- }, [queryClient]);
61
-
62
- const destroy = useCallback(async () => {
63
- invalidate();
64
- await AuthenticationApi!.logout();
65
- }, [invalidate]);
58
+ }, []);
66
59
 
67
60
  const context = useMemo(
68
61
  () => ({
69
62
  user,
70
63
  isPending,
71
64
  destroy,
72
- invalidate,
73
65
  login,
74
66
  register,
75
67
  }),
@@ -75,23 +75,16 @@ const JoanieSessionProvider = ({ children }: React.PropsWithChildren<{}>) => {
75
75
  AuthenticationApi!.register();
76
76
  }, [queryClient]);
77
77
 
78
- const invalidate = useCallback(() => {
79
- /*
80
- Invalidate all queries except 'user' as we can set it to null manually
81
- after logout to avoid extra requests
82
- */
78
+ const destroy = useCallback(async () => {
79
+ await AuthenticationApi!.logout();
80
+ sessionStorage.removeItem(REACT_QUERY_SETTINGS.cacheStorage.key);
83
81
  sessionStorage.removeItem(RICHIE_USER_TOKEN);
84
82
  queryClient.removeQueries({
85
83
  predicate: (query: any) =>
86
84
  query.options.queryKey.includes('user') && query.options.queryKey.length > 1,
87
85
  });
88
86
  queryClient.setQueryData(['user'], null);
89
- }, [queryClient]);
90
-
91
- const destroy = useCallback(async () => {
92
- invalidate();
93
- await AuthenticationApi!.logout();
94
- }, [invalidate]);
87
+ }, []);
95
88
 
96
89
  useEffect(() => {
97
90
  if (user) {
@@ -169,10 +169,8 @@ describe('SessionProvider', () => {
169
169
  jest.runOnlyPendingTimers();
170
170
  });
171
171
 
172
- expect(result.current.user).toBeNull();
173
- expect(sessionStorage.getItem(REACT_QUERY_SETTINGS.cacheStorage.key)).toMatch(
174
- /"data":null,.*"queryKey":\["user"]/,
175
- );
172
+ await waitFor(() => expect(result.current.user).toBeNull());
173
+ expect(sessionStorage.getItem(REACT_QUERY_SETTINGS.cacheStorage.key)).toBeNull();
176
174
  });
177
175
 
178
176
  it('does not make request if there is a valid session in cache', async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "richie-education",
3
- "version": "2.25.0-b2.dev34",
3
+ "version": "2.25.0-b2.dev35",
4
4
  "description": "A CMS to build learning portals for Open Education",
5
5
  "main": "sandbox/manage.py",
6
6
  "scripts": {