focalboard/server/integrationtests/pluginteststore.go

199 lines
4.7 KiB
Go
Raw Normal View History

Permissions integration tests (#2697) * Initial permissions review infrastructure * Adding more tests cases * Modifying a bit the tests approach and adding more tests * Adding more tests * Adding more tests for permissions * Adding more tests * Adding more permissions tests * Adding more tests * Adding more permission checks * Adding more permissions tests * Adding more permission tests * Adding more tests * Adding subscriptions tests * Adding more permissions tests * Adding tests for read tokens in the files * Update APIs and fix unit tests * Fix linter errors * Auto-assign category id from the database (as expected because is serial/auto_increment integer field) * Revert "Auto-assign category id from the database (as expected because is serial/auto_increment integer field)" This reverts commit 5c98fd76a32f1a7ef6a6258497ec7ac64e034640. * Fixing Category scheme in postgres and MySQL * Removing restriction about the channel_id and add it to all the databases * Moving everything to a new migration * Fix bad merge (?) * Update 000021_fix_categories.up.sql Fix Postgres ALTER COLUMN syntax * Update 000021_fix_categories.down.sql Fix Postgres ALTER COLUMN syntax * Update 000021_fix_categories.up.sql Remove unnecessary, and unsupported MODIFY COLUMNs for SQLite. * Update 000021_fix_categories.up.sql Remove not null from categories.channel_id * Update 000021_fix_categories.down.sql Migrate down removing not null from categories.channel_id * Update 000021_fix_categories.up.sql Fix drop not null on categories.channel_id * Update 000021_fix_categories.down.sql Fix down migration of drop not null from categories.channel_id. * Restore default notification level to debug Co-authored-by: Chen-I Lim <chenilim@gmail.com> Co-authored-by: Chen-I Lim <46905241+chenilim@users.noreply.github.com>
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"
)
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",
Props: map[string]interface{}{},
Username: "no-team-member",
Email: "no-team-member@sample.com",
CreateAt: model.GetMillis(),
UpdateAt: model.GetMillis(),
},
"team-member": {
ID: "team-member",
Props: map[string]interface{}{},
Username: "team-member",
Email: "team-member@sample.com",
CreateAt: model.GetMillis(),
UpdateAt: model.GetMillis(),
},
"viewer": {
ID: "viewer",
Props: map[string]interface{}{},
Username: "viewer",
Email: "viewer@sample.com",
CreateAt: model.GetMillis(),
UpdateAt: model.GetMillis(),
},
"commenter": {
ID: "commenter",
Props: map[string]interface{}{},
Username: "commenter",
Email: "commenter@sample.com",
CreateAt: model.GetMillis(),
UpdateAt: model.GetMillis(),
},
"editor": {
ID: "editor",
Props: map[string]interface{}{},
Username: "editor",
Email: "editor@sample.com",
CreateAt: model.GetMillis(),
UpdateAt: model.GetMillis(),
},
"admin": {
ID: "admin",
Props: map[string]interface{}{},
Username: "admin",
Email: "admin@sample.com",
CreateAt: model.GetMillis(),
UpdateAt: model.GetMillis(),
},
},
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
}
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
}
func (s *PluginTestStore) PatchUserProps(userID string, patch model.UserPropPatch) error {
user, err := s.GetUserByID(userID)
if err != nil {
return err
}
props := user.Props
for _, key := range patch.DeletedFields {
delete(props, key)
}
for key, value := range patch.UpdatedFields {
props[key] = value
}
user.Props = props
return nil
}
func (s *PluginTestStore) GetUsersByTeam(teamID string) ([]*model.User, error) {
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"],
}, 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
}
func (s *PluginTestStore) SearchUsersByTeam(teamID string, searchQuery string) ([]*model.User, error) {
users := []*model.User{}
teamUsers, err := s.GetUsersByTeam(teamID)
if err != nil {
return nil, err
}
for _, user := range teamUsers {
if strings.Contains(user.Username, searchQuery) {
users = append(users, user)
}
}
return users, nil
}