github.com/cs3org/reva/v2@v2.27.7/pkg/user/manager/nextcloud/nextcloud_server_mock.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 20 21 import ( 22 "context" 23 "fmt" 24 "io" 25 "net" 26 "net/http" 27 "net/http/httptest" 28 "strings" 29 ) 30 31 // Response contains data for the Nextcloud mock server to respond 32 // and to switch to a new server state 33 type Response struct { 34 code int 35 body string 36 newServerState string 37 } 38 39 const serverStateError = "ERROR" 40 const serverStateEmpty = "EMPTY" 41 const serverStateHome = "HOME" 42 43 var serverState = serverStateEmpty 44 45 var responses = map[string]Response{ 46 `POST /apps/sciencemesh/~unauthenticated/api/user/GetUser {"idp":"some-idp","opaque_id":"some-opaque-user-id","type":1}`: {200, `{"id":{"idp":"some-idp","opaque_id":"some-opaque-user-id","type":1}}`, serverStateHome}, 47 `POST /apps/sciencemesh/~tester/api/user/GetUserByClaim {"claim":"claim-string","value":"value-string"}`: {200, `{"id":{"idp":"some-idp","opaque_id":"some-opaque-user-id","type":1}}`, serverStateHome}, 48 `POST /apps/sciencemesh/~tester/api/user/GetUserGroups {"idp":"some-idp","opaque_id":"some-opaque-user-id","type":1}`: {200, `["wine-lovers"]`, serverStateHome}, 49 `POST /apps/sciencemesh/~tester/api/user/FindUsers some-query`: {200, `[{"id":{"idp":"some-idp","opaque_id":"some-opaque-user-id","type":1}}]`, serverStateHome}, 50 } 51 52 // GetNextcloudServerMock returns a handler that pretends to be a remote Nextcloud server 53 func GetNextcloudServerMock(called *[]string) http.Handler { 54 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 55 buf := new(strings.Builder) 56 _, err := io.Copy(buf, r.Body) 57 if err != nil { 58 panic("Error reading response into buffer") 59 } 60 var key = fmt.Sprintf("%s %s %s", r.Method, r.URL, buf.String()) 61 *called = append(*called, key) 62 response := responses[key] 63 if (response == Response{}) { 64 key = fmt.Sprintf("%s %s %s %s", r.Method, r.URL, buf.String(), serverState) 65 // *called = append(*called, key) 66 response = responses[key] 67 } 68 if (response == Response{}) { 69 fmt.Printf("%s %s %s %s", r.Method, r.URL, buf.String(), serverState) 70 response = Response{500, fmt.Sprintf("response not defined! %s", key), serverStateEmpty} 71 } 72 serverState = responses[key].newServerState 73 if serverState == `` { 74 serverState = serverStateError 75 } 76 w.WriteHeader(response.code) 77 // w.Header().Set("Etag", "mocker-etag") 78 _, err = w.Write([]byte(responses[key].body)) 79 if err != nil { 80 panic(err) 81 } 82 }) 83 } 84 85 // TestingHTTPClient thanks to https://itnext.io/how-to-stub-requests-to-remote-hosts-with-go-6c2c1db32bf2 86 // Ideally, this function would live in tests/helpers, but 87 // if we put it there, it gets excluded by .dockerignore, and the 88 // Docker build fails (see https://github.com/cs3org/reva/issues/1999) 89 // So putting it here for now - open to suggestions if someone knows 90 // a better way to inject this. 91 func TestingHTTPClient(handler http.Handler) (*http.Client, func()) { 92 s := httptest.NewServer(handler) 93 94 cli := &http.Client{ 95 Transport: &http.Transport{ 96 DialContext: func(_ context.Context, network, _ string) (net.Conn, error) { 97 return net.Dial(network, s.Listener.Addr().String()) 98 }, 99 }, 100 } 101 102 return cli, s.Close 103 }