Add integration tests for messages
This commit is contained in:
@@ -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")
|
||||
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user