github.com/minio/console@v1.4.1/api/user_session_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2022 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 "context" 21 "os" 22 "reflect" 23 "testing" 24 25 "github.com/minio/console/pkg/utils" 26 27 "github.com/minio/console/models" 28 "github.com/minio/console/pkg/auth/idp/oauth2" 29 "github.com/minio/console/pkg/auth/ldap" 30 "github.com/stretchr/testify/assert" 31 ) 32 33 func Test_getSessionResponse(t *testing.T) { 34 type args struct { 35 ctx context.Context 36 session *models.Principal 37 } 38 ctx := context.WithValue(context.Background(), utils.ContextClientIP, "127.0.0.1") 39 tests := []struct { 40 name string 41 args args 42 want *models.SessionResponse 43 wantErr bool 44 preFunc func() 45 postFunc func() 46 }{ 47 { 48 name: "empty session", 49 args: args{ 50 ctx: ctx, 51 session: nil, 52 }, 53 want: nil, 54 wantErr: true, 55 }, 56 { 57 name: "malformed session", 58 args: args{ 59 ctx: ctx, 60 session: &models.Principal{ 61 STSAccessKeyID: "W257A03HTI7L30F7YCRD", 62 STSSecretAccessKey: "g+QVorWQR8aSy+k3OHOoYn0qKpENld72faCMfYps", 63 STSSessionToken: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3NLZXkiOiJXMjU3QTAzSFRJN0wzMEY3WUNSRCIsImV4cCI6MTY1MTAxNjU1OCwicGFyZW50IjoibWluaW8ifQ.uFFIIEQ6qM_QvMM297ODi_uK2IA1pwvsDbyBGErkQKqtbY_Ynte8GUkNsSHBEMCT9Fr7uUwaxK41kUqjtbqAwA", 64 AccountAccessKey: "minio", 65 Hm: false, 66 }, 67 }, 68 want: nil, 69 wantErr: true, 70 }, 71 } 72 for _, tt := range tests { 73 tt := tt 74 t.Run(tt.name, func(_ *testing.T) { 75 if tt.preFunc != nil { 76 tt.preFunc() 77 } 78 session, err := getSessionResponse(tt.args.ctx, tt.args.session) 79 if (err != nil) != tt.wantErr { 80 t.Errorf("getSessionResponse() error = %v, wantErr %v", err, tt.wantErr) 81 return 82 } 83 if !reflect.DeepEqual(session, tt.want) { 84 t.Errorf("getSessionResponse() got = %v, want %v", session, tt.want) 85 } 86 if tt.postFunc != nil { 87 tt.postFunc() 88 } 89 }) 90 } 91 } 92 93 func Test_getListOfEnabledFeatures(t *testing.T) { 94 type args struct { 95 session *models.Principal 96 } 97 tests := []struct { 98 name string 99 args args 100 want []string 101 preFunc func() 102 postFunc func() 103 }{ 104 { 105 name: "all features are enabled", 106 args: args{ 107 session: &models.Principal{ 108 STSAccessKeyID: "", 109 STSSecretAccessKey: "", 110 STSSessionToken: "", 111 AccountAccessKey: "", 112 Hm: true, 113 }, 114 }, 115 want: []string{"log-search", "oidc-idp", "external-idp", "ldap-idp", "external-idp", "hide-menu"}, 116 preFunc: func() { 117 os.Setenv(ConsoleLogQueryURL, "http://logsearchapi:8080") 118 os.Setenv(oauth2.ConsoleIDPURL, "http://external-idp.com") 119 os.Setenv(oauth2.ConsoleIDPClientID, "eaeaeaeaeaea") 120 os.Setenv(ldap.ConsoleLDAPEnabled, "on") 121 }, 122 postFunc: func() { 123 os.Unsetenv(ConsoleLogQueryURL) 124 os.Unsetenv(oauth2.ConsoleIDPURL) 125 os.Unsetenv(oauth2.ConsoleIDPClientID) 126 os.Unsetenv(ldap.ConsoleLDAPEnabled) 127 }, 128 }, 129 } 130 for _, tt := range tests { 131 t.Run(tt.name, func(_ *testing.T) { 132 if tt.preFunc != nil { 133 tt.preFunc() 134 } 135 assert.Equalf(t, tt.want, getListOfEnabledFeatures(context.Background(), nil, tt.args.session), "getListOfEnabledFeatures(%v)", tt.args.session) 136 if tt.postFunc != nil { 137 tt.postFunc() 138 } 139 }) 140 } 141 }