2022-04-05 17:00:04 +02:00
|
|
|
package integrationtests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2022-07-07 16:46:53 +02:00
|
|
|
|
|
|
|
mmModel "github.com/mattermost/mattermost-server/v6/model"
|
2022-04-05 17:00:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var errTestStore = errors.New("plugin test store error")
|
|
|
|
|
|
|
|
type PluginTestStore struct {
|
|
|
|
store.Store
|
|
|
|
users map[string]*model.User
|
|
|
|
testTeam *model.Team
|
|
|
|
otherTeam *model.Team
|
|
|
|
emptyTeam *model.Team
|
|
|
|
baseTeam *model.Team
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPluginTestStore(innerStore store.Store) *PluginTestStore {
|
|
|
|
return &PluginTestStore{
|
|
|
|
Store: innerStore,
|
|
|
|
users: map[string]*model.User{
|
|
|
|
"no-team-member": {
|
|
|
|
ID: "no-team-member",
|
|
|
|
Username: "no-team-member",
|
|
|
|
Email: "no-team-member@sample.com",
|
|
|
|
CreateAt: model.GetMillis(),
|
|
|
|
UpdateAt: model.GetMillis(),
|
|
|
|
},
|
|
|
|
"team-member": {
|
|
|
|
ID: "team-member",
|
|
|
|
Username: "team-member",
|
|
|
|
Email: "team-member@sample.com",
|
|
|
|
CreateAt: model.GetMillis(),
|
|
|
|
UpdateAt: model.GetMillis(),
|
|
|
|
},
|
|
|
|
"viewer": {
|
|
|
|
ID: "viewer",
|
|
|
|
Username: "viewer",
|
|
|
|
Email: "viewer@sample.com",
|
|
|
|
CreateAt: model.GetMillis(),
|
|
|
|
UpdateAt: model.GetMillis(),
|
|
|
|
},
|
|
|
|
"commenter": {
|
|
|
|
ID: "commenter",
|
|
|
|
Username: "commenter",
|
|
|
|
Email: "commenter@sample.com",
|
|
|
|
CreateAt: model.GetMillis(),
|
|
|
|
UpdateAt: model.GetMillis(),
|
|
|
|
},
|
|
|
|
"editor": {
|
|
|
|
ID: "editor",
|
|
|
|
Username: "editor",
|
|
|
|
Email: "editor@sample.com",
|
|
|
|
CreateAt: model.GetMillis(),
|
|
|
|
UpdateAt: model.GetMillis(),
|
|
|
|
},
|
|
|
|
"admin": {
|
|
|
|
ID: "admin",
|
|
|
|
Username: "admin",
|
|
|
|
Email: "admin@sample.com",
|
|
|
|
CreateAt: model.GetMillis(),
|
|
|
|
UpdateAt: model.GetMillis(),
|
|
|
|
},
|
2022-08-25 00:08:58 +02:00
|
|
|
"guest": {
|
|
|
|
ID: "guest",
|
|
|
|
Username: "guest",
|
|
|
|
Email: "guest@sample.com",
|
|
|
|
CreateAt: model.GetMillis(),
|
|
|
|
UpdateAt: model.GetMillis(),
|
|
|
|
IsGuest: true,
|
|
|
|
},
|
2022-04-05 17:00:04 +02:00
|
|
|
},
|
|
|
|
testTeam: &model.Team{ID: "test-team", Title: "Test Team"},
|
|
|
|
otherTeam: &model.Team{ID: "other-team", Title: "Other Team"},
|
|
|
|
emptyTeam: &model.Team{ID: "empty-team", Title: "Empty Team"},
|
|
|
|
baseTeam: &model.Team{ID: "0", Title: "Base Team"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginTestStore) GetTeam(id string) (*model.Team, error) {
|
|
|
|
switch id {
|
|
|
|
case "0":
|
|
|
|
return s.baseTeam, nil
|
|
|
|
case "other-team":
|
|
|
|
return s.otherTeam, nil
|
|
|
|
case "test-team":
|
|
|
|
return s.testTeam, nil
|
|
|
|
case "empty-team":
|
|
|
|
return s.emptyTeam, nil
|
|
|
|
}
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginTestStore) GetTeamsForUser(userID string) ([]*model.Team, error) {
|
|
|
|
switch userID {
|
|
|
|
case "no-team-member":
|
|
|
|
return []*model.Team{}, nil
|
|
|
|
case "team-member":
|
|
|
|
return []*model.Team{s.testTeam, s.otherTeam}, nil
|
|
|
|
case "viewer":
|
|
|
|
return []*model.Team{s.testTeam, s.otherTeam}, nil
|
|
|
|
case "commenter":
|
|
|
|
return []*model.Team{s.testTeam, s.otherTeam}, nil
|
|
|
|
case "editor":
|
|
|
|
return []*model.Team{s.testTeam, s.otherTeam}, nil
|
|
|
|
case "admin":
|
|
|
|
return []*model.Team{s.testTeam, s.otherTeam}, nil
|
2022-08-25 00:08:58 +02:00
|
|
|
case "guest":
|
|
|
|
return []*model.Team{s.testTeam}, nil
|
2022-04-05 17:00:04 +02:00
|
|
|
}
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginTestStore) GetUserByID(userID string) (*model.User, error) {
|
|
|
|
user := s.users[userID]
|
|
|
|
if user == nil {
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginTestStore) GetUserByEmail(email string) (*model.User, error) {
|
|
|
|
for _, user := range s.users {
|
|
|
|
if user.Email == email {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginTestStore) GetUserByUsername(username string) (*model.User, error) {
|
|
|
|
for _, user := range s.users {
|
|
|
|
if user.Username == username {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
|
2022-08-29 09:25:12 +02:00
|
|
|
func (s *PluginTestStore) GetUserPreferences(userID string) (mmModel.Preferences, error) {
|
|
|
|
if userID == userTeamMember {
|
|
|
|
return mmModel.Preferences{{
|
|
|
|
UserId: userTeamMember,
|
|
|
|
Category: "focalboard",
|
|
|
|
Name: "test",
|
|
|
|
Value: "test",
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:07:10 +01:00
|
|
|
func (s *PluginTestStore) GetUsersByTeam(teamID string, asGuestID string, showEmail, showName bool) ([]*model.User, error) {
|
2022-08-25 00:08:58 +02:00
|
|
|
if asGuestID == "guest" {
|
|
|
|
return []*model.User{
|
|
|
|
s.users["viewer"],
|
|
|
|
s.users["commenter"],
|
|
|
|
s.users["editor"],
|
|
|
|
s.users["admin"],
|
|
|
|
s.users["guest"],
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-29 09:25:12 +02:00
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
switch {
|
|
|
|
case teamID == s.testTeam.ID:
|
|
|
|
return []*model.User{
|
|
|
|
s.users["team-member"],
|
|
|
|
s.users["viewer"],
|
|
|
|
s.users["commenter"],
|
|
|
|
s.users["editor"],
|
|
|
|
s.users["admin"],
|
2022-08-25 00:08:58 +02:00
|
|
|
s.users["guest"],
|
2022-04-05 17:00:04 +02:00
|
|
|
}, nil
|
|
|
|
case teamID == s.otherTeam.ID:
|
|
|
|
return []*model.User{
|
|
|
|
s.users["team-member"],
|
|
|
|
s.users["viewer"],
|
|
|
|
s.users["commenter"],
|
|
|
|
s.users["editor"],
|
|
|
|
s.users["admin"],
|
|
|
|
}, nil
|
|
|
|
case teamID == s.emptyTeam.ID:
|
|
|
|
return []*model.User{}, nil
|
|
|
|
}
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:07:10 +01:00
|
|
|
func (s *PluginTestStore) SearchUsersByTeam(teamID string, searchQuery string, asGuestID string, excludeBots bool, showEmail, showName bool) ([]*model.User, error) {
|
2022-04-05 17:00:04 +02:00
|
|
|
users := []*model.User{}
|
2022-11-02 21:07:10 +01:00
|
|
|
teamUsers, err := s.GetUsersByTeam(teamID, asGuestID, showEmail, showName)
|
2022-04-05 17:00:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range teamUsers {
|
2022-09-10 03:56:44 +02:00
|
|
|
if excludeBots && user.IsBot {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-05 17:00:04 +02:00
|
|
|
if strings.Contains(user.Username, searchQuery) {
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
2022-07-05 09:26:22 +02:00
|
|
|
|
2022-08-25 00:08:58 +02:00
|
|
|
func (s *PluginTestStore) CanSeeUser(seerID string, seenID string) (bool, error) {
|
|
|
|
user, err := s.GetUserByID(seerID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !user.IsGuest {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
seerMembers, err := s.GetMembersForUser(seerID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
seenMembers, err := s.GetMembersForUser(seenID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, seerMember := range seerMembers {
|
|
|
|
for _, seenMember := range seenMembers {
|
|
|
|
if seerMember.BoardID == seenMember.BoardID {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-07-07 16:46:53 +02:00
|
|
|
func (s *PluginTestStore) SearchUserChannels(teamID, userID, query string) ([]*mmModel.Channel, error) {
|
|
|
|
return []*mmModel.Channel{
|
|
|
|
{
|
|
|
|
TeamId: teamID,
|
|
|
|
Id: "valid-channel-id",
|
|
|
|
DisplayName: "Valid Channel",
|
|
|
|
Name: "valid-channel",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TeamId: teamID,
|
|
|
|
Id: "valid-channel-id-2",
|
|
|
|
DisplayName: "Valid Channel 2",
|
|
|
|
Name: "valid-channel-2",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginTestStore) GetChannel(teamID, channel string) (*mmModel.Channel, error) {
|
|
|
|
if channel == "valid-channel-id" {
|
|
|
|
return &mmModel.Channel{
|
|
|
|
TeamId: teamID,
|
|
|
|
Id: "valid-channel-id",
|
|
|
|
DisplayName: "Valid Channel",
|
|
|
|
Name: "valid-channel",
|
|
|
|
}, nil
|
|
|
|
} else if channel == "valid-channel-id-2" {
|
|
|
|
return &mmModel.Channel{
|
|
|
|
TeamId: teamID,
|
|
|
|
Id: "valid-channel-id-2",
|
|
|
|
DisplayName: "Valid Channel 2",
|
|
|
|
Name: "valid-channel-2",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, errTestStore
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:46:00 +01:00
|
|
|
func (s *PluginTestStore) SearchBoardsForUser(term string, field model.BoardSearchField, userID string, includePublicBoards bool) ([]*model.Board, error) {
|
|
|
|
boards, err := s.Store.SearchBoardsForUser(term, field, userID, includePublicBoards)
|
2022-07-05 09:26:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
teams, err := s.GetTeamsForUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resultBoards := []*model.Board{}
|
|
|
|
for _, board := range boards {
|
|
|
|
for _, team := range teams {
|
|
|
|
if team.ID == board.TeamID {
|
|
|
|
resultBoards = append(resultBoards, board)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resultBoards, nil
|
|
|
|
}
|