Skip to content

Commit

Permalink
add test for put data with same email
Browse files Browse the repository at this point in the history
relates #25
  • Loading branch information
rehabas committed Apr 2, 2020
1 parent c12f753 commit f31c5e7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server/controllers/routes/admin/student/putStudentData.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ const putStudentData = async (req, res, next) => {
} catch (err) {
if (err.errors) {
res.status(400).json({ statusCode: 400, data: { message: err.errors } });
} else if (err.detail) {
res.status(409).json({
statusCode: 409,
data: {
message: err.detail,
},
});
} else {
next(err);
}
Expand Down
60 changes: 60 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ describe('Admin, Put project', () => {
githubLink: 'github link',
cohortId: 1,
};
const duplicateData = {
name: 'Rehab',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
cohortId: 1,
};

test('PUT Route /alumni/1 status 200, json header, put data ', (done) => {
request(app)
Expand Down Expand Up @@ -219,6 +226,41 @@ describe('Admin, Put project', () => {
});
});

test('PUT Route /alumni/1 status 200, json header, put data with same email', (done) => {
request(app)
.put('/api/v1/alumni/1')
.send({
name: 'sara',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
cohortId: 1,
})
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
const { rows } = await connection.query(
'SELECT * from student WHERE id = 1',
);
expect(rows).toHaveLength(1);
expect(rows[0]).toEqual({
id: 1,
name: 'sara',
email: '[email protected]',
img_url:
'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
github_link: 'https://github.com/rehabas',
cohort_id: 1,
});
expect(message).toBe("Student's data updated successfully");
done();
});
});

test('PUT Route /alumni/11 status 404, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/11')
Expand Down Expand Up @@ -259,4 +301,22 @@ describe('Admin, Put project', () => {
done();
});
});

test('PUT Route /alumni/1 status 409, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/1')
.send(duplicateData)
.expect(409)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
expect(message).toBe(
`Key (email)=(${duplicateData.email}) already exists.`,
);
done();
});
});
});

0 comments on commit f31c5e7

Please sign in to comment.