Skip to content

Commit

Permalink
Add handlers for the Branding and Prompts API endpoints. (auth0#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisscott authored and Drew Fyock committed Jul 10, 2019
1 parent ae7d3d4 commit 3dca3a4
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 257 deletions.
32 changes: 32 additions & 0 deletions src/auth0/handlers/branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import DefaultHandler from './default';

export const schema = { type: 'object' };

export default class BrandingHandler extends DefaultHandler {
constructor(options) {
super({
...options,
type: 'branding'
});
}

async getType() {
try {
return await this.client.branding.getSettings();
} catch (err) {
if (err.statusCode === 404) return {};
throw err;
}
}

async processChanges(assets) {
const { branding } = assets;

// Do nothing if not set
if (!branding) return;

await this.client.branding.updateSettings(branding);
this.updated += 1;
this.didUpdate(branding);
}
}
6 changes: 5 additions & 1 deletion src/auth0/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import * as guardianFactors from './guardianFactors';
import * as guardianFactorProviders from './guardianFactorProviders';
import * as guardianFactorTemplates from './guardianFactorTemplates';
import * as roles from './roles';
import * as branding from './branding';
import * as prompts from './prompts';

export {
rules,
Expand All @@ -29,5 +31,7 @@ export {
guardianFactors,
guardianFactorProviders,
guardianFactorTemplates,
roles
roles,
branding,
prompts
};
32 changes: 32 additions & 0 deletions src/auth0/handlers/prompts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import DefaultHandler from './default';

export const schema = { type: 'object' };

export default class PromptsHandler extends DefaultHandler {
constructor(options) {
super({
...options,
type: 'prompts'
});
}

async getType() {
try {
return await this.client.prompts.getSettings();
} catch (err) {
if (err.statusCode === 404) return {};
throw err;
}
}

async processChanges(assets) {
const { prompts } = assets;

// Do nothing if not set
if (!prompts) return;

await this.client.prompts.updateSettings(prompts);
this.updated += 1;
this.didUpdate(prompts);
}
}
41 changes: 41 additions & 0 deletions tests/auth0/handlers/branding.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { expect } = require('chai');
const branding = require('../../../src/auth0/handlers/branding');

describe('#branding handler', () => {
describe('#branding process', () => {
it('should get branding', async () => {
const auth0 = {
branding: {
getSettings: () => ({
logo_url: 'https://example.com/logo.png'
})
}
};

const handler = new branding.default({ client: auth0 });
const data = await handler.getType();
expect(data).to.deep.equal({
logo_url: 'https://example.com/logo.png'
});
});

it('should update branding settings', async () => {
const auth0 = {
branding: {
updateSettings: (data) => {
expect(data).to.be.an('object');
expect(data.logo_url).to.equal('https://example.com/logo.png');
return Promise.resolve(data);
}
}
};

const handler = new branding.default({ client: auth0 });
const stageFn = Object.getPrototypeOf(handler).processChanges;

await stageFn.apply(handler, [
{ branding: { logo_url: 'https://example.com/logo.png' } }
]);
});
});
});
41 changes: 41 additions & 0 deletions tests/auth0/handlers/prompts.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { expect } = require('chai');
const prompts = require('../../../src/auth0/handlers/prompts');

describe('#prompts handler', () => {
describe('#prompts process', () => {
it('should get prompts', async () => {
const auth0 = {
prompts: {
getSettings: () => ({
universal_login_experience: 'new'
})
}
};

const handler = new prompts.default({ client: auth0 });
const data = await handler.getType();
expect(data).to.deep.equal({
universal_login_experience: 'new'
});
});

it('should update prompts settings', async () => {
const auth0 = {
prompts: {
updateSettings: (data) => {
expect(data).to.be.an('object');
expect(data.universal_login_experience).to.equal('new');
return Promise.resolve(data);
}
}
};

const handler = new prompts.default({ client: auth0 });
const stageFn = Object.getPrototypeOf(handler).processChanges;

await stageFn.apply(handler, [
{ prompts: { universal_login_experience: 'new' } }
]);
});
});
});
Loading

0 comments on commit 3dca3a4

Please sign in to comment.