Adding changes to separate unit tests and integration tests (#13670)

* Introducing unit (not integration) tests for the app layer

* Initial support for unit tests at the API

* Adding unit tests support to the store layer

* Add unit tests support in commands

* Adding last tests needed for run unit tests properly

* Fixing govet

* Removing some duplication

* Fixing tests

* Fixing tests

* Not compiling test helpers with the main module for api

* Revert "Not compiling test helpers with the main module for api"

This reverts commit 36a199bbe0f7503f665f5d6c7b41c53aabc2e54f.

* Fixing tests

* Fixing unit tests

* More consistency between api4/apiteslib.go and app/helper_test.go

* Renaming things to make more obvious the new Setup functions purpose

* Reverting change in go.sum

* Start with empty mock for app layer

* Start with empty mock for api layer

* Start with empty mock for web layer

* Renaming SetupWithStoreMockConfig to SetupConfigWithStoreMock

* Fixing tests on web package

* Removing unnecesary function
Этот коммит содержится в:
Jesús Espino
2020-03-02 17:13:39 +01:00
коммит произвёл GitHub
родитель cd36c9f041
Коммит 7035e09fe9
27 изменённых файлов: 720 добавлений и 187 удалений

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

@@ -10,6 +10,8 @@ import (
"github.com/mattermost/mattermost-server/v5/app"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin/plugintest/mock"
"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -19,7 +21,7 @@ func handlerForHTTPErrors(c *Context, w http.ResponseWriter, r *http.Request) {
}
func TestHandlerServeHTTPErrors(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
web := New(th.Server, th.Server.AppOptions, th.Server.Router)
@@ -59,9 +61,20 @@ func handlerForHTTPSecureTransport(c *Context, w http.ResponseWriter, r *http.Re
}
func TestHandlerServeHTTPSecureTransport(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
mockStore := th.App.Srv().Store.(*mocks.Store)
mockUserStore := mocks.UserStore{}
mockUserStore.On("Count", mock.Anything).Return(int64(10), nil)
mockPostStore := mocks.PostStore{}
mockPostStore.On("GetMaxPostSize").Return(65535, nil)
mockSystemStore := mocks.SystemStore{}
mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
th.App.UpdateConfig(func(config *model.Config) {
*config.ServiceSettings.TLSStrictTransport = true
*config.ServiceSettings.TLSStrictTransportMaxAge = 6000
@@ -243,7 +256,7 @@ func handlerForCSPHeader(c *Context, w http.ResponseWriter, r *http.Request) {
func TestHandlerServeCSPHeader(t *testing.T) {
t.Run("non-static", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
web := New(th.Server, th.Server.AppOptions, th.Server.Router)
@@ -265,7 +278,7 @@ func TestHandlerServeCSPHeader(t *testing.T) {
})
t.Run("static, without subpath", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
web := New(th.Server, th.Server.AppOptions, th.Server.Router)
@@ -287,9 +300,20 @@ func TestHandlerServeCSPHeader(t *testing.T) {
})
t.Run("static, with subpath", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
mockStore := th.App.Srv().Store.(*mocks.Store)
mockUserStore := mocks.UserStore{}
mockUserStore.On("Count", mock.Anything).Return(int64(10), nil)
mockPostStore := mocks.PostStore{}
mockPostStore.On("GetMaxPostSize").Return(65535, nil)
mockSystemStore := mocks.SystemStore{}
mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.SiteURL = *cfg.ServiceSettings.SiteURL + "/subpath"
})
@@ -379,7 +403,7 @@ func TestHandlerServeInvalidToken(t *testing.T) {
func TestCheckCSRFToken(t *testing.T) {
t.Run("should allow a POST request with a valid CSRF token header", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
h := &Handler{
@@ -409,7 +433,7 @@ func TestCheckCSRFToken(t *testing.T) {
})
t.Run("should allow a POST request with an X-Requested-With header", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
h := &Handler{
@@ -440,9 +464,20 @@ func TestCheckCSRFToken(t *testing.T) {
})
t.Run("should not allow a POST request with an X-Requested-With header with strict CSRF enforcement enabled", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
mockStore := th.App.Srv().Store.(*mocks.Store)
mockUserStore := mocks.UserStore{}
mockUserStore.On("Count", mock.Anything).Return(int64(10), nil)
mockPostStore := mocks.PostStore{}
mockPostStore.On("GetMaxPostSize").Return(65535, nil)
mockSystemStore := mocks.SystemStore{}
mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.ExperimentalStrictCSRFEnforcement = true
})
@@ -475,7 +510,7 @@ func TestCheckCSRFToken(t *testing.T) {
})
t.Run("should not allow a POST request without either header", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
h := &Handler{
@@ -504,7 +539,7 @@ func TestCheckCSRFToken(t *testing.T) {
})
t.Run("should not check GET requests", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
h := &Handler{
@@ -533,7 +568,7 @@ func TestCheckCSRFToken(t *testing.T) {
})
t.Run("should not check a request passing the auth token in a header", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
h := &Handler{
@@ -562,7 +597,7 @@ func TestCheckCSRFToken(t *testing.T) {
})
t.Run("should not check a request passing a nil session", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
h := &Handler{
@@ -587,7 +622,7 @@ func TestCheckCSRFToken(t *testing.T) {
})
t.Run("should check requests for handlers that don't require a session but have one", func(t *testing.T) {
th := Setup(t)
th := SetupWithStoreMock(t)
defer th.TearDown()
h := &Handler{

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

@@ -17,6 +17,9 @@ import (
"github.com/mattermost/mattermost-server/v5/config"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
"github.com/mattermost/mattermost-server/v5/testlib"
"github.com/mattermost/mattermost-server/v5/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -39,10 +42,28 @@ type TestHelper struct {
tempWorkspace string
}
func SetupWithStoreMock(tb testing.TB) *TestHelper {
if testing.Short() {
tb.SkipNow()
}
store := testlib.GetMockStoreForSetupFunctions()
th := setupTestHelper(tb, store)
emptyMockStore := mocks.Store{}
emptyMockStore.On("Close").Return(nil)
th.App.Srv().Store = &emptyMockStore
return th
}
func Setup(tb testing.TB) *TestHelper {
if testing.Short() {
tb.SkipNow()
}
store := mainHelper.GetStore()
store.DropAllTables()
return setupTestHelper(tb, store)
}
func setupTestHelper(t testing.TB, store store.Store) *TestHelper {
memoryStore, err := config.NewMemoryStoreWithOptions(&config.MemoryStoreOptions{IgnoreEnvironmentOverrides: true})
if err != nil {
panic("failed to initialize memory store: " + err.Error())