Skip to content

feat(search): add populated search endpoint #328

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

Merged
merged 3 commits into from
Mar 5, 2020
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
4 changes: 4 additions & 0 deletions server/controllers/like.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import Like from '../models/like.model';
import Post from '../models/post.model';

function syncAlgolia(query) {
if (process.env.NODE_ENV === 'test') {
return;
}

const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY,
Expand Down
16 changes: 6 additions & 10 deletions server/controllers/post.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,9 @@ function search(req, res, next) {
let isEnd = false;
let nextPage = 0;
let slugs = [];
const {
query = '',
page = 0,
} = req.query;

const { query = '' } = req.query;
const page = parseInt(req.query.page || '0', 10);

const client = algoliasearch(
process.env.ALGOLIA_APP_ID,
Expand All @@ -262,12 +261,9 @@ function search(req, res, next) {
slugs = reply.hits.map(h => h.slug);

Post.list({ slugs })
.then((posts) => {
res.json({
posts: getAdFreePostsIfSubscribed(posts, req.fullUser, next),
isEnd,
nextPage
});
.then(async (posts) => {
posts = await getAdFreePostsIfSubscribed(posts, req.fullUser, next); //eslint-disable-line
res.json({ posts, isEnd, nextPage });
})
.catch(e => next(e));
})
Expand Down
6 changes: 3 additions & 3 deletions server/routes/post.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ router.route('/')

router.route('/search')
.get(
expressJwt({ secret: config.jwtSecret, credentialsRequired: false })
, loadFullUser
, postCtrl.search
expressJwt({ secret: config.jwtSecret, credentialsRequired: false }),
loadFullUser,
postCtrl.search,
);

router.route('/recommendations')
Expand Down
92 changes: 92 additions & 0 deletions server/tests/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import httpStatus from 'http-status';
import chai, { expect } from 'chai';
import app from '../../index';
import Post from '../models/post.model';
import User from '../models/user.model';

chai.config.includeStack = true;

Expand Down Expand Up @@ -115,4 +116,95 @@ describe('## Post APIs', () => {
.catch(done);
});
});

describe('# POST /api/posts/:postId/like', () => {
const validUserCredentials = {
username: 'react',
password: 'express'
};

let userToken;
let postId;

before((done) => {
request(app)
.post('/api/auth/register')
.send(validUserCredentials)
.expect(httpStatus.CREATED)
.then((res) => {
expect(res.body).to.have.property('token');
userToken = res.body.token;
const post = new Post();
return post.save();
})
.then((post) => {
postId = post._id;
done();
})
.catch(done);
});

after((done) => {
User.remove({})
.exec()
.then(() => Post.remove({}).exec())
.then(() => {
done();
});
});

it('errors when not logged in', (done) => {
request(app)
.post(`/api/posts/${postId}/like`)
.expect(httpStatus.UNAUTHORIZED)
.then((res) => {
expect(res.body).to.exist; //eslint-disable-line
done();
});
});

it('likes a post', (done) => {
request(app)
.post(`/api/posts/${postId}/like`)
.set('Authorization', `Bearer ${userToken}`)
.expect(httpStatus.OK)
.then((res) => {
const like = res.body;
expect(like).to.have.property('likeCount').that.is.a('number');
expect(like).to.have.property('likeActive').that.is.a('boolean');
expect(like.likeActive).to.be.true; //eslint-disable-line
done();
})
.catch(done);
});

it('toggles the like for a post', (done) => {
request(app)
.post(`/api/posts/${postId}/like`)
.set('Authorization', `Bearer ${userToken}`)
.expect(httpStatus.OK)
.then((res) => {
const like = res.body;
expect(like).to.have.property('likeCount').that.is.a('number');
expect(like).to.have.property('likeActive').that.is.a('boolean');
expect(like.likeActive).to.be.false; //eslint-disable-line
done();
})
.catch(done);
});
});

xdescribe('# GET /api/posts/search', () => {
it('returns search results', (done) => {
request(app)
.get('/api/posts/search?query=apple&page=0')
.then((res) => {
expect(res.body).to.exist; //eslint-disable-line
expect(res.body.posts).to.have.property('length'); //eslint-disable-line
expect(res.body).to.have.property('isEnd').that.is.a('boolean');
expect(res.body).to.have.property('nextPage', 1).that.is.a('number');
done();
});
});
});
});