diff --git a/tests/messaging/channel_test.go b/tests/messaging/channel_test.go index c471ee0ae..2a77ed4c9 100644 --- a/tests/messaging/channel_test.go +++ b/tests/messaging/channel_test.go @@ -63,7 +63,6 @@ func (h helper) chAssertNotMember(ch *types.Channel, u *sysTypes.User) { h.a.NoError(err) h.a.NotNil(mm) h.a.NotContains(mm.AllMemberIDs(), u.ID, "not expecting to find a member") - } func (h helper) chAssertMember(ch *types.Channel, u *sysTypes.User, typ types.ChannelMembershipType) { @@ -73,5 +72,4 @@ func (h helper) chAssertMember(ch *types.Channel, u *sysTypes.User, typ types.Ch h.a.NotNil(mm) h.a.NotNil(mm.FindByUserID(u.ID), "expecting to find a member") h.a.Equal(typ, mm.FindByUserID(u.ID).Type, "expecting to find a member") - } diff --git a/tests/messaging/message_create_test.go b/tests/messaging/message_create_test.go new file mode 100644 index 000000000..ad1fbd84f --- /dev/null +++ b/tests/messaging/message_create_test.go @@ -0,0 +1,37 @@ +package messaging + +import ( + "fmt" + "net/http" + "testing" + + jsonpath "github.com/steinfletcher/apitest-jsonpath" + + "github.com/cortezaproject/corteza-server/tests/helpers" +) + +func TestMessagesCreate(t *testing.T) { + h := newHelper(t) + ch := h.makePublicCh() + + rval := struct { + Response struct { + ID uint64 `json:"messageID,string"` + } + }{} + + h.testAPI(). + Post(fmt.Sprintf("/channels/%d/messages/", ch.ID)). + JSON(`{"message":"new message"}`). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present(`$.response.messageID`)). + Assert(jsonpath.Equal(`$.response.message`, `new message`)). + End(). + JSON(&rval) + + m := h.msgExistingLoad(rval.Response.ID) + h.a.Equal(`new message`, m.Message) + +} diff --git a/tests/messaging/message_delete_test.go b/tests/messaging/message_delete_test.go new file mode 100644 index 000000000..ab92de2ff --- /dev/null +++ b/tests/messaging/message_delete_test.go @@ -0,0 +1,25 @@ +package messaging + +import ( + "fmt" + "net/http" + "testing" + + "github.com/cortezaproject/corteza-server/tests/helpers" +) + +func TestMessagesDelete(t *testing.T) { + h := newHelper(t) + msg := h.makeMessage("old", h.makePublicCh(), h.cUser) + + h.testAPI(). + Delete(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, msg.ID)). + JSON(`{"message":"new"}`). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + End() + + _, err := h.repoMessage().FindByID(msg.ID) + h.a.EqualError(err, "messaging.repository.MessageNotFound") +} diff --git a/tests/messaging/message_reply_test.go b/tests/messaging/message_reply_test.go new file mode 100644 index 000000000..73f7b7b76 --- /dev/null +++ b/tests/messaging/message_reply_test.go @@ -0,0 +1,40 @@ +package messaging + +import ( + "fmt" + "net/http" + "testing" + + jsonpath "github.com/steinfletcher/apitest-jsonpath" + + "github.com/cortezaproject/corteza-server/tests/helpers" +) + +func TestMessagesReply(t *testing.T) { + h := newHelper(t) + msg := h.makeMessage("old", h.makePublicCh(), h.cUser) + + rval := struct { + Response struct { + ID uint64 `json:"messageID,string"` + } + }{} + + h.testAPI(). + Debug(). + Post(fmt.Sprintf("/channels/%d/messages/%d/replies", msg.ChannelID, msg.ID)). + JSON(`{"message":"new reply"}`). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present(`$.response.messageID`)). + Assert(jsonpath.Present(`$.response.replyTo`)). + Assert(jsonpath.Equal(`$.response.message`, `new reply`)). + End(). + JSON(&rval) + + m := h.msgExistingLoad(rval.Response.ID) + h.a.Equal(`new reply`, m.Message) + h.a.Equal(msg.ID, m.ReplyTo) + +} diff --git a/tests/messaging/message_test.go b/tests/messaging/message_test.go new file mode 100644 index 000000000..7f1978194 --- /dev/null +++ b/tests/messaging/message_test.go @@ -0,0 +1,38 @@ +package messaging + +import ( + "context" + + "github.com/titpetric/factory" + + "github.com/cortezaproject/corteza-server/messaging/repository" + "github.com/cortezaproject/corteza-server/messaging/types" + sysTypes "github.com/cortezaproject/corteza-server/system/types" +) + +func (h helper) repoMessage() repository.MessageRepository { + var ( + ctx = context.Background() + db = factory.Database.MustGet("messaging").With(ctx) + ) + + return repository.Message(ctx, db) +} + +func (h helper) makeMessage(msg string, ch *types.Channel, u *sysTypes.User) *types.Message { + m, err := h.repoMessage().Create(&types.Message{ + Message: msg, + ChannelID: ch.ID, + UserID: u.ID, + }) + + h.a.NoError(err) + return m +} + +func (h helper) msgExistingLoad(ID uint64) *types.Message { + m, err := h.repoMessage().FindByID(ID) + h.a.NoError(err) + h.a.NotNil(m) + return m +} diff --git a/tests/messaging/message_update_test.go b/tests/messaging/message_update_test.go new file mode 100644 index 000000000..c94c0e22f --- /dev/null +++ b/tests/messaging/message_update_test.go @@ -0,0 +1,28 @@ +package messaging + +import ( + "fmt" + "net/http" + "testing" + + jsonpath "github.com/steinfletcher/apitest-jsonpath" + + "github.com/cortezaproject/corteza-server/tests/helpers" +) + +func TestMessagesUpdate(t *testing.T) { + h := newHelper(t) + msg := h.makeMessage("old", h.makePublicCh(), h.cUser) + + h.testAPI(). + Put(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, msg.ID)). + JSON(`{"message":"new"}`). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal(`$.response.message`, `new`)). + End() + + m := h.msgExistingLoad(msg.ID) + h.a.Equal(`new`, m.Message) +}