github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/plugin/helpers_config_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package plugin_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	"github.com/mattermost/mattermost-server/v5/plugin"
    13  	"github.com/mattermost/mattermost-server/v5/plugin/plugintest"
    14  )
    15  
    16  func TestCheckRequiredServerConfiguration(t *testing.T) {
    17  	for name, test := range map[string]struct {
    18  		SetupAPI     func(*plugintest.API) *plugintest.API
    19  		Input        *model.Config
    20  		ShouldReturn bool
    21  		ShouldError  bool
    22  	}{
    23  		"no required config therefore it should be compatible": {
    24  			SetupAPI: func(api *plugintest.API) *plugintest.API {
    25  				return api
    26  			},
    27  			Input:        nil,
    28  			ShouldReturn: true,
    29  			ShouldError:  false,
    30  		},
    31  		"contains required configuration": {
    32  			SetupAPI: func(api *plugintest.API) *plugintest.API {
    33  				api.On("GetConfig").Return(&model.Config{
    34  					ServiceSettings: model.ServiceSettings{
    35  						EnableCommands: model.NewBool(true),
    36  					},
    37  					TeamSettings: model.TeamSettings{
    38  						EnableUserCreation: model.NewBool(true),
    39  					},
    40  				})
    41  
    42  				return api
    43  			},
    44  			Input: &model.Config{
    45  				ServiceSettings: model.ServiceSettings{
    46  					EnableCommands: model.NewBool(true),
    47  				},
    48  			},
    49  			ShouldReturn: true,
    50  			ShouldError:  false,
    51  		},
    52  		"does not contain required configuration": {
    53  			SetupAPI: func(api *plugintest.API) *plugintest.API {
    54  				api.On("GetConfig").Return(&model.Config{
    55  					ServiceSettings: model.ServiceSettings{
    56  						EnableCommands: model.NewBool(true),
    57  					},
    58  				})
    59  
    60  				return api
    61  			},
    62  			Input: &model.Config{
    63  				ServiceSettings: model.ServiceSettings{
    64  					EnableCommands: model.NewBool(true),
    65  				},
    66  				TeamSettings: model.TeamSettings{
    67  					EnableUserCreation: model.NewBool(true),
    68  				},
    69  			},
    70  			ShouldReturn: false,
    71  			ShouldError:  false,
    72  		},
    73  		"different configurations": {
    74  			SetupAPI: func(api *plugintest.API) *plugintest.API {
    75  				api.On("GetConfig").Return(&model.Config{
    76  					ServiceSettings: model.ServiceSettings{
    77  						EnableCommands: model.NewBool(false),
    78  					},
    79  				})
    80  
    81  				return api
    82  			},
    83  			Input: &model.Config{
    84  				ServiceSettings: model.ServiceSettings{
    85  					EnableCommands: model.NewBool(true),
    86  				},
    87  			},
    88  			ShouldReturn: false,
    89  			ShouldError:  false,
    90  		},
    91  		"non-existent configuration": {
    92  			SetupAPI: func(api *plugintest.API) *plugintest.API {
    93  				api.On("GetConfig").Return(&model.Config{})
    94  
    95  				return api
    96  			},
    97  			Input: &model.Config{
    98  				ServiceSettings: model.ServiceSettings{
    99  					EnableCommands: model.NewBool(true),
   100  				},
   101  			},
   102  			ShouldReturn: false,
   103  			ShouldError:  false,
   104  		},
   105  	} {
   106  		t.Run(name, func(t *testing.T) {
   107  			api := test.SetupAPI(&plugintest.API{})
   108  			defer api.AssertExpectations(t)
   109  
   110  			p := &plugin.HelpersImpl{}
   111  			p.API = api
   112  
   113  			ok, err := p.CheckRequiredServerConfiguration(test.Input)
   114  
   115  			assert.Equal(t, test.ShouldReturn, ok)
   116  			if test.ShouldError {
   117  				assert.NotNil(t, err)
   118  			} else {
   119  				assert.Nil(t, err)
   120  			}
   121  		})
   122  	}
   123  }