From 1c2d243cc88327a26c53fe89875c014fe759f108 Mon Sep 17 00:00:00 2001 From: Mike Stone Date: Thu, 5 Jan 2017 19:41:32 -0500 Subject: [PATCH] Validate presence of the organization name in settings (#773) --- .../forms/admin/AppConfigForm/validate.js | 7 +- .../admin/AppConfigForm/validate.tests.js | 116 ++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 frontend/components/forms/admin/AppConfigForm/validate.tests.js diff --git a/frontend/components/forms/admin/AppConfigForm/validate.js b/frontend/components/forms/admin/AppConfigForm/validate.js index 4056bf6c00..549a92267c 100644 --- a/frontend/components/forms/admin/AppConfigForm/validate.js +++ b/frontend/components/forms/admin/AppConfigForm/validate.js @@ -5,16 +5,21 @@ export default (formData) => { const { authentication_type: authType, kolide_server_url: kolideServerUrl, + org_name: orgName, + password: smtpPassword, sender_address: smtpSenderAddress, server: smtpServer, user_name: smtpUserName, - password: smtpPassword, } = formData; if (!kolideServerUrl) { errors.kolide_server_url = 'Kolide Server URL must be present'; } + if (!orgName) { + errors.org_name = 'Organization Name must be present'; + } + if (some([smtpSenderAddress, smtpPassword, smtpServer, smtpUserName])) { if (!smtpSenderAddress) { errors.sender_address = 'SMTP Sender Address must be present'; diff --git a/frontend/components/forms/admin/AppConfigForm/validate.tests.js b/frontend/components/forms/admin/AppConfigForm/validate.tests.js new file mode 100644 index 0000000000..f3a9d90bdb --- /dev/null +++ b/frontend/components/forms/admin/AppConfigForm/validate.tests.js @@ -0,0 +1,116 @@ +import expect from 'expect'; + +import validate from 'components/forms/admin/AppConfigForm/validate'; + +describe('AppConfigForm - validations', () => { + const validFormData = { + org_name: 'The Gnar Co.', + authentication_type: 'username_password', + kolide_server_url: 'https://gnar.dog', + sender_address: 'hi@gnar.dog', + server: 'https://gnar.dog', + user_name: 'gnardog', + password: 'p@ssw0rd', + }; + + it('returns a valid object when the form data is valid', () => { + expect(validate(validFormData)).toEqual({ valid: true, errors: {} }); + }); + + it('validates presence of the org_name field', () => { + const invalidFormData = { + ...validFormData, + org_name: '', + }; + + expect(validate(invalidFormData)).toEqual({ + valid: false, + errors: { + org_name: 'Organization Name must be present', + }, + }); + }); + + it('validates presence of the kolide_server_url field', () => { + const invalidFormData = { + ...validFormData, + kolide_server_url: '', + }; + + expect(validate(invalidFormData)).toEqual({ + valid: false, + errors: { + kolide_server_url: 'Kolide Server URL must be present', + }, + }); + }); + + describe('smtp configurations', () => { + it('validates the sender address', () => { + const invalidFormData = { + ...validFormData, + sender_address: '', + }; + + expect(validate(invalidFormData)).toEqual({ + valid: false, + errors: { + sender_address: 'SMTP Sender Address must be present', + }, + }); + }); + + it('validates the smtp server', () => { + const invalidFormData = { + ...validFormData, + server: '', + }; + + expect(validate(invalidFormData)).toEqual({ + valid: false, + errors: { + server: 'SMTP Server must be present', + }, + }); + }); + + it('validates the password if auth type is not "none"', () => { + const invalidFormData = { + ...validFormData, + password: '', + }; + + expect(validate(invalidFormData)).toEqual({ + valid: false, + errors: { + password: 'SMTP Password must be present', + }, + }); + }); + + it('validates the user_name if auth type is not "none"', () => { + const invalidFormData = { + ...validFormData, + user_name: '', + }; + + expect(validate(invalidFormData)).toEqual({ + valid: false, + errors: { + user_name: 'SMTP Username must be present', + }, + }); + }); + + it('does not validate the user_name and password if the auth type is "none"', () => { + const formData = { + ...validFormData, + authentication_type: 'authtype_none', + password: '', + user_name: '', + }; + + expect(validate(formData)).toEqual({ valid: true, errors: {} }); + }); + }); +});