github.com/decred/politeia@v1.4.0/politeiad/politeiad_test.go (about) 1 // Copyright (c) 2021 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import "testing" 8 9 func TestParsePluginSetting(t *testing.T) { 10 var tests = []struct { 11 name string 12 13 // Input 14 setting string 15 16 // Expected output 17 err bool 18 pluginID string 19 settingKey string 20 settingValue string 21 }{ 22 { 23 "invalid setting not csv", 24 "somepluginsetting", 25 true, 26 "", 27 "", 28 "", 29 }, 30 { 31 "invalid setting not enough fields", 32 "pluginid,key", 33 true, 34 "", 35 "", 36 "", 37 }, 38 { 39 "invalid setting no closing brackets", 40 "pluginid,key,[value1,value2", 41 true, 42 "", 43 "", 44 "", 45 }, 46 { 47 "invalid setting not json", 48 "pluginid,key,[value1,value2,value3]", 49 true, 50 "", 51 "", 52 "", 53 }, 54 { 55 "single value normal", 56 "pluginid,key,value", 57 false, 58 "pluginid", 59 "key", 60 "value", 61 }, 62 { 63 "single value mixed case", 64 "pLugInID,KeY,vAlUe", 65 false, 66 "pluginid", 67 "key", 68 "vAlUe", 69 }, 70 { 71 "single value whitespaces", 72 " pluginid, key, value", 73 false, 74 "pluginid", 75 "key", 76 "value", 77 }, 78 { 79 "multi value normal", 80 `pluginid,key,["value1","value2","value3"]`, 81 false, 82 "pluginid", 83 "key", 84 `["value1","value2","value3"]`, 85 }, 86 { 87 "multi value whitespaces", 88 `pluginid,key,["value1", "value2", "value3"]`, 89 false, 90 "pluginid", 91 "key", 92 `["value1","value2","value3"]`, 93 }, 94 { 95 "multi value with one entry", 96 `pluginid,key,["value1"]`, 97 false, 98 "pluginid", 99 "key", 100 `["value1"]`, 101 }, 102 { 103 "multi value with escaped quotes", 104 `pluginid,key,["value1","value2","\"","value4"]`, 105 false, 106 "pluginid", 107 "key", 108 `["value1","value2","\"","value4"]`, 109 }, 110 { 111 "multi value with comma value", 112 `pluginid,key,["value1","value2",",","value4"]`, 113 false, 114 "pluginid", 115 "key", 116 `["value1","value2",",","value4"]`, 117 }, 118 } 119 for _, v := range tests { 120 t.Run(v.name, func(t *testing.T) { 121 pluginID, ps, err := parsePluginSetting(v.setting) 122 switch { 123 case v.err && err == nil: 124 t.Errorf("got nil error, expected failure for '%v'", 125 v.setting) 126 127 case v.err && err != nil: 128 // Receieved the expected error output. Continue. 129 130 case !v.err && err != nil: 131 t.Errorf("got error '%v', want nil error", err) 132 133 case pluginID != v.pluginID: 134 t.Errorf("invalid plugin id: got %v, want %v", 135 pluginID, v.pluginID) 136 137 case ps.Key != v.settingKey: 138 t.Errorf("invalid plugin setting key: got %v, want %v", 139 ps.Key, v.settingKey) 140 141 case ps.Value != v.settingValue: 142 t.Errorf("invalid plugin setting value: got %v, want %v", 143 ps.Value, v.settingValue) 144 } 145 }) 146 } 147 }