|
7 | 7 | "net/http" |
8 | 8 | "net/http/httptest" |
9 | 9 | "net/url" |
| 10 | + "os" |
10 | 11 | "reflect" |
11 | 12 | "testing" |
12 | 13 |
|
@@ -52,6 +53,111 @@ func TestOpen(t *testing.T) { |
52 | 53 | expectEquals(t, oc.oauth2Config.Endpoint.TokenURL, fmt.Sprintf("%s/oauth/token", s.URL)) |
53 | 54 | } |
54 | 55 |
|
| 56 | +func TestOpenWithEnvsSuccess(t *testing.T) { |
| 57 | + s := newTestServer(map[string]interface{}{}) |
| 58 | + defer s.Close() |
| 59 | + |
| 60 | + hostURL, err := url.Parse(s.URL) |
| 61 | + expectNil(t, err) |
| 62 | + |
| 63 | + _, err = http.NewRequest("GET", hostURL.String(), nil) |
| 64 | + expectNil(t, err) |
| 65 | + |
| 66 | + os.Setenv("TEST_CLIENT_ID", "testClientId") |
| 67 | + os.Setenv("TEST_CLIENT_SECRET", "testClientSecret") |
| 68 | + |
| 69 | + c := Config{ |
| 70 | + Issuer: s.URL, |
| 71 | + ClientIDFromEnv: "TEST_CLIENT_ID", |
| 72 | + ClientSecretFromEnv: "TEST_CLIENT_SECRET", |
| 73 | + RedirectURI: "https://localhost/callback", |
| 74 | + InsecureCA: true, |
| 75 | + } |
| 76 | + |
| 77 | + logger := logrus.New() |
| 78 | + |
| 79 | + oconfig, err := c.Open("id", logger) |
| 80 | + |
| 81 | + oc, ok := oconfig.(*openshiftConnector) |
| 82 | + |
| 83 | + expectNil(t, err) |
| 84 | + expectEquals(t, ok, true) |
| 85 | + expectEquals(t, oc.apiURL, s.URL) |
| 86 | + expectEquals(t, oc.clientID, "testClientId") |
| 87 | + expectEquals(t, oc.clientSecret, "testClientSecret") |
| 88 | + expectEquals(t, oc.redirectURI, "https://localhost/callback") |
| 89 | + expectEquals(t, oc.oauth2Config.Endpoint.AuthURL, fmt.Sprintf("%s/oauth/authorize", s.URL)) |
| 90 | + expectEquals(t, oc.oauth2Config.Endpoint.TokenURL, fmt.Sprintf("%s/oauth/token", s.URL)) |
| 91 | +} |
| 92 | + |
| 93 | +func TestOpenFailuresForEnvCases(t *testing.T) { |
| 94 | + s := newTestServer(map[string]interface{}{}) |
| 95 | + defer s.Close() |
| 96 | + |
| 97 | + hostURL, err := url.Parse(s.URL) |
| 98 | + expectNil(t, err) |
| 99 | + |
| 100 | + _, err = http.NewRequest("GET", hostURL.String(), nil) |
| 101 | + expectNil(t, err) |
| 102 | + |
| 103 | + tests := []struct { |
| 104 | + clientID string |
| 105 | + clientIDFromEnv string |
| 106 | + clientSecret string |
| 107 | + clientSecretFromEnv string |
| 108 | + expectedError error |
| 109 | + }{ |
| 110 | + { |
| 111 | + clientID: "", |
| 112 | + clientIDFromEnv: "", |
| 113 | + clientSecret: "testClientSecret", |
| 114 | + clientSecretFromEnv: "", |
| 115 | + expectedError: fmt.Errorf("invalid config: clientID or clientIDEnv are required for the OpenShift connector"), |
| 116 | + }, |
| 117 | + { |
| 118 | + clientID: "clientID", |
| 119 | + clientIDFromEnv: "TEST_CLIENT_ID", |
| 120 | + clientSecret: "", |
| 121 | + clientSecretFromEnv: "TEST_CLIENT_SECRET", |
| 122 | + expectedError: fmt.Errorf("invalid config: clientID and clientIDEnv are exclusive for the OpenShift connector"), |
| 123 | + }, |
| 124 | + { |
| 125 | + clientID: "clientID", |
| 126 | + clientIDFromEnv: "", |
| 127 | + clientSecret: "", |
| 128 | + clientSecretFromEnv: "", |
| 129 | + expectedError: fmt.Errorf("invalid config: clientSecret or clientSecretEnv are required for the OpenShift connector"), |
| 130 | + }, |
| 131 | + { |
| 132 | + clientID: "", |
| 133 | + clientIDFromEnv: "TEST_CLIENT_ID", |
| 134 | + clientSecret: "clientSecret", |
| 135 | + clientSecretFromEnv: "TEST_CLIENT_SECRET", |
| 136 | + expectedError: fmt.Errorf("invalid config: clientSecret and clientSecretEnv are exclusive for the OpenShift connector"), |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + for _, tc := range tests { |
| 141 | + c := Config{ |
| 142 | + Issuer: s.URL, |
| 143 | + ClientID: tc.clientID, |
| 144 | + ClientIDFromEnv: tc.clientIDFromEnv, |
| 145 | + ClientSecret: tc.clientSecret, |
| 146 | + ClientSecretFromEnv: tc.clientSecretFromEnv, |
| 147 | + RedirectURI: "https://localhost/callback", |
| 148 | + InsecureCA: true, |
| 149 | + } |
| 150 | + |
| 151 | + logger := logrus.New() |
| 152 | + |
| 153 | + oconfig, err := c.Open("id", logger) |
| 154 | + expectEquals(t, err, tc.expectedError) |
| 155 | + |
| 156 | + _, ok := oconfig.(*openshiftConnector) |
| 157 | + expectEquals(t, ok, false) |
| 158 | + } |
| 159 | +} |
| 160 | + |
55 | 161 | func TestGetUser(t *testing.T) { |
56 | 162 | s := newTestServer(map[string]interface{}{ |
57 | 163 | "/apis/user.openshift.io/v1/users/~": user{ |
|
0 commit comments