Skip to content

Commit

Permalink
ft(role-based): get single user by username
Browse files Browse the repository at this point in the history
Admins can get single user by username

[(Delivers) #167907830]
  • Loading branch information
encodedBicoding committed Aug 14, 2019
1 parent 784a277 commit 0e5b809
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
22 changes: 22 additions & 0 deletions server/controllers/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ class AdminController {
});
return successStat(res, 200, 'message', 'Comment deleted successfully');
}

/**
* @static
* @description gets a single user
* @param {*} req - Request Object
* @param {*} res - Response Object
* @returns {object} - user details containing active status
*/
static async getASingleUser(req, res) {
const { username } = req.params;
const foundUser = await models.User.findOne(
{
where: {
username
},
attributes: ['isActive', 'image', 'username', 'firstname', 'lastname', 'bio']
}
);
if (!foundUser) return errorStat(res, 404, 'User not found');

return successStat(res, 200, 'data', foundUser);
}
}

export default AdminController;
34 changes: 31 additions & 3 deletions server/docs/ah-commando-doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,12 @@ paths:
name: limit
schema:
type: integer
description: The number of articles to return
description: user can get all articles
parameters:
- in: query
name: searchQuery
schema:
type: string
description: The number of articles to return
description: user can get all articles
responses:
'200':
description: successfully view all articles
Expand Down Expand Up @@ -1677,6 +1676,35 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/errorResponse"
/admin/getUser/{username}:
get:
tags:
- Admin - Role based
security:
- bearerAuth: []
summary: get a single user by username
description: Admin can get single user by username
parameters:
- in: path
name: username
schema:
type: string
required: true
responses:
'200':
description: successfully view a single user found by username
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: "#/components/schemas/errorResponse"
'500':
description: Server error
content:
application/json:
schema:
$ref: "#/components/schemas/errorResponse"
components:
securitySchemes:
bearerAuth:
Expand Down
3 changes: 2 additions & 1 deletion server/routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
} = middlewares;

const {
assignRole, setActiveStatus, deleteAUser, deleteAnArticle, deleteAComment
assignRole, setActiveStatus, deleteAUser, deleteAnArticle, deleteAComment, getASingleUser
} = AdminController;
const adminRoute = express();

Expand All @@ -17,5 +17,6 @@ adminRoute.put('/setActiveStatus/:username', verifyToken, isActive, isJustAUser,
adminRoute.delete('/deleteUser/:username', verifyToken, isActive, isJustAUser, validateParamsInput, deleteAUser);
adminRoute.delete('/deleteArticle/:id', verifyToken, isActive, isJustAUser, validateParamsInput, deleteAnArticle);
adminRoute.delete('/deleteComment/:id', verifyToken, isActive, isJustAUser, validateParamsInput, deleteAComment);
adminRoute.get('/getUser/:username', verifyToken, isActive, isJustAUser, validateParamsInput, getASingleUser);

export default adminRoute;
2 changes: 0 additions & 2 deletions server/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ userRoute.get('/google/callback', passport.authenticate('google', { session: fal
userRoute.get('/facebook', passport.authenticate('facebook', { scope: ['email'] }));
userRoute.get('/facebook/callback', passport.authenticate('facebook', { session: false }), socialSignin);

userRoute.post('/logout', verifyToken, logout);

userRoute.get('/confirmEmail', confirmEmail);

userRoute.post('/logout', verifyToken, logout);
Expand Down
26 changes: 26 additions & 0 deletions server/tests/admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,30 @@ describe('Test admin functionality', () => {
});
});
});
describe('Handle admin get single user', () => {
it('Should fail if username is not found in the platform', (done) => {
chai
.request(app)
.get(`${baseUrl}/admin/getUser/idonotexist`)
.set('Authorization', godToken)
.end((err, res) => {
const { status, error } = res.body;
expect(status).to.equal(404);
expect(error).to.equal('User not found');
done();
});
});
it('Should pass if username is found', (done) => {
chai
.request(app)
.get(`${baseUrl}/admin/getUser/lundii`)
.set('Authorization', godToken)
.end((err, res) => {
const { status, data } = res.body;
expect(status).to.equal(200);
expect(data).to.be.an('object');
done();
});
});
});
});

0 comments on commit 0e5b809

Please sign in to comment.