github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/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  
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/store/sqlstore"
    15  	"github.com/mattermost/mattermost-server/utils"
    16  )
    17  
    18  func TestConfigListener(t *testing.T) {
    19  	th := Setup().InitBasic()
    20  	defer th.TearDown()
    21  
    22  	originalSiteName := th.App.Config().TeamSettings.SiteName
    23  	th.App.UpdateConfig(func(cfg *model.Config) {
    24  		cfg.TeamSettings.SiteName = "test123"
    25  	})
    26  
    27  	listenerCalled := false
    28  	listener := func(oldConfig *model.Config, newConfig *model.Config) {
    29  		if listenerCalled {
    30  			t.Fatal("listener called twice")
    31  		}
    32  
    33  		if oldConfig.TeamSettings.SiteName != "test123" {
    34  			t.Fatal("old config contains incorrect site name")
    35  		} else if newConfig.TeamSettings.SiteName != originalSiteName {
    36  			t.Fatal("new config contains incorrect site name")
    37  		}
    38  
    39  		listenerCalled = true
    40  	}
    41  	listenerId := th.App.AddConfigListener(listener)
    42  	defer th.App.RemoveConfigListener(listenerId)
    43  
    44  	listener2Called := false
    45  	listener2 := func(oldConfig *model.Config, newConfig *model.Config) {
    46  		if listener2Called {
    47  			t.Fatal("listener2 called twice")
    48  		}
    49  
    50  		listener2Called = true
    51  	}
    52  	listener2Id := th.App.AddConfigListener(listener2)
    53  	defer th.App.RemoveConfigListener(listener2Id)
    54  
    55  	th.App.ReloadConfig()
    56  
    57  	if !listenerCalled {
    58  		t.Fatal("listener should've been called")
    59  	} else if !listener2Called {
    60  		t.Fatal("listener 2 should've been called")
    61  	}
    62  }
    63  
    64  func TestAsymmetricSigningKey(t *testing.T) {
    65  	th := Setup().InitBasic()
    66  	defer th.TearDown()
    67  	assert.NotNil(t, th.App.AsymmetricSigningKey())
    68  	assert.NotEmpty(t, th.App.ClientConfig()["AsymmetricSigningPublicKey"])
    69  }
    70  
    71  func TestClientConfigWithComputed(t *testing.T) {
    72  	th := Setup().InitBasic()
    73  	defer th.TearDown()
    74  
    75  	config := th.App.ClientConfigWithComputed()
    76  	if _, ok := config["NoAccounts"]; !ok {
    77  		t.Fatal("expected NoAccounts in returned config")
    78  	}
    79  	if _, ok := config["MaxPostSize"]; !ok {
    80  		t.Fatal("expected MaxPostSize in returned config")
    81  	}
    82  }
    83  
    84  func TestEnsureInstallationDate(t *testing.T) {
    85  	th := Setup()
    86  	defer th.TearDown()
    87  
    88  	tt := []struct {
    89  		Name                     string
    90  		PrevInstallationDate     *int64
    91  		UsersCreationDates       []int64
    92  		ExpectedInstallationDate *int64
    93  	}{
    94  		{
    95  			Name:                     "New installation: no users, no installation date",
    96  			PrevInstallationDate:     nil,
    97  			UsersCreationDates:       nil,
    98  			ExpectedInstallationDate: model.NewInt64(utils.MillisFromTime(time.Now())),
    99  		},
   100  		{
   101  			Name:                     "Old installation: users, no installation date",
   102  			PrevInstallationDate:     nil,
   103  			UsersCreationDates:       []int64{10000000000, 30000000000, 20000000000},
   104  			ExpectedInstallationDate: model.NewInt64(10000000000),
   105  		},
   106  		{
   107  			Name:                     "New installation, second run: no users, installation date",
   108  			PrevInstallationDate:     model.NewInt64(80000000000),
   109  			UsersCreationDates:       []int64{10000000000, 30000000000, 20000000000},
   110  			ExpectedInstallationDate: model.NewInt64(80000000000),
   111  		},
   112  		{
   113  			Name:                     "Old installation already updated: users, installation date",
   114  			PrevInstallationDate:     model.NewInt64(90000000000),
   115  			UsersCreationDates:       []int64{10000000000, 30000000000, 20000000000},
   116  			ExpectedInstallationDate: model.NewInt64(90000000000),
   117  		},
   118  	}
   119  
   120  	for _, tc := range tt {
   121  		t.Run(tc.Name, func(t *testing.T) {
   122  			sqlStore := th.App.Srv.Store.User().(*sqlstore.SqlUserStore)
   123  			sqlStore.GetMaster().Exec("DELETE FROM Users")
   124  
   125  			for _, createAt := range tc.UsersCreationDates {
   126  				user := th.CreateUser()
   127  				user.CreateAt = createAt
   128  				sqlStore.GetMaster().Exec("UPDATE Users SET CreateAt = :CreateAt WHERE Id = :UserId", map[string]interface{}{"CreateAt": createAt, "UserId": user.Id})
   129  			}
   130  
   131  			if tc.PrevInstallationDate == nil {
   132  				<-th.App.Srv.Store.System().PermanentDeleteByName(model.SYSTEM_INSTALLATION_DATE_KEY)
   133  			} else {
   134  				<-th.App.Srv.Store.System().SaveOrUpdate(&model.System{
   135  					Name:  model.SYSTEM_INSTALLATION_DATE_KEY,
   136  					Value: strconv.FormatInt(*tc.PrevInstallationDate, 10),
   137  				})
   138  			}
   139  
   140  			err := th.App.ensureInstallationDate()
   141  
   142  			if tc.ExpectedInstallationDate == nil {
   143  				assert.Error(t, err)
   144  			} else {
   145  				assert.NoError(t, err)
   146  
   147  				result := <-th.App.Srv.Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY)
   148  				assert.Nil(t, result.Err)
   149  				data, _ := result.Data.(*model.System)
   150  				value, _ := strconv.ParseInt(data.Value, 10, 64)
   151  				assert.True(t, *tc.ExpectedInstallationDate <= value && *tc.ExpectedInstallationDate+1000 >= value)
   152  			}
   153  
   154  			sqlStore.GetMaster().Exec("DELETE FROM Users")
   155  		})
   156  	}
   157  }