[MM-55028] Added OAuthOutgoingConnection store (#25221)

* OAuthOutgoingConnection model

* added store

* make generated

* add missing license headers

* fix receiver name

* i18n

* i18n sorting

* update migrations from master

* make migrations-extract

* update retrylayer tests

* replaced sql query with id pagination

* fixed flaky tests

* missing columns

* missing columns on save/update

* typo

* improved tests

* remove enum from mysql colum

* add password credentials to store

* renamed migrations

* model change suggestions

* refactor test functionsn

* migration typo

* refactor store table names

* updated sanitize test

* oauthoutgoingconnection -> outgoingoauthconnection

* signature change

* i18n update

* granttype typo

* uppercase typo

* lowercase store name
Этот коммит содержится в:
Felipe Martin
2023-11-20 15:42:07 +01:00
коммит произвёл GitHub
родитель 8158c0e614
Коммит b40366dbdf
20 изменённых файлов: 1419 добавлений и 15 удалений

Просмотреть файл

@@ -0,0 +1,149 @@
// Code generated by mockery v2.23.2. DO NOT EDIT.
// Regenerate this file using `make store-mocks`.
package mocks
import (
model "github.com/mattermost/mattermost/server/public/model"
request "github.com/mattermost/mattermost/server/public/shared/request"
mock "github.com/stretchr/testify/mock"
)
// OutgoingOAuthConnectionStore is an autogenerated mock type for the OutgoingOAuthConnectionStore type
type OutgoingOAuthConnectionStore struct {
mock.Mock
}
// DeleteConnection provides a mock function with given fields: c, id
func (_m *OutgoingOAuthConnectionStore) DeleteConnection(c request.CTX, id string) error {
ret := _m.Called(c, id)
var r0 error
if rf, ok := ret.Get(0).(func(request.CTX, string) error); ok {
r0 = rf(c, id)
} else {
r0 = ret.Error(0)
}
return r0
}
// GetConnection provides a mock function with given fields: c, id
func (_m *OutgoingOAuthConnectionStore) GetConnection(c request.CTX, id string) (*model.OutgoingOAuthConnection, error) {
ret := _m.Called(c, id)
var r0 *model.OutgoingOAuthConnection
var r1 error
if rf, ok := ret.Get(0).(func(request.CTX, string) (*model.OutgoingOAuthConnection, error)); ok {
return rf(c, id)
}
if rf, ok := ret.Get(0).(func(request.CTX, string) *model.OutgoingOAuthConnection); ok {
r0 = rf(c, id)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.OutgoingOAuthConnection)
}
}
if rf, ok := ret.Get(1).(func(request.CTX, string) error); ok {
r1 = rf(c, id)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetConnections provides a mock function with given fields: c, filters
func (_m *OutgoingOAuthConnectionStore) GetConnections(c request.CTX, filters model.OutgoingOAuthConnectionGetConnectionsFilter) ([]*model.OutgoingOAuthConnection, error) {
ret := _m.Called(c, filters)
var r0 []*model.OutgoingOAuthConnection
var r1 error
if rf, ok := ret.Get(0).(func(request.CTX, model.OutgoingOAuthConnectionGetConnectionsFilter) ([]*model.OutgoingOAuthConnection, error)); ok {
return rf(c, filters)
}
if rf, ok := ret.Get(0).(func(request.CTX, model.OutgoingOAuthConnectionGetConnectionsFilter) []*model.OutgoingOAuthConnection); ok {
r0 = rf(c, filters)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*model.OutgoingOAuthConnection)
}
}
if rf, ok := ret.Get(1).(func(request.CTX, model.OutgoingOAuthConnectionGetConnectionsFilter) error); ok {
r1 = rf(c, filters)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// SaveConnection provides a mock function with given fields: c, conn
func (_m *OutgoingOAuthConnectionStore) SaveConnection(c request.CTX, conn *model.OutgoingOAuthConnection) (*model.OutgoingOAuthConnection, error) {
ret := _m.Called(c, conn)
var r0 *model.OutgoingOAuthConnection
var r1 error
if rf, ok := ret.Get(0).(func(request.CTX, *model.OutgoingOAuthConnection) (*model.OutgoingOAuthConnection, error)); ok {
return rf(c, conn)
}
if rf, ok := ret.Get(0).(func(request.CTX, *model.OutgoingOAuthConnection) *model.OutgoingOAuthConnection); ok {
r0 = rf(c, conn)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.OutgoingOAuthConnection)
}
}
if rf, ok := ret.Get(1).(func(request.CTX, *model.OutgoingOAuthConnection) error); ok {
r1 = rf(c, conn)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// UpdateConnection provides a mock function with given fields: c, conn
func (_m *OutgoingOAuthConnectionStore) UpdateConnection(c request.CTX, conn *model.OutgoingOAuthConnection) (*model.OutgoingOAuthConnection, error) {
ret := _m.Called(c, conn)
var r0 *model.OutgoingOAuthConnection
var r1 error
if rf, ok := ret.Get(0).(func(request.CTX, *model.OutgoingOAuthConnection) (*model.OutgoingOAuthConnection, error)); ok {
return rf(c, conn)
}
if rf, ok := ret.Get(0).(func(request.CTX, *model.OutgoingOAuthConnection) *model.OutgoingOAuthConnection); ok {
r0 = rf(c, conn)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.OutgoingOAuthConnection)
}
}
if rf, ok := ret.Get(1).(func(request.CTX, *model.OutgoingOAuthConnection) error); ok {
r1 = rf(c, conn)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
type mockConstructorTestingTNewOutgoingOAuthConnectionStore interface {
mock.TestingT
Cleanup(func())
}
// NewOutgoingOAuthConnectionStore creates a new instance of OutgoingOAuthConnectionStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewOutgoingOAuthConnectionStore(t mockConstructorTestingTNewOutgoingOAuthConnectionStore) *OutgoingOAuthConnectionStore {
mock := &OutgoingOAuthConnectionStore{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}

Просмотреть файл

@@ -492,6 +492,22 @@ func (_m *Store) OAuth() store.OAuthStore {
return r0
}
// OutgoingOAuthConnection provides a mock function with given fields:
func (_m *Store) OutgoingOAuthConnection() store.OutgoingOAuthConnectionStore {
ret := _m.Called()
var r0 store.OutgoingOAuthConnectionStore
if rf, ok := ret.Get(0).(func() store.OutgoingOAuthConnectionStore); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.OutgoingOAuthConnectionStore)
}
}
return r0
}
// Plugin provides a mock function with given fields:
func (_m *Store) Plugin() store.PluginStore {
ret := _m.Called()

Просмотреть файл

@@ -0,0 +1,239 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package storetest
import (
"sort"
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/stretchr/testify/require"
)
func newValidOutgoingOAuthConnection() *model.OutgoingOAuthConnection {
return &model.OutgoingOAuthConnection{
CreatorId: model.NewId(),
Name: "Test Connection",
ClientId: model.NewId(),
ClientSecret: model.NewId(),
OAuthTokenURL: "https://nowhere.com/oauth/token",
GrantType: model.OutgoingOAuthConnectionGrantTypeClientCredentials,
Audiences: []string{"https://nowhere.com"},
}
}
func cleanupOutgoingOAuthConnections(t *testing.T, ss store.Store) func() {
return func() {
// Delete all outgoing connections
connections, err := ss.OutgoingOAuthConnection().GetConnections(request.TestContext(t), model.OutgoingOAuthConnectionGetConnectionsFilter{
Limit: 100,
})
require.NoError(t, err)
for _, conn := range connections {
err := ss.OutgoingOAuthConnection().DeleteConnection(request.TestContext(t), conn.Id)
require.NoError(t, err)
}
}
}
func TestOutgoingOAuthConnectionStore(t *testing.T, rctx request.CTX, ss store.Store) {
t.Run("SaveConnection", func(t *testing.T) {
t.Cleanup(cleanupOutgoingOAuthConnections(t, ss))
testSaveOutgoingOAuthConnection(t, ss)
})
t.Run("UpdateConnection", func(t *testing.T) {
t.Cleanup(cleanupOutgoingOAuthConnections(t, ss))
testUpdateOutgoingOAuthConnection(t, ss)
})
t.Run("GetConnection", func(t *testing.T) {
t.Cleanup(cleanupOutgoingOAuthConnections(t, ss))
testGetOutgoingOAuthConnection(t, ss)
})
t.Run("GetConnections", func(t *testing.T) {
t.Cleanup(cleanupOutgoingOAuthConnections(t, ss))
testGetOutgoingOAuthConnections(t, ss)
})
t.Run("DeleteConnection", func(t *testing.T) {
t.Cleanup(cleanupOutgoingOAuthConnections(t, ss))
testDeleteOutgoingOAuthConnection(t, ss)
})
}
func testSaveOutgoingOAuthConnection(t *testing.T, ss store.Store) {
c := request.TestContext(t)
t.Run("save/get", func(t *testing.T) {
// Define test data
connection := newValidOutgoingOAuthConnection()
// Save the connection
_, err := ss.OutgoingOAuthConnection().SaveConnection(c, connection)
require.NoError(t, err)
// Retrieve the connection
storeConn, err := ss.OutgoingOAuthConnection().GetConnection(c, connection.Id)
require.NoError(t, err)
require.Equal(t, connection, storeConn)
})
t.Run("save without id should fail", func(t *testing.T) {
connection := &model.OutgoingOAuthConnection{
Id: model.NewId(),
}
_, err := ss.OutgoingOAuthConnection().SaveConnection(c, connection)
require.Error(t, err)
})
t.Run("save with incorrect grant type should fail", func(t *testing.T) {
connection := newValidOutgoingOAuthConnection()
connection.GrantType = "incorrect"
_, err := ss.OutgoingOAuthConnection().SaveConnection(c, connection)
require.Error(t, err)
})
}
func testUpdateOutgoingOAuthConnection(t *testing.T, ss store.Store) {
c := request.TestContext(t)
t.Run("update/get", func(t *testing.T) {
// Define test data
connection := newValidOutgoingOAuthConnection()
// Save the connection
_, err := ss.OutgoingOAuthConnection().SaveConnection(c, connection)
require.NoError(t, err)
// Update the connection
connection.Name = "Updated Name"
_, err = ss.OutgoingOAuthConnection().UpdateConnection(c, connection)
require.NoError(t, err)
// Retrieve the connection
storeConn, err := ss.OutgoingOAuthConnection().GetConnection(c, connection.Id)
require.NoError(t, err)
require.Equal(t, connection, storeConn)
})
t.Run("update non-existing", func(t *testing.T) {
connection := newValidOutgoingOAuthConnection()
connection.Id = model.NewId()
_, err := ss.OutgoingOAuthConnection().UpdateConnection(c, connection)
require.Error(t, err)
})
t.Run("update without id should fail", func(t *testing.T) {
connection := &model.OutgoingOAuthConnection{
Id: model.NewId(),
}
_, err := ss.OutgoingOAuthConnection().UpdateConnection(c, connection)
require.Error(t, err)
})
t.Run("update should update all fields", func(t *testing.T) {
// Define test data
connection := newValidOutgoingOAuthConnection()
// Save the connection
_, err := ss.OutgoingOAuthConnection().SaveConnection(c, connection)
require.NoError(t, err)
// Update the connection
connection.Name = "Updated Name"
connection.ClientId = "Updated ClientId"
connection.ClientSecret = "Updated ClientSecret"
connection.OAuthTokenURL = "https://nowhere.com/updated"
// connection.GrantType = "client_credentials" // ignoring since we only allow one for now
connection.Audiences = []string{"https://nowhere.com/updated"}
_, err = ss.OutgoingOAuthConnection().UpdateConnection(c, connection)
require.NoError(t, err)
// Retrieve the connection
storeConn, err := ss.OutgoingOAuthConnection().GetConnection(c, connection.Id)
require.NoError(t, err)
require.Equal(t, connection, storeConn)
})
}
func testGetOutgoingOAuthConnection(t *testing.T, ss store.Store) {
c := request.TestContext(t)
t.Run("get non-existing", func(t *testing.T) {
nonExistingId := model.NewId()
var expected *store.ErrNotFound
_, err := ss.OutgoingOAuthConnection().GetConnection(c, nonExistingId)
require.ErrorAs(t, err, &expected)
})
}
func testGetOutgoingOAuthConnections(t *testing.T, ss store.Store) {
c := request.TestContext(t)
// Define test data
connection1 := newValidOutgoingOAuthConnection()
connection2 := newValidOutgoingOAuthConnection()
connection3 := newValidOutgoingOAuthConnection()
// Save the connections
connection1, err := ss.OutgoingOAuthConnection().SaveConnection(c, connection1)
require.NoError(t, err)
connection2, err = ss.OutgoingOAuthConnection().SaveConnection(c, connection2)
require.NoError(t, err)
connection3, err = ss.OutgoingOAuthConnection().SaveConnection(c, connection3)
require.NoError(t, err)
connections := []*model.OutgoingOAuthConnection{connection1, connection2, connection3}
sort.Slice(connections, func(i, j int) bool {
return connections[i].Id < connections[j].Id
})
t.Run("get all", func(t *testing.T) {
// Retrieve the connections
conns, err := ss.OutgoingOAuthConnection().GetConnections(c, model.OutgoingOAuthConnectionGetConnectionsFilter{Limit: 3})
require.NoError(t, err)
require.Len(t, conns, 3)
})
t.Run("get connections using pagination", func(t *testing.T) {
// Retrieve the first page
conns, err := ss.OutgoingOAuthConnection().GetConnections(c, model.OutgoingOAuthConnectionGetConnectionsFilter{Limit: 1})
require.NoError(t, err)
require.Len(t, conns, 1)
require.Equal(t, connections[0].Id, conns[0].Id, "should return the first connection")
// Retrieve the second page
conns, err = ss.OutgoingOAuthConnection().GetConnections(c, model.OutgoingOAuthConnectionGetConnectionsFilter{OffsetId: connections[0].Id})
require.NoError(t, err)
require.Len(t, conns, 2)
require.Equal(t, connections[1].Id, conns[0].Id, "should return the second connection")
require.Equal(t, connections[2].Id, conns[1].Id, "should return the third connection")
})
}
func testDeleteOutgoingOAuthConnection(t *testing.T, ss store.Store) {
c := request.TestContext(t)
t.Run("delete", func(t *testing.T) {
// Define test data
connection := newValidOutgoingOAuthConnection()
// Save the connection
_, err := ss.OutgoingOAuthConnection().SaveConnection(c, connection)
require.NoError(t, err)
// Delete the connection
err = ss.OutgoingOAuthConnection().DeleteConnection(c, connection.Id)
require.NoError(t, err)
// Retrieve the connection
_, err = ss.OutgoingOAuthConnection().GetConnection(c, connection.Id)
var expected *store.ErrNotFound
require.ErrorAs(t, err, &expected)
})
}

Просмотреть файл

@@ -29,6 +29,7 @@ type Store struct {
ComplianceStore mocks.ComplianceStore
SessionStore mocks.SessionStore
OAuthStore mocks.OAuthStore
OutgoingOAuthConnectionStore mocks.OutgoingOAuthConnectionStore
SystemStore mocks.SystemStore
WebhookStore mocks.WebhookStore
CommandStore mocks.CommandStore
@@ -64,21 +65,24 @@ type Store struct {
DesktopTokensStore mocks.DesktopTokensStore
}
func (s *Store) SetContext(context context.Context) { s.context = context }
func (s *Store) Context() context.Context { return s.context }
func (s *Store) Team() store.TeamStore { return &s.TeamStore }
func (s *Store) Channel() store.ChannelStore { return &s.ChannelStore }
func (s *Store) Post() store.PostStore { return &s.PostStore }
func (s *Store) User() store.UserStore { return &s.UserStore }
func (s *Store) RetentionPolicy() store.RetentionPolicyStore { return &s.RetentionPolicyStore }
func (s *Store) Bot() store.BotStore { return &s.BotStore }
func (s *Store) ProductNotices() store.ProductNoticesStore { return &s.ProductNoticesStore }
func (s *Store) Audit() store.AuditStore { return &s.AuditStore }
func (s *Store) ClusterDiscovery() store.ClusterDiscoveryStore { return &s.ClusterDiscoveryStore }
func (s *Store) RemoteCluster() store.RemoteClusterStore { return &s.RemoteClusterStore }
func (s *Store) Compliance() store.ComplianceStore { return &s.ComplianceStore }
func (s *Store) Session() store.SessionStore { return &s.SessionStore }
func (s *Store) OAuth() store.OAuthStore { return &s.OAuthStore }
func (s *Store) SetContext(context context.Context) { s.context = context }
func (s *Store) Context() context.Context { return s.context }
func (s *Store) Team() store.TeamStore { return &s.TeamStore }
func (s *Store) Channel() store.ChannelStore { return &s.ChannelStore }
func (s *Store) Post() store.PostStore { return &s.PostStore }
func (s *Store) User() store.UserStore { return &s.UserStore }
func (s *Store) RetentionPolicy() store.RetentionPolicyStore { return &s.RetentionPolicyStore }
func (s *Store) Bot() store.BotStore { return &s.BotStore }
func (s *Store) ProductNotices() store.ProductNoticesStore { return &s.ProductNoticesStore }
func (s *Store) Audit() store.AuditStore { return &s.AuditStore }
func (s *Store) ClusterDiscovery() store.ClusterDiscoveryStore { return &s.ClusterDiscoveryStore }
func (s *Store) RemoteCluster() store.RemoteClusterStore { return &s.RemoteClusterStore }
func (s *Store) Compliance() store.ComplianceStore { return &s.ComplianceStore }
func (s *Store) Session() store.SessionStore { return &s.SessionStore }
func (s *Store) OAuth() store.OAuthStore { return &s.OAuthStore }
func (s *Store) OutgoingOAuthConnection() store.OutgoingOAuthConnectionStore {
return &s.OutgoingOAuthConnectionStore
}
func (s *Store) System() store.SystemStore { return &s.SystemStore }
func (s *Store) Webhook() store.WebhookStore { return &s.WebhookStore }
func (s *Store) Command() store.CommandStore { return &s.CommandStore }