summaryrefslogtreecommitdiffstats
path: root/api4/reaction_test.go
diff options
context:
space:
mode:
authorSaturnino Abril <saturnino.abril@gmail.com>2017-04-22 21:52:03 +0900
committerHarrison Healey <harrisonmhealey@gmail.com>2017-04-22 08:52:03 -0400
commitecb10ed62fdff179e34f82b0ff2569da8390f4ad (patch)
treee2405ed87a31cca42275b98a76e1312c0a1867eb /api4/reaction_test.go
parente62afeace04e2abd23fa78a0a54e0a5d2e17e0b7 (diff)
downloadchat-ecb10ed62fdff179e34f82b0ff2569da8390f4ad.tar.gz
chat-ecb10ed62fdff179e34f82b0ff2569da8390f4ad.tar.bz2
chat-ecb10ed62fdff179e34f82b0ff2569da8390f4ad.zip
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
Diffstat (limited to 'api4/reaction_test.go')
-rw-r--r--api4/reaction_test.go124
1 files changed, 124 insertions, 0 deletions
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")
+ }
+}