2021-06-20 15:16:06 +02:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-06-22 11:27:53 +02:00
|
|
|
var defaultConfig []byte = []byte(
|
2021-06-20 15:39:54 +02:00
|
|
|
`gitea:
|
2021-06-20 15:16:06 +02:00
|
|
|
url: https://example.com/gitea
|
2021-06-21 11:08:56 +02:00
|
|
|
token:
|
|
|
|
value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
|
2021-06-21 11:00:31 +02:00
|
|
|
webhook:
|
|
|
|
secret: haxxor-gitea-secret
|
2021-06-20 19:47:55 +02:00
|
|
|
sonarqube:
|
|
|
|
url: https://example.com/sonarqube
|
2021-06-21 11:08:56 +02:00
|
|
|
token:
|
|
|
|
value: a09eb5785b25bb2cbacf48808a677a0709f02d8e
|
2021-06-21 11:00:31 +02:00
|
|
|
webhook:
|
|
|
|
secret: haxxor-sonarqube-secret
|
2021-06-22 11:27:53 +02:00
|
|
|
projects:
|
|
|
|
- sonarqube:
|
|
|
|
key: gitea-sonarqube-pr-bot
|
|
|
|
gitea:
|
|
|
|
owner: example-organization
|
|
|
|
name: pr-bot
|
2021-06-20 15:16:06 +02:00
|
|
|
`)
|
|
|
|
|
|
|
|
func WriteConfigFile(t *testing.T, content []byte) {
|
|
|
|
dir := os.TempDir()
|
|
|
|
config := path.Join(dir, "config.yaml")
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Remove(config)
|
|
|
|
})
|
|
|
|
|
|
|
|
_ = ioutil.WriteFile(config, content,0444)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadWithMissingFile(t *testing.T) {
|
|
|
|
assert.Panics(t, func() { Load(os.TempDir()) }, "No panic while reading missing file")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadWithExistingFile(t *testing.T) {
|
2021-06-22 11:27:53 +02:00
|
|
|
WriteConfigFile(t, defaultConfig)
|
2021-06-20 15:16:06 +02:00
|
|
|
|
|
|
|
assert.NotPanics(t, func() { Load(os.TempDir()) }, "Unexpected panic while reading existing file")
|
|
|
|
}
|
2021-06-20 16:49:12 +02:00
|
|
|
|
|
|
|
func TestLoadGiteaStructure(t *testing.T) {
|
2021-06-22 11:27:53 +02:00
|
|
|
WriteConfigFile(t, defaultConfig)
|
2021-06-20 16:49:12 +02:00
|
|
|
Load(os.TempDir())
|
|
|
|
|
2021-06-20 18:24:12 +02:00
|
|
|
expected := GiteaConfig{
|
2021-06-20 16:49:12 +02:00
|
|
|
Url: "https://example.com/gitea",
|
2021-06-21 11:08:56 +02:00
|
|
|
Token: Token{
|
|
|
|
Value: "d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565",
|
|
|
|
},
|
2021-06-21 11:00:31 +02:00
|
|
|
Webhook: Webhook{
|
|
|
|
Secret: "haxxor-gitea-secret",
|
|
|
|
},
|
2021-06-20 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 18:24:12 +02:00
|
|
|
assert.EqualValues(t, expected, Gitea)
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:00:31 +02:00
|
|
|
func TestLoadGiteaStructureInjectedEnvs(t *testing.T) {
|
|
|
|
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRET", "injected-webhook-secret")
|
2021-06-21 11:08:56 +02:00
|
|
|
os.Setenv("PRBOT_GITEA_TOKEN_VALUE", "injected-token")
|
2021-06-22 11:27:53 +02:00
|
|
|
WriteConfigFile(t, defaultConfig)
|
2021-06-20 18:24:12 +02:00
|
|
|
Load(os.TempDir())
|
|
|
|
|
|
|
|
expected := GiteaConfig{
|
|
|
|
Url: "https://example.com/gitea",
|
2021-06-21 11:08:56 +02:00
|
|
|
Token: Token{
|
|
|
|
Value: "injected-token",
|
|
|
|
},
|
2021-06-21 11:00:31 +02:00
|
|
|
Webhook: Webhook{
|
|
|
|
Secret: "injected-webhook-secret",
|
|
|
|
},
|
2021-06-20 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, expected, Gitea)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2021-06-21 11:00:31 +02:00
|
|
|
os.Unsetenv("PRBOT_GITEA_WEBHOOK_SECRET")
|
2021-06-21 11:08:56 +02:00
|
|
|
os.Unsetenv("PRBOT_GITEA_TOKEN_VALUE")
|
2021-06-20 18:24:12 +02:00
|
|
|
})
|
2021-06-20 16:49:12 +02:00
|
|
|
}
|
2021-06-20 19:47:55 +02:00
|
|
|
|
2021-06-21 11:00:31 +02:00
|
|
|
func TestLoadSonarQubeStructure(t *testing.T) {
|
2021-06-22 11:27:53 +02:00
|
|
|
WriteConfigFile(t, defaultConfig)
|
2021-06-21 11:00:31 +02:00
|
|
|
Load(os.TempDir())
|
|
|
|
|
|
|
|
expected := SonarQubeConfig{
|
|
|
|
Url: "https://example.com/sonarqube",
|
2021-06-21 11:08:56 +02:00
|
|
|
Token: Token{
|
|
|
|
Value: "a09eb5785b25bb2cbacf48808a677a0709f02d8e",
|
|
|
|
},
|
2021-06-21 11:00:31 +02:00
|
|
|
Webhook: Webhook{
|
|
|
|
Secret: "haxxor-sonarqube-secret",
|
|
|
|
},
|
|
|
|
}
|
2021-06-20 19:47:55 +02:00
|
|
|
|
2021-06-21 11:00:31 +02:00
|
|
|
assert.EqualValues(t, expected, SonarQube)
|
|
|
|
}
|
2021-06-20 19:47:55 +02:00
|
|
|
|
2021-06-21 11:00:31 +02:00
|
|
|
func TestLoadSonarQubeStructureInjectedEnvs(t *testing.T) {
|
|
|
|
os.Setenv("PRBOT_SONARQUBE_WEBHOOK_SECRET", "injected-webhook-secret")
|
2021-06-21 11:08:56 +02:00
|
|
|
os.Setenv("PRBOT_SONARQUBE_TOKEN_VALUE", "injected-token")
|
2021-06-22 11:27:53 +02:00
|
|
|
WriteConfigFile(t, defaultConfig)
|
2021-06-20 19:47:55 +02:00
|
|
|
Load(os.TempDir())
|
|
|
|
|
2021-06-21 11:00:31 +02:00
|
|
|
expected := SonarQubeConfig{
|
|
|
|
Url: "https://example.com/sonarqube",
|
2021-06-21 11:08:56 +02:00
|
|
|
Token: Token{
|
|
|
|
Value: "injected-token",
|
|
|
|
},
|
2021-06-21 11:00:31 +02:00
|
|
|
Webhook: Webhook{
|
|
|
|
Secret: "injected-webhook-secret",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, expected, SonarQube)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Unsetenv("PRBOT_SONARQUBE_WEBHOOK_SECRET")
|
2021-06-21 11:08:56 +02:00
|
|
|
os.Unsetenv("PRBOT_SONARQUBE_TOKEN_VALUE")
|
2021-06-21 11:00:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadStructureWithFileReferenceResolving(t *testing.T) {
|
2021-06-21 11:30:02 +02:00
|
|
|
giteaWebhookSecretFile := path.Join(os.TempDir(), "webhook-secret-gitea")
|
|
|
|
_ = ioutil.WriteFile(giteaWebhookSecretFile, []byte(`gitea-totally-secret`),0444)
|
2021-06-21 11:00:31 +02:00
|
|
|
|
2021-06-21 11:30:02 +02:00
|
|
|
giteaTokenFile := path.Join(os.TempDir(), "token-secret-gitea")
|
|
|
|
_ = ioutil.WriteFile(giteaTokenFile, []byte(`d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565`),0444)
|
|
|
|
|
|
|
|
sonarqubeWebhookSecretFile := path.Join(os.TempDir(), "webhook-secret-sonarqube")
|
|
|
|
_ = ioutil.WriteFile(sonarqubeWebhookSecretFile, []byte(`sonarqube-totally-secret`),0444)
|
|
|
|
|
|
|
|
sonarqubeTokenFile := path.Join(os.TempDir(), "token-secret-sonarqube")
|
|
|
|
_ = ioutil.WriteFile(sonarqubeTokenFile, []byte(`a09eb5785b25bb2cbacf48808a677a0709f02d8e`),0444)
|
2021-06-21 11:00:31 +02:00
|
|
|
|
|
|
|
WriteConfigFile(t, []byte(
|
|
|
|
`gitea:
|
|
|
|
url: https://example.com/gitea
|
2021-06-21 11:08:56 +02:00
|
|
|
token:
|
2021-06-21 11:30:02 +02:00
|
|
|
value: fake-gitea-token
|
2021-06-21 11:00:31 +02:00
|
|
|
sonarqube:
|
|
|
|
url: https://example.com/sonarqube
|
2021-06-21 11:08:56 +02:00
|
|
|
token:
|
2021-06-21 11:30:02 +02:00
|
|
|
value: fake-sonarqube-token
|
2021-06-24 10:36:52 +02:00
|
|
|
projects:
|
|
|
|
- sonarqube:
|
|
|
|
key: gitea-sonarqube-pr-bot
|
|
|
|
gitea:
|
|
|
|
owner: example-organization
|
|
|
|
name: pr-bot
|
2021-06-21 11:00:31 +02:00
|
|
|
`))
|
2021-06-21 11:30:02 +02:00
|
|
|
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRETFILE", giteaWebhookSecretFile)
|
|
|
|
os.Setenv("PRBOT_GITEA_TOKEN_FILE", giteaTokenFile)
|
|
|
|
os.Setenv("PRBOT_SONARQUBE_WEBHOOK_SECRETFILE", sonarqubeWebhookSecretFile)
|
|
|
|
os.Setenv("PRBOT_SONARQUBE_TOKEN_FILE", sonarqubeTokenFile)
|
2021-06-21 11:00:31 +02:00
|
|
|
|
2021-06-20 19:47:55 +02:00
|
|
|
expectedGitea := GiteaConfig{
|
|
|
|
Url: "https://example.com/gitea",
|
2021-06-21 11:08:56 +02:00
|
|
|
Token: Token{
|
|
|
|
Value: "d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565",
|
2021-06-21 11:30:02 +02:00
|
|
|
File: giteaTokenFile,
|
2021-06-21 11:08:56 +02:00
|
|
|
},
|
2021-06-21 11:00:31 +02:00
|
|
|
Webhook: Webhook{
|
|
|
|
Secret: "gitea-totally-secret",
|
2021-06-21 11:30:02 +02:00
|
|
|
SecretFile: giteaWebhookSecretFile,
|
2021-06-20 19:47:55 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedSonarQube := SonarQubeConfig{
|
|
|
|
Url: "https://example.com/sonarqube",
|
2021-06-21 11:08:56 +02:00
|
|
|
Token: Token{
|
|
|
|
Value: "a09eb5785b25bb2cbacf48808a677a0709f02d8e",
|
2021-06-21 11:30:02 +02:00
|
|
|
File: sonarqubeTokenFile,
|
2021-06-21 11:08:56 +02:00
|
|
|
},
|
2021-06-21 11:00:31 +02:00
|
|
|
Webhook: Webhook{
|
|
|
|
Secret: "sonarqube-totally-secret",
|
2021-06-21 11:30:02 +02:00
|
|
|
SecretFile: sonarqubeWebhookSecretFile,
|
2021-06-20 19:47:55 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:00:31 +02:00
|
|
|
Load(os.TempDir())
|
2021-06-20 19:47:55 +02:00
|
|
|
assert.EqualValues(t, expectedGitea, Gitea)
|
|
|
|
assert.EqualValues(t, expectedSonarQube, SonarQube)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2021-06-21 11:30:02 +02:00
|
|
|
os.Remove(giteaWebhookSecretFile)
|
|
|
|
os.Remove(giteaTokenFile)
|
|
|
|
os.Remove(sonarqubeWebhookSecretFile)
|
|
|
|
os.Remove(sonarqubeTokenFile)
|
2021-06-21 11:00:31 +02:00
|
|
|
os.Unsetenv("PRBOT_GITEA_WEBHOOK_SECRETFILE")
|
2021-06-21 11:30:02 +02:00
|
|
|
os.Unsetenv("PRBOT_GITEA_TOKEN_FILE")
|
2021-06-21 11:00:31 +02:00
|
|
|
os.Unsetenv("PRBOT_SONARQUBE_WEBHOOK_SECRETFILE")
|
2021-06-21 11:30:02 +02:00
|
|
|
os.Unsetenv("PRBOT_SONARQUBE_TOKEN_FILE")
|
2021-06-20 19:47:55 +02:00
|
|
|
})
|
|
|
|
}
|
2021-06-22 11:27:53 +02:00
|
|
|
|
|
|
|
func TestLoadProjectsStructure(t *testing.T) {
|
|
|
|
WriteConfigFile(t, defaultConfig)
|
|
|
|
Load(os.TempDir())
|
|
|
|
|
|
|
|
expectedProjects := []Project{
|
|
|
|
Project{
|
|
|
|
SonarQube: struct {Key string}{
|
|
|
|
Key: "gitea-sonarqube-pr-bot",
|
|
|
|
},
|
|
|
|
Gitea: GiteaRepository{
|
|
|
|
Owner: "example-organization",
|
|
|
|
Name: "pr-bot",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, expectedProjects, Projects)
|
|
|
|
}
|
2021-06-24 10:36:52 +02:00
|
|
|
|
|
|
|
func TestLoadProjectsStructureWithNoMapping(t *testing.T) {
|
|
|
|
invalidConfig := []byte(
|
|
|
|
`gitea:
|
|
|
|
url: https://example.com/gitea
|
|
|
|
token:
|
|
|
|
value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
|
|
|
|
webhook:
|
|
|
|
secret: haxxor-gitea-secret
|
|
|
|
sonarqube:
|
|
|
|
url: https://example.com/sonarqube
|
|
|
|
token:
|
|
|
|
value: a09eb5785b25bb2cbacf48808a677a0709f02d8e
|
|
|
|
webhook:
|
|
|
|
secret: haxxor-sonarqube-secret
|
|
|
|
projects: []
|
|
|
|
`)
|
|
|
|
WriteConfigFile(t, invalidConfig)
|
|
|
|
|
|
|
|
assert.Panics(t, func() { Load(os.TempDir()) }, "No panic for empty project mapping that is required")
|
|
|
|
}
|