Skip to content

Commit

Permalink
Added tests for BitbucketServerOAuthAuthenticatorProvider.
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Kabashniuk <skabashniuk@redhat.com>
  • Loading branch information
skabashnyuk committed Jan 20, 2021
1 parent 222aaf7 commit 4699856
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
10 changes: 10 additions & 0 deletions wsmaster/che-core-api-auth-bitbucket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,15 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public OAuthAuthenticator get() {
private static OAuthAuthenticator getOAuthAuthenticator(
String consumerKeyPath, String privateKeyPath, String bitbucketEndpoint, String apiEndpoint)
throws IOException {
if (!isNullOrEmpty(bitbucketEndpoint) && consumerKeyPath != null && privateKeyPath != null) {
if (!isNullOrEmpty(bitbucketEndpoint)
&& !isNullOrEmpty(consumerKeyPath)
&& !isNullOrEmpty(privateKeyPath)) {
String consumerKey = Files.readString(Path.of(consumerKeyPath));
String privateKey = Files.readString(Path.of(privateKeyPath));
if (!isNullOrEmpty(consumerKey) && !isNullOrEmpty(privateKey)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.security.oauth1;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class BitbucketServerOAuthAuthenticatorProviderTest {
private File cfgFile;
private File emptyFile;

@BeforeClass
public void setup() throws IOException {
cfgFile = File.createTempFile("BitbucketServerOAuthAuthenticatorProviderTest-", "-cfg");
Files.asCharSink(cfgFile, Charset.defaultCharset()).write("tmp-data");
cfgFile.deleteOnExit();
emptyFile = File.createTempFile("BitbucketServerOAuthAuthenticatorProviderTest-", "-empty");
emptyFile.deleteOnExit();
}

@Test(dataProvider = "noopConfig")
public void shouldProvideNoopOAuthAuthenticatorIfSomeConfigurationIsNotSet(
String consumerKeyPath, String privateKeyPath, String bitbucketEndpoint) throws IOException {
// given
BitbucketServerOAuthAuthenticatorProvider provider =
new BitbucketServerOAuthAuthenticatorProvider(
consumerKeyPath, privateKeyPath, bitbucketEndpoint, "http://che.server.com");
// when
OAuthAuthenticator actual = provider.get();
// then
assertNotNull(actual);
assertTrue(NoopOAuthAuthenticator.class.isAssignableFrom(actual.getClass()));
}

@Test
public void shouldBeAbleToConfigureValidBitbucketServerOAuthAuthenticator() throws IOException {
// given
BitbucketServerOAuthAuthenticatorProvider provider =
new BitbucketServerOAuthAuthenticatorProvider(
cfgFile.getPath(), cfgFile.getPath(), "http://bitubucket.com", "http://che.server.com");
// when
OAuthAuthenticator actual = provider.get();
// then
assertNotNull(actual);
assertTrue(BitbucketServerOAuthAuthenticator.class.isAssignableFrom(actual.getClass()));
}

@DataProvider(name = "noopConfig")
public Object[][] noopConfig() {
return new Object[][] {
{null, null, null},
{cfgFile.getPath(), null, null},
{null, cfgFile.getPath(), null},
{cfgFile.getPath(), cfgFile.getPath(), null},
{emptyFile.getPath(), null, null},
{null, emptyFile.getPath(), null},
{emptyFile.getPath(), emptyFile.getPath(), null},
{cfgFile.getPath(), emptyFile.getPath(), null},
{emptyFile.getPath(), cfgFile.getPath(), null},
{emptyFile.getPath(), emptyFile.getPath(), "http://bitubucket.com"},
{cfgFile.getPath(), emptyFile.getPath(), "http://bitubucket.com"},
{emptyFile.getPath(), cfgFile.getPath(), "http://bitubucket.com"},
{null, null, "http://bitubucket.com"}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2012-2018 Red Hat, Inc.
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
Contributors:
Red Hat, Inc. - initial API and implementation
-->
<configuration>

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-41(%date[%.15thread]) %-45([%-5level] [%.30logger{30} %L]) - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="stdout"/>
</root>

</configuration>

0 comments on commit 4699856

Please sign in to comment.