Added authorization pieces + test functions

This commit is contained in:
Wyatt Johnson
2016-11-22 17:15:14 -07:00
parent 32d715e784
commit 325162cb3e
17 changed files with 190 additions and 153 deletions
+45 -16
View File
@@ -5,23 +5,25 @@ const expect = require('chai').expect;
describe('Action: models', () => {
let mockActions;
beforeEach(() => {
return Action.create([{
action_type: 'flag',
item_id: '123',
item_type: 'comments'
item_type: 'comment',
user_id: 'flagginguserid'
}, {
action_type: 'flag',
item_id: '456',
item_type: 'comments'
item_type: 'comment'
}, {
action_type: 'flag',
item_id: '123',
item_type: 'comments'
item_type: 'comment'
}, {
action_type: 'like',
item_id: '123',
item_type: 'comments'
item_type: 'comment'
}]).then((actions) => {
mockActions = actions;
});
@@ -30,8 +32,7 @@ describe('Action: models', () => {
describe('#findById()', () => {
it('should find an action by id', () => {
return Action.findById(mockActions[0].id).then((result) => {
expect(result).to.have.property('action_type')
.and.to.equal('flag');
expect(result).to.have.property('action_type', 'flag');
});
});
});
@@ -46,27 +47,55 @@ describe('Action: models', () => {
describe('#getActionSummaries()', () => {
it('should return properly formatted summaries from an array of item_ids', () => {
return Action.getActionSummaries(['123', '789']).then((result) => {
expect(result).to.have.length(2);
return Action.getActionSummaries(['123', '789']).then((summaries) => {
expect(summaries).to.have.length(2);
const sorted = result.sort((a, b) => a.count - b.count);
expect(sorted[0]).to.deep.equal({
expect(summaries).to.deep.include({
action_type: 'like',
count: 1,
item_id: '123',
item_type: 'comments',
current_user: false
item_type: 'comment',
current_user: null
});
expect(sorted[1]).to.deep.equal({
expect(summaries).to.deep.include({
action_type: 'flag',
count: 2,
item_id: '123',
item_type: 'comments',
current_user: false
item_type: 'comment',
current_user: null
});
});
});
it('should include a current user when one is passed', () => {
return Action
.getActionSummaries(['123'], 'flagginguserid')
.then((summaries) => {
expect(summaries).to.have.length(2);
let summary = summaries.find((s) => s.item_id === '123' && s.action_type === 'flag');
expect(summary).to.not.be.undefined;
expect(summary.current_user).to.not.be.null;
expect(summary.current_user).to.have.property('item_id', '123');
expect(summary.current_user).to.have.property('item_type', 'comment');
expect(summary.current_user).to.have.property('user_id', 'flagginguserid');
expect(summary.current_user).to.have.property('action_type', 'flag');
});
});
it('should not include a current user when one is passed for a user that doesn\'t have an action', () => {
return Action
.getActionSummaries(['123'], 'flagginguserid2')
.then((summaries) => {
expect(summaries).to.have.length(2);
summaries.forEach((summary) => {
expect(summary).to.not.be.undefined;
expect(summary).to.have.property('current_user', null);
});
});
});
});
});