3
0

Improve tests, simplify helpers

This commit is contained in:
Denis Arh
2019-09-09 19:36:21 +02:00
parent a32874ded6
commit c1226e1f7a
14 changed files with 129 additions and 62 deletions
+43 -9
View File
@@ -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()
}
+45 -10
View File
@@ -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()
}
+4 -4
View File
@@ -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).
+1 -1
View File
@@ -10,7 +10,7 @@ import (
func TestChannelList(t *testing.T) {
h := newHelper(t)
h.testAPI().
h.apiInit().
Get("/channels/").
Expect(t).
Status(http.StatusOK).
+12 -12
View File
@@ -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)
}
+2 -2
View File
@@ -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).
+5 -5
View File
@@ -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)
+3 -3
View File
@@ -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")
+1 -1
View File
@@ -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.
+3 -3
View File
@@ -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)
}
+2 -3
View File
@@ -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).
+3 -4
View File
@@ -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)
+2 -2
View File
@@ -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)
+3 -3
View File
@@ -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)
}