From c1226e1f7aecf89bbb4c2ae40d09d2a27afbd600 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Mon, 9 Sep 2019 19:36:21 +0200 Subject: [PATCH] Improve tests, simplify helpers --- tests/messaging/channel_attach_test.go | 52 +++++++++++++++++++---- tests/messaging/channel_create_test.go | 55 ++++++++++++++++++++----- tests/messaging/channel_flags_test.go | 8 ++-- tests/messaging/channel_list_test.go | 2 +- tests/messaging/channel_members_test.go | 24 +++++------ tests/messaging/channel_state_test.go | 4 +- tests/messaging/channel_test.go | 10 ++--- tests/messaging/channel_update_test.go | 6 +-- tests/messaging/main_test.go | 2 +- tests/messaging/message_create_test.go | 6 +-- tests/messaging/message_delete_test.go | 5 +-- tests/messaging/message_reply_test.go | 7 ++-- tests/messaging/message_test.go | 4 +- tests/messaging/message_update_test.go | 6 +-- 14 files changed, 129 insertions(+), 62 deletions(-) diff --git a/tests/messaging/channel_attach_test.go b/tests/messaging/channel_attach_test.go index f1e5f32cc..a8a7bcd71 100644 --- a/tests/messaging/channel_attach_test.go +++ b/tests/messaging/channel_attach_test.go @@ -17,7 +17,7 @@ import ( "github.com/cortezaproject/corteza-server/tests/helpers" ) -func chAttach(h helper, ch *types.Channel, file []byte) *apitest.Response { +func (h helper) apiChAttach(ch *types.Channel, file []byte) *apitest.Response { body := &bytes.Buffer{} writer := multipart.NewWriter(body) part, err := writer.CreateFormFile("upload", "test.txt") @@ -27,8 +27,7 @@ func chAttach(h helper, ch *types.Channel, file []byte) *apitest.Response { h.a.NoError(err) h.a.NoError(writer.Close()) - return h.testAPI(). - Debug(). + return h.apiInit(). Post(fmt.Sprintf("/channels/%v/attach", ch.ID)). Body(body.String()). ContentType(writer.FormDataContentType()). @@ -40,9 +39,9 @@ func chAttach(h helper, ch *types.Channel, file []byte) *apitest.Response { func TestChannelAttachNotMember(t *testing.T) { h := newHelper(t) - ch := h.makePrivateCh() + ch := h.repoMakePrivateCh() - chAttach(h, ch, []byte("NOPE")). + h.apiChAttach(ch, []byte("NOPE")). Assert(helpers.AssertError("messaging.service.NoPermissions")). End() } @@ -52,10 +51,10 @@ func TestChannelAttach(t *testing.T) { uploadFileContent := "hello corteza, time here is " + time.Now().String() - ch := h.makePublicCh() - h.makeMember(ch, h.cUser) + ch := h.repoMakePublicCh() + h.repoMakeMember(ch, h.cUser) - out := chAttach(h, ch, []byte(uploadFileContent)). + out := h.apiChAttach(ch, []byte(uploadFileContent)). Assert(helpers.AssertNoErrors). Assert(jsonpath.Present(`$.response.url`)). Assert(jsonpath.Present(`$.response.previewUrl`)). @@ -72,7 +71,7 @@ func TestChannelAttach(t *testing.T) { attUrl, err := url.Parse(rval.Response.Url) assert.NoError(t, err) - h.testAPI(). + h.apiInit(). Get(attUrl.Path). Query("sign", attUrl.Query().Get("sign")). Query("userID", attUrl.Query().Get("userID")). @@ -81,3 +80,38 @@ func TestChannelAttach(t *testing.T) { Body(uploadFileContent). End() } + +func TestChannelAttachAndDelete(t *testing.T) { + h := newHelper(t) + + ch := h.repoMakePublicCh() + h.repoMakeMember(ch, h.cUser) + + h.apiChAttach(ch, []byte("dummy")). + Assert(helpers.AssertNoErrors). + End() + + var rval = struct { + Response []struct { + MessageID string + } + }{} + + h.apiInit(). + Get("/search/messages"). + Query("channelID", fmt.Sprintf("%d", ch.ID)). + Expect(t). + Assert(helpers.AssertNoErrors). + End(). + JSON(&rval) + + h.a.Len(rval.Response, 1) + + h.apiInit(). + Delete(fmt.Sprintf("/channels/%d/messages/%s", ch.ID, rval.Response[0].MessageID)). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + End() + +} diff --git a/tests/messaging/channel_create_test.go b/tests/messaging/channel_create_test.go index ba2343ebb..9a8b5f046 100644 --- a/tests/messaging/channel_create_test.go +++ b/tests/messaging/channel_create_test.go @@ -1,25 +1,43 @@ package messaging import ( + "encoding/json" "net/http" "strconv" + "strings" "testing" + "github.com/steinfletcher/apitest" jsonpath "github.com/steinfletcher/apitest-jsonpath" "github.com/cortezaproject/corteza-server/messaging/types" "github.com/cortezaproject/corteza-server/tests/helpers" ) +func (h helper) apiChCreate(name string, t types.ChannelType) *apitest.Response { + payload, err := json.Marshal(struct { + Name string `json:"name"` + Type string `json:"type"` + }{name, t.String()}) + + h.a.NoError(err) + + return h.apiInit(). + Post("/channels/"). + JSON(string(payload)). + Expect(h.t). + Status(http.StatusOK) +} + +func (h helper) apiChPubCreate(name string) *apitest.Response { + return h.apiChCreate(name, types.ChannelTypePublic) +} + func TestChannelCreateDenied(t *testing.T) { h := newHelper(t) h.deny(types.MessagingPermissionResource, "channel.public.create") - h.testAPI(). - Post("/channels/"). - JSON(`{"name":"test channel","type":"public"}`). - Expect(t). - Status(http.StatusOK). + h.apiChPubCreate("should not be created"). Assert(helpers.AssertError("messaging.service.NoPermissions")). End() } @@ -28,11 +46,7 @@ func TestChannelCreate(t *testing.T) { h := newHelper(t) h.allow(types.MessagingPermissionResource, "channel.public.create") - h.testAPI(). - Post("/channels/"). - JSON(`{"name":"test channel","type":"public"}`). - Expect(t). - Status(http.StatusOK). + h.apiChPubCreate("test channel"). Assert(helpers.AssertNoErrors). Assert(jsonpath.Present(`$.response.name`)). Assert(jsonpath.Present(`$.response.channelID`)). @@ -40,3 +54,24 @@ func TestChannelCreate(t *testing.T) { Assert(jsonpath.Contains(`$.response.members`, strconv.FormatUint(h.cUser.ID, 10))). End() } + +func TestChannelCreateWithShortName(t *testing.T) { + h := newHelper(t) + h.allow(types.MessagingPermissionResource, "channel.public.create") + + h.apiChPubCreate(""). + Status(http.StatusOK). + Assert(helpers.AssertError("channel name not provided")). + End() +} + +func TestChannelCreateWithLongName(t *testing.T) { + h := newHelper(t) + h.allow(types.MessagingPermissionResource, "channel.public.create") + + h.apiChPubCreate(strings.Repeat("X ", 1000)). + Status(http.StatusOK). + Assert(helpers.AssertError("channel name (2000 characters) too long (max: 40)")). + End() + +} diff --git a/tests/messaging/channel_flags_test.go b/tests/messaging/channel_flags_test.go index 096db1113..63a840119 100644 --- a/tests/messaging/channel_flags_test.go +++ b/tests/messaging/channel_flags_test.go @@ -14,8 +14,8 @@ import ( func TestChannelSetFlag(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() - h.makeMember(ch, h.cUser) + ch := h.repoMakePublicCh() + h.repoMakeMember(ch, h.cUser) flagCheck := func(ID uint64, flag string) { mm, err := h.repoChMember().Find(&types.ChannelMemberFilter{ChannelID: ID, MemberID: h.cUser.ID}) @@ -25,7 +25,7 @@ func TestChannelSetFlag(t *testing.T) { } flagChannel := func(ID uint64, flag string) *apitest.Response { - return h.testAPI(). + return h.apiInit(). Put(fmt.Sprintf("/channels/%d/flag", ID)). FormData("flag", flag). Expect(t). @@ -41,7 +41,7 @@ func TestChannelSetFlag(t *testing.T) { } unflagChannel := func(ID uint64) { - h.testAPI(). + h.apiInit(). Delete(fmt.Sprintf("/channels/%d/flag", ID)). Expect(t). Status(http.StatusOK). diff --git a/tests/messaging/channel_list_test.go b/tests/messaging/channel_list_test.go index de100cd54..06c90e698 100644 --- a/tests/messaging/channel_list_test.go +++ b/tests/messaging/channel_list_test.go @@ -10,7 +10,7 @@ import ( func TestChannelList(t *testing.T) { h := newHelper(t) - h.testAPI(). + h.apiInit(). Get("/channels/"). Expect(t). Status(http.StatusOK). diff --git a/tests/messaging/channel_members_test.go b/tests/messaging/channel_members_test.go index fcbd5021e..cfcb70f36 100644 --- a/tests/messaging/channel_members_test.go +++ b/tests/messaging/channel_members_test.go @@ -15,9 +15,9 @@ import ( func TestChannelMemberList(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() + ch := h.repoMakePublicCh() - h.testAPI(). + h.apiInit(). Get(fmt.Sprintf("/channels/%d/members", ch.ID)). Expect(t). Status(http.StatusOK). @@ -28,49 +28,49 @@ func TestChannelMemberList(t *testing.T) { func TestChannelMemberJoinSelf(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() + ch := h.repoMakePublicCh() - h.testAPI(). + h.apiInit(). Put(fmt.Sprintf("/channels/%d/members/%d", ch.ID, h.cUser.ID)). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors). End() - h.chAssertMember(ch, h.cUser, types.ChannelMembershipTypeMember) + h.repoChAssertMember(ch, h.cUser, types.ChannelMembershipTypeMember) } func TestChannelMemberLeaveSelf(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() - h.makeMember(ch, h.cUser) + ch := h.repoMakePublicCh() + h.repoMakeMember(ch, h.cUser) - h.testAPI(). + h.apiInit(). Delete(fmt.Sprintf("/channels/%d/members/%d", ch.ID, h.cUser.ID)). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors). End() - h.chAssertNotMember(ch, h.cUser) + h.repoChAssertNotMember(ch, h.cUser) } func TestChannelMemberInvite(t *testing.T) { t.SkipNow() h := newHelper(t) - ch := h.makePublicCh() + ch := h.repoMakePublicCh() h.allow(ch.PermissionResource(), "members.manage") invitee := &sysType.User{ID: factory.Sonyflake.NextID()} - h.testAPI(). + h.apiInit(). Put(fmt.Sprintf("/channels/%d/members/%d", ch.ID, invitee.ID)). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors). End() - h.chAssertMember(ch, invitee, types.ChannelMembershipTypeInvitee) + h.repoChAssertMember(ch, invitee, types.ChannelMembershipTypeInvitee) } diff --git a/tests/messaging/channel_state_test.go b/tests/messaging/channel_state_test.go index 04e58c8b8..71cb3abe2 100644 --- a/tests/messaging/channel_state_test.go +++ b/tests/messaging/channel_state_test.go @@ -12,7 +12,7 @@ import ( func TestChannelAlterState(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() + ch := h.repoMakePublicCh() stateUrl := fmt.Sprintf("/channels/%d/state", ch.ID) @@ -21,7 +21,7 @@ func TestChannelAlterState(t *testing.T) { h.mockPermissionsWithAccess() h.allow(types.ChannelPermissionResource.AppendWildcard(), permissions.Operation(state)) - h.testAPI(). + h.apiInit(). Put(stateUrl). FormData("state", state). Expect(t). diff --git a/tests/messaging/channel_test.go b/tests/messaging/channel_test.go index 2a77ed4c9..3dab0c2fb 100644 --- a/tests/messaging/channel_test.go +++ b/tests/messaging/channel_test.go @@ -29,7 +29,7 @@ func (h helper) repoChMember() repository.ChannelMemberRepository { return repository.ChannelMember(ctx, db) } -func (h helper) makePublicCh() *types.Channel { +func (h helper) repoMakePublicCh() *types.Channel { ch, err := h.repoChannel().Create(&types.Channel{ Name: "Test channel " + time.Now().String(), Type: types.ChannelTypePublic, @@ -39,7 +39,7 @@ func (h helper) makePublicCh() *types.Channel { return ch } -func (h helper) makePrivateCh() *types.Channel { +func (h helper) repoMakePrivateCh() *types.Channel { ch, err := h.repoChannel().Create(&types.Channel{ Name: "Test channel " + time.Now().String(), Type: types.ChannelTypePrivate, @@ -49,7 +49,7 @@ func (h helper) makePrivateCh() *types.Channel { return ch } -func (h helper) makeMember(ch *types.Channel, u *sysTypes.User) *types.ChannelMember { +func (h helper) repoMakeMember(ch *types.Channel, u *sysTypes.User) *types.ChannelMember { m, err := h. repoChMember(). Create(&types.ChannelMember{ChannelID: ch.ID, UserID: h.cUser.ID, Type: types.ChannelMembershipTypeMember}) @@ -58,14 +58,14 @@ func (h helper) makeMember(ch *types.Channel, u *sysTypes.User) *types.ChannelMe return m } -func (h helper) chAssertNotMember(ch *types.Channel, u *sysTypes.User) { +func (h helper) repoChAssertNotMember(ch *types.Channel, u *sysTypes.User) { mm, err := h.repoChMember().Find(&types.ChannelMemberFilter{ChannelID: ch.ID, MemberID: h.cUser.ID}) 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) { +func (h helper) repoChAssertMember(ch *types.Channel, u *sysTypes.User, typ types.ChannelMembershipType) { mm, err := h.repoChMember().Find(&types.ChannelMemberFilter{ChannelID: ch.ID, MemberID: h.cUser.ID}) h.a.NoError(err) diff --git a/tests/messaging/channel_update_test.go b/tests/messaging/channel_update_test.go index 957eb1462..ea8786772 100644 --- a/tests/messaging/channel_update_test.go +++ b/tests/messaging/channel_update_test.go @@ -20,7 +20,7 @@ func (h helper) chUpdate(ch *request.ChannelUpdate) *apitest.Response { payload, err := json.Marshal(ch) h.a.NoError(err) - return h.testAPI(). + return h.apiInit(). Put(fmt.Sprintf("/channels/%v", ch.ChannelID)). JSON(string(payload)). Expect(h.t). @@ -55,7 +55,7 @@ func TestChannelUpdateNonexistent(t *testing.T) { func TestChannelUpdateDenied(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() + ch := h.repoMakePublicCh() h.deny(ch.PermissionResource(), "update") @@ -69,7 +69,7 @@ func TestChannelUpdateDenied(t *testing.T) { func TestChannelUpdate(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() + ch := h.repoMakePublicCh() h.allow(ch.PermissionResource(), "update") diff --git a/tests/messaging/main_test.go b/tests/messaging/main_test.go index c94d3e6e5..e4b059c19 100644 --- a/tests/messaging/main_test.go +++ b/tests/messaging/main_test.go @@ -119,7 +119,7 @@ func newHelper(t *testing.T) helper { } // apitest basics, initialize, set handler, add auth -func (h helper) testAPI() *apitest.APITest { +func (h helper) apiInit() *apitest.APITest { InitApp() return apitest. diff --git a/tests/messaging/message_create_test.go b/tests/messaging/message_create_test.go index ad1fbd84f..7d6b5c888 100644 --- a/tests/messaging/message_create_test.go +++ b/tests/messaging/message_create_test.go @@ -12,7 +12,7 @@ import ( func TestMessagesCreate(t *testing.T) { h := newHelper(t) - ch := h.makePublicCh() + ch := h.repoMakePublicCh() rval := struct { Response struct { @@ -20,7 +20,7 @@ func TestMessagesCreate(t *testing.T) { } }{} - h.testAPI(). + h.apiInit(). Post(fmt.Sprintf("/channels/%d/messages/", ch.ID)). JSON(`{"message":"new message"}`). Expect(t). @@ -31,7 +31,7 @@ func TestMessagesCreate(t *testing.T) { End(). JSON(&rval) - m := h.msgExistingLoad(rval.Response.ID) + m := h.repoMsgExistingLoad(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 index ab92de2ff..4e5312e1d 100644 --- a/tests/messaging/message_delete_test.go +++ b/tests/messaging/message_delete_test.go @@ -10,11 +10,10 @@ import ( func TestMessagesDelete(t *testing.T) { h := newHelper(t) - msg := h.makeMessage("old", h.makePublicCh(), h.cUser) + msg := h.repoMakeMessage("old", h.repoMakePublicCh(), h.cUser) - h.testAPI(). + h.apiInit(). Delete(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, msg.ID)). - JSON(`{"message":"new"}`). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors). diff --git a/tests/messaging/message_reply_test.go b/tests/messaging/message_reply_test.go index 73f7b7b76..58b65bfff 100644 --- a/tests/messaging/message_reply_test.go +++ b/tests/messaging/message_reply_test.go @@ -12,7 +12,7 @@ import ( func TestMessagesReply(t *testing.T) { h := newHelper(t) - msg := h.makeMessage("old", h.makePublicCh(), h.cUser) + msg := h.repoMakeMessage("old", h.repoMakePublicCh(), h.cUser) rval := struct { Response struct { @@ -20,8 +20,7 @@ func TestMessagesReply(t *testing.T) { } }{} - h.testAPI(). - Debug(). + h.apiInit(). Post(fmt.Sprintf("/channels/%d/messages/%d/replies", msg.ChannelID, msg.ID)). JSON(`{"message":"new reply"}`). Expect(t). @@ -33,7 +32,7 @@ func TestMessagesReply(t *testing.T) { End(). JSON(&rval) - m := h.msgExistingLoad(rval.Response.ID) + m := h.repoMsgExistingLoad(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 index 7f1978194..8ac799f9f 100644 --- a/tests/messaging/message_test.go +++ b/tests/messaging/message_test.go @@ -19,7 +19,7 @@ func (h helper) repoMessage() repository.MessageRepository { return repository.Message(ctx, db) } -func (h helper) makeMessage(msg string, ch *types.Channel, u *sysTypes.User) *types.Message { +func (h helper) repoMakeMessage(msg string, ch *types.Channel, u *sysTypes.User) *types.Message { m, err := h.repoMessage().Create(&types.Message{ Message: msg, ChannelID: ch.ID, @@ -30,7 +30,7 @@ func (h helper) makeMessage(msg string, ch *types.Channel, u *sysTypes.User) *ty return m } -func (h helper) msgExistingLoad(ID uint64) *types.Message { +func (h helper) repoMsgExistingLoad(ID uint64) *types.Message { m, err := h.repoMessage().FindByID(ID) h.a.NoError(err) h.a.NotNil(m) diff --git a/tests/messaging/message_update_test.go b/tests/messaging/message_update_test.go index c94c0e22f..995d5bde7 100644 --- a/tests/messaging/message_update_test.go +++ b/tests/messaging/message_update_test.go @@ -12,9 +12,9 @@ import ( func TestMessagesUpdate(t *testing.T) { h := newHelper(t) - msg := h.makeMessage("old", h.makePublicCh(), h.cUser) + msg := h.repoMakeMessage("old", h.repoMakePublicCh(), h.cUser) - h.testAPI(). + h.apiInit(). Put(fmt.Sprintf("/channels/%d/messages/%d", msg.ChannelID, msg.ID)). JSON(`{"message":"new"}`). Expect(t). @@ -23,6 +23,6 @@ func TestMessagesUpdate(t *testing.T) { Assert(jsonpath.Equal(`$.response.message`, `new`)). End() - m := h.msgExistingLoad(msg.ID) + m := h.repoMsgExistingLoad(msg.ID) h.a.Equal(`new`, m.Message) }