Mono repo -> Master (#22553)
Combines the following repositories into one: https://github.com/mattermost/mattermost-server https://github.com/mattermost/mattermost-webapp https://github.com/mattermost/focalboard https://github.com/mattermost/mattermost-plugin-playbooks
Этот коммит содержится в:
70
server/boards/auth/auth.go
Обычный файл
70
server/boards/auth/auth.go
Обычный файл
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
//go:generate mockgen -copyright_file=../../copyright.txt -destination=mocks/mockauth_interface.go -package mocks . AuthInterface
|
||||
package auth
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/model"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/services/config"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/services/permissions"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/services/store"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/utils"
|
||||
)
|
||||
|
||||
type AuthInterface interface {
|
||||
GetSession(token string) (*model.Session, error)
|
||||
IsValidReadToken(boardID string, readToken string) (bool, error)
|
||||
DoesUserHaveTeamAccess(userID string, teamID string) bool
|
||||
}
|
||||
|
||||
// Auth authenticates sessions.
|
||||
type Auth struct {
|
||||
config *config.Configuration
|
||||
store store.Store
|
||||
permissions permissions.PermissionsService
|
||||
}
|
||||
|
||||
// New returns a new Auth.
|
||||
func New(config *config.Configuration, store store.Store, permissions permissions.PermissionsService) *Auth {
|
||||
return &Auth{config: config, store: store, permissions: permissions}
|
||||
}
|
||||
|
||||
// GetSession Get a user active session and refresh the session if needed.
|
||||
func (a *Auth) GetSession(token string) (*model.Session, error) {
|
||||
if len(token) < 1 {
|
||||
return nil, errors.New("no session token")
|
||||
}
|
||||
|
||||
session, err := a.store.GetSession(token, a.config.SessionExpireTime)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "unable to get the session for the token")
|
||||
}
|
||||
if session.UpdateAt < (utils.GetMillis() - utils.SecondsToMillis(a.config.SessionRefreshTime)) {
|
||||
_ = a.store.RefreshSession(session)
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// IsValidReadToken validates the read token for a board.
|
||||
func (a *Auth) IsValidReadToken(boardID string, readToken string) (bool, error) {
|
||||
sharing, err := a.store.GetSharing(boardID)
|
||||
if model.IsErrNotFound(err) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if sharing != nil && (sharing.ID == boardID && sharing.Enabled && sharing.Token == readToken) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (a *Auth) DoesUserHaveTeamAccess(userID string, teamID string) bool {
|
||||
return a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam)
|
||||
}
|
||||
148
server/boards/auth/auth_test.go
Обычный файл
148
server/boards/auth/auth_test.go
Обычный файл
@@ -0,0 +1,148 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/model"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/services/config"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/services/permissions/localpermissions"
|
||||
mockpermissions "github.com/mattermost/mattermost-server/v6/server/boards/services/permissions/mocks"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/services/store/mockstore"
|
||||
"github.com/mattermost/mattermost-server/v6/server/boards/utils"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/server/platform/shared/mlog"
|
||||
)
|
||||
|
||||
type TestHelper struct {
|
||||
Auth *Auth
|
||||
Session model.Session
|
||||
Store *mockstore.MockStore
|
||||
}
|
||||
|
||||
var mockSession = &model.Session{
|
||||
ID: utils.NewID(utils.IDTypeSession),
|
||||
Token: "goodToken",
|
||||
UserID: "12345",
|
||||
CreateAt: utils.GetMillis() - utils.SecondsToMillis(2000),
|
||||
UpdateAt: utils.GetMillis() - utils.SecondsToMillis(2000),
|
||||
}
|
||||
|
||||
func setupTestHelper(t *testing.T) *TestHelper {
|
||||
ctrl := gomock.NewController(t)
|
||||
ctrlPermissions := gomock.NewController(t)
|
||||
cfg := config.Configuration{}
|
||||
mockStore := mockstore.NewMockStore(ctrl)
|
||||
mockPermissions := mockpermissions.NewMockStore(ctrlPermissions)
|
||||
logger, err := mlog.NewLogger()
|
||||
require.NoError(t, err)
|
||||
newAuth := New(&cfg, mockStore, localpermissions.New(mockPermissions, logger))
|
||||
|
||||
// called during default template setup for every test
|
||||
mockStore.EXPECT().GetTemplateBoards("0", "").AnyTimes()
|
||||
mockStore.EXPECT().RemoveDefaultTemplates(gomock.Any()).AnyTimes()
|
||||
mockStore.EXPECT().InsertBlock(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
|
||||
return &TestHelper{
|
||||
Auth: newAuth,
|
||||
Session: *mockSession,
|
||||
Store: mockStore,
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSession(t *testing.T) {
|
||||
th := setupTestHelper(t)
|
||||
|
||||
testcases := []struct {
|
||||
title string
|
||||
token string
|
||||
refreshTime int64
|
||||
isError bool
|
||||
}{
|
||||
{"fail, no token", "", 0, true},
|
||||
{"fail, invalid username", "badToken", 0, true},
|
||||
{"success, good token", "goodToken", 1000, false},
|
||||
}
|
||||
|
||||
th.Store.EXPECT().GetSession("badToken", gomock.Any()).Return(nil, errors.New("Invalid Token"))
|
||||
th.Store.EXPECT().GetSession("goodToken", gomock.Any()).Return(mockSession, nil)
|
||||
th.Store.EXPECT().RefreshSession(gomock.Any()).Return(nil)
|
||||
|
||||
for _, test := range testcases {
|
||||
t.Run(test.title, func(t *testing.T) {
|
||||
if test.refreshTime > 0 {
|
||||
th.Auth.config.SessionRefreshTime = test.refreshTime
|
||||
}
|
||||
|
||||
session, err := th.Auth.GetSession(test.token)
|
||||
if test.isError {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, session)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidReadToken(t *testing.T) {
|
||||
// ToDo: reimplement
|
||||
|
||||
// th := setupTestHelper(t)
|
||||
|
||||
// validBlockID := "testBlockID"
|
||||
// mockContainer := store.Container{
|
||||
// TeamID: "testTeamID",
|
||||
// }
|
||||
// validReadToken := "testReadToken"
|
||||
// mockSharing := model.Sharing{
|
||||
// ID: "testRootID",
|
||||
// Enabled: true,
|
||||
// Token: validReadToken,
|
||||
// }
|
||||
|
||||
// testcases := []struct {
|
||||
// title string
|
||||
// container store.Container
|
||||
// blockID string
|
||||
// readToken string
|
||||
// isError bool
|
||||
// isSuccess bool
|
||||
// }{
|
||||
// {"fail, error GetRootID", mockContainer, "badBlock", "", true, false},
|
||||
// {"fail, rootID not found", mockContainer, "goodBlockID", "", false, false},
|
||||
// {"fail, sharing throws error", mockContainer, "goodBlockID2", "", true, false},
|
||||
// {"fail, bad readToken", mockContainer, validBlockID, "invalidReadToken", false, false},
|
||||
// {"success", mockContainer, validBlockID, validReadToken, false, true},
|
||||
// }
|
||||
|
||||
// th.Store.EXPECT().GetRootID(gomock.Eq(mockContainer), "badBlock").Return("", errors.New("invalid block"))
|
||||
// th.Store.EXPECT().GetRootID(gomock.Eq(mockContainer), "goodBlockID").Return("rootNotFound", nil)
|
||||
// th.Store.EXPECT().GetRootID(gomock.Eq(mockContainer), "goodBlockID2").Return("rootError", nil)
|
||||
// th.Store.EXPECT().GetRootID(gomock.Eq(mockContainer), validBlockID).Return("testRootID", nil).Times(2)
|
||||
// th.Store.EXPECT().GetSharing(gomock.Eq(mockContainer), "rootNotFound").Return(nil, sql.ErrNoRows)
|
||||
// th.Store.EXPECT().GetSharing(gomock.Eq(mockContainer), "rootError").Return(nil, errors.New("another error"))
|
||||
// th.Store.EXPECT().GetSharing(gomock.Eq(mockContainer), "testRootID").Return(&mockSharing, nil).Times(2)
|
||||
|
||||
// for _, test := range testcases {
|
||||
// t.Run(test.title, func(t *testing.T) {
|
||||
// success, err := th.Auth.IsValidReadToken(test.container, test.blockID, test.readToken)
|
||||
// if test.isError {
|
||||
// require.Error(t, err)
|
||||
// } else {
|
||||
// require.NoError(t, err)
|
||||
// }
|
||||
// if test.isSuccess {
|
||||
// require.True(t, success)
|
||||
// } else {
|
||||
// require.False(t, success)
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
}
|
||||
82
server/boards/auth/mocks/mockauth_interface.go
Обычный файл
82
server/boards/auth/mocks/mockauth_interface.go
Обычный файл
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/mattermost/mattermost-server/v6/server/boards/auth (interfaces: AuthInterface)
|
||||
|
||||
// Package mocks is a generated GoMock package.
|
||||
package mocks
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
model "github.com/mattermost/mattermost-server/v6/server/boards/model"
|
||||
)
|
||||
|
||||
// MockAuthInterface is a mock of AuthInterface interface.
|
||||
type MockAuthInterface struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockAuthInterfaceMockRecorder
|
||||
}
|
||||
|
||||
// MockAuthInterfaceMockRecorder is the mock recorder for MockAuthInterface.
|
||||
type MockAuthInterfaceMockRecorder struct {
|
||||
mock *MockAuthInterface
|
||||
}
|
||||
|
||||
// NewMockAuthInterface creates a new mock instance.
|
||||
func NewMockAuthInterface(ctrl *gomock.Controller) *MockAuthInterface {
|
||||
mock := &MockAuthInterface{ctrl: ctrl}
|
||||
mock.recorder = &MockAuthInterfaceMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockAuthInterface) EXPECT() *MockAuthInterfaceMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// DoesUserHaveTeamAccess mocks base method.
|
||||
func (m *MockAuthInterface) DoesUserHaveTeamAccess(arg0, arg1 string) bool {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "DoesUserHaveTeamAccess", arg0, arg1)
|
||||
ret0, _ := ret[0].(bool)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// DoesUserHaveTeamAccess indicates an expected call of DoesUserHaveTeamAccess.
|
||||
func (mr *MockAuthInterfaceMockRecorder) DoesUserHaveTeamAccess(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoesUserHaveTeamAccess", reflect.TypeOf((*MockAuthInterface)(nil).DoesUserHaveTeamAccess), arg0, arg1)
|
||||
}
|
||||
|
||||
// GetSession mocks base method.
|
||||
func (m *MockAuthInterface) GetSession(arg0 string) (*model.Session, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetSession", arg0)
|
||||
ret0, _ := ret[0].(*model.Session)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetSession indicates an expected call of GetSession.
|
||||
func (mr *MockAuthInterfaceMockRecorder) GetSession(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSession", reflect.TypeOf((*MockAuthInterface)(nil).GetSession), arg0)
|
||||
}
|
||||
|
||||
// IsValidReadToken mocks base method.
|
||||
func (m *MockAuthInterface) IsValidReadToken(arg0, arg1 string) (bool, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "IsValidReadToken", arg0, arg1)
|
||||
ret0, _ := ret[0].(bool)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// IsValidReadToken indicates an expected call of IsValidReadToken.
|
||||
func (mr *MockAuthInterfaceMockRecorder) IsValidReadToken(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsValidReadToken", reflect.TypeOf((*MockAuthInterface)(nil).IsValidReadToken), arg0, arg1)
|
||||
}
|
||||
Ссылка в новой задаче
Block a user