Skip to content

Commit

Permalink
chore(content-preview): Pass token generator to preview library (#1689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze authored and mergify[bot] committed Oct 31, 2019
1 parent 2702367 commit 38469a9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/elements/content-preview/ContentPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,7 @@ class ContentPreview extends React.PureComponent<Props, State> {
}

const fileOpts = { ...fileOptions };
const typedId: string = getTypedFileId(fileId);
const token: TokenLiteral = await TokenService.getReadToken(typedId, tokenOrTokenFunction);
const token = typedId => TokenService.getReadTokens(typedId, tokenOrTokenFunction);

if (selectedVersion) {
fileOpts[fileId] = fileOpts[fileId] || {};
Expand Down
21 changes: 2 additions & 19 deletions src/elements/content-preview/__tests__/ContentPreview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,6 @@ describe('elements/content-preview/ContentPreview', () => {
file = { id: '123' };
});

test('should get read token for preview', async () => {
props = {
token: 'token',
fileId: file.id,
};
const origGetReadToken = TokenService.default.getReadToken;
TokenService.default.getReadToken = jest.fn().mockReturnValueOnce(Promise.resolve(props.token));
const wrapper = getWrapper(props);
wrapper.setState({ file });
const instance = wrapper.instance();
await instance.loadPreview();
expect(TokenService.default.getReadToken).toHaveBeenCalledWith('file_123', props.token);
TokenService.default.getReadToken = origGetReadToken;
});

test('should bind onPreviewError prop to preview "preview_error" event', async () => {
props = {
onError: jest.fn(),
Expand Down Expand Up @@ -241,14 +226,13 @@ describe('elements/content-preview/ContentPreview', () => {
token: 'token',
fileId: file.id,
};
TokenService.getReadToken = jest.fn().mockReturnValueOnce(Promise.resolve(props.token));
const wrapper = getWrapper(props);
wrapper.setState({ file });
const instance = wrapper.instance();
await instance.loadPreview();
expect(instance.preview.show).toHaveBeenCalledWith(
file.id,
props.token,
expect.any(Function),
expect.objectContaining({
showDownload: false,
skipServerUpdate: true,
Expand All @@ -265,7 +249,6 @@ describe('elements/content-preview/ContentPreview', () => {
token: 'token',
fileId: file.id,
};
TokenService.getReadToken = jest.fn().mockReturnValueOnce(Promise.resolve(props.token));
const wrapper = getWrapper(props);
wrapper.setState({
file,
Expand All @@ -277,7 +260,7 @@ describe('elements/content-preview/ContentPreview', () => {
await instance.loadPreview();
expect(instance.preview.show).toHaveBeenCalledWith(
file.id,
props.token,
expect.any(Function),
expect.objectContaining({
container: expect.stringContaining('.bcpr-content'),
header: 'none',
Expand Down
23 changes: 22 additions & 1 deletion src/utils/TokenService.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TokenService {
*
* @public
* @param {string} id - box item typed id
* @param {string} tokenOrTokenFunction - Optional token or token function
* @param {Token} tokenOrTokenFunction - Optional token or token function
* @return {Promise} that resolves to a token
*/
static async getReadToken(id: string, tokenOrTokenFunction: Token): Promise<?string> {
Expand All @@ -69,6 +69,27 @@ class TokenService {
return token;
}

/**
* Gets read tokens.
*
* @public
* @param {string|string[]} id - box item typed id(s)
* @param {Token} tokenOrTokenFunction - Token to use or token generation function
* @return {Promise} Promise that resolves with id to token map
*/
static async getReadTokens(id: string | string[], tokenOrTokenFunction: Token): Promise<Object> {
const ids: string[] = Array.isArray(id) ? id : [id];
const promises: Promise<?string>[] = ids.map((typedId: string) =>
TokenService.getReadToken(typedId, tokenOrTokenFunction),
);
const tokens: (?string)[] = await Promise.all(promises);
const tokenMap = {};
tokens.forEach((token, index) => {
tokenMap[ids[index]] = token;
});
return Promise.resolve(tokenMap);
}

/**
* Gets a string write token.
* Defaults to either the read token or a simple token string.
Expand Down
15 changes: 15 additions & 0 deletions src/utils/__tests__/TokenService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ describe('util/Tokenservice', () => {
expect(Tokenservice.getReadToken('file_123', junkTokenGenerator)).rejects.toThrow(/Bad id or auth token/));
});

describe('getReadTokens()', () => {
test('should call Tokenservice.getReadToken', () => {
Tokenservice.getReadToken = jest.fn();
return Tokenservice.getReadTokens('file_123', readTokenGenerator).then(() => {
expect(Tokenservice.getReadToken).toHaveBeenCalledWith('file_123', readTokenGenerator);
});
});
test('should return a token map', () => {
expect(Tokenservice.getReadTokens(['file_123', 'file_456'], readTokenGenerator)).resolves.toEqual({
file_123: 'read_token',
file_456: 'read_token',
});
});
});

describe('cacheTokens()', () => {
test('should call the token generator function', async () => {
const generator = jest.fn();
Expand Down

0 comments on commit 38469a9

Please sign in to comment.