Skip to content
Open
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
31 changes: 31 additions & 0 deletions common/types-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var factory = function() {
return {};
};

var schema = {
type: 'object',
patternProperties: {
'.*_.*': {
type: 'object',
properties: {
reactions: {
type: 'object'
},
tops: {
type: 'array',
minItems: 1,
items: require('./types-post')
},
summary:{
type: 'object'
}
},
required: ['reactions', 'tops', 'summary']
}
}
};

module.exports = {
schema: schema,
factory: factory
};
30 changes: 30 additions & 0 deletions common/types-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var factory = function() {
return {}
};

var schema = {
type: 'object',
properties: {
id: {
type: 'string',
pattern: '\\d+_\\d+'
},
type:{
enum : require('./fb-api').REACTION_TYPES
},
count: {
type: 'number'
},
message:{
type: 'string'
},
caption: {
type: 'string'
}
}
};

module.exports = {
schema: schema,
factory: factory
};
15 changes: 15 additions & 0 deletions common/types-reactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var factory = function() {
return {}
};

var schema = {
type: 'object',
patternProperties: {
'.*': { type: 'integer', default: 0 }
}
};

module.exports = {
schema: schema,
factory: factory
};
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha test/**.spec.js",
"deploy": "apex -s FB_APP_ID='$FB_APP_ID' -s FB_APP_SECRET=='$FB_APP_SECRET' deploy"
},
"author": "",
"license": "MIT",
"eslint": {
"extends": "google",
"rules": {}
},
"dependencies": {
"bluebird": "^3.3.5",
"chai": "^3.5.0",
"dotenv": "^2.0.0",
"limiter": "^1.1.0",
"lodash": "*",
"mocha": "^3.2.0",
"moment": "^2.13.0",
"redis": "^2.6.0-1",
"request-promise": "*",
"winston": "^2.2.0"
},
"devDependencies": {
"eslint": "^3.11.1",
"eslint-config-google": "^0.7.1"
}
}
1 change: 1 addition & 0 deletions test/fixture-api.js

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions test/types.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var Ajv = require('ajv');
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
var ReactionsType = require('../common/types-reactions');
var ApiType = require('../common/types-api');
var PostType = require('../common/types-post')
var fixture = require('./fixture-api.js');
describe('types', function() {

describe('reactions', function() {
it('validate reaction types ', function () {
var reactions = _.cloneDeep(fixture["1480348800000_1480435199999"].reactions);
var isValid = ajv.validate(ReactionsType.schema, reactions);
console.error(ajv.errors);
expect(isValid).to.be.true;
});
it('invalidate wrong reaction types ', function () {
var reactions = _.cloneDeep(fixture["1480348800000_1480435199999"].reactions);
reactions.HAHA = '0';
var isValid = ajv.validate(ReactionsType.schema, reactions);
console.error(ajv.errors);
expect(isValid).to.be.false;
});
});

describe('post', function() {
it('validate post', function () {
var post = _.cloneDeep(fixture["1480348800000_1480435199999"].tops)[0];
var isValid = ajv.validate(PostType.schema, post);
console.error(ajv.errors);
expect(isValid).to.be.true;
});
it('invalidate incorrect post', function () {
var post = _.cloneDeep(fixture["1480348800000_1480435199999"].tops)[0];
post.type = 'HIHI'
var isValid = ajv.validate(PostType.schema, post);
console.error(ajv.errors);
expect(isValid).to.be.false;
});
});

describe('api', function() {
it('validate api responses', function () {
var apiResponse = _.cloneDeep(fixture);
var isValid = ajv.validate(ApiType.schema, apiResponse);
console.error(ajv.errors);
expect(isValid).to.be.true;
});

it('invalidate incorrect api responses', function () {
var apiResponse = _.cloneDeep(fixture);
apiResponse["1480348800000_1480435199999"].tops = null
var isValid = ajv.validate(ApiType.schema, apiResponse);
console.error(ajv.errors);
expect(isValid).to.be.false;
});

});
});
24 changes: 19 additions & 5 deletions ui/src/app/fetchReactions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { FETCH_AGG, fetchAndUpdateSelectedDate } from './store/actions'

function getEndpoint (location, offset) {
var url = 'https://8zbfsx31e0.execute-api.ap-northeast-1.amazonaws.com/prod/reactions'
url += '?location=' + location
url += '&offset=' + offset || 0
return url
const API_ENDPOINT = 'https://8zbfsx31e0.execute-api.ap-northeast-1.amazonaws.com/prod/reactions'

var Ajv = require('ajv');
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
var ApiType = require('../common/types-api');

var validateApi = ajv.compile(ApiType.schema)

function getEndpoint (location, offset) {
offset = offset || 0
location = location || ''
return `${API_ENDPOINT}?location=${location}&offset=${offset}`
}

export default (dispatch) => function (offset) {
Expand All @@ -13,6 +20,13 @@ export default (dispatch) => function (offset) {
.then(function (res) {
return res.json()
})
.then(function (data) {
var isValid = validateApi(data)
console.log('API res valid:'+isValid);
console.log(ajv.errors);
// silent failure
return data;
})
.then(function (byDayData) {
dispatch(fetchAndUpdateSelectedDate(location, byDayData))
})
Expand Down
7 changes: 0 additions & 7 deletions ui/src/app/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ import { FETCH_AGG } from './store/actions'
// won't auto update until gulp script
let REACTION_TYPES = require('./meta.js').REACTION_TYPES

function getEndpoint (location, offset) {
var url = 'https://8zbfsx31e0.execute-api.ap-northeast-1.amazonaws.com/prod/reactions'
url += '?location=' + location
url += '&offset=' + offset || 0
return url
}

// TODO by daily (HK) emotion?
// http://stackoverflow.com/questions/260857/changing-website-favicon-dynamically
(function () {
Expand Down