Skip to content

Commit

Permalink
Returning null instead of empty array when tenant doesn't support the…
Browse files Browse the repository at this point in the history
…mes functionality
  • Loading branch information
Will Vedder committed May 31, 2022
1 parent 12a66fe commit ac3aee4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

42 changes: 23 additions & 19 deletions src/tools/auth0/handlers/themes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { cloneDeep } from 'lodash';
import { Assets } from '../../../types';
import { Asset, Assets } from '../../../types';
import log from '../../../logger';
import DefaultHandler from './default';

export default class ThemesHandler extends DefaultHandler {
existing: Theme[];
existing: Theme[] | null;

constructor(options: DefaultHandler) {
super({
...options,
type: 'themes',
identifiers: ['themeId'],
id: 'themeId',
});
}

objString(theme: Theme): string {
return theme.displayName || JSON.stringify(theme);
}

async getType(): Promise<Theme[] | null> {
if (!this.existing) {
this.existing = await this.getThemes();
Expand Down Expand Up @@ -44,11 +47,12 @@ export default class ThemesHandler extends DefaultHandler {
}

// if theme exists we need to delete it
const currentTheme = (await this.getThemes())[0];
if (!currentTheme?.themeId) {
const currentThemes = await this.getThemes();
if (currentThemes === null || currentThemes.length === 0) {
return;
}

const currentTheme = currentThemes[0];
await this.client.branding.deleteTheme({ id: currentTheme.themeId });

this.deleted += 1;
Expand All @@ -60,31 +64,31 @@ export default class ThemesHandler extends DefaultHandler {
log.warn('Only one theme is supported per tenant');
}

const currentTheme = (await this.getThemes())[0];
const currentThemes = await this.getThemes();

// if theme exists, overwrite it otherwise create it
if (currentTheme?.themeId) {
await this.client.branding.updateTheme({ id: currentTheme.themeId }, themes[0]);
} else {
if (currentThemes === null || currentThemes.length === 0) {
await this.client.branding.createTheme(themes[0]);
} else {
const currentTheme = currentThemes[0];
// if theme exists, overwrite it otherwise create it
await this.client.branding.updateTheme({ id: currentTheme.themeId }, themes[0]);
}

this.updated += 1;
this.didUpdate(themes[0]);
}

async getThemes(): Promise<Theme[]> {
async getThemes(): Promise<Theme[] | null> {
try {
const theme = (await this.client.branding.getDefaultTheme()) as Theme;
const theme = await this.client.branding.getDefaultTheme();
return [theme];
} catch (err) {
if (err.statusCode === 404) return [];
if (err.statusCode === 400) return null;

// Errors other than 404 (theme doesn't exist) or 400 (no-code not enabled) shouldn't be expected
if (err.statusCode !== 404 && err.statusCode !== 400) {
throw err;
}
throw err;
}

return [];
}
}

Expand Down Expand Up @@ -581,6 +585,6 @@ export interface Theme {
borders: Borders;
widget: Widget;
page_background: PageBackground;
themeId?: string;
themeId: string;
displayName?: string;
}
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ export type BaseAuth0APIClient = {
getUniversalLoginTemplate: () => Promise<Asset>;
updateSettings: ({}, Asset) => Promise<void>;
setUniversalLoginTemplate: ({}, Asset) => Promise<void>;
getDefaultTheme: () => Promise<Asset>;
updateTheme: (arg0: { id: string }, Theme) => Promise<Theme>;
getDefaultTheme: () => Promise<Theme>;
updateTheme: (arg0: { id: string }, Theme) => Promise<Omit<Theme, 'themeId'>>;
createTheme: (arg0: Theme) => Promise<Omit<Theme, 'themeId'>>;
deleteTheme: (arg0: { id: string }) => Promise<void>;
createTheme: (arg0: Theme) => Promise<Theme>;
};
clients: APIClientBaseFunctions;
clientGrants: APIClientBaseFunctions;
Expand Down
2 changes: 1 addition & 1 deletion test/tools/auth0/handlers/themes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('#themes handler', () => {
const handler = new ThemesHandler({ client: auth0 });
const data = await handler.getType();

expect(data).to.deep.equal([]);
expect(data).to.deep.equal(null);
expect(auth0.branding.getDefaultTheme.called).to.equal(true);
expect(auth0.branding.getDefaultTheme.callCount).to.equal(1);
});
Expand Down

0 comments on commit ac3aee4

Please sign in to comment.