From f725bd492fccc8c3651aeea7c0f10c38d5b00ec8 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 22 Feb 2016 11:48:38 -0800 Subject: [PATCH] Framework: add connectionsSchema for sharing/publicize --- client/state/sharing/publicize/reducer.js | 7 +++- client/state/sharing/publicize/schema.js | 31 ++++++++++++++++ .../state/sharing/publicize/test/reducer.js | 36 +++++++++++++++++-- 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 client/state/sharing/publicize/schema.js diff --git a/client/state/sharing/publicize/reducer.js b/client/state/sharing/publicize/reducer.js index c59991ca2a2d4..11cf4ba4d727a 100644 --- a/client/state/sharing/publicize/reducer.js +++ b/client/state/sharing/publicize/reducer.js @@ -14,6 +14,8 @@ import { SERIALIZE, DESERIALIZE } from 'state/action-types'; +import { connectionsSchema } from './schema'; +import { isValidStateWithSchema } from 'state/utils'; /** * Track the current status for fetching connections. Maps site ID to the @@ -55,8 +57,11 @@ export function connections( state = {}, action ) { case PUBLICIZE_CONNECTIONS_RECEIVE: return Object.assign( {}, state, keyBy( action.data.connections, 'ID' ) ); case SERIALIZE: - return {}; + return state; case DESERIALIZE: + if ( isValidStateWithSchema( state, connectionsSchema ) ) { + return state; + } return {}; } diff --git a/client/state/sharing/publicize/schema.js b/client/state/sharing/publicize/schema.js new file mode 100644 index 0000000000000..97b0c0d53b8bc --- /dev/null +++ b/client/state/sharing/publicize/schema.js @@ -0,0 +1,31 @@ +export const connectionsSchema = { + type: 'object', + patternProperties: { + '^\\d+$': { + type: 'object', + required: [ 'ID', 'site_ID' ], + properties: { + ID: { type: 'integer' }, + site_ID: { type: 'integer' }, + user_ID: { type: 'integer' }, + keyring_connection_ID: { type: 'integer' }, + keyring_connection_user_ID: { type: 'integer' }, + shared: { type: 'boolean' }, + service: { type: 'string' }, + label: { type: 'string' }, + issued: { type: 'string' }, + expires: { type: 'string' }, + external_ID: { type: [ 'string', 'null' ] }, + external_name: { type: [ 'string', 'null' ] }, + external_display: { type: [ 'string', 'null' ] }, + external_profile_picture: { type: [ 'string', 'null' ] }, + external_profile_URL: { type: [ 'string', 'null' ] }, + external_follower_count: { type: [ 'integer', 'null' ] }, + status: { type: 'string' }, + refresh_URL: { type: 'string' }, + meta: { type: 'object' } + } + } + }, + additionalProperties: false +}; diff --git a/client/state/sharing/publicize/test/reducer.js b/client/state/sharing/publicize/test/reducer.js index 080e510797474..34b6db99f27f1 100644 --- a/client/state/sharing/publicize/test/reducer.js +++ b/client/state/sharing/publicize/test/reducer.js @@ -3,6 +3,7 @@ */ import { expect } from 'chai'; import deepFreeze from 'deep-freeze'; +import sinon from 'sinon'; /** * Internal dependencies @@ -122,16 +123,23 @@ describe( '#connections()', () => { } ); describe( 'persistence', () => { - it( 'does not persist data because this is not implemented yet', () => { + before( () => { + sinon.stub( console, 'warn' ); + } ); + after( () => { + console.warn.restore(); + } ); + + it( 'should persist data', () => { const state = deepFreeze( { 1: { ID: 1, site_ID: 2916284 }, 2: { ID: 2, site_ID: 2916284 } } ); const persistedState = connections( state, { type: SERIALIZE } ); - expect( persistedState ).to.eql( {} ); + expect( persistedState ).to.eql( state ); } ); - it( 'does not load persisted data because this is not implemented yet', () => { + it( 'should load valid data', () => { const persistedState = deepFreeze( { 1: { ID: 1, site_ID: 2916284 }, 2: { ID: 2, site_ID: 2916284 } @@ -139,6 +147,28 @@ describe( '#connections()', () => { const state = connections( persistedState, { type: DESERIALIZE } ); + expect( state ).to.eql( persistedState ); + } ); + + it( 'should ignore loading data with invalid keys', () => { + const persistedState = deepFreeze( { + foo: { ID: 1, site_ID: 2916284 }, + bar: { ID: 2, site_ID: 2916284 } + } ); + const state = connections( persistedState, { + type: DESERIALIZE + } ); + expect( state ).to.eql( {} ); + } ); + + it( 'should ignore loading data with invalid values', () => { + const persistedState = deepFreeze( { + 1: { ID: 1, site_ID: 'foo' }, + 2: { ID: 2, site_ID: 2916284 } + } ); + const state = connections( persistedState, { + type: DESERIALIZE + } ); expect( state ).to.eql( {} ); } ); } );