1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-05 13:25:19 +02:00
sap-jenkins-library/pkg/config/trustengine_test.go
Jordi van Liempt aa1e67547a
chore(trustengine): Rename Trust Engine to System Trust in logs and docs (#5210)
* rename Trust Engine to System Trust

* Revert "rename Trust Engine to System Trust"

This reverts commit 66d4680324310790d41b70eed7421d121aac0e03.

* rename Trust Engine to System Trust in logs and docs
2024-12-19 13:31:42 +00:00

76 lines
2.4 KiB
Go

//go:build unit
// +build unit
package config
import (
"fmt"
"net/http"
"testing"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/trustengine"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
const secretName = "sonar"
const secretNameInTrustEngine = "sonarTrustengineSecretName"
const testServerURL = "https://www.project-piper.io"
const testTokenEndPoint = "tokens"
const testTokenQueryParamName = "systems"
const mockSonarToken = "mockSonarToken"
var testFullURL = fmt.Sprintf("%s/%s?%s=", testServerURL, testTokenEndPoint, testTokenQueryParamName)
var mockSingleTokenResponse = fmt.Sprintf("{\"sonar\": \"%s\"}", mockSonarToken)
func TestTrustEngineConfig(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, testFullURL+"sonar", httpmock.NewStringResponder(200, mockSingleTokenResponse))
stepParams := []StepParameters{createStepParam(secretName, RefTypeTrustengineSecret, secretNameInTrustEngine, secretName)}
var trustEngineConfiguration = trustengine.Configuration{
Token: "testToken",
ServerURL: testServerURL,
TokenEndPoint: testTokenEndPoint,
TokenQueryParamName: testTokenQueryParamName,
}
client := &piperhttp.Client{}
client.SetOptions(piperhttp.ClientOptions{MaxRetries: -1, UseDefaultTransport: true})
t.Run("Load secret from System Trust - secret not set yet by Vault or config.yml", func(t *testing.T) {
stepConfig := &StepConfig{Config: map[string]interface{}{
secretName: "",
}}
resolveAllTrustEngineReferences(stepConfig, stepParams, trustEngineConfiguration, client)
assert.Equal(t, mockSonarToken, stepConfig.Config[secretName])
})
t.Run("Load secret from System Trust - secret already by Vault or config.yml", func(t *testing.T) {
stepConfig := &StepConfig{Config: map[string]interface{}{
secretName: "aMockTokenFromVault",
}}
resolveAllTrustEngineReferences(stepConfig, stepParams, trustEngineConfiguration, client)
assert.NotEqual(t, mockSonarToken, stepConfig.Config[secretName])
})
}
func createStepParam(name, refType, trustengineSecretNameProperty, defaultSecretNameName string) StepParameters {
return StepParameters{
Name: name,
Aliases: []Alias{},
ResourceRef: []ResourceReference{
{
Type: refType,
Name: trustengineSecretNameProperty,
Default: defaultSecretNameName,
},
},
}
}