github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/config_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/mock"
    13  
    14  	"github.com/mattermost/mattermost-server/v5/model"
    15  	"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
    16  	"github.com/mattermost/mattermost-server/v5/utils"
    17  )
    18  
    19  func TestConfigListener(t *testing.T) {
    20  	th := Setup(t)
    21  	defer th.TearDown()
    22  
    23  	originalSiteName := th.App.Config().TeamSettings.SiteName
    24  
    25  	listenerCalled := false
    26  	listener := func(oldConfig *model.Config, newConfig *model.Config) {
    27  		assert.False(t, listenerCalled, "listener called twice")
    28  
    29  		assert.Equal(t, *originalSiteName, *oldConfig.TeamSettings.SiteName, "old config contains incorrect site name")
    30  		assert.Equal(t, "test123", *newConfig.TeamSettings.SiteName, "new config contains incorrect site name")
    31  
    32  		listenerCalled = true
    33  	}
    34  	listenerId := th.App.AddConfigListener(listener)
    35  	defer th.App.RemoveConfigListener(listenerId)
    36  
    37  	listener2Called := false
    38  	listener2 := func(oldConfig *model.Config, newConfig *model.Config) {
    39  		assert.False(t, listener2Called, "listener2 called twice")
    40  
    41  		listener2Called = true
    42  	}
    43  	listener2Id := th.App.AddConfigListener(listener2)
    44  	defer th.App.RemoveConfigListener(listener2Id)
    45  
    46  	th.App.UpdateConfig(func(cfg *model.Config) {
    47  		*cfg.TeamSettings.SiteName = "test123"
    48  	})
    49  
    50  	assert.True(t, listenerCalled, "listener should've been called")
    51  	assert.True(t, listener2Called, "listener 2 should've been called")
    52  }
    53  
    54  func TestAsymmetricSigningKey(t *testing.T) {
    55  	th := SetupWithStoreMock(t)
    56  	defer th.TearDown()
    57  	assert.NotNil(t, th.App.AsymmetricSigningKey())
    58  	assert.NotEmpty(t, th.App.ClientConfig()["AsymmetricSigningPublicKey"])
    59  }
    60  
    61  func TestPostActionCookieSecret(t *testing.T) {
    62  	th := SetupWithStoreMock(t)
    63  	defer th.TearDown()
    64  	assert.Equal(t, 32, len(th.App.PostActionCookieSecret()))
    65  }
    66  
    67  func TestClientConfigWithComputed(t *testing.T) {
    68  	th := SetupWithStoreMock(t)
    69  	defer th.TearDown()
    70  
    71  	mockStore := th.App.Srv().Store.(*mocks.Store)
    72  	mockUserStore := mocks.UserStore{}
    73  	mockUserStore.On("Count", mock.Anything).Return(int64(10), nil)
    74  	mockPostStore := mocks.PostStore{}
    75  	mockPostStore.On("GetMaxPostSize").Return(65535, nil)
    76  	mockSystemStore := mocks.SystemStore{}
    77  	mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)
    78  	mockStore.On("User").Return(&mockUserStore)
    79  	mockStore.On("Post").Return(&mockPostStore)
    80  	mockStore.On("System").Return(&mockSystemStore)
    81  
    82  	config := th.App.ClientConfigWithComputed()
    83  	_, ok := config["NoAccounts"]
    84  	assert.True(t, ok, "expected NoAccounts in returned config")
    85  	_, ok = config["MaxPostSize"]
    86  	assert.True(t, ok, "expected MaxPostSize in returned config")
    87  }
    88  
    89  func TestEnsureInstallationDate(t *testing.T) {
    90  	th := Setup(t)
    91  	defer th.TearDown()
    92  
    93  	tt := []struct {
    94  		Name                     string
    95  		PrevInstallationDate     *int64
    96  		UsersCreationDates       []int64
    97  		ExpectedInstallationDate *int64
    98  	}{
    99  		{
   100  			Name:                     "New installation: no users, no installation date",
   101  			PrevInstallationDate:     nil,
   102  			UsersCreationDates:       nil,
   103  			ExpectedInstallationDate: model.NewInt64(utils.MillisFromTime(time.Now())),
   104  		},
   105  		{
   106  			Name:                     "Old installation: users, no installation date",
   107  			PrevInstallationDate:     nil,
   108  			UsersCreationDates:       []int64{10000000000, 30000000000, 20000000000},
   109  			ExpectedInstallationDate: model.NewInt64(10000000000),
   110  		},
   111  		{
   112  			Name:                     "New installation, second run: no users, installation date",
   113  			PrevInstallationDate:     model.NewInt64(80000000000),
   114  			UsersCreationDates:       []int64{10000000000, 30000000000, 20000000000},
   115  			ExpectedInstallationDate: model.NewInt64(80000000000),
   116  		},
   117  		{
   118  			Name:                     "Old installation already updated: users, installation date",
   119  			PrevInstallationDate:     model.NewInt64(90000000000),
   120  			UsersCreationDates:       []int64{10000000000, 30000000000, 20000000000},
   121  			ExpectedInstallationDate: model.NewInt64(90000000000),
   122  		},
   123  	}
   124  
   125  	for _, tc := range tt {
   126  		t.Run(tc.Name, func(t *testing.T) {
   127  			sqlStore := th.GetSqlSupplier()
   128  			sqlStore.GetMaster().Exec("DELETE FROM Users")
   129  
   130  			for _, createAt := range tc.UsersCreationDates {
   131  				user := th.CreateUser()
   132  				user.CreateAt = createAt
   133  				sqlStore.GetMaster().Exec("UPDATE Users SET CreateAt = :CreateAt WHERE Id = :UserId", map[string]interface{}{"CreateAt": createAt, "UserId": user.Id})
   134  			}
   135  
   136  			if tc.PrevInstallationDate == nil {
   137  				th.App.Srv().Store.System().PermanentDeleteByName(model.SYSTEM_INSTALLATION_DATE_KEY)
   138  			} else {
   139  				th.App.Srv().Store.System().SaveOrUpdate(&model.System{
   140  					Name:  model.SYSTEM_INSTALLATION_DATE_KEY,
   141  					Value: strconv.FormatInt(*tc.PrevInstallationDate, 10),
   142  				})
   143  			}
   144  
   145  			err := th.App.Srv().ensureInstallationDate()
   146  
   147  			if tc.ExpectedInstallationDate == nil {
   148  				assert.Error(t, err)
   149  			} else {
   150  				assert.NoError(t, err)
   151  
   152  				data, err := th.App.Srv().Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY)
   153  				assert.Nil(t, err)
   154  				value, _ := strconv.ParseInt(data.Value, 10, 64)
   155  				assert.True(t, *tc.ExpectedInstallationDate <= value && *tc.ExpectedInstallationDate+1000 >= value)
   156  			}
   157  
   158  			sqlStore.GetMaster().Exec("DELETE FROM Users")
   159  		})
   160  	}
   161  }