| 1 | package testutils |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/onsi/ginkgo/v2" |
| 8 | "github.com/onsi/gomega" |
| 9 | |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 11 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 | ) |
| 13 | |
| 14 | type dummyConfig struct { |
| 15 | standard.EnumlessConfig |
| 16 | |
| 17 | Foo uint64 `default:"-1" key:"foo"` |
| 18 | } |
| 19 | |
| 20 | type dummyService struct { |
| 21 | standard.Standard |
| 22 | |
| 23 | Config dummyConfig |
| 24 | } |
| 25 | |
| 26 | var _ = ginkgo.Describe("the testutils package", func() { |
| 27 | ginkgo.When("calling function TestLogger", func() { |
| 28 | ginkgo.It("should not return nil", func() { |
| 29 | gomega.Expect(TestLogger()).NotTo(gomega.BeNil()) |
| 30 | }) |
| 31 | ginkgo.It(`should have the prefix "[Test] "`, func() { |
| 32 | gomega.Expect(TestLogger().Prefix()).To(gomega.Equal("[Test] ")) |
| 33 | }) |
| 34 | }) |
| 35 | |
| 36 | ginkgo.Describe("Must helpers", func() { |
| 37 | ginkgo.Describe("URLMust", func() { |
| 38 | ginkgo.It("should panic when an invalid URL is passed", func() { |
| 39 | failures := gomega.InterceptGomegaFailures(func() { URLMust(":") }) |
| 40 | gomega.Expect(failures).To(gomega.HaveLen(1)) |
| 41 | }) |
| 42 | }) |
| 43 | |
| 44 | ginkgo.Describe("JSONRespondMust", func() { |
| 45 | ginkgo.It("should panic when an invalid struct is passed", func() { |
| 46 | notAValidJSONSource := func() {} |
| 47 | failures := gomega.InterceptGomegaFailures( |
| 48 | func() { JSONRespondMust(200, notAValidJSONSource) }, |
| 49 | ) |
| 50 | gomega.Expect(failures).To(gomega.HaveLen(1)) |
| 51 | }) |
| 52 | }) |
| 53 | }) |
| 54 | |
| 55 | ginkgo.Describe("Config test helpers", func() { |
| 56 | var config dummyConfig |
| 57 | |
| 58 | ginkgo.BeforeEach(func() { |
| 59 | config = dummyConfig{ |
| 60 | EnumlessConfig: standard.EnumlessConfig{}, |
| 61 | Foo: 0, |
| 62 | } |
| 63 | }) |
| 64 | ginkgo.Describe("TestConfigSetInvalidQueryValue", func() { |
| 65 | ginkgo.It("should fail when not correctly implemented", func() { |
| 66 | failures := gomega.InterceptGomegaFailures(func() { |
| 67 | TestConfigSetInvalidQueryValue(&config, "mock://host?invalid=value") |
| 68 | }) |
| 69 | gomega.Expect(failures).To(gomega.HaveLen(1)) |
| 70 | }) |
| 71 | }) |
| 72 | |
| 73 | ginkgo.Describe("TestConfigGetInvalidQueryValue", func() { |
| 74 | ginkgo.It("should fail when not correctly implemented", func() { |
| 75 | failures := gomega.InterceptGomegaFailures(func() { |
| 76 | TestConfigGetInvalidQueryValue(&config) |
| 77 | }) |
| 78 | gomega.Expect(failures).To(gomega.HaveLen(1)) |
| 79 | }) |
| 80 | }) |
| 81 | |
| 82 | ginkgo.Describe("TestConfigSetDefaultValues", func() { |
| 83 | ginkgo.It("should fail when not correctly implemented", func() { |
| 84 | failures := gomega.InterceptGomegaFailures(func() { |
| 85 | TestConfigSetDefaultValues(&config) |
| 86 | }) |
| 87 | gomega.Expect(failures).NotTo(gomega.BeEmpty()) |
| 88 | }) |
| 89 | }) |
| 90 | |
| 91 | ginkgo.Describe("TestConfigGetEnumsCount", func() { |
| 92 | ginkgo.It("should fail when not correctly implemented", func() { |
| 93 | failures := gomega.InterceptGomegaFailures(func() { |
| 94 | TestConfigGetEnumsCount(&config, 99) |
| 95 | }) |
| 96 | gomega.Expect(failures).NotTo(gomega.BeEmpty()) |
| 97 | }) |
| 98 | }) |
| 99 | |
| 100 | ginkgo.Describe("TestConfigGetFieldsCount", func() { |
| 101 | ginkgo.It("should fail when not correctly implemented", func() { |
| 102 | failures := gomega.InterceptGomegaFailures(func() { |
| 103 | TestConfigGetFieldsCount(&config, 99) |
| 104 | }) |
| 105 | gomega.Expect(failures).NotTo(gomega.BeEmpty()) |
| 106 | }) |
| 107 | }) |
| 108 | }) |
| 109 | |
| 110 | ginkgo.Describe("Service test helpers", func() { |
| 111 | var service dummyService |
| 112 | |
| 113 | ginkgo.BeforeEach(func() { |
| 114 | service = dummyService{ |
| 115 | Standard: standard.Standard{ |
| 116 | Logger: standard.Logger{}, |
| 117 | Templater: standard.Templater{}, |
| 118 | }, |
| 119 | Config: dummyConfig{ |
| 120 | EnumlessConfig: standard.EnumlessConfig{}, |
| 121 | Foo: 0, |
| 122 | }, |
| 123 | } |
| 124 | }) |
| 125 | ginkgo.Describe("TestConfigSetInvalidQueryValue", func() { |
| 126 | ginkgo.It("should fail when not correctly implemented", func() { |
| 127 | failures := gomega.InterceptGomegaFailures(func() { |
| 128 | TestServiceSetInvalidParamValue(&service, "invalid", "value") |
| 129 | }) |
| 130 | gomega.Expect(failures).To(gomega.HaveLen(1)) |
| 131 | }) |
| 132 | }) |
| 133 | }) |
| 134 | }) |
| 135 | |
| 136 | func (dc *dummyConfig) Get(string) (string, error) { return "", nil } |
| 137 | func (dc *dummyConfig) GetURL() *url.URL { |
| 138 | return &url.URL{ |
| 139 | Scheme: "", |
| 140 | Opaque: "", |
| 141 | User: nil, |
| 142 | Host: "", |
| 143 | Path: "", |
| 144 | RawPath: "", |
| 145 | OmitHost: false, |
| 146 | ForceQuery: false, |
| 147 | RawQuery: "", |
| 148 | Fragment: "", |
| 149 | RawFragment: "", |
| 150 | } |
| 151 | } |
| 152 | func (dc *dummyConfig) QueryFields() []string { return []string{} } |
| 153 | func (dc *dummyConfig) Set(string, string) error { return nil } |
| 154 | func (dc *dummyConfig) SetURL(_ *url.URL) error { return nil } |
| 155 | |
| 156 | func (s *dummyService) GetID() string { return "dummy" } |
| 157 | func (s *dummyService) Initialize(_ *url.URL, _ types.StdLogger) error { return nil } |
| 158 | func (s *dummyService) Send(_ string, _ *types.Params) error { return nil } |
| 159 | |
| 160 | func TestTestUtils(t *testing.T) { |
| 161 | t.Parallel() |
| 162 | gomega.RegisterFailHandler(ginkgo.Fail) |
| 163 | |
| 164 | ginkgo.RunSpecs(t, "Shoutrrr TestUtils Suite") |
| 165 | } |