summaryrefslogtreecommitdiffstats
path: root/api4
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2017-04-06 17:15:57 +0200
committerJoram Wilander <jwawilander@gmail.com>2017-04-06 11:15:57 -0400
commit48d9099882db5dc9fd61c4820ba7e2aeb189e4f9 (patch)
tree343c133158900c54455c3a4651df2249f5a5ade2 /api4
parent1a09b2d07faef06c121319945639c46d1e3f5744 (diff)
downloadchat-48d9099882db5dc9fd61c4820ba7e2aeb189e4f9.tar.gz
chat-48d9099882db5dc9fd61c4820ba7e2aeb189e4f9.tar.bz2
chat-48d9099882db5dc9fd61c4820ba7e2aeb189e4f9.zip
implement POST /emoji for apiV4 (#5868)
Diffstat (limited to 'api4')
-rw-r--r--api4/api.go1
-rw-r--r--api4/apitestlib.go15
-rw-r--r--api4/emoji.go72
-rw-r--r--api4/emoji_test.go139
4 files changed, 227 insertions, 0 deletions
diff --git a/api4/api.go b/api4/api.go
index ea46c8ee5..6451e4102 100644
--- a/api4/api.go
+++ b/api4/api.go
@@ -176,6 +176,7 @@ func InitApi(full bool) {
InitCommand()
InitStatus()
InitWebSocket()
+ InitEmoji()
app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))
diff --git a/api4/apitestlib.go b/api4/apitestlib.go
index bd36f49cc..8b5861026 100644
--- a/api4/apitestlib.go
+++ b/api4/apitestlib.go
@@ -564,6 +564,21 @@ func CheckInternalErrorStatus(t *testing.T, resp *model.Response) {
}
}
+func CheckPayLoadTooLargeStatus(t *testing.T, resp *model.Response) {
+ if resp.Error == nil {
+ debug.PrintStack()
+ t.Fatal("should have errored with status:" + strconv.Itoa(http.StatusRequestEntityTooLarge))
+ return
+ }
+
+ if resp.StatusCode != http.StatusRequestEntityTooLarge {
+ debug.PrintStack()
+ t.Log("actual: " + strconv.Itoa(resp.StatusCode))
+ t.Log("expected: " + strconv.Itoa(http.StatusRequestEntityTooLarge))
+ t.Fatal("wrong status code")
+ }
+}
+
func readTestFile(name string) ([]byte, error) {
path := utils.FindDir("tests")
file, err := os.Open(path + "/" + name)
diff --git a/api4/emoji.go b/api4/emoji.go
new file mode 100644
index 000000000..a32436b64
--- /dev/null
+++ b/api4/emoji.go
@@ -0,0 +1,72 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api4
+
+import (
+ "net/http"
+ "strings"
+
+ l4g "github.com/alecthomas/log4go"
+
+ "github.com/mattermost/platform/app"
+ "github.com/mattermost/platform/einterfaces"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+func InitEmoji() {
+ l4g.Debug(utils.T("api.emoji.init.debug"))
+
+ BaseRoutes.Emojis.Handle("", ApiSessionRequired(createEmoji)).Methods("POST")
+}
+
+func createEmoji(c *Context, w http.ResponseWriter, r *http.Request) {
+ if !*utils.Cfg.ServiceSettings.EnableCustomEmoji {
+ c.Err = model.NewLocAppError("createEmoji", "api.emoji.disabled.app_error", nil, "")
+ c.Err.StatusCode = http.StatusNotImplemented
+ return
+ }
+
+ if emojiInterface := einterfaces.GetEmojiInterface(); emojiInterface != nil &&
+ !emojiInterface.CanUserCreateEmoji(c.Session.Roles, c.Session.TeamMembers) {
+ c.Err = model.NewLocAppError("createEmoji", "api.emoji.create.permissions.app_error", nil, "user_id="+c.Session.UserId)
+ c.Err.StatusCode = http.StatusUnauthorized
+ return
+ }
+
+ if len(utils.Cfg.FileSettings.DriverName) == 0 {
+ c.Err = model.NewLocAppError("createEmoji", "api.emoji.storage.app_error", nil, "")
+ c.Err.StatusCode = http.StatusNotImplemented
+ return
+ }
+
+ if r.ContentLength > app.MaxEmojiFileSize {
+ c.Err = model.NewLocAppError("createEmoji", "api.emoji.create.too_large.app_error", nil, "")
+ c.Err.StatusCode = http.StatusRequestEntityTooLarge
+ return
+ }
+
+ if err := r.ParseMultipartForm(app.MaxEmojiFileSize); err != nil {
+ c.Err = model.NewLocAppError("createEmoji", "api.emoji.create.parse.app_error", nil, err.Error())
+ c.Err.StatusCode = http.StatusBadRequest
+ return
+ }
+
+ m := r.MultipartForm
+ props := m.Value
+
+ emoji := model.EmojiFromJson(strings.NewReader(props["emoji"][0]))
+ if emoji == nil {
+ c.SetInvalidParam("createEmoji")
+ return
+ }
+
+ newEmoji, err := app.CreateEmoji(c.Session.UserId, emoji, m)
+ if err != nil {
+ c.Err = err
+ return
+ } else {
+ w.Write([]byte(newEmoji.ToJson()))
+ }
+}
diff --git a/api4/emoji_test.go b/api4/emoji_test.go
new file mode 100644
index 000000000..9db231d4a
--- /dev/null
+++ b/api4/emoji_test.go
@@ -0,0 +1,139 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api4
+
+import (
+ "testing"
+
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+func TestCreateEmoji(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 = false
+
+ emoji := &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ // try to create an emoji when they're disabled
+ _, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckNotImplementedStatus(t, resp)
+
+ *utils.Cfg.ServiceSettings.EnableCustomEmoji = true
+ // try to create a valid gif emoji when they're enabled
+ newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+ if newEmoji.Name != emoji.Name {
+ t.Fatal("create with wrong name")
+ }
+
+ // try to create an emoji with a duplicate name
+ emoji2 := &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: newEmoji.Name,
+ }
+ _, resp = Client.CreateEmoji(emoji2, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckBadRequestStatus(t, resp)
+ CheckErrorMessage(t, resp, "api.emoji.create.duplicate.app_error")
+
+ // try to create a valid animated gif emoji
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 10, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+ if newEmoji.Name != emoji.Name {
+ t.Fatal("create with wrong name")
+ }
+
+ // try to create a valid jpeg emoji
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestJpeg(t, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+ if newEmoji.Name != emoji.Name {
+ t.Fatal("create with wrong name")
+ }
+
+ // try to create a valid png emoji
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestPng(t, 10, 10), "image.gif")
+ CheckNoError(t, resp)
+ if newEmoji.Name != emoji.Name {
+ t.Fatal("create with wrong name")
+ }
+
+ // try to create an emoji that's too wide
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 1000, 10), "image.gif")
+ CheckNoError(t, resp)
+ if newEmoji.Name != emoji.Name {
+ t.Fatal("create with wrong name")
+ }
+
+ // try to create an emoji that's too tall
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 1000), "image.gif")
+ CheckNoError(t, resp)
+ if newEmoji.Name != emoji.Name {
+ t.Fatal("create with wrong name")
+ }
+
+ // try to create an emoji that's too large
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ _, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 100, 100, 10000), "image.gif")
+ if resp.Error == nil {
+ t.Fatal("should fail - emoji is too big")
+ }
+
+ // try to create an emoji with data that isn't an image
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser.Id,
+ Name: model.NewId(),
+ }
+
+ _, resp = Client.CreateEmoji(emoji, make([]byte, 100, 100), "image.gif")
+ CheckBadRequestStatus(t, resp)
+ CheckErrorMessage(t, resp, "api.emoji.upload.image.app_error")
+
+ // try to create an emoji as another user
+ emoji = &model.Emoji{
+ CreatorId: th.BasicUser2.Id,
+ Name: model.NewId(),
+ }
+
+ _, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
+ CheckForbiddenStatus(t, resp)
+}