|
1 | 1 | package org.kohsuke.github; |
2 | 2 |
|
| 3 | +import org.apache.commons.lang3.SystemUtils; |
3 | 4 | import org.junit.Assume; |
4 | 5 | import org.junit.Test; |
| 6 | +import org.kohsuke.github.authorization.AuthorizationProvider; |
5 | 7 | import org.kohsuke.github.authorization.UserAuthorizationProvider; |
6 | 8 |
|
| 9 | +import java.io.File; |
| 10 | +import java.io.FileOutputStream; |
7 | 11 | import java.io.IOException; |
8 | 12 | import java.lang.reflect.Field; |
9 | 13 | import java.util.Collections; |
10 | 14 | import java.util.HashMap; |
11 | 15 | import java.util.Map; |
| 16 | +import java.util.Properties; |
12 | 17 |
|
13 | 18 | import static org.hamcrest.Matchers.*; |
14 | 19 |
|
@@ -137,6 +142,147 @@ public void testGitHubBuilderFromCustomEnvironment() throws IOException { |
137 | 142 | assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login")); |
138 | 143 | } |
139 | 144 |
|
| 145 | + @Test |
| 146 | + public void testGitHubBuilderFromCredentialsWithEnvironment() throws IOException { |
| 147 | + // we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+ |
| 148 | + Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0)); |
| 149 | + Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); |
| 150 | + |
| 151 | + Map<String, String> props = new HashMap<String, String>(); |
| 152 | + |
| 153 | + props.put("endpoint", "bogus endpoint url"); |
| 154 | + props.put("oauth", "bogus oauth token string"); |
| 155 | + setupEnvironment(props); |
| 156 | + GitHubBuilder builder = GitHubBuilder.fromCredentials(); |
| 157 | + |
| 158 | + assertThat(builder.endpoint, equalTo("bogus endpoint url")); |
| 159 | + |
| 160 | + assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class)); |
| 161 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("token bogus oauth token string")); |
| 162 | + assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), nullValue()); |
| 163 | + |
| 164 | + props.put("login", "bogus login"); |
| 165 | + setupEnvironment(props); |
| 166 | + builder = GitHubBuilder.fromCredentials(); |
| 167 | + |
| 168 | + assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class)); |
| 169 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("token bogus oauth token string")); |
| 170 | + assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login")); |
| 171 | + |
| 172 | + props.put("jwt", "bogus jwt token string"); |
| 173 | + setupEnvironment(props); |
| 174 | + builder = GitHubBuilder.fromCredentials(); |
| 175 | + |
| 176 | + assertThat(builder.authorizationProvider, not(instanceOf(UserAuthorizationProvider.class))); |
| 177 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), equalTo("Bearer bogus jwt token string")); |
| 178 | + |
| 179 | + props.put("password", "bogus weak password"); |
| 180 | + setupEnvironment(props); |
| 181 | + builder = GitHubBuilder.fromCredentials(); |
| 182 | + |
| 183 | + assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class)); |
| 184 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), |
| 185 | + equalTo("Basic Ym9ndXMgbG9naW46Ym9ndXMgd2VhayBwYXNzd29yZA==")); |
| 186 | + assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login")); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testGitHubBuilderFromCredentialsWithPropertyFile() throws IOException { |
| 191 | + // we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+ |
| 192 | + Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0)); |
| 193 | + Assume.assumeFalse(SystemUtils.IS_OS_WINDOWS); |
| 194 | + |
| 195 | + Map<String, String> props = new HashMap<String, String>(); |
| 196 | + |
| 197 | + // Clear the environment |
| 198 | + setupEnvironment(props); |
| 199 | + try { |
| 200 | + GitHubBuilder.HOME_DIRECTORY = new File(getTestDirectory()); |
| 201 | + try { |
| 202 | + GitHubBuilder builder = GitHubBuilder.fromCredentials(); |
| 203 | + fail(); |
| 204 | + } catch (Exception e) { |
| 205 | + assertThat(e, instanceOf(IOException.class)); |
| 206 | + assertThat(e.getMessage(), equalTo("Failed to resolve credentials from ~/.github or the environment.")); |
| 207 | + } |
| 208 | + |
| 209 | + props = new HashMap<String, String>(); |
| 210 | + |
| 211 | + props.put("endpoint", "bogus endpoint url"); |
| 212 | + props.put("oauth", "bogus oauth token string"); |
| 213 | + |
| 214 | + setupPropertyFile(props); |
| 215 | + |
| 216 | + GitHubBuilder builder = GitHubBuilder.fromCredentials(); |
| 217 | + |
| 218 | + assertThat(builder.endpoint, equalTo("bogus endpoint url")); |
| 219 | + |
| 220 | + assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class)); |
| 221 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), |
| 222 | + equalTo("token bogus oauth token string")); |
| 223 | + assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), nullValue()); |
| 224 | + |
| 225 | + props.put("login", "bogus login"); |
| 226 | + setupPropertyFile(props); |
| 227 | + builder = GitHubBuilder.fromCredentials(); |
| 228 | + |
| 229 | + assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class)); |
| 230 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), |
| 231 | + equalTo("token bogus oauth token string")); |
| 232 | + assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login")); |
| 233 | + |
| 234 | + props.put("jwt", "bogus jwt token string"); |
| 235 | + setupPropertyFile(props); |
| 236 | + builder = GitHubBuilder.fromCredentials(); |
| 237 | + |
| 238 | + assertThat(builder.authorizationProvider, not(instanceOf(UserAuthorizationProvider.class))); |
| 239 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), |
| 240 | + equalTo("Bearer bogus jwt token string")); |
| 241 | + |
| 242 | + props.put("password", "bogus weak password"); |
| 243 | + setupPropertyFile(props); |
| 244 | + builder = GitHubBuilder.fromCredentials(); |
| 245 | + |
| 246 | + assertThat(builder.authorizationProvider, instanceOf(UserAuthorizationProvider.class)); |
| 247 | + assertThat(builder.authorizationProvider.getEncodedAuthorization(), |
| 248 | + equalTo("Basic Ym9ndXMgbG9naW46Ym9ndXMgd2VhayBwYXNzd29yZA==")); |
| 249 | + assertThat(((UserAuthorizationProvider) builder.authorizationProvider).getLogin(), equalTo("bogus login")); |
| 250 | + } finally { |
| 251 | + GitHubBuilder.HOME_DIRECTORY = null; |
| 252 | + File propertyFile = new File(getTestDirectory(), ".github"); |
| 253 | + propertyFile.delete(); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + private void setupPropertyFile(Map<String, String> props) throws IOException { |
| 258 | + File propertyFile = new File(getTestDirectory(), ".github"); |
| 259 | + Properties properties = new Properties(); |
| 260 | + properties.putAll(props); |
| 261 | + properties.store(new FileOutputStream(propertyFile), ""); |
| 262 | + } |
| 263 | + |
| 264 | + private String getTestDirectory() { |
| 265 | + return new File("target").getAbsolutePath(); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void testAnonymous() throws IOException { |
| 270 | + // we disable this test for JDK 16+ as the current hacks in setupEnvironment() don't work with JDK 16+ |
| 271 | + Assume.assumeThat(Double.valueOf(System.getProperty("java.specification.version")), lessThan(16.0)); |
| 272 | + |
| 273 | + Map<String, String> props = new HashMap<String, String>(); |
| 274 | + |
| 275 | + props.put("endpoint", mockGitHub.apiServer().baseUrl()); |
| 276 | + setupEnvironment(props); |
| 277 | + |
| 278 | + // No values present except endpoint |
| 279 | + GitHubBuilder builder = GitHubBuilder |
| 280 | + .fromEnvironment("customLogin", "customPassword", "customOauth", "endpoint"); |
| 281 | + |
| 282 | + assertThat(builder.endpoint, equalTo(mockGitHub.apiServer().baseUrl())); |
| 283 | + assertThat(builder.authorizationProvider, sameInstance(AuthorizationProvider.ANONYMOUS)); |
| 284 | + } |
| 285 | + |
140 | 286 | @Test |
141 | 287 | public void testGithubBuilderWithAppInstallationToken() throws Exception { |
142 | 288 |
|
|
0 commit comments