<commit_before>package api_test

import (
	"fmt"
	"io/ioutil"
	"os"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	. "github.com/starkandwayne/shield/api"
)

var _ = Describe("API Config", func() {
	Describe("When loading configs", func() {
		defaultCfg := &Config{Backends: map[string]string{}, Aliases: map[string]string{}}
		BeforeEach(func() {
			os.Chmod("test/etc/unreadable.yml", 0200)
		})
		AfterEach(func() {
			os.Chmod("test/etc/unreadable.yml", 0644)
		})
		It("Throws an error on invalid yaml files", func() {
			Expect(LoadConfig("test/etc/invalid.yml")).ShouldNot(Succeed())
			defaultCfg.Path = "test/etc/invalid.yml"
			Expect(Cfg).Should(Equal(defaultCfg))
		})
		It("Throws an error on unreadable files", func() {
			Expect(LoadConfig("test/etc/unreadable.yml")).ShouldNot(Succeed())
			defaultCfg.Path = "test/etc/unreadable.yml"
			Expect(Cfg).Should(Equal(defaultCfg))
		})
		It("Succeeds if no config was found", func() {
			Expect(LoadConfig("test/etc/missing.yml")).Should(Succeed())
			defaultCfg.Path = "test/etc/missing.yml"
			Expect(Cfg).Should(Equal(defaultCfg))
		})
		It("Reads configs and sets up the api.Cfg variable if config was valid", func() {
			Expect(LoadConfig("test/etc/valid.yml")).Should(Succeed())

			valid := &Config{
				Backends: map[string]string{
					"http://first":  "basic mytoken1",
					"http://second": "basic mytoken2",
				},
				Aliases: map[string]string{
					"first":  "http://first",
					"second": "http://second",
				},
				Backend: "first",
				Path:    "test/etc/valid.yml",
			}<commit_msg>Don't test unreadable files if euid is not 0<commit_after>package api_test

import (
	"fmt"
	"io/ioutil"
	"os"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	. "github.com/starkandwayne/shield/api"
)

var _ = Describe("API Config", func() {
	Describe("When loading configs", func() {
		defaultCfg := &Config{Backends: map[string]string{}, Aliases: map[string]string{}}
		BeforeEach(func() {
			os.Chmod("test/etc/unreadable.yml", 0200)
		})
		AfterEach(func() {
			os.Chmod("test/etc/unreadable.yml", 0644)
		})
		It("Throws an error on invalid yaml files", func() {
			Expect(LoadConfig("test/etc/invalid.yml")).ShouldNot(Succeed())
			defaultCfg.Path = "test/etc/invalid.yml"
			Expect(Cfg).Should(Equal(defaultCfg))
		})

		It("Throws an error on unreadable files", func() {
			if os.Geteuid() == 0 {
				Skip("Cannot test unreadable files when euid = 0")
			}
			Expect(LoadConfig("test/etc/unreadable.yml")).ShouldNot(Succeed())
			defaultCfg.Path = "test/etc/unreadable.yml"
			Expect(Cfg).Should(Equal(defaultCfg))
		})
		It("Succeeds if no config was found", func() {
			Expect(LoadConfig("test/etc/missing.yml")).Should(Succeed())
			defaultCfg.Path = "test/etc/missing.yml"
			Expect(Cfg).Should(Equal(defaultCfg))
		})
		It("Reads configs and sets up the api.Cfg variable if config was valid", func() {
			Expect(LoadConfig("test/etc/valid.yml")).Should(Succeed())

			valid := &Config{
				Backends: map[string]string{
					"http://first":  "basic mytoken1",
					"http://second": "basic mytoken2",
				},
				Aliases: map[string]string{
					"first":  "http://first",
					"second": "http://second",
				},
				Backend: "first",
				Path:    "test/etc/valid.yml",
			}
{'fraction_non_alphanumeric': 0.18084455324357404, 'fraction_numerical': 0.007037943696450428, 'mean_word_length': 3.9157894736842107, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6494079', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1166, 'n_words': 210}