-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests and error handling for delete and put routes
- Loading branch information
1 parent
8fc7752
commit 1486882
Showing
8 changed files
with
216 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// tests/unit/delete.test.js | ||
|
||
const request = require('supertest'); | ||
const app = require('../../src/app'); | ||
|
||
// jest.mock('../../src/model/fragment'); | ||
|
||
describe('DELETE /v1/fragments/:id', () => { | ||
// If the request is missing the Authorization header, it should be forbidden | ||
test('unauthenticated requests are denied', () => | ||
request(app).delete('/v1/fragments/1').expect(401) | ||
); | ||
|
||
// If the wrong username/password pair are invalid, it should not work | ||
test('incorrect credentials are denied', () => | ||
request(app).delete('/v1/fragments/1').auth('hey@email.com', 'badpassweord').expect(401) | ||
); | ||
|
||
// If the id is not found, it should return a 404 error | ||
test('fragment not found returns 404', async () => { | ||
|
||
const res = await request(app) | ||
.delete('/v1/fragments/doesntexistid') | ||
.auth('user1@email.com', 'password1'); | ||
|
||
expect(res.statusCode).toBe(404); | ||
expect(res.body.status).toBe('error'); | ||
expect(res.body.error.message).toBe('Fragment not found'); | ||
}); | ||
|
||
// Authenticated users can delete a fragment successfully | ||
test('authenticated users can delete a fragment successfully', async () => { | ||
// create a fragment | ||
const postRequest = await request(app) | ||
.post('/v1/fragments') | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
.send(Buffer.from('yoooooo')); | ||
|
||
const fragmentId = postRequest.body.fragment.id; | ||
|
||
// delete the fragment | ||
const deleteResponse = await request(app) | ||
.delete(`/v1/fragments/${fragmentId}`) | ||
.auth('user1@email.com', 'password1'); | ||
|
||
expect(deleteResponse.statusCode).toBe(200); | ||
expect(deleteResponse.body.status).toBe('ok'); | ||
|
||
// Verify the fragment no longer exists | ||
const getResponse = await request(app) | ||
.get(`/v1/fragments/${fragmentId}`) | ||
.auth('user1@email.com', 'password1'); | ||
|
||
expect(getResponse.statusCode).toBe(404); | ||
expect(getResponse.body.status).toBe('error'); | ||
expect(getResponse.body.error.message).toBe('Fragment not found'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// tests/unit/put.test.js | ||
|
||
const request = require('supertest'); | ||
const app = require('../../src/app'); | ||
|
||
describe('PUT /v1/fragments/:id', () => { | ||
// Unauthenticated requests are denied | ||
test('unauthenticated requests are denied', () => | ||
request(app).put('/v1/fragments/1').expect(401) | ||
); | ||
|
||
// Requests with incorrect credentials are denied | ||
test('incorrect credentials are denied', () => | ||
request(app).put('/v1/fragments/1').auth('invalid@email.com', 'incorrect_password').expect(401) | ||
); | ||
|
||
// Fragment not found returns 404 | ||
test('fragment not found returns 404', async () => { | ||
const res = await request(app) | ||
.put('/v1/fragments/blahblahblah') | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
.send(Buffer.from('updated data')); | ||
|
||
expect(res.statusCode).toBe(404); | ||
expect(res.body.status).toBe('error'); | ||
expect(res.body.error.message).toBe('Fragment not found'); | ||
}); | ||
|
||
// Unsupported media type returns 415 | ||
test('unsupported media type returns 415', async () => { | ||
// First, create a fragment with a supported type | ||
const postRequest = await request(app) | ||
.post('/v1/fragments') | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
.send(Buffer.from('hello world')); | ||
|
||
const fragmentId = postRequest.body.fragment.id; | ||
|
||
// Attempt to update with an unsupported media type | ||
const res = await request(app) | ||
.put(`/v1/fragments/${fragmentId}`) | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'application/unsupported') | ||
.send(Buffer.from('updated data')); | ||
|
||
expect(res.statusCode).toBe(415); | ||
expect(res.body.status).toBe('error'); | ||
expect(res.body.error.message).toBe('unsupported media type'); | ||
}); | ||
|
||
|
||
// Fragment type mismatch returns 400 | ||
test('fragment type mismatch returns 400', async () => { | ||
|
||
const postRequest = await request(app) | ||
.post('/v1/fragments') | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
.send(Buffer.from('hello world')); | ||
|
||
const fragmentId = postRequest.body.fragment.id; | ||
|
||
const res = await request(app) | ||
.put(`/v1/fragments/${fragmentId}`) | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/html') | ||
.send(Buffer.from('<p>updated data</p>')); | ||
|
||
expect(res.statusCode).toBe(400); | ||
expect(res.body.status).toBe('error'); | ||
expect(res.body.error.message).toBe('Fragment type did not match'); | ||
}); | ||
|
||
// No buffer found in request body returns 400 | ||
test('no buffer found in request body returns 400', async () => { | ||
|
||
const postRequest = await request(app) | ||
.post('/v1/fragments') | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
.send(Buffer.from('hello world')); | ||
|
||
const fragmentId = postRequest.body.fragment.id; | ||
|
||
const res = await request(app) | ||
.put(`/v1/fragments/${fragmentId}`) | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
|
||
expect(res.statusCode).toBe(400); | ||
expect(res.body.status).toBe('error'); | ||
expect(res.body.error.message).toBe('no buffer found in request body'); | ||
}); | ||
|
||
|
||
// Successful fragment update | ||
test('authenticated users can update a fragment successfully', async () => { | ||
const postRequest = await request(app) | ||
.post('/v1/fragments') | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
.send(Buffer.from('hello world')); | ||
|
||
const fragmentId = postRequest.body.fragment.id; | ||
|
||
// Update the fragment | ||
const res = await request(app) | ||
.put(`/v1/fragments/${fragmentId}`) | ||
.auth('user1@email.com', 'password1') | ||
.set('Content-Type', 'text/plain') | ||
.send(Buffer.from('updated data')); | ||
|
||
expect(res.statusCode).toBe(200); | ||
expect(res.body.status).toBe('ok'); | ||
expect(res.body.fragment.id).toBe(fragmentId); | ||
expect(res.body.fragment.type).toBe('text/plain'); | ||
expect(res.body.fragment.size).toBe(Buffer.from('updated data').length); | ||
}); | ||
}); |