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

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package config
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  )
    13  
    14  func TestGetStructFields(t *testing.T) {
    15  	type testStruct struct {
    16  		FieldOne       string
    17  		SecondField    bool
    18  		SomeOtherField int
    19  	}
    20  
    21  	fields := getStructFields(testStruct{})
    22  	require.Equal(t,
    23  		[]string{
    24  			"FieldOne",
    25  			"SecondField",
    26  			"SomeOtherField",
    27  		},
    28  		fields,
    29  	)
    30  
    31  	featureFlagsFields := getStructFields(model.FeatureFlags{})
    32  	require.Contains(t, featureFlagsFields, "TestFeature")
    33  }
    34  
    35  func TestFeatureFlagsFromMap(t *testing.T) {
    36  	for name, tc := range map[string]struct {
    37  		FeatureMap        map[string]string
    38  		Base              model.FeatureFlags
    39  		ExpectedTestValue string
    40  	}{
    41  		"empty": {
    42  			FeatureMap:        map[string]string{},
    43  			Base:              model.FeatureFlags{},
    44  			ExpectedTestValue: "",
    45  		},
    46  		"no base value": {
    47  			FeatureMap:        map[string]string{"TestFeature": "expectedvalue"},
    48  			Base:              model.FeatureFlags{},
    49  			ExpectedTestValue: "expectedvalue",
    50  		},
    51  		"only base value": {
    52  			FeatureMap:        map[string]string{},
    53  			Base:              model.FeatureFlags{TestFeature: "somebasevalue"},
    54  			ExpectedTestValue: "somebasevalue",
    55  		},
    56  		"override base value": {
    57  			FeatureMap:        map[string]string{"TestFeature": "overridevalue"},
    58  			Base:              model.FeatureFlags{TestFeature: "somebasevalue"},
    59  			ExpectedTestValue: "overridevalue",
    60  		},
    61  		"override base value with extras": {
    62  			FeatureMap:        map[string]string{"TestFeature": "overridevalue", "SomeOldFlag": "oldvalue"},
    63  			Base:              model.FeatureFlags{TestFeature: "somebasevalue"},
    64  			ExpectedTestValue: "overridevalue",
    65  		},
    66  		"all values do not exist": {
    67  			FeatureMap:        map[string]string{"SomeOldFlag": "oldvalue"},
    68  			Base:              model.FeatureFlags{},
    69  			ExpectedTestValue: "",
    70  		},
    71  		"bool on": {
    72  			FeatureMap:        map[string]string{"TestBoolFeature": "on"},
    73  			Base:              model.FeatureFlags{TestBoolFeature: true},
    74  			ExpectedTestValue: "",
    75  		},
    76  		"bool true": {
    77  			FeatureMap:        map[string]string{"TestBoolFeature": "true"},
    78  			Base:              model.FeatureFlags{TestBoolFeature: true},
    79  			ExpectedTestValue: "",
    80  		},
    81  		"bool True": {
    82  			FeatureMap:        map[string]string{"TestBoolFeature": "True"},
    83  			Base:              model.FeatureFlags{TestBoolFeature: true},
    84  			ExpectedTestValue: "",
    85  		},
    86  		"bool 1": {
    87  			FeatureMap:        map[string]string{"TestBoolFeature": "1"},
    88  			Base:              model.FeatureFlags{TestBoolFeature: true},
    89  			ExpectedTestValue: "",
    90  		},
    91  		"bool off": {
    92  			FeatureMap:        map[string]string{"TestBoolFeature": "off"},
    93  			Base:              model.FeatureFlags{},
    94  			ExpectedTestValue: "",
    95  		},
    96  		"bool false": {
    97  			FeatureMap:        map[string]string{"TestBoolFeature": "false"},
    98  			Base:              model.FeatureFlags{},
    99  			ExpectedTestValue: "",
   100  		},
   101  		"bool other value": {
   102  			FeatureMap:        map[string]string{"TestBoolFeature": "someotherbadvalue"},
   103  			Base:              model.FeatureFlags{},
   104  			ExpectedTestValue: "",
   105  		},
   106  	} {
   107  		t.Run(name, func(t *testing.T) {
   108  			require.Equal(t, tc.ExpectedTestValue, featureFlagsFromMap(tc.FeatureMap, tc.Base).TestFeature)
   109  		})
   110  	}
   111  }
   112  
   113  func TestFeatureFlagsToMap(t *testing.T) {
   114  	for name, tc := range map[string]struct {
   115  		Flags            model.FeatureFlags
   116  		TestFeatureValue string
   117  	}{
   118  		"empty": {
   119  			TestFeatureValue: "",
   120  			Flags:            model.FeatureFlags{},
   121  		},
   122  		"simple value": {
   123  			TestFeatureValue: "expectedvalue",
   124  			Flags:            model.FeatureFlags{TestFeature: "expectedvalue"},
   125  		},
   126  		"empty value": {
   127  			TestFeatureValue: "",
   128  			Flags:            model.FeatureFlags{TestFeature: ""},
   129  		},
   130  	} {
   131  		t.Run(name, func(t *testing.T) {
   132  			require.Equal(t, tc.TestFeatureValue, featureFlagsToMap(&tc.Flags)["TestFeature"])
   133  		})
   134  	}
   135  }
   136  
   137  func TestFeatureFlagsToMapBool(t *testing.T) {
   138  	for name, tc := range map[string]struct {
   139  		Flags            model.FeatureFlags
   140  		TestFeatureValue string
   141  	}{
   142  		"false": {
   143  			TestFeatureValue: "false",
   144  			Flags:            model.FeatureFlags{},
   145  		},
   146  		"true": {
   147  			TestFeatureValue: "true",
   148  			Flags:            model.FeatureFlags{TestBoolFeature: true},
   149  		},
   150  	} {
   151  		t.Run(name, func(t *testing.T) {
   152  			require.Equal(t, tc.TestFeatureValue, featureFlagsToMap(&tc.Flags)["TestBoolFeature"])
   153  		})
   154  	}
   155  }