Skip to content

Commit

Permalink
fix(2051): fix validate function that does not exist (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshwata authored Dec 7, 2020
1 parent 1775497 commit cbc52b6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ const SCHEMA_ADDRESSES = Joi.array()
.min(1);
const SCHEMA_STATUSES = Joi.array()
.items(schema.plugins.notifications.schemaStatus)
.min(1);
.min(0);
const SCHEMA_EMAIL = Joi.alternatives().try(
Joi.object().keys({ addresses: SCHEMA_ADDRESSES, statuses: SCHEMA_STATUSES }),
SCHEMA_ADDRESS, SCHEMA_ADDRESSES
);
const SCHEMA_BUILD_SETTINGS = Joi.object()
const SCHEMA_EMAIL_SETTINGS = Joi.object()
.keys({
email: SCHEMA_EMAIL.required()
}).unknown(true);
const SCHEMA_BUILD_DATA = Joi.object()
.keys({
...schema.plugins.notifications.schemaBuildData,
settings: SCHEMA_BUILD_SETTINGS.required()
settings: SCHEMA_EMAIL_SETTINGS.required()
});
const SCHEMA_SMTP_CONFIG = Joi.object()
.keys({
Expand Down Expand Up @@ -181,7 +181,7 @@ class EmailNotifier extends NotificationBase {

// Validate the settings email object
static validateConfig(config) {
return Joi.validate(config, SCHEMA_EMAIL);
return SCHEMA_EMAIL_SETTINGS.validate(config);
}
}

Expand Down
92 changes: 92 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,98 @@ describe('index', () => {
assert.equal(err.name, 'ValidationError');
}
});

it('is valid config with complete parameters', () => {
configMock = {
email: {
addresses: ['[email protected]', '[email protected]'],
statuses: ['SUCCESS', 'FAILURE']
}
};

const res = EmailNotifier.validateConfig(configMock);

assert.isUndefined(res.error);
});

it('is valid config with empty statuses', () => {
configMock = {
email: {
addresses: ['[email protected]', '[email protected]'],
statuses: []
}
};

const { error } = EmailNotifier.validateConfig(configMock);

assert.isUndefined(error);
});

it('is valid config with addresses', () => {
configMock = {
email: {
addresses: ['[email protected]', '[email protected]']
}
};

const { error } = EmailNotifier.validateConfig(configMock);

assert.isUndefined(error);
});

it('is invalid config with empty parameters', () => {
configMock = {};
const { error } = EmailNotifier.validateConfig(configMock);

assert.instanceOf(error, Error);
assert.equal(error.name, 'ValidationError');
});

it('valid config with empty email settings', () => {
configMock = {
email: {}
};
const { error } = EmailNotifier.validateConfig(configMock);

assert.isUndefined(error);
});

it('valid config without addresses', () => {
configMock = {
email: {
statuses: ['SUCCESS', 'FAILURE']
}
};
const { error } = EmailNotifier.validateConfig(configMock);

assert.isUndefined(error);
});

it('invalid config with empty addresses', () => {
configMock = {
email: {
addresses: [],
statuses: ['SUCCESS', 'FAILURE']
}
};
const { error } = EmailNotifier.validateConfig(configMock);

assert.instanceOf(error, Error);
assert.equal(error.name, 'ValidationError');
});

it('invalid unknown status', () => {
configMock = {
email: {
addresses: ['[email protected]'],
statuses: ['DUMMY_STATUS']
}
};
const { error } = EmailNotifier.validateConfig(configMock);

assert.instanceOf(error, Error);
assert.equal(error.name, 'ValidationError');
});
});

describe('buildData is validated', () => {
Expand Down

0 comments on commit cbc52b6

Please sign in to comment.