github.com/minio/console@v1.4.1/api/utils_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2021 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "net/http" 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestDifferenceArrays(t *testing.T) { 28 assert := assert.New(t) 29 exampleArrayAMock := []string{"a", "b", "c"} 30 exampleArrayBMock := []string{"b", "d"} 31 resultABArrayMock := []string{"a", "c"} 32 resultBAArrayMock := []string{"d"} 33 34 // Test-1: test DifferenceArrays() with array a vs array b 35 diffArray := DifferenceArrays(exampleArrayAMock, exampleArrayBMock) 36 assert.ElementsMatchf(diffArray, resultABArrayMock, "return array AB doesn't match %s") 37 38 // Test-2: test DifferenceArrays() with array b vs array a 39 diffArray2 := DifferenceArrays(exampleArrayBMock, exampleArrayAMock) 40 assert.ElementsMatchf(diffArray2, resultBAArrayMock, "return array BA doesn't match %s") 41 } 42 43 func TestIsElementInArray(t *testing.T) { 44 assert := assert.New(t) 45 46 exampleElementsArray := []string{"c", "a", "d", "b"} 47 48 // Test-1: test IsElementInArray() with element that is in the list 49 responseArray := IsElementInArray(exampleElementsArray, "a") 50 assert.Equal(true, responseArray) 51 52 // Test-2: test IsElementInArray() with element that is not in the list 53 responseArray2 := IsElementInArray(exampleElementsArray, "e") 54 assert.Equal(false, responseArray2) 55 } 56 57 func TestUniqueKeys(t *testing.T) { 58 assert := assert.New(t) 59 60 exampleMixedArray := []string{"a", "b", "c", "e", "d", "b", "c", "h", "f", "g"} 61 exampleUniqueArray := []string{"a", "b", "c", "e", "d", "h", "f", "g"} 62 63 // Test-1 test UniqueKeys returns an array with unique elements 64 responseArray := UniqueKeys(exampleMixedArray) 65 assert.ElementsMatchf(responseArray, exampleUniqueArray, "returned array doesn't contain the correct elements %s") 66 } 67 68 func TestRandomCharStringWithAlphabet(t *testing.T) { 69 type args struct { 70 n int 71 alphabet string 72 } 73 tests := []struct { 74 name string 75 args args 76 want string 77 }{ 78 { 79 name: "generated random string has the right length", 80 args: args{ 81 n: 10, 82 alphabet: "A", 83 }, 84 want: "AAAAAAAAAA", 85 }, 86 } 87 for _, tt := range tests { 88 t.Run(tt.name, func(_ *testing.T) { 89 assert.Equalf(t, tt.want, RandomCharStringWithAlphabet(tt.args.n, tt.args.alphabet), "RandomCharStringWithAlphabet(%v, %v)", tt.args.n, tt.args.alphabet) 90 }) 91 } 92 } 93 94 func TestNewSessionCookieForConsole(t *testing.T) { 95 type args struct { 96 token string 97 } 98 tests := []struct { 99 name string 100 args args 101 want http.Cookie 102 }{ 103 { 104 name: "session cookie has the right token an security configuration", 105 args: args{ 106 token: "jwt-xxxxxxxxx", 107 }, 108 want: http.Cookie{ 109 Path: "/", 110 Value: "jwt-xxxxxxxxx", 111 HttpOnly: true, 112 SameSite: http.SameSiteLaxMode, 113 Name: "token", 114 MaxAge: 43200, 115 Expires: time.Now().Add(1 * time.Hour), 116 }, 117 }, 118 } 119 for _, tt := range tests { 120 t.Run(tt.name, func(_ *testing.T) { 121 got := NewSessionCookieForConsole(tt.args.token) 122 assert.Equalf(t, tt.want.Value, got.Value, "NewSessionCookieForConsole(%v)", tt.args.token) 123 assert.Equalf(t, tt.want.Path, got.Path, "NewSessionCookieForConsole(%v)", tt.args.token) 124 assert.Equalf(t, tt.want.HttpOnly, got.HttpOnly, "NewSessionCookieForConsole(%v)", tt.args.token) 125 assert.Equalf(t, tt.want.Name, got.Name, "NewSessionCookieForConsole(%v)", tt.args.token) 126 assert.Equalf(t, tt.want.MaxAge, got.MaxAge, "NewSessionCookieForConsole(%v)", tt.args.token) 127 assert.Equalf(t, tt.want.SameSite, got.SameSite, "NewSessionCookieForConsole(%v)", tt.args.token) 128 }) 129 } 130 } 131 132 func TestExpireSessionCookie(t *testing.T) { 133 tests := []struct { 134 name string 135 want http.Cookie 136 }{ 137 { 138 name: "cookie is expired correctly", 139 want: http.Cookie{ 140 Name: "token", 141 Value: "", 142 MaxAge: -1, 143 }, 144 }, 145 } 146 for _, tt := range tests { 147 t.Run(tt.name, func(_ *testing.T) { 148 got := ExpireSessionCookie() 149 assert.Equalf(t, tt.want.Name, got.Name, "ExpireSessionCookie()") 150 assert.Equalf(t, tt.want.Value, got.Value, "ExpireSessionCookie()") 151 assert.Equalf(t, tt.want.MaxAge, got.MaxAge, "ExpireSessionCookie()") 152 }) 153 } 154 } 155 156 func TestSanitizeEncodedPrefix(t *testing.T) { 157 type args struct { 158 rawPrefix string 159 } 160 tests := []struct { 161 name string 162 args args 163 want string 164 }{ 165 { 166 name: "replace spaces with +", 167 args: args{ 168 rawPrefix: "hello world", 169 }, 170 want: "hello+world", 171 }, 172 { 173 name: "replace spaces with +", 174 args: args{ 175 rawPrefix: " hello-world ", 176 }, 177 want: "+++hello-world+++", 178 }, 179 } 180 for _, tt := range tests { 181 t.Run(tt.name, func(_ *testing.T) { 182 assert.Equalf(t, tt.want, SanitizeEncodedPrefix(tt.args.rawPrefix), "SanitizeEncodedPrefix(%v)", tt.args.rawPrefix) 183 }) 184 } 185 } 186 187 func Test_isSafeToPreview(t *testing.T) { 188 type args struct { 189 str string 190 } 191 tests := []struct { 192 name string 193 args args 194 want bool 195 }{ 196 { 197 name: "mime type is safe to preview", 198 args: args{ 199 str: "image/jpeg", 200 }, 201 want: true, 202 }, 203 { 204 name: "mime type is not safe to preview", 205 args: args{ 206 str: "application/zip", 207 }, 208 want: false, 209 }, 210 } 211 for _, tt := range tests { 212 t.Run(tt.name, func(_ *testing.T) { 213 assert.Equalf(t, tt.want, isSafeToPreview(tt.args.str), "isSafeToPreview(%v)", tt.args.str) 214 }) 215 } 216 } 217 218 func TestRandomCharString(t *testing.T) { 219 type args struct { 220 n int 221 } 222 tests := []struct { 223 name string 224 args args 225 wantLength int 226 }{ 227 { 228 name: "valid string", 229 args: args{ 230 n: 1, 231 }, 232 wantLength: 1, 233 }, { 234 name: "valid string", 235 args: args{ 236 n: 64, 237 }, 238 wantLength: 64, 239 }, 240 } 241 for _, tt := range tests { 242 t.Run(tt.name, func(_ *testing.T) { 243 assert.Equalf(t, tt.wantLength, len(RandomCharString(tt.args.n)), "RandomCharString(%v)", tt.args.n) 244 }) 245 } 246 } 247 248 func TestValidateEncodedStyles(t *testing.T) { 249 type args struct { 250 encodedStyles string 251 } 252 tests := []struct { 253 name string 254 args args 255 wantErr bool 256 }{ 257 { 258 name: "valid", 259 args: args{ 260 encodedStyles: "ewogICJiYWNrZ3JvdW5kQ29sb3IiOiAiIzFjMWMxYyIsCiAgImZvbnRDb2xvciI6ICJ3aGl0ZSIsCiAgInNlY29uZGFyeUZvbnRDb2xvciI6ICJncmV5IiwKICAiYm9yZGVyQ29sb3IiOiAieWVsbG93IiwKICAibG9hZGVyQ29sb3IiOiAicmVkIiwKICAiYm94QmFja2dyb3VuZCI6ICIjMDU3OWFmIiwKICAib2tDb2xvciI6ICIjMDhhZjA1IiwKICAiZXJyb3JDb2xvciI6ICIjYmYxZTQ2IiwKICAid2FybkNvbG9yIjogIiNiZmFjMWUiLAogICJsaW5rQ29sb3IiOiAiIzFlYmZiZiIsCiAgImRpc2FibGVkTGlua0NvbG9yIjogIiM5ZGEwYTAiLAogICJob3ZlckxpbmtDb2xvciI6ICIjMGY0ZWJjIiwKICAidGFibGVDb2xvcnMiOiB7CiAgICAiYm9yZGVyIjogIiM0YmJjMGYiLAogICAgImRpc2FibGVkQm9yZGVyIjogIiM3MjhlNjMiLAogICAgImRpc2FibGVkQkciOiAiIzcyOGU2MyIsCiAgICAic2VsZWN0ZWQiO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IiOiAiIzc1OGU4ZCIsCiAgICAiZGlzYWJsZWRUZXh0IjogIiNkOWRkZGQiCiAgfSwKICAiaW5wdXRCb3giOiB7CiAgICAiYm9yZGVyIjogImdyZWVuIiwKICAgICJob3ZlckJvcmRlciI6ICJibHVlIiwKICAgICJ0ZXh0Q29sb3IiOiAid2hpdGUiLAogICAgImJhY2tncm91bmRDb2xvciI6ICIjMjhkNGZmIgogIH0sCiAgInN3aXRjaCI6IHsKICAgICJzd2l0Y2hCYWNrZ3JvdW5kIjogIiMyOGQ0ZmYiLAogICAgImJ1bGxldEJvcmRlckNvbG9yIjogIiNhNmFjYWQiLAogICAgImJ1bGxldEJHQ29sb3IiOiAiI2RjZTFlMiIsCiAgICAiZGlzYWJsZWRCYWNrZ3JvdW5kIjogIiM0NzQ5NDkiLAogICAgImRpc2FibGVkQnVsbGV0Qm9yZGVyQ29sb3IiOiAiIzQ3NDk0OSIsCiAgICAiZGlzYWJsZWRCdWxsZXRCR0NvbG9yIjogIiM3Mzk3YTAiCiAgfQp9", 261 }, 262 wantErr: false, 263 }, 264 { 265 name: "invalid config", 266 args: args{ 267 encodedStyles: "ewogICJvb3JnbGUiOiAic3MiCn0===", 268 }, 269 wantErr: true, 270 }, 271 { 272 name: "invalid style config", 273 args: args{ 274 encodedStyles: "e30=", 275 }, 276 wantErr: true, 277 }, 278 { 279 name: "invalid base64", 280 args: args{ 281 encodedStyles: "duck", 282 }, 283 wantErr: true, 284 }, 285 } 286 for _, tt := range tests { 287 t.Run(tt.name, func(_ *testing.T) { 288 if tt.wantErr { 289 assert.NotNilf(t, ValidateEncodedStyles(tt.args.encodedStyles), "Wanted an error") 290 } else { 291 assert.Nilf(t, ValidateEncodedStyles(tt.args.encodedStyles), "Did not wanted an error") 292 } 293 }) 294 } 295 } 296 297 func TestSanitizeEncodedPrefix1(t *testing.T) { 298 type args struct { 299 rawPrefix string 300 } 301 tests := []struct { 302 name string 303 args args 304 want string 305 }{ 306 { 307 name: "input sanitized", 308 args: args{ 309 rawPrefix: "x y", 310 }, 311 want: "x+y", 312 }, 313 } 314 for _, tt := range tests { 315 t.Run(tt.name, func(_ *testing.T) { 316 assert.Equalf(t, tt.want, SanitizeEncodedPrefix(tt.args.rawPrefix), "SanitizeEncodedPrefix(%v)", tt.args.rawPrefix) 317 }) 318 } 319 }