Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
Improve integration/api/post.js
Browse files Browse the repository at this point in the history
  • Loading branch information
voidxnull committed Jun 26, 2017
1 parent 491fc0e commit 0ad2ccf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion test-helpers/factories/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ export default PostFactory;

export async function createPost(attrs = {}) {
return await new Post(PostFactory.build(attrs))
.save(null, { method: 'insert' });
.save(null, { method: 'insert', require: true });
}
45 changes: 22 additions & 23 deletions test/integration/api/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,24 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-env node, mocha */
/* global $dbConfig */
import _ from 'lodash';

import expect from '../../../test-helpers/expect';
import initBookshelf from '../../../src/api/db';
import HashtagFactory from '../../../test-helpers/factories/hashtag';
import SchoolFactory from '../../../test-helpers/factories/school';
import GeotagFactory from '../../../test-helpers/factories/geotag';
import PostFactory from '../../../test-helpers/factories/post';
import UserFactory from '../../../test-helpers/factories/user';
import { createPost } from '../../../test-helpers/factories/post';
import { createUser } from '../../../test-helpers/factories/user';
import { login } from '../../../test-helpers/api';
import { knex } from '../../../test-helpers/db';


const bookshelf = initBookshelf($dbConfig);
const knex = bookshelf.knex;
const Post = bookshelf.model('Post');
const User = bookshelf.model('User');

describe('Post', () => {
describe('Not authenticated user', () => {
let user;
let post;

before(async () => {
const userAttrs = UserFactory.build();
user = await User.create(userAttrs.username, userAttrs.password, userAttrs.email);
post = await new Post(PostFactory.build({ user_id: user.id })).save(null, { method: 'insert' });
user = await createUser();
post = await createPost({ user_id: user.id });
});

after(async () => {
Expand Down Expand Up @@ -141,7 +132,7 @@ describe('Post', () => {
let postHashtagLike;

before(async () => {
postHashtagLike = await new Post(PostFactory.build({ type: 'hashtag_like' })).save(null, { method: 'insert' });
postHashtagLike = await createPost({ type: 'hashtag_like' });
});

after(async () => {
Expand All @@ -156,11 +147,24 @@ describe('Post', () => {
);
});
});
});

describe('Authenticated user', () => {
let user, otherUser, sessionId;

before(async () => {
user = await createUser();
sessionId = await login(user.get('username'), user.get('password'));
otherUser = await createUser();
});

after(async () => {
await user.destroy();
await otherUser.destroy();
});

describe('subscriptions', () => {
let post;
let user;
let sessionId;

async function countPostSubscriptions(user_id, post_id) {
const result = await knex('post_subscriptions').where({ user_id, post_id }).count();
Expand All @@ -169,15 +173,10 @@ describe('Post', () => {
}

before(async () => {
const userAttrs = UserFactory.build();
user = await new User().save(_.omit(userAttrs, 'password'), { method: 'insert', require: true });
sessionId = await login(userAttrs.username, userAttrs.password);

post = await new Post(PostFactory.build({ user_id: user.id })).save(null, { method: 'insert' });
post = await createPost({ user_id: user.id });
});

after(async () => {
await user.destroy();
await post.destroy();
});

Expand Down

0 comments on commit 0ad2ccf

Please sign in to comment.