2021-12-10 16:46:37 +01:00
|
|
|
package integrationtests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/client"
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func createTestSubscriptions(client *client.Client, num int) ([]*model.Subscription, string, error) {
|
2021-12-10 16:46:37 +01:00
|
|
|
newSubs := make([]*model.Subscription, 0, num)
|
|
|
|
|
|
|
|
user, resp := client.GetMe()
|
|
|
|
if resp.Error != nil {
|
|
|
|
return nil, "", fmt.Errorf("cannot get current user: %w", resp.Error)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
board := &model.Board{
|
|
|
|
TeamID: "0",
|
|
|
|
Type: model.BoardTypeOpen,
|
2022-01-05 10:02:06 +01:00
|
|
|
CreateAt: 1,
|
|
|
|
UpdateAt: 1,
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
board, resp = client.CreateBoard(board)
|
2022-01-05 10:02:06 +01:00
|
|
|
if resp.Error != nil {
|
|
|
|
return nil, "", fmt.Errorf("cannot insert test board block: %w", resp.Error)
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
for n := 0; n < num; n++ {
|
2022-01-05 10:02:06 +01:00
|
|
|
newBlock := model.Block{
|
|
|
|
ID: utils.NewID(utils.IDTypeCard),
|
2022-03-22 15:24:34 +01:00
|
|
|
BoardID: board.ID,
|
2022-01-05 10:02:06 +01:00
|
|
|
CreateAt: 1,
|
|
|
|
UpdateAt: 1,
|
|
|
|
Type: model.TypeCard,
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
newBlocks, resp := client.InsertBlocks(board.ID, []model.Block{newBlock})
|
2022-01-05 10:02:06 +01:00
|
|
|
if resp.Error != nil {
|
|
|
|
return nil, "", fmt.Errorf("cannot insert test card block: %w", resp.Error)
|
|
|
|
}
|
|
|
|
newBlock = newBlocks[0]
|
|
|
|
|
2021-12-10 16:46:37 +01:00
|
|
|
sub := &model.Subscription{
|
2022-01-05 10:02:06 +01:00
|
|
|
BlockType: newBlock.Type,
|
|
|
|
BlockID: newBlock.ID,
|
2021-12-10 16:46:37 +01:00
|
|
|
SubscriberType: model.SubTypeUser,
|
|
|
|
SubscriberID: user.ID,
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
subNew, resp := client.CreateSubscription(sub)
|
2021-12-10 16:46:37 +01:00
|
|
|
if resp.Error != nil {
|
|
|
|
return nil, "", resp.Error
|
|
|
|
}
|
|
|
|
newSubs = append(newSubs, subNew)
|
|
|
|
}
|
|
|
|
return newSubs, user.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateSubscription(t *testing.T) {
|
2022-03-22 15:24:34 +01:00
|
|
|
th := SetupTestHelper(t).InitBasic()
|
2021-12-10 16:46:37 +01:00
|
|
|
defer th.TearDown()
|
|
|
|
|
|
|
|
t.Run("Create valid subscription", func(t *testing.T) {
|
2022-03-22 15:24:34 +01:00
|
|
|
subs, userID, err := createTestSubscriptions(th.Client, 5)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, subs, 5)
|
|
|
|
|
|
|
|
// fetch the newly created subscriptions and compare
|
2022-03-22 15:24:34 +01:00
|
|
|
subsFound, resp := th.Client.GetSubscriptions(userID)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
require.Len(t, subsFound, 5)
|
|
|
|
assert.ElementsMatch(t, subs, subsFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Create invalid subscription", func(t *testing.T) {
|
|
|
|
user, resp := th.Client.GetMe()
|
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
|
|
|
|
sub := &model.Subscription{
|
|
|
|
SubscriberID: user.ID,
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
_, resp = th.Client.CreateSubscription(sub)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.Error(t, resp.Error)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Create subscription for another user", func(t *testing.T) {
|
|
|
|
sub := &model.Subscription{
|
|
|
|
SubscriberID: utils.NewID(utils.IDTypeUser),
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
_, resp := th.Client.CreateSubscription(sub)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.Error(t, resp.Error)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSubscriptions(t *testing.T) {
|
2022-03-22 15:24:34 +01:00
|
|
|
th := SetupTestHelper(t).InitBasic()
|
2021-12-10 16:46:37 +01:00
|
|
|
defer th.TearDown()
|
|
|
|
|
|
|
|
t.Run("Get subscriptions for user", func(t *testing.T) {
|
2022-03-22 15:24:34 +01:00
|
|
|
mySubs, user1ID, err := createTestSubscriptions(th.Client, 5)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, mySubs, 5)
|
|
|
|
|
|
|
|
// create more subscriptions with different user
|
2022-03-22 15:24:34 +01:00
|
|
|
otherSubs, _, err := createTestSubscriptions(th.Client2, 10)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, otherSubs, 10)
|
|
|
|
|
|
|
|
// fetch the newly created subscriptions for current user, making sure only
|
|
|
|
// the ones created for the current user are returned.
|
2022-03-22 15:24:34 +01:00
|
|
|
subsFound, resp := th.Client.GetSubscriptions(user1ID)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
require.Len(t, subsFound, 5)
|
|
|
|
assert.ElementsMatch(t, mySubs, subsFound)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteSubscription(t *testing.T) {
|
2022-03-22 15:24:34 +01:00
|
|
|
th := SetupTestHelper(t).InitBasic()
|
2021-12-10 16:46:37 +01:00
|
|
|
defer th.TearDown()
|
|
|
|
|
|
|
|
t.Run("Delete valid subscription", func(t *testing.T) {
|
2022-03-22 15:24:34 +01:00
|
|
|
subs, userID, err := createTestSubscriptions(th.Client, 3)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, subs, 3)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
resp := th.Client.DeleteSubscription(subs[1].BlockID, userID)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
|
|
|
|
// fetch the subscriptions and ensure the list is correct
|
2022-03-22 15:24:34 +01:00
|
|
|
subsFound, resp := th.Client.GetSubscriptions(userID)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
require.Len(t, subsFound, 2)
|
|
|
|
|
|
|
|
assert.Contains(t, subsFound, subs[0])
|
|
|
|
assert.Contains(t, subsFound, subs[2])
|
|
|
|
assert.NotContains(t, subsFound, subs[1])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete invalid subscription", func(t *testing.T) {
|
|
|
|
user, resp := th.Client.GetMe()
|
|
|
|
require.NoError(t, resp.Error)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
resp = th.Client.DeleteSubscription("bogus", user.ID)
|
2021-12-10 16:46:37 +01:00
|
|
|
require.Error(t, resp.Error)
|
|
|
|
})
|
|
|
|
}
|