From 46998563d049e22e6e2e670f417413e540340809 Mon Sep 17 00:00:00 2001 From: Sergii Kabashniuk Date: Tue, 19 Jan 2021 15:00:46 +0200 Subject: [PATCH] Added tests for BitbucketServerOAuthAuthenticatorProvider. Signed-off-by: Sergii Kabashniuk --- wsmaster/che-core-api-auth-bitbucket/pom.xml | 10 +++ ...ucketServerOAuthAuthenticatorProvider.java | 4 +- ...tServerOAuthAuthenticatorProviderTest.java | 83 +++++++++++++++++++ .../src/test/resources/logback-test.xml | 26 ++++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 wsmaster/che-core-api-auth-bitbucket/src/test/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProviderTest.java create mode 100644 wsmaster/che-core-api-auth-bitbucket/src/test/resources/logback-test.xml diff --git a/wsmaster/che-core-api-auth-bitbucket/pom.xml b/wsmaster/che-core-api-auth-bitbucket/pom.xml index 8d1a6ccb1d8..8851a46fb14 100644 --- a/wsmaster/che-core-api-auth-bitbucket/pom.xml +++ b/wsmaster/che-core-api-auth-bitbucket/pom.xml @@ -51,5 +51,15 @@ org.slf4j slf4j-api + + ch.qos.logback + logback-classic + test + + + org.testng + testng + test + diff --git a/wsmaster/che-core-api-auth-bitbucket/src/main/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProvider.java b/wsmaster/che-core-api-auth-bitbucket/src/main/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProvider.java index 48bd3255470..3898c05c4ae 100644 --- a/wsmaster/che-core-api-auth-bitbucket/src/main/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProvider.java +++ b/wsmaster/che-core-api-auth-bitbucket/src/main/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProvider.java @@ -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)) { diff --git a/wsmaster/che-core-api-auth-bitbucket/src/test/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProviderTest.java b/wsmaster/che-core-api-auth-bitbucket/src/test/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProviderTest.java new file mode 100644 index 00000000000..1a13932e699 --- /dev/null +++ b/wsmaster/che-core-api-auth-bitbucket/src/test/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticatorProviderTest.java @@ -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"} + }; + } +} diff --git a/wsmaster/che-core-api-auth-bitbucket/src/test/resources/logback-test.xml b/wsmaster/che-core-api-auth-bitbucket/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..2250aaa5aa7 --- /dev/null +++ b/wsmaster/che-core-api-auth-bitbucket/src/test/resources/logback-test.xml @@ -0,0 +1,26 @@ + + + + + + + %-41(%date[%.15thread]) %-45([%-5level] [%.30logger{30} %L]) - %msg%n + + + + + + +