github.com/cs3org/reva/v2@v2.27.7/pkg/user/manager/nextcloud/nextcloud_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 nextcloud_test 20 21 import ( 22 "context" 23 "os" 24 25 "google.golang.org/grpc/metadata" 26 "google.golang.org/protobuf/testing/protocmp" 27 28 userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 29 30 "github.com/cs3org/reva/v2/pkg/auth/scope" 31 ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" 32 jwt "github.com/cs3org/reva/v2/pkg/token/manager/jwt" 33 "github.com/cs3org/reva/v2/pkg/user/manager/nextcloud" 34 "github.com/cs3org/reva/v2/tests/helpers" 35 36 . "github.com/onsi/ginkgo/v2" 37 . "github.com/onsi/gomega" 38 ) 39 40 func setUpNextcloudServer() (*nextcloud.Manager, *[]string, func()) { 41 var conf *nextcloud.UserManagerConfig 42 43 ncHost := os.Getenv("NEXTCLOUD") 44 if len(ncHost) == 0 { 45 conf = &nextcloud.UserManagerConfig{ 46 EndPoint: "http://mock.com/apps/sciencemesh/", 47 MockHTTP: true, 48 } 49 nc, _ := nextcloud.NewUserManager(conf) 50 called := make([]string, 0) 51 h := nextcloud.GetNextcloudServerMock(&called) 52 mock, teardown := nextcloud.TestingHTTPClient(h) 53 nc.SetHTTPClient(mock) 54 return nc, &called, teardown 55 } 56 conf = &nextcloud.UserManagerConfig{ 57 EndPoint: ncHost + "/apps/sciencemesh/", 58 MockHTTP: false, 59 } 60 nc, _ := nextcloud.NewUserManager(conf) 61 return nc, nil, func() {} 62 } 63 64 func checkCalled(called *[]string, expected string) { 65 if called == nil { 66 return 67 } 68 Expect(len(*called)).To(Equal(1)) 69 Expect((*called)[0]).To(Equal(expected)) 70 } 71 72 var _ = Describe("Nextcloud", func() { 73 var ( 74 ctx context.Context 75 options map[string]interface{} 76 tmpRoot string 77 user = &userpb.User{ 78 Id: &userpb.UserId{ 79 Idp: "0.0.0.0:19000", 80 OpaqueId: "f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c", 81 Type: userpb.UserType_USER_TYPE_PRIMARY, 82 }, 83 Username: "tester", 84 } 85 ) 86 87 BeforeEach(func() { 88 var err error 89 tmpRoot, err := helpers.TempDir("reva-unit-tests-*-root") 90 Expect(err).ToNot(HaveOccurred()) 91 92 options = map[string]interface{}{ 93 "root": tmpRoot, 94 "enable_home": true, 95 "share_folder": "/Shares", 96 } 97 98 ctx = context.Background() 99 100 // Add auth token 101 tokenManager, err := jwt.New(map[string]interface{}{"secret": "changemeplease"}) 102 Expect(err).ToNot(HaveOccurred()) 103 scope, err := scope.AddOwnerScope(nil) 104 Expect(err).ToNot(HaveOccurred()) 105 t, err := tokenManager.MintToken(ctx, user, scope) 106 Expect(err).ToNot(HaveOccurred()) 107 ctx = ctxpkg.ContextSetToken(ctx, t) 108 ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, t) 109 ctx = ctxpkg.ContextSetUser(ctx, user) 110 }) 111 112 AfterEach(func() { 113 if tmpRoot != "" { 114 os.RemoveAll(tmpRoot) 115 } 116 }) 117 118 Describe("New", func() { 119 It("returns a new instance", func() { 120 _, err := nextcloud.New(options) 121 Expect(err).ToNot(HaveOccurred()) 122 }) 123 }) 124 125 // GetUser(ctx context.Context, uid *userpb.UserId) (*userpb.User, error) 126 Describe("GetUser", func() { 127 It("calls the GetUser endpoint", func() { 128 um, called, teardown := setUpNextcloudServer() 129 defer teardown() 130 131 user, err := um.GetUser(ctx, &userpb.UserId{ 132 Idp: "some-idp", 133 OpaqueId: "some-opaque-user-id", 134 Type: 1, 135 }, 136 false) 137 Expect(err).ToNot(HaveOccurred()) 138 Expect(user).To(Equal(&userpb.User{ 139 Id: &userpb.UserId{ 140 Idp: "some-idp", 141 OpaqueId: "some-opaque-user-id", 142 Type: 1, 143 }, 144 Username: "", 145 Mail: "", 146 MailVerified: false, 147 DisplayName: "", 148 Groups: nil, 149 Opaque: nil, 150 UidNumber: 0, 151 GidNumber: 0, 152 })) 153 checkCalled(called, `POST /apps/sciencemesh/~unauthenticated/api/user/GetUser {"idp":"some-idp","opaque_id":"some-opaque-user-id","type":1}`) 154 }) 155 }) 156 157 // GetUserByClaim(ctx context.Context, claim, value string) (*userpb.User, error) 158 Describe("GetUserByClaim", func() { 159 It("calls the GetUserByClaim endpoint", func() { 160 um, called, teardown := setUpNextcloudServer() 161 defer teardown() 162 163 user, err := um.GetUserByClaim(ctx, "claim-string", "value-string", false) 164 Expect(err).ToNot(HaveOccurred()) 165 Expect(user).To(Equal(&userpb.User{ 166 Id: &userpb.UserId{ 167 Idp: "some-idp", 168 OpaqueId: "some-opaque-user-id", 169 Type: 1, 170 }, 171 Username: "", 172 Mail: "", 173 MailVerified: false, 174 DisplayName: "", 175 Groups: nil, 176 Opaque: nil, 177 UidNumber: 0, 178 GidNumber: 0, 179 })) 180 checkCalled(called, `POST /apps/sciencemesh/~tester/api/user/GetUserByClaim {"claim":"claim-string","value":"value-string"}`) 181 }) 182 }) 183 184 // GetUserGroups(ctx context.Context, uid *userpb.UserId) ([]string, error) 185 Describe("GetUserGroups", func() { 186 It("calls the GetUserGroups endpoint", func() { 187 um, called, teardown := setUpNextcloudServer() 188 defer teardown() 189 190 groups, err := um.GetUserGroups(ctx, &userpb.UserId{ 191 Idp: "some-idp", 192 OpaqueId: "some-opaque-user-id", 193 Type: 1, 194 }) 195 Expect(err).ToNot(HaveOccurred()) 196 Expect(groups).To(Equal([]string{"wine-lovers"})) 197 checkCalled(called, `POST /apps/sciencemesh/~tester/api/user/GetUserGroups {"idp":"some-idp","opaque_id":"some-opaque-user-id","type":1}`) 198 }) 199 }) 200 201 // FindUsers(ctx context.Context, query string) ([]*userpb.User, error) 202 Describe("FindUsers", func() { 203 It("calls the FindUsers endpoint", func() { 204 um, called, teardown := setUpNextcloudServer() 205 defer teardown() 206 207 users, err := um.FindUsers(ctx, "some-query", false) 208 Expect(err).ToNot(HaveOccurred()) 209 Expect(len(users)).To(Equal(1)) 210 Expect(users[0]).To(BeComparableTo(&userpb.User{ 211 Id: &userpb.UserId{ 212 Idp: "some-idp", 213 OpaqueId: "some-opaque-user-id", 214 Type: 1, 215 }, 216 Username: "", 217 Mail: "", 218 MailVerified: false, 219 DisplayName: "", 220 Groups: nil, 221 Opaque: nil, 222 UidNumber: 0, 223 GidNumber: 0, 224 }, protocmp.Transform())) 225 checkCalled(called, `POST /apps/sciencemesh/~tester/api/user/FindUsers some-query`) 226 }) 227 }) 228 })