From ecb10ed62fdff179e34f82b0ff2569da8390f4ad Mon Sep 17 00:00:00 2001 From: Saturnino Abril Date: Sat, 22 Apr 2017 21:52:03 +0900 Subject: APIv4 DELETE /users/{user_id}/posts/{post_id}/reactions/name (#6117) * APIv4 DELETE /users/{user_id}/posts/{post_id}/reactions/name * updated v3 deleteReaction endpoint * update parameter of app.DeleteReactionForPost() * update utils.IsValidAlphaNum, add utils.IsValidAlphaNumHyphenUnderscore, and add related tests --- api4/reaction_test.go | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) (limited to 'api4/reaction_test.go') diff --git a/api4/reaction_test.go b/api4/reaction_test.go index 980a96d68..b80c96118 100644 --- a/api4/reaction_test.go +++ b/api4/reaction_test.go @@ -193,3 +193,127 @@ func TestGetReactions(t *testing.T) { _, resp = th.SystemAdminClient.GetReactions(postId) CheckNoError(t, resp) } + +func TestDeleteReaction(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + userId := th.BasicUser.Id + user2Id := th.BasicUser2.Id + postId := th.BasicPost.Id + + r1 := &model.Reaction{ + UserId: userId, + PostId: postId, + EmojiName: "smile", + } + + app.SaveReactionForPost(r1) + if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 1 { + t.Fatal("didn't save reaction correctly") + } + + ok, resp := Client.DeleteReaction(r1) + CheckNoError(t, resp) + + if !ok { + t.Fatal("should have returned true") + } + + if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 0 { + t.Fatal("should have deleted reaction") + } + + // deleting one reaction when a post has multiple reactions + r2 := &model.Reaction{ + UserId: userId, + PostId: postId, + EmojiName: "smile-", + } + + app.SaveReactionForPost(r1) + app.SaveReactionForPost(r2) + if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 2 { + t.Fatal("didn't save reactions correctly") + } + + _, resp = Client.DeleteReaction(r2) + CheckNoError(t, resp) + + if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 1 || *reactions[0] != *r1 { + t.Fatal("should have deleted 1 reaction only") + } + + // deleting a reaction made by another user + r3 := &model.Reaction{ + UserId: user2Id, + PostId: postId, + EmojiName: "smile_", + } + + th.LoginBasic2() + app.SaveReactionForPost(r3) + if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 2 { + t.Fatal("didn't save reaction correctly") + } + + th.LoginBasic() + + ok, resp = Client.DeleteReaction(r3) + CheckForbiddenStatus(t, resp) + + if ok { + t.Fatal("should have returned false") + } + + if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 2 { + t.Fatal("should have not deleted a reaction") + } + + r1.PostId = GenerateTestId() + _, resp = Client.DeleteReaction(r1) + CheckForbiddenStatus(t, resp) + + r1.PostId = "junk" + + _, resp = Client.DeleteReaction(r1) + CheckBadRequestStatus(t, resp) + + r1.PostId = postId + r1.UserId = GenerateTestId() + + _, resp = Client.DeleteReaction(r1) + CheckForbiddenStatus(t, resp) + + r1.UserId = "junk" + + _, resp = Client.DeleteReaction(r1) + CheckBadRequestStatus(t, resp) + + r1.UserId = userId + r1.EmojiName = "" + + _, resp = Client.DeleteReaction(r1) + CheckNotFoundStatus(t, resp) + + r1.EmojiName = strings.Repeat("a", 65) + + _, resp = Client.DeleteReaction(r1) + CheckBadRequestStatus(t, resp) + + Client.Logout() + r1.EmojiName = "smile" + + _, resp = Client.DeleteReaction(r1) + CheckUnauthorizedStatus(t, resp) + + _, resp = th.SystemAdminClient.DeleteReaction(r1) + CheckNoError(t, resp) + + _, resp = th.SystemAdminClient.DeleteReaction(r3) + CheckNoError(t, resp) + + if reactions, err := app.GetReactionsForPost(postId); err != nil || len(reactions) != 0 { + t.Fatal("should have deleted both reactions") + } +} -- cgit v1.2.3-1-g7c22