Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update account data with undefined from team-info (WEBAPP-4342) #953

Merged
merged 1 commit into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions electron/renderer/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ export const abortAccountCreation = (id) => {

export const updateAccountData = (id, data) => {
return (dispatch, getState) => {
const isValidAccountData = verifyObjectProperties(data, {
const validatedAccountData = verifyObjectProperties(data, {
'teamID': 'String',
'userID': 'String',
'picture': 'String',
'name': 'String',
'accentID': 'Number',
});

if (isValidAccountData) {
dispatch(updateAccount(id, data));
if (validatedAccountData) {
dispatch(updateAccount(id, validatedAccountData));
} else {
console.warn(`Got invalid account data ${JSON.stringify(data)}`);
}
Expand Down
21 changes: 15 additions & 6 deletions electron/renderer/src/lib/__tests__/verifyObjectProperties.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import verifyObjectProperties from '../verifyObjectProperties';

describe('verifyObjectProperties', () => {

it('should return true if object contains all properties specified in the config', () => {
it('should return the object if object contains all properties specified in the config', () => {
const obj = {
foo: 'test',
bla: 2,
Expand All @@ -30,18 +30,27 @@ describe('verifyObjectProperties', () => {
foo: 'String',
bla: 'Number',
};
expect(verifyObjectProperties(obj, config)).toBeTruthy();

const validatedObject = verifyObjectProperties(obj, config);
expect(validatedObject).toBeTruthy();
expect(Object.keys(validatedObject).length).toBe(2);
});

it('should return true if object contains only a subset of properties specified in the config', () => {
it('should return the object with defaults if object contains only a subset of properties specified in the config', () => {
const obj = {
bla: 2,
};
const config = {
foo: 'String',
bla: 'Number',
some: 'Number',
};
expect(verifyObjectProperties(obj, config)).toBeTruthy();

const validatedObject = verifyObjectProperties(obj, config);
expect(validatedObject).toBeTruthy();
expect(Object.keys(validatedObject).length).toBe(3);
expect(validatedObject['foo']).toBe('');
expect(validatedObject['some']).toBeUndefined();
});

it('should return false if object contains a property with a wrong type', () => {
Expand All @@ -58,7 +67,7 @@ describe('verifyObjectProperties', () => {

it('should return false if object contains a property with undefined but config excepted a type', () => {
const obj = {
foo: 1,
foo: undefined,
bla: 2,
};
const config = {
Expand All @@ -79,4 +88,4 @@ describe('verifyObjectProperties', () => {
expect(verifyObjectProperties(obj, config)).toBeFalsy();
});

});
});
17 changes: 12 additions & 5 deletions electron/renderer/src/lib/verifyObjectProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@
*/

function is(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
return obj != null && clas === type;
const getType = Object.prototype.toString.call(obj).slice(8, -1);
return obj != null && getType === type;
}

export default function(data, config) {
const dataKeys = Object.keys(data);
const configKeys = Object.keys(config);

if (dataKeys.length > configKeys) {
if (dataKeys.length > configKeys.length) {
return false;
}

return dataKeys.every((key) => {
return config.hasOwnProperty(key) && is(config[key], data[key]);
const isValidObject = configKeys.every((key) => {
if (!data.hasOwnProperty(key)) {
data[key] = config[key] === 'String' ? '' : undefined;
return true;
}

return is(config[key], data[key]);
});

return isValidObject ? data : false;
}