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
|
|
|
|
token: 1337
|
2021-06-20 16:49:12 +02:00
|
|
|
webhookSecret: ""
|
2021-06-20 15:16:06 +02:00
|
|
|
repositories: []
|
|
|
|
sonarqube:
|
|
|
|
url: https://example.com/sonarqube
|
|
|
|
token: 42
|
2021-06-20 16:49:12 +02:00
|
|
|
webhookSecret: ""
|
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 TestLoadWithMissingGiteaStructure(t *testing.T) {
|
|
|
|
WriteConfigFile(t, []byte(``))
|
|
|
|
|
|
|
|
assert.Panics(t, func() { Load(os.TempDir()) }, "No panic when Gitea is not configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadGiteaStructure(t *testing.T) {
|
|
|
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
|
|
|
Load(os.TempDir())
|
|
|
|
|
|
|
|
expected := &GiteaConfig{
|
|
|
|
Url: "https://example.com/gitea",
|
|
|
|
Token: "1337",
|
|
|
|
WebhookSecret: "",
|
|
|
|
Repositories: []GiteaRepository{},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, assert.ObjectsAreEqualValues(&Gitea, expected))
|
|
|
|
}
|