|
| 1 | +//go:build all || slow || auth |
| 2 | +// +build all slow auth |
| 3 | + |
| 4 | +package core |
| 5 | + |
| 6 | +// (C) Copyright IBM Corp. 2023. |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "net/http" |
| 23 | + "net/http/httptest" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +// Note: this unit test depends on some bogus hostnames being defined in /etc/hosts. |
| 31 | +// Append this to your /etc/hosts file: |
| 32 | +// # for testing |
| 33 | +// 127.0.0.1 region1.cloud.ibm.com region2.cloud.ibm.com region1.notcloud.ibm.com region2.notcloud.ibm.com |
| 34 | + |
| 35 | +var ( |
| 36 | + operationPath string = "/api/redirector" |
| 37 | + |
| 38 | + // To enable debug mode while running these tests, set this to LevelDebug. |
| 39 | + redirectTestLogLevel LogLevel = LevelError |
| 40 | +) |
| 41 | + |
| 42 | +// Start a mock server that will redirect requests to the second mock server |
| 43 | +// located at "redirectServerURL" |
| 44 | +func startMockServer1(t *testing.T, redirectServerURL string) *httptest.Server { |
| 45 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 46 | + t.Logf(`server1 received request: %s %s`, req.Method, req.URL.String()) |
| 47 | + |
| 48 | + // Make sure the Authorization header was sent. |
| 49 | + assert.NotEmpty(t, req.Header.Get("Authorization")) |
| 50 | + |
| 51 | + path := req.URL.Path |
| 52 | + location := redirectServerURL + path |
| 53 | + |
| 54 | + // Create the response (a 302 redirect). |
| 55 | + w.Header().Add("Location", location) |
| 56 | + w.WriteHeader(http.StatusFound) |
| 57 | + t.Logf(`Sent redirect request to: %s`, location) |
| 58 | + })) |
| 59 | + return server |
| 60 | +} |
| 61 | + |
| 62 | +// Start a second mock server to which redirected requests will be sent. |
| 63 | +func startMockServer2(t *testing.T) *httptest.Server { |
| 64 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 65 | + t.Logf(`server2 received request: %s %s`, req.Method, req.URL.String()) |
| 66 | + |
| 67 | + // Create the response. |
| 68 | + if req.Header.Get("Authorization") != "" { |
| 69 | + w.Header().Set("Content-Type", "application/json") |
| 70 | + w.WriteHeader(http.StatusOK) |
| 71 | + fmt.Fprintf(w, `{"name":"Jason Bourne"}`) |
| 72 | + } else { |
| 73 | + w.WriteHeader(http.StatusUnauthorized) |
| 74 | + } |
| 75 | + })) |
| 76 | + return server |
| 77 | +} |
| 78 | + |
| 79 | +func startServers(t *testing.T, host1 string, host2 string) (server1 *httptest.Server, server1URL string, |
| 80 | + server2 *httptest.Server, server2URL string) { |
| 81 | + server2 = startMockServer2(t) |
| 82 | + server2URL = strings.ReplaceAll(server2.URL, "127.0.0.1", host2) |
| 83 | + t.Logf(`Server 2 listening on endpoint: %s (%s)`, server2URL, server2.URL) |
| 84 | + |
| 85 | + server1 = startMockServer1(t, server2URL) |
| 86 | + server1URL = strings.ReplaceAll(server1.URL, "127.0.0.1", host1) |
| 87 | + t.Logf(`Server 1 listening on endpoint: %s (%s)`, server1URL, server1.URL) |
| 88 | + |
| 89 | + return |
| 90 | +} |
| 91 | + |
| 92 | +func testRedirection(t *testing.T, host1 string, host2 string, expectedStatusCode int) { |
| 93 | + GetLogger().SetLogLevel(redirectTestLogLevel) |
| 94 | + |
| 95 | + // Both servers within trusted domain. |
| 96 | + server1, server1URL, server2, _ := startServers(t, host1, host2) |
| 97 | + defer server1.Close() |
| 98 | + defer server2.Close() |
| 99 | + |
| 100 | + builder := NewRequestBuilder("GET") |
| 101 | + _, err := builder.ResolveRequestURL(server1URL, operationPath, nil) |
| 102 | + assert.Nil(t, err) |
| 103 | + req, _ := builder.Build() |
| 104 | + |
| 105 | + authenticator, err := NewBearerTokenAuthenticator("this is not a secret") |
| 106 | + assert.Nil(t, err) |
| 107 | + assert.NotNil(t, authenticator) |
| 108 | + |
| 109 | + options := &ServiceOptions{ |
| 110 | + URL: server1.URL, |
| 111 | + Authenticator: authenticator, |
| 112 | + } |
| 113 | + service, err := NewBaseService(options) |
| 114 | + assert.Nil(t, err) |
| 115 | + |
| 116 | + var foo *Foo |
| 117 | + detailedResponse, err := service.Request(req, &foo) |
| 118 | + assert.NotNil(t, detailedResponse) |
| 119 | + assert.Equal(t, expectedStatusCode, detailedResponse.StatusCode) |
| 120 | + if expectedStatusCode >= 200 && expectedStatusCode <= 299 { |
| 121 | + assert.Nil(t, err) |
| 122 | + |
| 123 | + result, ok := detailedResponse.Result.(*Foo) |
| 124 | + assert.Equal(t, true, ok) |
| 125 | + assert.NotNil(t, result) |
| 126 | + assert.NotNil(t, foo) |
| 127 | + assert.Equal(t, "Jason Bourne", *result.Name) |
| 128 | + } else { |
| 129 | + assert.NotNil(t, err) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestRedirectAuthSuccess1(t *testing.T) { |
| 134 | + testRedirection(t, "region1.cloud.ibm.com", "region2.cloud.ibm.com", http.StatusOK) |
| 135 | +} |
| 136 | + |
| 137 | +func TestRedirectAuthSuccess2(t *testing.T) { |
| 138 | + testRedirection(t, "region1.cloud.ibm.com", "region1.cloud.ibm.com", http.StatusOK) |
| 139 | +} |
| 140 | + |
| 141 | +func TestRedirectAuthSuccess3(t *testing.T) { |
| 142 | + testRedirection(t, "region1.notcloud.ibm.com", "region1.notcloud.ibm.com", http.StatusOK) |
| 143 | +} |
| 144 | + |
| 145 | +func TestRedirectAuthFail1(t *testing.T) { |
| 146 | + testRedirection(t, "region1.notcloud.ibm.com", "region2.cloud.ibm.com", http.StatusUnauthorized) |
| 147 | +} |
| 148 | + |
| 149 | +func TestRedirectAuthFail2(t *testing.T) { |
| 150 | + testRedirection(t, "region1.cloud.ibm.com", "region2.notcloud.ibm.com", http.StatusUnauthorized) |
| 151 | +} |
| 152 | + |
| 153 | +func TestRedirectAuthFail3(t *testing.T) { |
| 154 | + testRedirection(t, "region1.notcloud.ibm.com", "region2.notcloud.ibm.com", http.StatusUnauthorized) |
| 155 | +} |
0 commit comments