Skip to content
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

Delete post #155

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fded4f6
Annc Initial Setup
csanann Jun 5, 2023
77f9897
Adds and Rechecks files and folders
csanann Jun 6, 2023
ff6658e
fixed failing cypress test in signing in
Jun 7, 2023
96bfb07
all stuff so far so cyryl can see it
Jun 7, 2023
e725fb9
test passes but needs to clear the db before ech test
Jun 7, 2023
63bef3d
added method on the frontend to make a new post, tested the frontend …
Jun 7, 2023
14119d6
added comments so viewers know whats happen
Jun 7, 2023
f84f221
Merge pull request #2 from cshjp/making-post-frontend
cshjp Jun 7, 2023
3d4f2e2
Merge pull request #1 from cshjp/ann-setup
cshjp Jun 7, 2023
74f7ddb
test added and passed
CyrylG Jun 8, 2023
c2d64d9
Created test for adding multiple posts through the post model
bwilton93 Jun 8, 2023
b298e7b
Created a test to find by a single post ID
bwilton93 Jun 8, 2023
a98f915
tested post model for findByIdAndUpdate()
bwilton93 Jun 8, 2023
196d20a
Not working yet
bwilton93 Jun 8, 2023
4814637
bunch of failed bs added
CyrylG Jun 8, 2023
9688d3e
username- implementation + tests pass- backend
Ormeline Jun 8, 2023
dbbe755
wip app.js and usernamepage.js
Ormeline Jun 8, 2023
da66f4a
Test for returning a single code, and code to satisfy test, implemented
bwilton93 Jun 9, 2023
5ce083c
Show single post is now working
bwilton93 Jun 9, 2023
a213437
updated
Jun 9, 2023
5fe20d0
changed .json to .send -receive plain text rspnse
Ormeline Jun 9, 2023
d7e4e00
auhtor added to posts
Jun 9, 2023
a409d0f
little bit of refactoring
Jun 9, 2023
0c7e515
Can update a post in the backend
bwilton93 Jun 9, 2023
33f2bc1
Started work on showing a single post on the front end
bwilton93 Jun 9, 2023
d02d1df
Started playing around with react
bwilton93 Jun 9, 2023
3eefc2b
Figured out how to extract the post_id from the params in react
bwilton93 Jun 9, 2023
ff5779e
Can extract the placeholder content to put in the form, so the user k…
bwilton93 Jun 9, 2023
173948b
Created frontend react component for updating a post - still need to …
bwilton93 Jun 9, 2023
ea83d64
Added update button to posts on feed. This doesn't check if the user …
bwilton93 Jun 9, 2023
5ef15b2
ready to merge
bwilton93 Jun 9, 2023
b737dc5
Merge pull request #5 from cshjp/update-posts-backend
bwilton93 Jun 9, 2023
80c06f3
Merge pull request #6 from cshjp/update-posts-frontend
bwilton93 Jun 9, 2023
aa49409
Ignore cypress videos & screenshots and remove checked in ones
neoeno Jun 9, 2023
589b38d
Merge branch 'main' into postcreationtest
bwilton93 Jun 12, 2023
fd64872
Merge pull request #4 from cshjp/postcreationtest
bwilton93 Jun 12, 2023
6384774
Merge pull request #8 from neoeno/remove-cypress-videos-and-screenshots
bwilton93 Jun 12, 2023
235ef51
adds delete function
Ormeline Jun 12, 2023
855dcee
Fixed failing tests for deleting posts, and creating new users
bwilton93 Jun 12, 2023
d918c80
Merge branch 'main' into delete-post
bwilton93 Jun 12, 2023
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
Prev Previous commit
Next Next commit
adds delete function
Ormeline committed Jun 12, 2023

Verified

This commit was signed with the committer’s verified signature.
lballabio Luigi Ballabio
commit 235ef5107c1850cbae40bfae8947ee0dd1d90e50
11 changes: 11 additions & 0 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -22,6 +22,17 @@ const PostsController = {
res.status(201).json({ message: 'OK', token: token });
});
},

Delete: async (req, res) => {
try {
const { post_id } = req.params;
await Post.deleteOne({ _id: post_id });
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(200).json({ message: 'Post deleted', token: token });
} catch (err) {
res.status(400).json({ message: 'Bad request' });
}
}
};

module.exports = PostsController;
3 changes: 3 additions & 0 deletions api/routes/posts.js
Original file line number Diff line number Diff line change
@@ -5,5 +5,8 @@ const PostsController = require("../controllers/posts");

router.get("/", PostsController.Index);
router.post("/", PostsController.Create);
router.delete("/:post_id", PostsController.Delete);



module.exports = router;
45 changes: 45 additions & 0 deletions api/spec/controllers/posts.spec.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ const JWT = require("jsonwebtoken");
const secret = process.env.JWT_SECRET;

let token;
let postId;

describe("/posts", () => {
beforeAll( async () => {
@@ -157,4 +158,48 @@ describe("/posts", () => {
expect(response.body.token).toEqual(undefined);
})
})

describe("DELETE, when token is present", () => {
beforeEach(async () => {
const post = new Post({ message: "Delete" });
await post.save();
postId = post._id;
});

test("deletes a post and returns a confirmation message", async () => {
let response = await request(app)
.delete(`/posts/${postId}`)
.set("Authorization", `Bearer ${token}`);
expect(response.status).toEqual(200);
expect(response.body.message).toEqual("Post deleted");
});
});

describe("DELETE, when token is missing", () => {
beforeEach(async () => {
const post = new Post({ message: "Delete" });
await post.save();
postId = post._id;
});

test("responds with a 401", async () => { //401 = unauthorised. It confirms that the server
// denies access to the delete function due to the missing token.
let response = await request(app)
.delete(`/posts/${postId}`);
expect(response.status).toEqual(401);
});

test("does not delete the post", async () => {
await request(app)
.delete(`/posts/${postId}`);
let post = await Post.findById(postId);
expect(post).not.toBeNull(); // if post is not null, it means the post was not deleted
});

test("returns a bad request message", async () => {
let response = await request(app)
.delete(`/posts/${postId}`);
expect(response.body.message).toEqual("Bad request");
});
});
});
23 changes: 23 additions & 0 deletions api/spec/models/post.spec.js
Original file line number Diff line number Diff line change
@@ -37,4 +37,27 @@ describe("Post model", () => {
});
});
});

it("can delete a post", (done) => {
var post = new Post({ message: "delete post" });

post.save((err, savedPost) => {
expect(err).toBeNull();

Post.findById(savedPost._id, (err, foundPost) => {
expect(err).toBeNull();
expect(foundPost).toBeDefined();

foundPost.deleteOne((err) => {
expect(err).toBeNull();

Post.find((err, posts) => {
expect(err).toBeNull();
expect(posts).toEqual([]);
done();
});
});
});
});
});
});