Improve tests for message deleting

This commit is contained in:
Tomaž Jerman
2019-11-20 12:43:00 +01:00
parent 3c68b3b770
commit 16f89ad6b8
2 changed files with 130 additions and 0 deletions
+97
View File
@@ -5,6 +5,8 @@ import (
"net/http"
"testing"
"github.com/cortezaproject/corteza-server/messaging/types"
sysTypes "github.com/cortezaproject/corteza-server/system/types"
"github.com/cortezaproject/corteza-server/tests/helpers"
)
@@ -22,3 +24,98 @@ func TestMessagesDelete(t *testing.T) {
_, err := h.repoMessage().FindByID(msg.ID)
h.a.EqualError(err, "messaging.repository.MessageNotFound")
}
func TestMessagesDelete_forbiden(t *testing.T) {
h := newHelper(t)
msg := h.repoMakeMessage("old", h.repoMakePublicCh(), h.cUser)
h.deny(types.ChannelPermissionResource.AppendWildcard(), "message.update.own")
h.apiInit().
Delete(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, msg.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("messaging.service.NoPermissions")).
End()
_, err := h.repoMessage().FindByID(msg.ID)
h.a.Nil(err)
}
func TestMessagesDeleteOwnThreadMessage(t *testing.T) {
// Covers deleting messages that reply to my own thread
h := newHelper(t)
msg := h.repoMakeMessage("old", h.repoMakePublicCh(), h.cUser)
thrMsg := h.apiMessageCreateReply("thr1", msg)
h.apiInit().
Delete(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, thrMsg.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
_, err := h.repoMessage().FindByID(thrMsg.ID)
h.a.EqualError(err, "messaging.repository.MessageNotFound")
}
func TestMessagesDeleteOwnThreadMessage_forbidenNotOwner(t *testing.T) {
// Covers deleting someone elses messages that reply to my own thread
h := newHelper(t)
msg := h.repoMakeMessage("old", h.repoMakePublicCh(), h.cUser)
nh := newHelper(t)
thrMsg := nh.apiMessageCreateReply("thr1", msg)
h.apiInit().
Delete(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, thrMsg.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("messaging.service.NoPermissions")).
End()
_, err := h.repoMessage().FindByID(thrMsg.ID)
h.a.Nil(err)
}
func TestMessagesDeleteThreadMessage(t *testing.T) {
// Covers deleting messages that reply to someone elses thread
h := newHelper(t)
u := &sysTypes.User{ID: 10}
msg := h.repoMakeMessage("old", h.repoMakePublicCh(), u)
thrMsg := h.apiMessageCreateReply("thr1", msg)
h.apiInit().
Delete(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, thrMsg.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
End()
_, err := h.repoMessage().FindByID(thrMsg.ID)
h.a.EqualError(err, "messaging.repository.MessageNotFound")
}
func TestMessagesDeleteThreadMessage_forbidenNotOwner(t *testing.T) {
// Covers deleting someone elses messages that reply to someone elses thread
h := newHelper(t)
u := &sysTypes.User{ID: 10}
msg := h.repoMakeMessage("old", h.repoMakePublicCh(), u)
nh := newHelper(t)
thrMsg := nh.apiMessageCreateReply("thr1", msg)
h.apiInit().
Delete(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, thrMsg.ID)).
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("messaging.service.NoPermissions")).
End()
_, err := h.repoMessage().FindByID(thrMsg.ID)
h.a.Nil(err)
}
+33
View File
@@ -2,12 +2,24 @@ package messaging
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/cortezaproject/corteza-server/messaging/repository"
"github.com/cortezaproject/corteza-server/messaging/types"
sysTypes "github.com/cortezaproject/corteza-server/system/types"
)
type (
stdMessageResponse struct {
Response struct {
MessageID string `json:"messageID"`
} `json:"response"`
}
)
func (h helper) repoMessage() repository.MessageRepository {
return repository.Message(context.Background(), db())
}
@@ -26,6 +38,27 @@ func (h helper) repoMakeMessage(msg string, ch *types.Channel, u *sysTypes.User)
return m
}
func (h helper) apiMessageCreateReply(msg string, o *types.Message) *types.Message {
rsp := h.apiInit().
Post(fmt.Sprintf("/channels/%d/messages/%d/replies", o.ChannelID, o.ID)).
JSON(fmt.Sprintf(`{"message": "%s"}`, msg)).
Expect(h.t).
Status(http.StatusOK).
End()
thrMsg := stdMessageResponse{}
if err := json.NewDecoder(rsp.Response.Body).Decode(&thrMsg); err != nil {
h.a.Fail(err.Error())
}
msgID, err := strconv.ParseInt(thrMsg.Response.MessageID, 10, 64)
h.a.Nil(err)
return &types.Message{
ID: uint64(msgID),
Type: types.MessageTypeSimpleMessage,
}
}
func (h helper) repoMsgExistingLoad(ID uint64) *types.Message {
m, err := h.repoMessage().FindByID(ID)
h.a.NoError(err)