Skip to content

Commit

Permalink
Add end to end test
Browse files Browse the repository at this point in the history
Added end to end test of the compnent
  • Loading branch information
SARAVANA1501 authored Nov 21, 2023
1 parent b637906 commit c50400f
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 11 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/DtrackContractCheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: DtrackContractCheck
on:
schedule:
- cron: "*/15 * * * *"
jobs:
contract-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest ]
steps:
- name: Checkout the repository
uses: actions/checkout@v4
- uses: isbang/[email protected]
with:
compose-file: https://dependencytrack.org/docker-compose.yml
- name: Make test call
run: curl -X GET http://localhost:8081/api/version
71 changes: 61 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
name: Build

on:
push:
branches:
- main
tags:
- "*"
paths-ignore:
- "README.md"
schedule:
- cron: "0 10 * * *" # everyday at 10am

pull_request:
branches: [ master ]
types: [ opened, synchronize, closed ]

push:
branches-ignore:
- master

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest]
os: [ ubuntu-latest ]

steps:
- name: Checkout the repository
uses: actions/checkout@v4

- name: Fetch all tags and branches
run: git fetch --prune --unshallow

- uses: actions/[email protected]
with:
# need at least .NET 6 to build
Expand Down Expand Up @@ -58,4 +60,53 @@ jobs:
cake-version: 3.1.0
arguments: |
token: ${{ secrets.SONAR_TOKEN }}
mode: CI
mode: CI
- name: Copy integrate test files
uses: canastro/copy-file-action@master
with:
source: "tests/Cake.DependencyTrack.EndToEnd.Tests/docker/docker-compose.yml"
target: "artifact/docker-compose.yml"
- name: Copy integrate test files
uses: canastro/copy-file-action@master
with:
source: "tests/Cake.DependencyTrack.EndToEnd.Tests/build.cake"
target: "artifact/build.cake"
- uses: actions/upload-artifact@v3
with:
name: artifact
path: artifact



end-to-end-test:
needs: build
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest ]

steps:
- name: Checkout the repository
uses: actions/checkout@v4
- uses: actions/download-artifact@v3
with:
name: artifact
path: artifact
- uses: actions/download-artifact@v3
with:
name: artifact
path: tests/Cake.DependencyTrack.EndToEnd.Tests
- name: Display structure of downloaded files
run: ls -R
working-directory: .
- uses: isbang/[email protected]
with:
compose-file: ./tests/Cake.DependencyTrack.EndToEnd.Tests/docker-compose.yml
- name: Make test call
run: curl -X GET http://localhost:8081/api/version
- name: Build project
uses: cake-build/[email protected]
with:
script-path: ./tests/Cake.DependencyTrack.EndToEnd.Tests/build.cake
verbosity: Diagnostic
cake-version: 3.1.0
13 changes: 12 additions & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ Task("Test")
});
});

Task("Publish")
.IsDependentOn("Test")
.Does(() =>
{
DotNetPublish("./src/Cake.DependencyTrack/Cake.DependencyTrack.csproj", new DotNetPublishSettings {
Configuration = configuration,
OutputDirectory = "./artifact/lib",
Framework = "net6.0"
});
});



Task("SonarEnd")
.WithCriteria(isCI)
.IsDependentOn("Test")
.IsDependentOn("Publish")
.Does(() => {
SonarEnd(new SonarEndSettings{
Token=token,
Expand Down
52 changes: 52 additions & 0 deletions tests/Cake.DependencyTrack.EndToEnd.Tests/TestData/bom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<bom xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" serialNumber="urn:uuid:f9808d9d-03b6-44d6-9993-5ae418f2e911" version="1" xmlns="http://cyclonedx.org/schema/bom/1.3">
<metadata>
<tools>
<tool>
<vendor>CycloneDX</vendor>
<name>CycloneDX module for .NET</name>
<version>2.2.0.0</version>
</tool>
</tools>
<component type="application" bom-ref="[email protected]">
<name>Test</name>
<version>0.0.0</version>
</component>
</metadata>
<components>
<component type="library" bom-ref="pkg:nuget/[email protected]">
<publisher>Amazon Web Services</publisher>
<name>Amazon.Extensions.Configuration.SystemsManager</name>
<version>3.0.0</version>
<description>.NET Configuration Extensions for AWS Systems Manager</description>
<scope>required</scope>
<hashes>
<hash alg="SHA-512">D5D427FD2447EE0DEA3367ECA8CC05D9962E21089C61532F8EAD8E565BFBB530FB156849D857648F2DB3447F9C423B03731F94B327A4EDCAE1E7C68B8D50E9D5</hash>
</hashes>
<licenses>
<license>
<url>https://aka.ms/deprecateLicenseUrl</url>
</license>
</licenses>
<copyright>2018-2021</copyright>
<purl>pkg:nuget/[email protected]</purl>
<externalReferences>
<reference type="website">
<url>https://github.com/aws/aws-dotnet-extensions-configuration/</url>
</reference>
<reference type="vcs">
<url>https://github.com/aws/aws-dotnet-extensions-configuration/</url>
</reference>
</externalReferences>
</component>
</components>
<dependencies>
<dependency ref="pkg:nuget/[email protected]">
<dependency ref="pkg:nuget/[email protected]" />
<dependency ref="pkg:nuget/[email protected]" />
<dependency ref="pkg:nuget/[email protected]" />
<dependency ref="pkg:nuget/[email protected]" />
<dependency ref="pkg:nuget/[email protected]" />
</dependency>
</dependencies>
</bom>
111 changes: 111 additions & 0 deletions tests/Cake.DependencyTrack.EndToEnd.Tests/build.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#r "lib/Cake.DependencyTrack.dll"

using Cake.DependencyTrack;
using Cake.DependencyTrack.Models;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Nodes;
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
string apiKey="";

///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////

Setup(ctx =>
{
// Executed BEFORE the first task.
Information("Running tasks...");
});

Teardown(ctx =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});

///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////

Task("Prepare")
.Does(async (context) => {
//Dtrack end to end test preparation consists of 4 steps
//1. Default password reset, Dtrack requires to change default password
//2. Login and generate a token
//3. Get current team id which can be used for API key generation
//4. Generate the API key for the given team id.
//Reset password
var data = new[]
{
new KeyValuePair<string, string>("username", "admin"),
new KeyValuePair<string, string>("password", "admin"),
new KeyValuePair<string, string>("newPassword", "1234567"),
new KeyValuePair<string, string>("confirmPassword", "1234567"),
};
var httpClient = new HttpClient();
var response = await httpClient.PostAsync("http://localhost:8081/api/v1/user/forceChangePassword", new FormUrlEncodedContent(data));
response.EnsureSuccessStatusCode();

//Generate token
var data1 = new[]
{
new KeyValuePair<string, string>("username", "admin"),
new KeyValuePair<string, string>("password", "1234567")
};
var response1 =
await httpClient.PostAsync("http://localhost:8081/api/v1/user/login", new FormUrlEncodedContent(data1));
response1.EnsureSuccessStatusCode();
var token = await response1.Content.ReadAsStringAsync();

//Get self group
var selfGroupRequest = new HttpRequestMessage(HttpMethod.Get, "http://localhost:8081/api/v1/user/self");
selfGroupRequest.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", token);

var selfGroupResponse = await httpClient.SendAsync(selfGroupRequest);
selfGroupResponse.EnsureSuccessStatusCode();
var responseBody = await selfGroupResponse.Content.ReadAsStreamAsync();
var teamuuid = (await JsonSerializer.DeserializeAsync<JsonObject>(responseBody))?["teams"]?[0]?["uuid"]?.ToString();

//Generate API key
var generateAPIRequest = new HttpRequestMessage(HttpMethod.Put, $"http://localhost:8081/api/v1/team/{teamuuid}/key");
generateAPIRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var generateAPIResponse = await httpClient.SendAsync(generateAPIRequest);
generateAPIResponse.EnsureSuccessStatusCode();
var keyStream = await generateAPIResponse.Content.ReadAsStreamAsync();
apiKey = (await JsonSerializer.DeserializeAsync<JsonObject>(keyStream))?["key"]?.ToString();
});

Task("Default")
.IsDependentOn("Prepare")
.Does(async (context) => {
FileInfo f = new FileInfo("TestData/bom.xml");
var settings = new UploadBomSettings{
ProjectName="test",
Version="CI",
AutoCreate=true,
AbsoluteBomFilePath=f.FullName,
ServerSettings=new ServerSettings{
BaseServerUrl="http://localhost:8081",
ApiKey=apiKey
},
ShouldValidateMetrics=true,
MetricsThresholdSettings=new MetricsThresholdSettings{
CriticalCount=2,
HighCount=2,
MediumCount=2,
LowCount=2
}
};
await context.UploadBomFile(settings);
});

RunTarget(target);
Loading

0 comments on commit c50400f

Please sign in to comment.