fix: change default open authoring scope, make it configurable (#2821)

This commit is contained in:
Erez Rokah 2019-11-07 19:39:47 +02:00 committed by Shawn Erquhart
parent da2dab305a
commit 002cdd77a8
3 changed files with 61 additions and 19 deletions

View File

@ -75,12 +75,17 @@ export default class GitHubAuthenticationPage extends React.Component {
};
const auth = new NetlifyAuthenticator(cfg);
auth.authenticate({ provider: 'github', scope: 'repo' }, (err, data) => {
const openAuthoring = this.props.config.getIn(['backend', 'open_authoring'], false);
const scope = this.props.config.getIn(
['backend', 'auth_scope'],
openAuthoring ? 'public_repo' : 'repo',
);
auth.authenticate({ provider: 'github', scope }, (err, data) => {
if (err) {
this.setState({ loginError: err.toString() });
return;
}
if (this.props.config.getIn(['backend', 'open_authoring'])) {
if (openAuthoring) {
return this.loginWithOpenAuthoring(data).then(() => this.props.onLogin(data));
}
this.props.onLogin(data);

View File

@ -1,3 +1,4 @@
import { merge } from 'lodash';
import { validateConfig } from '../configSchema';
describe('config', () => {
@ -6,26 +7,27 @@ describe('config', () => {
* log test failures and associated errors as expected.
*/
beforeEach(() => {
jest.spyOn(console, 'error');
jest.spyOn(console, 'error').mockImplementation(() => {});
});
describe('validateConfig', () => {
const validConfig = {
foo: 'bar',
backend: { name: 'bar' },
media_folder: 'baz',
collections: [
{
name: 'posts',
label: 'Posts',
folder: '_posts',
fields: [{ name: 'title', label: 'title', widget: 'string' }],
},
],
};
it('should not throw if no errors', () => {
const config = {
foo: 'bar',
backend: { name: 'bar' },
media_folder: 'baz',
collections: [
{
name: 'posts',
label: 'Posts',
folder: '_posts',
fields: [{ name: 'title', label: 'title', widget: 'string' }],
},
],
};
expect(() => {
validateConfig(config);
validateConfig(validConfig);
}).not.toThrowError();
});
@ -47,6 +49,33 @@ describe('config', () => {
}).toThrowError("'backend.name' should be string");
});
it('should throw if backend.open_authoring is not a boolean in config', () => {
expect(() => {
validateConfig(merge(validConfig, { backend: { open_authoring: 'true' } }));
}).toThrowError("'backend.open_authoring' should be boolean");
});
it('should not throw if backend.open_authoring is boolean in config', () => {
expect(() => {
validateConfig(merge(validConfig, { backend: { open_authoring: true } }));
}).not.toThrowError();
});
it('should throw if backend.auth_scope is not "repo" or "public_repo" in config', () => {
expect(() => {
validateConfig(merge(validConfig, { backend: { auth_scope: 'user' } }));
}).toThrowError("'backend.auth_scope' should be equal to one of the allowed values");
});
it('should not throw if backend.auth_scope is one of "repo" or "public_repo" in config', () => {
expect(() => {
validateConfig(merge(validConfig, { backend: { auth_scope: 'repo' } }));
}).not.toThrowError();
expect(() => {
validateConfig(merge(validConfig, { backend: { auth_scope: 'public_repo' } }));
}).not.toThrowError();
});
it('should throw if media_folder is not defined in config', () => {
expect(() => {
validateConfig({ foo: 'bar', backend: { name: 'bar' } });

View File

@ -31,7 +31,15 @@ const getConfigSchema = () => ({
properties: {
backend: {
type: 'object',
properties: { name: { type: 'string', examples: ['test-repo'] } },
properties: {
name: { type: 'string', examples: ['test-repo'] },
auth_scope: {
type: 'string',
examples: ['repo', 'public_repo'],
enum: ['repo', 'public_repo'],
},
open_authoring: { type: 'boolean', examples: [true] },
},
required: ['name'],
},
site_url: { type: 'string', examples: ['https://example.com'] },
@ -169,7 +177,7 @@ class ConfigError extends Error {
* the config that is passed in.
*/
export function validateConfig(config) {
const ajv = new AJV({ allErrors: true });
const ajv = new AJV({ allErrors: true, jsonPointers: true });
ajvErrors(ajv);
const valid = ajv.validate(getConfigSchema(), config);