summaryrefslogtreecommitdiffstats
path: root/api4
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2017-04-17 16:07:28 +0200
committerHarrison Healey <harrisonmhealey@gmail.com>2017-04-17 10:07:28 -0400
commit80684ad69f641bb759095beff0e1a15db0aa33b1 (patch)
treeaecc0fd9fe73bc10f28467aa1f3d7194bb043e41 /api4
parent62974f19cd0025a3a5098741210f1b61b08765a9 (diff)
downloadchat-80684ad69f641bb759095beff0e1a15db0aa33b1.tar.gz
chat-80684ad69f641bb759095beff0e1a15db0aa33b1.tar.bz2
chat-80684ad69f641bb759095beff0e1a15db0aa33b1.zip
implement DELETE /emoji/{emoji_id} fro apiV4 (#6021)
implement GET /emoji/{emoji_id} for apiv4
Diffstat (limited to 'api4')
-rw-r--r--api4/context.go11
-rw-r--r--api4/emoji.go48
-rw-r--r--api4/emoji_test.go112
3 files changed, 170 insertions, 1 deletions
diff --git a/api4/context.go b/api4/context.go
index 90f39ce5f..0566fbc23 100644
--- a/api4/context.go
+++ b/api4/context.go
@@ -405,6 +405,17 @@ func (c *Context) RequireReportId() *Context {
return c
}
+func (c *Context) RequireEmojiId() *Context {
+ if c.Err != nil {
+ return c
+ }
+
+ if len(c.Params.EmojiId) != 26 {
+ c.SetInvalidUrlParam("emoji_id")
+ }
+ return c
+}
+
func (c *Context) RequireTeamName() *Context {
if c.Err != nil {
return c
diff --git a/api4/emoji.go b/api4/emoji.go
index 2452f87c4..ff4919860 100644
--- a/api4/emoji.go
+++ b/api4/emoji.go
@@ -20,6 +20,8 @@ func InitEmoji() {
BaseRoutes.Emojis.Handle("", ApiSessionRequired(createEmoji)).Methods("POST")
BaseRoutes.Emojis.Handle("", ApiSessionRequired(getEmojiList)).Methods("GET")
+ BaseRoutes.Emoji.Handle("", ApiSessionRequired(deleteEmoji)).Methods("DELETE")
+ BaseRoutes.Emoji.Handle("", ApiSessionRequired(getEmoji)).Methods("GET")
}
func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -81,3 +83,49 @@ func getEmojiList(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.EmojiListToJson(listEmoji)))
}
}
+
+func deleteEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireEmojiId()
+ if c.Err != nil {
+ return
+ }
+
+ emoji, err := app.GetEmoji(c.Params.EmojiId)
+ if err != nil {
+ c.Err = err
+ return
+ }
+
+ if c.Session.UserId != emoji.CreatorId && !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
+ c.Err = model.NewAppError("deleteImage", "api.emoji.delete.permissions.app_error", nil, "user_id="+c.Session.UserId, http.StatusUnauthorized)
+ return
+ }
+
+ err = app.DeleteEmoji(emoji)
+ if err != nil {
+ c.Err = err
+ return
+ } else {
+ ReturnStatusOK(w)
+ }
+}
+
+func getEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireEmojiId()
+ if c.Err != nil {
+ return
+ }
+
+ if !*utils.Cfg.ServiceSettings.EnableCustomEmoji {
+ c.Err = model.NewAppError("getEmoji", "api.emoji.disabled.app_error", nil, "", http.StatusNotImplemented)
+ return
+ }
+
+ emoji, err := app.GetEmoji(c.Params.EmojiId)
+ if err != nil {
+ c.Err = err
+ return
+ } else {
+ w.Write([]byte(emoji.ToJson()))
+ }
+}
diff --git a/api4/emoji_test.go b/api4/emoji_test.go
index 40199919f..23188a3d2 100644
--- a/api4/emoji_test.go
+++ b/api4/emoji_test.go
@@ -185,6 +185,116 @@ func TestGetEmojiList(t *testing.T) {
}
}
- // ADD delete test when create the delete endpoint
+ _, resp = Client.DeleteEmoji(emojis[0].Id)
+ CheckNoError(t, resp)
+ listEmoji, resp = Client.GetEmojiList()
+ CheckNoError(t, resp)
+ found := false
+ for _, savedEmoji := range listEmoji {
+ if savedEmoji.Id == emojis[0].Id {
+ found = true
+ break
+ }
+ if found {
+ t.Fatalf("should not get a deleted emoji %v", emojis[0].Id)
+ }
+ }
+
+}
+
+func TestDeleteEmoji(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+
+ EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
+ defer func() {
+ *utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
+ }()
+ *utils.Cfg.ServiceSettings.EnableCustomEmoji = true
+
+ emoji := &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+
+ ok, resp := Client.DeleteEmoji(newEmoji.Id)
+ CheckNoError(t, resp)
+ if ok != true {
+ t.Fatal("should return true")
+ } else {
+ _, err := Client.GetEmoji(newEmoji.Id)
+ if err == nil {
+ t.Fatal("should not return the emoji it was deleted")
+ }
+ }
+
+ //Admin can delete other users emoji
+ newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+
+ ok, resp = th.SystemAdminClient.DeleteEmoji(newEmoji.Id)
+ CheckNoError(t, resp)
+ if ok != true {
+ t.Fatal("should return true")
+ } else {
+ _, err := th.SystemAdminClient.GetEmoji(newEmoji.Id)
+ if err == nil {
+ t.Fatal("should not return the emoji it was deleted")
+ }
+ }
+
+ // Try to delete just deleted emoji
+ _, resp = Client.DeleteEmoji(newEmoji.Id)
+ CheckInternalErrorStatus(t, resp)
+
+ //Try to delete non-existing emoji
+ _, resp = Client.DeleteEmoji(model.NewId())
+ CheckInternalErrorStatus(t, resp)
+
+ //Try to delete without Id
+ _, resp = Client.DeleteEmoji("")
+ CheckNotFoundStatus(t, resp)
+
+ //Try to delete other user's custom emoji
+ newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+
+ Client.Logout()
+ th.LoginBasic2()
+ ok, resp = Client.DeleteEmoji(newEmoji.Id)
+ CheckUnauthorizedStatus(t, resp)
+}
+
+func TestGetEmoji(t *testing.T) {
+ th := Setup().InitBasic()
+ defer TearDown()
+ Client := th.Client
+
+ EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
+ defer func() {
+ *utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
+ }()
+ *utils.Cfg.ServiceSettings.EnableCustomEmoji = true
+
+ emoji := &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+
+ emoji, resp = Client.GetEmoji(newEmoji.Id)
+ CheckNoError(t, resp)
+ if emoji.Id != newEmoji.Id {
+ t.Fatal("wrong emoji was returned")
+ }
+
+ _, resp = Client.GetEmoji(model.NewId())
+ CheckInternalErrorStatus(t, resp)
}