Skip to content

Commit

Permalink
fix: updating to frontend-auth 7 (openedx#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjoy authored Sep 20, 2019
1 parent 566f2d3 commit eaa1d91
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 55 deletions.
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ coverage:
project:
default:
target: auto
threshold: 10%
threshold: 0%
patch:
default:
target: auto
threshold: 10%
threshold: 0%
44 changes: 21 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"@commitlint/config-angular": "8.1.0",
"@commitlint/prompt": "8.1.0",
"@commitlint/prompt-cli": "8.1.0",
"@edx/frontend-analytics": "2.0.0",
"@edx/frontend-auth": "6.0.0",
"@edx/frontend-i18n": "3.0.2",
"@edx/frontend-logging": "3.0.1",
"@edx/paragon": "7.1.2",
"@edx/frontend-analytics": "^3.0.0",
"@edx/frontend-auth": "^7.0.1",
"@edx/frontend-i18n": "^3.0.2",
"@edx/frontend-logging": "^3.0.1",
"@edx/paragon": "^7.1.3",
"babel-eslint": "10.0.3",
"babel-jest": "24.9.0",
"babel-loader": "8.0.6",
Expand Down
1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class App {
static _apiClient = null;
static history = null;
static authenticatedUser = defaultAuthenticatedUser;
static decodedAccessToken = null;
static getQueryParams = memoize(getQueryParameters);
static error = null;

Expand Down
7 changes: 0 additions & 7 deletions src/frontendAuthWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,3 @@ export const fetchUserAccount = (username) => {
const userAccountApiService = new UserAccountApiService(App.apiClient, App.config.LMS_BASE_URL);
_fetchUserAccount(userAccountApiService, username);
};

export const getAuthenticatedUser = accessToken => ({
userId: accessToken.user_id,
username: accessToken.preferred_username,
roles: accessToken.roles ? accessToken.roles : [],
administrator: accessToken.administrator,
});
15 changes: 7 additions & 8 deletions src/handlers/authentication.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
/* eslint-disable no-param-reassign */
import { getAuthenticatedAPIClient } from '@edx/frontend-auth';

import { getAuthenticatedUser } from '../frontendAuthWrapper';

export default async function authentication(app) {
app.apiClient = getAuthenticatedAPIClient({
appBaseUrl: app.config.BASE_URL,
authBaseUrl: app.config.LMS_BASE_URL,
accessTokenCookieName: app.config.ACCESS_TOKEN_COOKIE_NAME,
userInfoCookieName: app.config.USER_INFO_COOKIE_NAME,
csrfTokenApiPath: app.config.CSRF_TOKEN_API_PATH,
loginUrl: app.config.LOGIN_URL,
logoutUrl: app.config.LOGOUT_URL,
csrfTokenApiPath: app.config.CSRF_TOKEN_API_PATH,
refreshAccessTokenEndpoint: app.config.REFRESH_ACCESS_TOKEN_ENDPOINT,
accessTokenCookieName: app.config.ACCESS_TOKEN_COOKIE_NAME,
userInfoCookieName: app.config.USER_INFO_COOKIE_NAME,
csrfCookieName: app.config.CSRF_COOKIE_NAME,
loggingService: app.loggingService,
});

// Get a valid access token for authenticated API access.
const accessToken =
await app.apiClient.ensurePublicOrAuthenticationAndCookies(global.location.pathname);
const { authenticatedUser, decodedAccessToken } =
await app.apiClient.ensureAuthenticatedUser(global.location.pathname);
// Once we have refreshed our authentication, extract it for use later.
app.authenticatedUser = getAuthenticatedUser(accessToken);
app.authenticatedUser = authenticatedUser;
app.decodedAccessToken = decodedAccessToken;
}
26 changes: 20 additions & 6 deletions src/handlers/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import { defaultAuthenticatedUser } from '../frontendAuthWrapper';

jest.mock('@edx/frontend-auth', () => ({
getAuthenticatedAPIClient: jest.fn(() => ({
ensurePublicOrAuthenticationAndCookies: jest.fn(async () => ({
user_id: 'user123',
preferred_username: 'user_person',
roles: [],
administrator: true,
ensureAuthenticatedUser: jest.fn(async () => ({
authenticatedUser: {
userId: 'user123',
username: 'user_person',
roles: [],
administrator: true,
},
decodedAccessToken: {
user_id: 'user123',
preferred_username: 'user_person',
roles: [],
administrator: true,
},
})),
})),
}));
Expand Down Expand Up @@ -50,11 +58,17 @@ it('should create an API client, ensure we have an authenticated user, and extra
loggingService: 'logging service',
});
// TODO: There's an async in here - we probably need to wait for it.
expect(app.apiClient.ensurePublicOrAuthenticationAndCookies).toHaveBeenCalledWith('/i/am/a/path');
expect(app.apiClient.ensureAuthenticatedUser).toHaveBeenCalledWith('/i/am/a/path');
expect(app.authenticatedUser).toEqual({
userId: 'user123',
username: 'user_person',
roles: [],
administrator: true,
});
expect(app.decodedAccessToken).toEqual({
user_id: 'user123',
preferred_username: 'user_person',
roles: [],
administrator: true,
});
});
5 changes: 1 addition & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export { default as ErrorBoundary } from './ErrorBoundary';
export { default as ErrorPage } from './ErrorPage';
export { default as getQueryParameters } from './getQueryParameters';
export { default as validateConfig } from './validateConfig';
export {
fetchUserAccount,
getAuthenticatedUser,
} from './frontendAuthWrapper';
export { fetchUserAccount } from './frontendAuthWrapper';

// Handlers
export { default as analytics } from './handlers/analytics';
Expand Down

0 comments on commit eaa1d91

Please sign in to comment.