2021-06-20 15:16:06 +02:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-06-20 16:49:12 +02:00
|
|
|
var defaultConfigInlineSecrets []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
|
|
|
|
repositories:
|
|
|
|
- owner: some-owner
|
|
|
|
name: a-repository-name
|
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-20 15:16:06 +02:00
|
|
|
projects: []
|
|
|
|
`)
|
|
|
|
|
|
|
|
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-20 16:49:12 +02:00
|
|
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
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) {
|
|
|
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
Repositories: []GiteaRepository{
|
|
|
|
GiteaRepository{
|
|
|
|
Owner: "some-owner",
|
|
|
|
Name: "a-repository-name",
|
|
|
|
},
|
2021-06-20 19:47:55 +02:00
|
|
|
},
|
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-20 18:24:12 +02:00
|
|
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
Repositories: []GiteaRepository{
|
|
|
|
GiteaRepository{
|
|
|
|
Owner: "some-owner",
|
|
|
|
Name: "a-repository-name",
|
|
|
|
},
|
2021-06-20 19:47:55 +02:00
|
|
|
},
|
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) {
|
|
|
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
Projects: []string{},
|
|
|
|
}
|
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-21 11:00:31 +02:00
|
|
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
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",
|
|
|
|
},
|
|
|
|
Projects: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
giteaSecretFile := path.Join(os.TempDir(), "webhook-secret-gitea")
|
|
|
|
sonarqubeSecretFile := path.Join(os.TempDir(), "webhook-secret-sonarqube")
|
|
|
|
|
|
|
|
_ = ioutil.WriteFile(giteaSecretFile, []byte(`gitea-totally-secret`),0444)
|
|
|
|
_ = ioutil.WriteFile(sonarqubeSecretFile, []byte(`sonarqube-totally-secret`),0444)
|
|
|
|
|
|
|
|
WriteConfigFile(t, []byte(
|
|
|
|
`gitea:
|
|
|
|
url: https://example.com/gitea
|
2021-06-21 11:08:56 +02:00
|
|
|
token:
|
|
|
|
value: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
|
2021-06-21 11:00:31 +02:00
|
|
|
repositories: []
|
|
|
|
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
|
|
|
projects: []
|
|
|
|
`))
|
|
|
|
os.Setenv("PRBOT_GITEA_WEBHOOK_SECRETFILE", giteaSecretFile)
|
|
|
|
os.Setenv("PRBOT_SONARQUBE_WEBHOOK_SECRETFILE", sonarqubeSecretFile)
|
|
|
|
|
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:00:31 +02:00
|
|
|
Webhook: Webhook{
|
|
|
|
Secret: "gitea-totally-secret",
|
|
|
|
SecretFile: giteaSecretFile,
|
2021-06-20 19:47:55 +02:00
|
|
|
},
|
|
|
|
Repositories: []GiteaRepository{},
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedSonarQube := 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: "sonarqube-totally-secret",
|
|
|
|
SecretFile: sonarqubeSecretFile,
|
2021-06-20 19:47:55 +02:00
|
|
|
},
|
|
|
|
Projects: []string{},
|
|
|
|
}
|
|
|
|
|
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:00:31 +02:00
|
|
|
os.Remove(giteaSecretFile)
|
|
|
|
os.Remove(sonarqubeSecretFile)
|
|
|
|
os.Unsetenv("PRBOT_GITEA_WEBHOOK_SECRETFILE")
|
|
|
|
os.Unsetenv("PRBOT_SONARQUBE_WEBHOOK_SECRETFILE")
|
2021-06-20 19:47:55 +02:00
|
|
|
})
|
|
|
|
}
|