github.com/cs3org/reva/v2@v2.27.7/pkg/user/manager/demo/demo_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 demo 20 21 import ( 22 "context" 23 "reflect" 24 "testing" 25 26 userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 27 "github.com/cs3org/reva/v2/pkg/errtypes" 28 "github.com/google/go-cmp/cmp" 29 "google.golang.org/protobuf/proto" 30 "google.golang.org/protobuf/testing/protocmp" 31 ) 32 33 var ctx = context.Background() 34 35 func TestUserManager(t *testing.T) { 36 // get manager 37 manager, _ := New(nil) 38 39 // setup test data 40 uidEinstein := &userpb.UserId{Idp: "http://localhost:9998", OpaqueId: "4c510ada-c86b-4815-8820-42cdf82c3d51", Type: userpb.UserType_USER_TYPE_PRIMARY} 41 userEinstein := &userpb.User{ 42 Id: uidEinstein, 43 Username: "einstein", 44 Groups: []string{"sailing-lovers", "violin-haters", "physics-lovers"}, 45 Mail: "einstein@example.org", 46 DisplayName: "Albert Einstein", 47 UidNumber: 123, 48 GidNumber: 987, 49 } 50 userEinsteinWithoutGroups := &userpb.User{ 51 Id: uidEinstein, 52 Username: "einstein", 53 Mail: "einstein@example.org", 54 DisplayName: "Albert Einstein", 55 UidNumber: 123, 56 GidNumber: 987, 57 } 58 59 uidFake := &userpb.UserId{Idp: "nonesense", OpaqueId: "fakeUser"} 60 groupsEinstein := []string{"sailing-lovers", "violin-haters", "physics-lovers"} 61 62 // positive test GetUserByClaim by uid 63 resUserByUID, _ := manager.GetUserByClaim(ctx, "uid", "123", false) 64 if !proto.Equal(resUserByUID, userEinstein) { 65 t.Fatalf("user differs: expected=%v got=%v", userEinstein, resUserByUID) 66 } 67 68 // negative test GetUserByClaim by uid 69 expectedErr := errtypes.NotFound("789") 70 _, err := manager.GetUserByClaim(ctx, "uid", "789", false) 71 if !reflect.DeepEqual(err, expectedErr) { 72 t.Fatalf("user not found error differs: expected='%v' got='%v'", expectedErr, err) 73 } 74 75 // positive test GetUserByClaim by mail 76 resUserByEmail, _ := manager.GetUserByClaim(ctx, "mail", "einstein@example.org", false) 77 if !proto.Equal(resUserByEmail, userEinstein) { 78 t.Fatalf("user differs: expected=%v got=%v", userEinstein, resUserByEmail) 79 } 80 81 // positive test GetUserByClaim by uid without groups 82 resUserByUIDWithoutGroups, _ := manager.GetUserByClaim(ctx, "uid", "123", true) 83 if !proto.Equal(resUserByUIDWithoutGroups, userEinsteinWithoutGroups) { 84 t.Fatalf("user differs: expected=%v got=%v", userEinsteinWithoutGroups, resUserByUIDWithoutGroups) 85 } 86 87 // positive test GetUserGroups 88 resGroups, _ := manager.GetUserGroups(ctx, uidEinstein) 89 if !reflect.DeepEqual(resGroups, groupsEinstein) { 90 t.Fatalf("groups differ: expected=%v got=%v", groupsEinstein, resGroups) 91 } 92 93 // negative test GetUserGroups 94 expectedErr = errtypes.NotFound(uidFake.OpaqueId) 95 _, err = manager.GetUserGroups(ctx, uidFake) 96 if !reflect.DeepEqual(err, expectedErr) { 97 t.Fatalf("user not found error differs: expected='%v' got='%v'", expectedErr, err) 98 } 99 100 // test FindUsers 101 resUser, _ := manager.FindUsers(ctx, "einstein", false) 102 if !cmp.Equal(resUser, []*userpb.User{userEinstein}, protocmp.Transform()) { 103 t.Fatalf("user differs: expected=%v got=%v", []*userpb.User{userEinstein}, resUser) 104 } 105 106 // negative test FindUsers 107 resUsers, _ := manager.FindUsers(ctx, "notARealUser", false) 108 if len(resUsers) > 0 { 109 t.Fatalf("user not in group: expected=%v got=%v", []*userpb.User{}, resUsers) 110 } 111 }