github.com/cs3org/reva/v2@v2.27.7/pkg/auth/manager/json/json_test.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package json 20 21 import ( 22 "context" 23 "os" 24 "testing" 25 26 user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 var ctx = context.Background() 31 32 var input map[string]interface{} 33 34 type ExpectedError struct { 35 message string 36 } 37 38 func TestGetManagerWithInvalidUser(t *testing.T) { 39 tests := []struct { 40 name string 41 user interface{} 42 expectedError string 43 }{ 44 { 45 "Boolean in user", 46 false, 47 "error decoding conf: 1 error(s) decoding:\n\n* " + 48 "'users' expected type 'string', got unconvertible type 'bool', value: 'false'", 49 }, 50 { 51 "Nil in user", 52 nil, 53 "open /etc/revad/users.json: no such file or directory", 54 }, 55 } 56 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 input = map[string]interface{}{ 60 "users": tt.user, 61 } 62 63 manager, err := New(input) 64 65 assert.Empty(t, manager) 66 assert.EqualError(t, err, tt.expectedError) 67 }) 68 } 69 } 70 71 func TestGetManagerWithJSONObject(t *testing.T) { 72 tests := []struct { 73 name string 74 user string 75 expectManager bool 76 expectedError *ExpectedError 77 }{ 78 { 79 "Invalid JSON object", 80 "[{", 81 false, 82 &ExpectedError{"unexpected end of JSON input"}, 83 }, 84 { 85 "JSON object with correct user metadata", 86 `[{"username":"einstein","secret":"albert"}]`, 87 true, 88 nil, 89 }, 90 } 91 92 var tmpFile *os.File 93 94 // add tempdir 95 tmpDir, err := os.MkdirTemp("", "json_test") 96 if err != nil { 97 t.Fatalf("Error while creating temp dir: %v", err) 98 } 99 defer os.RemoveAll(tmpDir) 100 101 for _, tt := range tests { 102 // get file handler for temporary file 103 tmpFile, err = os.CreateTemp(tmpDir, "json_test") 104 if err != nil { 105 t.Fatalf("Error while opening temp file: %v", err) 106 } 107 108 // write json object to tempdir 109 _, err = tmpFile.WriteString(tt.user) 110 if err != nil { 111 t.Fatalf("Error while writing temp file: %v", err) 112 } 113 114 t.Run(tt.name, func(t *testing.T) { 115 // get manager 116 input = map[string]interface{}{ 117 "users": tmpFile.Name(), 118 } 119 120 manager, err := New(input) 121 122 if tt.expectManager { 123 assert.Equal(t, nil, err) 124 } else if !tt.expectManager { 125 assert.Empty(t, manager) 126 assert.EqualError(t, err, tt.expectedError.message) 127 } 128 }) 129 // cleanup 130 os.Remove(tmpFile.Name()) 131 } 132 } 133 134 func TestGetAuthenticatedManager(t *testing.T) { 135 tests := []struct { 136 name string 137 username string 138 secret string 139 expectAuthenticated bool 140 expectedError *ExpectedError 141 }{ 142 { 143 "Authenticate with incorrect user password", 144 "einstein", 145 "NotARealPassword", 146 false, 147 &ExpectedError{ 148 "error: invalid credentials: einstein", 149 }, 150 }, 151 { 152 "Authenticate with correct user auth credentials", 153 "einstein", 154 "albert", 155 true, 156 nil, 157 }, 158 } 159 160 // add tempdir 161 tempdir, err := os.MkdirTemp("", "json_test") 162 if err != nil { 163 t.Fatalf("Error while creating temp dir: %v", err) 164 } 165 defer os.RemoveAll(tempdir) 166 167 // get file handler for temporary file 168 tempFile, err := os.CreateTemp(tempdir, "json_test") 169 if err != nil { 170 t.Fatalf("Error while opening temp file: %v", err) 171 } 172 173 // write json object to tempdir 174 _, err = tempFile.WriteString(`[{"username":"einstein","secret":"albert"}]`) 175 if err != nil { 176 t.Fatalf("Error while writing temp file: %v", err) 177 } 178 179 // get manager 180 input := map[string]interface{}{ 181 "users": tempFile.Name(), 182 } 183 manager, _ := New(input) 184 185 for _, tt := range tests { 186 t.Run(tt.name, func(t *testing.T) { 187 authenticated, _, err := manager.Authenticate(ctx, tt.username, tt.secret) 188 if !tt.expectAuthenticated { 189 assert.Empty(t, authenticated) 190 assert.EqualError(t, err, tt.expectedError.message) 191 } else { 192 assert.IsType(t, &user.User{}, authenticated) 193 assert.Empty(t, err) 194 assert.Equal(t, tt.username, authenticated.Username) 195 } 196 // cleanup 197 os.Remove(tempFile.Name()) 198 }) 199 } 200 }