|
1 | 1 | package com.contrastsecurity.gradle.plugin.e2e;
|
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
3 | 4 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
5 | 6 |
|
| 7 | +import com.contrastsecurity.gradle.plugin.EnvironmentUtils; |
| 8 | +import com.contrastsecurity.gradle.plugin.GradleRunnerTest; |
6 | 9 | import com.contrastsecurity.gradle.plugin.InstallAgentTask;
|
7 | 10 | import com.contrastsecurity.sdk.ContrastSDK;
|
8 | 11 | import com.contrastsecurity.sdk.UserAgentProduct;
|
| 12 | +import java.io.IOException; |
9 | 13 | import java.nio.file.Path;
|
| 14 | +import java.util.Collection; |
| 15 | +import java.util.HashSet; |
10 | 16 | import org.gradle.testfixtures.ProjectBuilder;
|
| 17 | +import org.gradle.testkit.runner.BuildResult; |
| 18 | +import org.gradle.testkit.runner.GradleRunner; |
| 19 | +import org.gradle.testkit.runner.TaskOutcome; |
11 | 20 | import org.junit.jupiter.api.Test;
|
12 | 21 |
|
13 |
| -public class EndToEndTests { |
| 22 | +public class EndToEndTests extends GradleRunnerTest { |
14 | 23 |
|
15 | 24 | @Test
|
16 | 25 | void verify_retrieval_of_agent_from_teamserver() {
|
17 |
| - final String username = System.getenv("CONTRAST__API__USER_NAME"); |
18 |
| - final String apiUrl = System.getenv("CONTRAST__API__URL"); |
19 |
| - final String serviceKey = System.getenv("CONTRAST__API__SERVICE_KEY"); |
20 |
| - final String apiKey = System.getenv("CONTRAST__API__API_KEY"); |
21 |
| - final String orgUuid = System.getenv("CONTRAST__API__ORGANIZATION_ID"); |
22 | 26 |
|
23 | 27 | final ContrastSDK connection =
|
24 |
| - new ContrastSDK.Builder(username, serviceKey, apiKey) |
25 |
| - .withApiUrl(apiUrl) |
| 28 | + new ContrastSDK.Builder( |
| 29 | + EnvironmentUtils.getUsername(), |
| 30 | + EnvironmentUtils.getServiceKey(), |
| 31 | + EnvironmentUtils.getApiKey()) |
| 32 | + .withApiUrl(EnvironmentUtils.getApiUrl()) |
26 | 33 | .withUserAgentProduct(UserAgentProduct.of("contrast-gradle-plugin"))
|
27 | 34 | .build();
|
28 | 35 | final Path agentPath =
|
29 |
| - InstallAgentTask.retrieveAgent(connection, null, orgUuid, ProjectBuilder.builder().build()); |
| 36 | + InstallAgentTask.retrieveAgent( |
| 37 | + connection, null, EnvironmentUtils.getOrgUuid(), ProjectBuilder.builder().build()); |
30 | 38 | assertNotNull(agentPath);
|
31 | 39 | assertTrue(agentPath.endsWith("contrast.jar"));
|
32 | 40 | }
|
| 41 | + |
| 42 | + @Test |
| 43 | + void verify_attaches_agent_to_tests() throws IOException { |
| 44 | + writeString(getSettingsFile(), ""); |
| 45 | + String config = writeContrastBuildFile(); |
| 46 | + writeString(getBuildFile(), config); |
| 47 | + |
| 48 | + final GradleRunner testRunner = GradleRunner.create(); |
| 49 | + testRunner.forwardOutput(); |
| 50 | + testRunner.withPluginClasspath(); |
| 51 | + testRunner.withArguments("installAgent"); |
| 52 | + testRunner.withProjectDir(projectDir); |
| 53 | + final BuildResult result = testRunner.build(); |
| 54 | + |
| 55 | + result |
| 56 | + .getTasks() |
| 57 | + .forEach( |
| 58 | + buildTask -> { |
| 59 | + assertEquals(buildTask.getOutcome(), TaskOutcome.SUCCESS); |
| 60 | + }); |
| 61 | + |
| 62 | + for (final String arg : AGENT_ARGS) { |
| 63 | + assertTrue(result.getOutput().contains(arg)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private String writeContrastBuildFile() { |
| 68 | + return "plugins { id('com.contrastsecurity.java') }\n" |
| 69 | + + "contrastConfiguration {\n" |
| 70 | + + " username = " |
| 71 | + + "'" |
| 72 | + + EnvironmentUtils.getUsername() |
| 73 | + + "'" |
| 74 | + + "\n" |
| 75 | + + " apiKey = " |
| 76 | + + "'" |
| 77 | + + EnvironmentUtils.getApiKey() |
| 78 | + + "'" |
| 79 | + + "\n" |
| 80 | + + " serviceKey = " |
| 81 | + + "'" |
| 82 | + + EnvironmentUtils.getServiceKey() |
| 83 | + + "'" |
| 84 | + + "\n" |
| 85 | + + " apiUrl = " |
| 86 | + + "'" |
| 87 | + + EnvironmentUtils.getApiUrl() |
| 88 | + + "'" |
| 89 | + + "\n" |
| 90 | + + " orgUuid = " |
| 91 | + + "'" |
| 92 | + + EnvironmentUtils.getOrgUuid() |
| 93 | + + "'" |
| 94 | + + "\n" |
| 95 | + + " appName = 'gradle-end-to-end-test'\n" |
| 96 | + + " serverName = 'server1'\n" |
| 97 | + + " appVersion = '0.0.1'\n" |
| 98 | + + " attachToTests = true\n" |
| 99 | + + "}\n" |
| 100 | + + "tasks.register('fakeTask', org.gradle.api.tasks.testing.Test) { \nSystem.out.println('test') \n}"; |
| 101 | + } |
| 102 | + |
| 103 | + private static final Collection<String> AGENT_ARGS = new HashSet<>(); |
| 104 | + |
| 105 | + static { |
| 106 | + AGENT_ARGS.add("-javaagent:"); |
| 107 | + AGENT_ARGS.add("-Dcontrast.override.appname=gradle-end-to-end-test"); |
| 108 | + AGENT_ARGS.add("-Dcontrast.server=server"); |
| 109 | + AGENT_ARGS.add("-Dcontrast.env=qa"); |
| 110 | + AGENT_ARGS.add("-Dcontrast.override.appversion=0.0.1"); |
| 111 | + } |
33 | 112 | }
|
0 commit comments