From ac3aee405f5b44afbe71147775284fb36daa9fc3 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 31 May 2022 16:08:33 -0400 Subject: [PATCH] Returning null instead of empty array when tenant doesn't support themes functionality --- package-lock.json | 2 +- src/tools/auth0/handlers/themes.ts | 42 +++++++++++++---------- src/types.ts | 6 ++-- test/tools/auth0/handlers/themes.tests.js | 2 +- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6f6213de7..2eb85bd82 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10572,7 +10572,7 @@ "istanbul-lib-processinfo": "^2.0.2", "istanbul-lib-report": "^3.0.0", "istanbul-lib-source-maps": "^4.0.0", - "istanbul-reports": "^3.0.2", + "istanbul-reports": "3.1.4", "make-dir": "^3.0.0", "node-preload": "^0.2.1", "p-map": "^3.0.0", diff --git a/src/tools/auth0/handlers/themes.ts b/src/tools/auth0/handlers/themes.ts index a1e2c4822..2f08080e5 100644 --- a/src/tools/auth0/handlers/themes.ts +++ b/src/tools/auth0/handlers/themes.ts @@ -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 { if (!this.existing) { this.existing = await this.getThemes(); @@ -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; @@ -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 { + async getThemes(): Promise { 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 []; } } @@ -581,6 +585,6 @@ export interface Theme { borders: Borders; widget: Widget; page_background: PageBackground; - themeId?: string; + themeId: string; displayName?: string; } diff --git a/src/types.ts b/src/types.ts index d51cf2d78..ae987470c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -64,10 +64,10 @@ export type BaseAuth0APIClient = { getUniversalLoginTemplate: () => Promise; updateSettings: ({}, Asset) => Promise; setUniversalLoginTemplate: ({}, Asset) => Promise; - getDefaultTheme: () => Promise; - updateTheme: (arg0: { id: string }, Theme) => Promise; + getDefaultTheme: () => Promise; + updateTheme: (arg0: { id: string }, Theme) => Promise>; + createTheme: (arg0: Theme) => Promise>; deleteTheme: (arg0: { id: string }) => Promise; - createTheme: (arg0: Theme) => Promise; }; clients: APIClientBaseFunctions; clientGrants: APIClientBaseFunctions; diff --git a/test/tools/auth0/handlers/themes.tests.js b/test/tools/auth0/handlers/themes.tests.js index ce92488a5..46bb7179d 100644 --- a/test/tools/auth0/handlers/themes.tests.js +++ b/test/tools/auth0/handlers/themes.tests.js @@ -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); });