|
| 1 | +# Integration Tests |
| 2 | + |
| 3 | +This project includes integration tests that run against a real Contrast TeamServer instance. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +1. **Copy the environment template:** |
| 8 | + ```bash |
| 9 | + cp .env.integration-test.template .env.integration-test |
| 10 | + ``` |
| 11 | + |
| 12 | +2. **Fill in your Contrast credentials:** |
| 13 | + Edit `.env.integration-test` with your actual credentials: |
| 14 | + - `CONTRAST_HOST_NAME` - Your TeamServer host (e.g., `app.contrastsecurity.com`) |
| 15 | + - `CONTRAST_API_KEY` - Your API key |
| 16 | + - `CONTRAST_SERVICE_KEY` - Your service key |
| 17 | + - `CONTRAST_USERNAME` - Your username |
| 18 | + - `CONTRAST_ORG_ID` - Your organization ID |
| 19 | + |
| 20 | +3. **Source the environment file:** |
| 21 | + ```bash |
| 22 | + source .env.integration-test |
| 23 | + ``` |
| 24 | + |
| 25 | +## Running Integration Tests |
| 26 | + |
| 27 | +### Run integration tests only: |
| 28 | +```bash |
| 29 | +mvn verify |
| 30 | +``` |
| 31 | + |
| 32 | +### Run all tests (unit + integration): |
| 33 | +```bash |
| 34 | +mvn clean verify |
| 35 | +``` |
| 36 | + |
| 37 | +### Skip integration tests: |
| 38 | +```bash |
| 39 | +mvn verify -DskipITs |
| 40 | +``` |
| 41 | + |
| 42 | +### Run only unit tests (default): |
| 43 | +```bash |
| 44 | +mvn test |
| 45 | +``` |
| 46 | + |
| 47 | +## How It Works |
| 48 | + |
| 49 | +- **Unit tests** (`*Test.java`) run during the `test` phase via Maven Surefire |
| 50 | +- **Integration tests** (`*IT.java`) run during the `verify` phase via Maven Failsafe |
| 51 | +- Integration tests only execute if `CONTRAST_HOST_NAME` environment variable is set |
| 52 | +- If environment variables are missing, integration tests are automatically skipped |
| 53 | + |
| 54 | +## GitHub Actions / CI |
| 55 | + |
| 56 | +For GitHub Actions, add these secrets to your repository: |
| 57 | +- `CONTRAST_HOST_NAME` |
| 58 | +- `CONTRAST_API_KEY` |
| 59 | +- `CONTRAST_SERVICE_KEY` |
| 60 | +- `CONTRAST_USERNAME` |
| 61 | +- `CONTRAST_ORG_ID` |
| 62 | + |
| 63 | +Example GitHub Actions workflow: |
| 64 | + |
| 65 | +```yaml |
| 66 | +- name: Run integration tests |
| 67 | + run: mvn verify |
| 68 | + env: |
| 69 | + CONTRAST_HOST_NAME: ${{ secrets.CONTRAST_HOST_NAME }} |
| 70 | + CONTRAST_API_KEY: ${{ secrets.CONTRAST_API_KEY }} |
| 71 | + CONTRAST_SERVICE_KEY: ${{ secrets.CONTRAST_SERVICE_KEY }} |
| 72 | + CONTRAST_USERNAME: ${{ secrets.CONTRAST_USERNAME }} |
| 73 | + CONTRAST_ORG_ID: ${{ secrets.CONTRAST_ORG_ID }} |
| 74 | +``` |
| 75 | +
|
| 76 | +## Current Integration Tests |
| 77 | +
|
| 78 | +### EnvironmentsIT.java |
| 79 | +
|
| 80 | +Tests that environments and tags are properly populated from TeamServer API: |
| 81 | +- `testEnvironmentsAndTagsArePopulated()` - Verifies vulnerability responses include environments and tags |
| 82 | +- `testVulnerabilitiesHaveBasicFields()` - Verifies basic vulnerability fields are present |
| 83 | + |
| 84 | +## Adding New Integration Tests |
| 85 | + |
| 86 | +1. Create a new test class in `src/test/java` with the `IT` suffix (e.g., `MyFeatureIT.java`) |
| 87 | +2. Annotate with `@EnabledIfEnvironmentVariable(named = "CONTRAST_HOST_NAME", matches = ".+")` |
| 88 | +3. Use real Contrast SDK calls (no mocking) |
| 89 | +4. Run with `mvn verify` to execute |
| 90 | + |
| 91 | +## Troubleshooting |
| 92 | + |
| 93 | +**Integration tests don't run:** |
| 94 | +- Verify environment variables are set: `echo $CONTRAST_HOST_NAME` |
| 95 | +- Make sure you're running `mvn verify` (not just `mvn test`) |
| 96 | +- Check that test class name ends with `IT.java` |
| 97 | + |
| 98 | +**Tests fail with authentication errors:** |
| 99 | +- Verify your credentials are correct |
| 100 | +- Check that your API key has appropriate permissions |
| 101 | +- Ensure your organization ID is correct |
0 commit comments