Skip to content

Commit

Permalink
Merge 6c3791e into ae90a99
Browse files Browse the repository at this point in the history
  • Loading branch information
rwajon committed Aug 6, 2019
2 parents ae90a99 + 6c3791e commit b3072b6
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/controllers/AuthLocalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class AuthLocalController {
}

/**
* @description methode to find one user
* @description method to find one user
* @param {object} req user request object
* @param {object} res response object from server
* @returns {object} return all users
Expand Down
4 changes: 1 addition & 3 deletions src/controllers/AuthPassportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export default class AuthPassportController {
if (profile.emails) {
user.email = profile.emails[0].value;
}
if (profile.username) {
user.username = profile.username;
}
user.username = profile.username || `${user.firstName}.${user.lastName}`;
return Object.keys(profile).length
? {
...user,
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/ReportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export default class ReportController {
* @returns {object} a response to the client
*/
static async deleteSingle(req, res) {
const { articleSlug, reportId } = req.params;
const deleteSingle = await report.remove({ articleSlug, id: reportId });
const { reportId } = req.params;
const deleteSingle = await report.remove({ id: reportId });
return res
.status(status.OK)
.json({ message: 'Report deleted Successfully', report: deleteSingle, reportId });
Expand Down
20 changes: 20 additions & 0 deletions src/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ export default class UserController {
});
}

/**
* @param {object} req
* @param {object} res
* @return {object} return all users in database
*/
static async getAllByUsername(req, res) {
const { username } = req.params;
const { offset, limit } = req.query;
const users = await User.searchUsersByUsername(username, offset, limit);

return (
(users
&& users.length
&& res.status(status.OK).json({
users: users.map(user => delete user.get().password && user)
}))
|| res.status(status.NOT_FOUND).json({ errors: { user: 'no user with this username found' } })
);
}

/**
* Make a user an admin
* @param {Object} req express request object
Expand Down
2 changes: 2 additions & 0 deletions src/queries/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import update from './updateUser';
import findOrCreate from './findOrCreateUser';
import * as permissions from '../permissions';
import getAllUser from './getAllUser';
import searchUsersByUsername from './searchUsersByUsername';
import * as follow from '../follows';

export {
create, findOne, update, findOrCreate
};
export { getAllUser, permissions, follow };
export { searchUsersByUsername };
17 changes: 17 additions & 0 deletions src/queries/users/searchUsersByUsername.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import db from '../../models';
import { dbFindAll } from '../../helpers/queryHelper';

export default async (username, offset = 0, limit = 50) => dbFindAll(
db.User,
{
[db.Op.and]: [
{
username: {
[db.Op.iLike]: `${username}%`
}
}
]
},
offset,
limit
);
1 change: 0 additions & 1 deletion src/routes/api/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ router.get(
router.delete(
'/:articleSlug/report/:reportId',
verifyToken,
checkArticle,
checkReportExist,
asyncHandler(ReportController.deleteSingle)
);
Expand Down
8 changes: 8 additions & 0 deletions src/routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ router.put(
checkUpdateUser,
UserController.update
); // update user profile

router.get(
'/username/:username',
verifyToken,
verifyAdmin,
checkUpdateUserPermission,
UserController.getAllByUsername
);
router.get('/email/confirm/:token', verifyToken, UserController.confirmEmailUpdate); // confirm email update
router.get('/authors', verifyToken, asyncHandler(UserController.getAllAuthors));
router.put(
Expand Down
2 changes: 1 addition & 1 deletion src/tests/controllers/AuthPassportController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('Passport Authentication controller', () => {
chai
.request(app)
.post('/api/v1/auth')
.send({ ...userFacebook, provider: 'another' })
.send({ ...userFacebook, provider: 'another', username: 'username' })
.end((err, res) => {
expect(res).to.redirect;
expect(res.redirects[0].indexOf('email=409')).to.be.above(0);
Expand Down
26 changes: 26 additions & 0 deletions src/tests/routes/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,30 @@ describe('Users routes', () => {
done();
});
});

it('return all users whose username include the provided characters', (done) => {
chai
.request(app)
.get(`/api/v1/users/username/${createdUserTwo.username}?limit=1&offset=0`)
.set('access-token', accessTokenAdmin)
.end((err, res) => {
res.body.should.be.an('object');
expect(res.body).to.include.keys('users');
expect(res.body.users.length).to.be.greaterThan(0);
res.status.should.be.equal(status.OK);
done();
});
});

it('should not return all users if no user with the provided username is found ', (done) => {
chai
.request(app)
.get('/api/v1/users/username/fake-username?limit=1&offset=0')
.set('access-token', accessTokenAdmin)
.end((err, res) => {
res.body.should.be.an('object');
res.status.should.be.equal(status.NOT_FOUND);
done();
});
});
});

0 comments on commit b3072b6

Please sign in to comment.