Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

maven plugin deploys test catalog with or without a galasa.token #113

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ Phase: `deploy`

Input Parameters/Properties:
- `galasa.test.stream` required. A string.
- `galasa.token` required. An access token for the galasa ecosystem.
- `galasa.token` optional. An access token for the galasa ecosystem, if that ecosytem is using authentication.
- `galasa.bootstrap` required. A URL to the ecosystem.
- `galasa.skip.bundletestcatalog` optional. A boolean.
- `galasa.skip.deploytestcatalog` optional. A boolean.
- `galasa.skip.bundletestcatalog` optional. A boolean. If set to true, the test catalog is not deployed to the Galasa server.
- `galasa.skip.deploytestcatalog` optional. A boolean. If set to true, the test catalog is not deployed to the Galasa server.

For example:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,26 @@ public class DeployTestCatalog extends AbstractMojo {
@Parameter(defaultValue = "${galasa.skip.bundletestcatatlog}", readonly = true, required = false)
public boolean skipBundleTestCatalogOldSpelling;

@Parameter(defaultValue = "${galasa.skip.bundletestcatalog}", name="skip" , readonly = true, required = false)
@Parameter(defaultValue = "${galasa.skip.bundletestcatalog}", readonly = true, required = false)
public boolean skipBundleTestCatalog;

// This spelling of the property is old/wrong/deprecated.
@Parameter(defaultValue = "${galasa.skip.deploytestcatatlog}" , readonly = true, required = false)
public boolean skipDeployTestCatalogOldSpelling;

@Parameter(defaultValue = "${galasa.skip.deploytestcatalog}", name="skipDeploy" , readonly = true, required = false)
@Parameter(defaultValue = "${galasa.skip.deploytestcatalog}", readonly = true, required = false)
public boolean skipDeployTestCatalog;

// A protected variable so we can inject a mock factory if needed during unit testing.
protected AuthenticationServiceFactory authFactory = new AuthenticationServiceFactoryImpl();

protected BootstrapLoader bootstrapLoader = new BootstrapLoaderImpl();

public static final boolean DEFAULT_ASSUMPTION_AUTHENTICATION_ENABLED = false ;

public static final String BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED = "framework.auth.isEnabled";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this constant is no longer required



public void execute() throws MojoExecutionException, MojoFailureException {

boolean skip = (skipBundleTestCatalog || skipBundleTestCatalogOldSpelling);
Expand Down Expand Up @@ -100,21 +105,36 @@ public void execute() throws MojoExecutionException, MojoFailureException {

URL testcatalogUrl = calculateTestCatalogUrl(bootstrapProperties, this.testStream, this.bootstrapUrl);

checkGalasaAccessTokenIsValid(this.galasaAccessToken);

String jwt = getAuthenticatedJwt(this.authFactory, this.galasaAccessToken, this.bootstrapUrl) ;
String jwt = null ;
// For now, if no galasa token is supplied, that's ok. It's optional.
if (this.galasaAccessToken==null || this.galasaAccessToken.isEmpty()) {
techcobweb marked this conversation as resolved.
Show resolved Hide resolved
// No galasa access token supplied by the user. So not going to pass a JWT.
} else {
jwt = getAuthenticatedJwt(this.authFactory, this.galasaAccessToken, this.bootstrapUrl) ;
}

publishTestCatalogToGalasaServer(testcatalogUrl,jwt, testCatalogArtifact);
}

private void checkGalasaAccessTokenIsValid(String galasaAccessToken) throws MojoExecutionException {
if (galasaAccessToken==null || galasaAccessToken.isEmpty()) {
String msg = "No Galasa authentication token supplied. Set the galasa.token property."+
" The token is required to communicate with the Galasa server."+
" Obtain a personal access token using the Galasa web user interface for the ecosystem you are trying to publish the test catalog to.";
getLog().error(msg);
throw new MojoExecutionException(msg);
// Protected so that we can easily unit test this method.
protected boolean calculateWhetherAuthenticationIsEnabledOnServer(Properties bootstrapProperties) {
boolean isAuthEnabled = DEFAULT_ASSUMPTION_AUTHENTICATION_ENABLED;

String isEnabledPropStr = bootstrapProperties.getProperty(BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED);
if (isEnabledPropStr==null) {
if (DEFAULT_ASSUMPTION_AUTHENTICATION_ENABLED) {
getLog().info("Bootstrap properties from server do not include "+BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED+" so we assume server is insisting on authentication.");
} else {
getLog().info("Bootstrap properties from server do not include "+BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED+" so we assume server is not insisting on authentication.");
}
} else {
if (isEnabledPropStr.equalsIgnoreCase("true") ) {
getLog().info("Bootstrap properties from server include the property "+BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED+" so we know the server requires authentication.");
isAuthEnabled = true;
}
techcobweb marked this conversation as resolved.
Show resolved Hide resolved
}

return isAuthEnabled;
}

private Artifact getTestCatalogArtifact() {
Expand Down Expand Up @@ -164,7 +184,13 @@ private void postTestCatalogToGalasaServer(HttpURLConnection conn, URL testCatal
conn.addRequestProperty("Content-Type", "application/json");
conn.addRequestProperty("Accept", "application/json");

conn.addRequestProperty("Authorization", "Bearer "+jwt);
// Only add the jwt header if we have a jwt value.
if (jwt == null) {
getLog().info("Not sending a JWT bearer token to the server, as the galasa.token was not supplied.");
} else {
conn.addRequestProperty("Authorization", "Bearer "+jwt);
}

conn.addRequestProperty("ClientApiVersion","0.32.0"); // The version of the API we coded this against.

FileUtils.copyFile(testCatalogArtifact.getFile(), conn.getOutputStream());
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests in here should test if when a token is not supplied it makes a connection without the authorization header and with it if the token is present

Original file line number Diff line number Diff line change
Expand Up @@ -254,39 +254,51 @@ public void TestCalculateTestCatalogUrlSpotsBootstrapUrlWhichDoesntEndInBootstra
assertThat(ex).hasMessageContaining("Unable to calculate the test catalog url, the bootstrap url does not end with /bootstrap, need a framework.testcatalog.url property in the bootstrap");
}

@SuppressWarnings("deprecation")
@Test
public void TestFailsIfNoGalasaAccessToken() throws Exception {
public void testCalcServerUsesAuthenticationWhenPropMissingShouldBeFalse() {
DeployTestCatalog command = new DeployTestCatalog();
MockLog mockLog = new MockLog();
command.setLog(mockLog);
Properties props = new Properties();
boolean isAuthEnabled = command.calculateWhetherAuthenticationIsEnabledOnServer(props);
assertThat(isAuthEnabled).isFalse();
}

command.testStream = "myTestStream";
command.bootstrapUrl = new URL("http://myBootstrapUrl/bootstrap");
@Test
public void testCalcServerUsesAuthenticationWhenPropFalseShouldBeFalse() {
DeployTestCatalog command = new DeployTestCatalog();
Properties props = new Properties();
props.setProperty(DeployTestCatalog.BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED,"false");
boolean isAuthEnabled = command.calculateWhetherAuthenticationIsEnabledOnServer(props);
assertThat(isAuthEnabled).isFalse();
}

MavenProject project = new MavenProject();
project.setPackaging("galasa-obr");
command.project = project;
@Test
public void testCalcServerUsesAuthenticationWhenPropTRUEShouldBeTrue() {
DeployTestCatalog command = new DeployTestCatalog();
Properties props = new Properties();
props.setProperty(DeployTestCatalog.BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED,"TRUE");
boolean isAuthEnabled = command.calculateWhetherAuthenticationIsEnabledOnServer(props);
assertThat(isAuthEnabled).isTrue();
}

MockArtifact testCatalogArtifact = new MockArtifact();
project.addAttachedArtifact(testCatalogArtifact);
testCatalogArtifact.type = "json";
testCatalogArtifact.classifier = "testcatalog";

// Set a mock boostrap loader...
command.bootstrapLoader = new BootstrapLoader() {
@Override
public Properties getBootstrapProperties(URL bootstrapUrl, Log log) throws MojoExecutionException {
return new Properties();
}
};

Exception ex = catchException( ()-> command.execute() );
assertThat(ex).isInstanceOf(MojoExecutionException.class);
assertThat(ex).hasMessageContaining("No Galasa authentication token supplied. Set the galasa.token property. The token is required to communicate with the Galasa server");
@Test
public void testCalcServerUsesAuthenticationWhenPropTrueShouldBeTrue() {
DeployTestCatalog command = new DeployTestCatalog();
Properties props = new Properties();
props.setProperty(DeployTestCatalog.BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED,"True");
boolean isAuthEnabled = command.calculateWhetherAuthenticationIsEnabledOnServer(props);
assertThat(isAuthEnabled).isTrue();
}

@Test
public void testCalcServerUsesAuthenticationWhenProptrueShouldBeTrue() {
DeployTestCatalog command = new DeployTestCatalog();
Properties props = new Properties();
props.setProperty(DeployTestCatalog.BOOTSTRAP_PROPERTY_NAME_IS_AUTH_ENABLED,"true");
boolean isAuthEnabled = command.calculateWhetherAuthenticationIsEnabledOnServer(props);
assertThat(isAuthEnabled).isTrue();
}


// This is my exploration unit test.
//
// The unit tests are not yet complete, as they don't test the last piece where the test catalog file is
Expand Down