github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/enterprise_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  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/v5/einterfaces"
    10  	"github.com/mattermost/mattermost-server/v5/einterfaces/mocks"
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	storemocks "github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/mock"
    15  )
    16  
    17  func TestSAMLSettings(t *testing.T) {
    18  	tt := []struct {
    19  		name              string
    20  		setSAMLInterface  bool
    21  		setNewInterface   bool
    22  		useNewSAMLLibrary bool
    23  		isNil             bool
    24  		metadata          string
    25  	}{
    26  		{
    27  			name:              "No SAML Interfaces, default setting",
    28  			setSAMLInterface:  false,
    29  			setNewInterface:   false,
    30  			useNewSAMLLibrary: false,
    31  			isNil:             true,
    32  		},
    33  		{
    34  			name:              "No SAML Interfaces, set config true",
    35  			setSAMLInterface:  false,
    36  			setNewInterface:   false,
    37  			useNewSAMLLibrary: true,
    38  			isNil:             true,
    39  		},
    40  		{
    41  			name:              "Orignal SAML Interface, default setting",
    42  			setSAMLInterface:  true,
    43  			setNewInterface:   false,
    44  			useNewSAMLLibrary: false,
    45  			isNil:             false,
    46  			metadata:          "samlOne",
    47  		},
    48  		{
    49  			name:              "Orignal SAML Interface, config true",
    50  			setSAMLInterface:  true,
    51  			setNewInterface:   false,
    52  			useNewSAMLLibrary: true,
    53  			isNil:             false,
    54  			metadata:          "samlOne",
    55  		},
    56  		{
    57  			name:              "Both SAML Interfaces, default setting",
    58  			setSAMLInterface:  true,
    59  			setNewInterface:   true,
    60  			useNewSAMLLibrary: false,
    61  			isNil:             false,
    62  			metadata:          "samlOne",
    63  		},
    64  		{
    65  			name:              "Both SAML Interfaces, config true",
    66  			setSAMLInterface:  true,
    67  			setNewInterface:   true,
    68  			useNewSAMLLibrary: true,
    69  			isNil:             false,
    70  			metadata:          "samlTwo",
    71  		},
    72  	}
    73  
    74  	for _, tc := range tt {
    75  		t.Run(tc.name, func(t *testing.T) {
    76  			saml := &mocks.SamlInterface{}
    77  			saml.Mock.On("ConfigureSP").Return(nil)
    78  			saml.Mock.On("GetMetadata").Return("samlOne", nil)
    79  			if tc.setSAMLInterface {
    80  				RegisterSamlInterface(func(a *App) einterfaces.SamlInterface {
    81  					return saml
    82  				})
    83  			} else {
    84  				RegisterSamlInterface(nil)
    85  			}
    86  
    87  			saml2 := &mocks.SamlInterface{}
    88  			saml2.Mock.On("ConfigureSP").Return(nil)
    89  			saml2.Mock.On("GetMetadata").Return("samlTwo", nil)
    90  			if tc.setNewInterface {
    91  				RegisterNewSamlInterface(func(a *App) einterfaces.SamlInterface {
    92  					return saml2
    93  				})
    94  			} else {
    95  				RegisterNewSamlInterface(nil)
    96  			}
    97  
    98  			th := SetupEnterpriseWithStoreMock(t)
    99  			defer th.TearDown()
   100  
   101  			mockStore := th.App.Srv().Store.(*storemocks.Store)
   102  			mockUserStore := storemocks.UserStore{}
   103  			mockUserStore.On("Count", mock.Anything).Return(int64(10), nil)
   104  			mockPostStore := storemocks.PostStore{}
   105  			mockPostStore.On("GetMaxPostSize").Return(65535, nil)
   106  			mockSystemStore := storemocks.SystemStore{}
   107  			mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)
   108  			mockSystemStore.On("GetByName", "FirstServerRunTimestamp").Return(&model.System{Name: "FirstServerRunTimestamp", Value: "10"}, nil)
   109  
   110  			mockStore.On("User").Return(&mockUserStore)
   111  			mockStore.On("Post").Return(&mockPostStore)
   112  			mockStore.On("System").Return(&mockSystemStore)
   113  
   114  			if tc.useNewSAMLLibrary {
   115  				th.App.UpdateConfig(func(cfg *model.Config) {
   116  					*cfg.ExperimentalSettings.UseNewSAMLLibrary = tc.useNewSAMLLibrary
   117  				})
   118  			}
   119  
   120  			th.Server.initEnterprise()
   121  			th.App.initEnterprise()
   122  			if tc.isNil {
   123  				assert.Nil(t, th.App.Srv().Saml)
   124  			} else {
   125  				assert.NotNil(t, th.App.Srv().Saml)
   126  				metadata, err := th.App.Srv().Saml.GetMetadata()
   127  				assert.Nil(t, err)
   128  				assert.Equal(t, tc.metadata, metadata)
   129  			}
   130  		})
   131  	}
   132  }