You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4 KiB

  1. package config_test
  2. import (
  3. "github.com/jaedle/mirror-to-gitea/internal/config"
  4. . "github.com/onsi/ginkgo"
  5. . "github.com/onsi/gomega"
  6. "os"
  7. )
  8. const mirrorMode = "MIRROR_MODE"
  9. const giteaUrl = "GITEA_URL"
  10. const giteaToken = "GITEA_TOKEN"
  11. const publicMirrorMode = "PUBLIC"
  12. const privateAndPublicMirrorMode = "PRIVATE_AND_PUBLIC"
  13. const unknownMirrorMode = "UNKNOWN"
  14. var _ = Describe("Read", func() {
  15. BeforeEach(func() {
  16. os.Clearenv()
  17. })
  18. Context("Gitea", func() {
  19. It("parses configuration", func() {
  20. aValidEnv()
  21. setEnv(giteaUrl, "https://gitea.url/api")
  22. setEnv(giteaToken, "a-gitea-token")
  23. c, err := config.Read()
  24. Expect(err).NotTo(HaveOccurred())
  25. Expect(c.Gitea.GiteaUrl).To(Equal("https://gitea.url/api"))
  26. Expect(c.Gitea.GiteaToken).To(Equal("a-gitea-token"))
  27. })
  28. It("fails on missing gitea url", func() {
  29. aValidEnv()
  30. unsetEnv(giteaUrl)
  31. c, err := config.Read()
  32. Expect(err).To(HaveOccurred())
  33. Expect(c).To(BeNil())
  34. })
  35. It("fails on missing gitea token", func() {
  36. aValidEnv()
  37. unsetEnv(giteaToken)
  38. c, err := config.Read()
  39. Expect(err).To(HaveOccurred())
  40. Expect(c).To(BeNil())
  41. })
  42. })
  43. Context("mirror mode", func() {
  44. It("sets default mirror mode", func() {
  45. aValidEnv()
  46. unsetEnv(mirrorMode)
  47. c, err := config.Read()
  48. Expect(err).NotTo(HaveOccurred())
  49. Expect(c.MirrorMode).To(Equal(config.MirrorModePublic))
  50. })
  51. It("allows public mirror mode PUBLIC", func() {
  52. aValidEnv()
  53. setEnv(mirrorMode, publicMirrorMode)
  54. c, err := config.Read()
  55. Expect(err).NotTo(HaveOccurred())
  56. Expect(c.MirrorMode).To(Equal(config.MirrorModePublic))
  57. })
  58. It("allows mirror mode PRIVATE_AND_PUBLIC", func() {
  59. aValidEnv()
  60. setEnv(mirrorMode, privateAndPublicMirrorMode)
  61. c, err := config.Read()
  62. Expect(err).NotTo(HaveOccurred())
  63. Expect(c.MirrorMode).To(Equal(config.MirrorModePrivateAndPublic))
  64. })
  65. It("fails on unknown mirror mode", func() {
  66. aValidEnv()
  67. setEnv(mirrorMode, unknownMirrorMode)
  68. c, err := config.Read()
  69. Expect(err).To(HaveOccurred())
  70. Expect(c).To(BeNil())
  71. })
  72. })
  73. })
  74. func setEnv(k string, v string) {
  75. err := os.Setenv(k, v)
  76. Expect(err).NotTo(HaveOccurred())
  77. }
  78. func unsetEnv(k string) {
  79. err := os.Unsetenv(k)
  80. Expect(err).NotTo(HaveOccurred())
  81. }
  82. func aValidEnv() {
  83. setEnv(mirrorMode, "PUBLIC")
  84. setEnv(giteaUrl, "https://gitea.url")
  85. setEnv(giteaToken, "valid")
  86. }