| 1 | package ntfy_test |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "testing" |
| 6 | "testing/synctest" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/services/push/ntfy" |
| 11 | ) |
| 12 | |
| 13 | const validNtfyURL = "ntfy://ntfy.example.com/mytopic" |
| 14 | |
| 15 | func TestServiceInitialization(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | synctest.Test(t, func(t *testing.T) { |
| 18 | mockClient := &MockHTTPClient{} |
| 19 | service := createTestService( |
| 20 | t, |
| 21 | validNtfyURL, |
| 22 | mockClient, |
| 23 | ) |
| 24 | |
| 25 | require.NotNil(t, service) |
| 26 | require.Equal(t, "ntfy", service.GetID()) |
| 27 | |
| 28 | mockClient.AssertExpectations(t) |
| 29 | }) |
| 30 | } |
| 31 | |
| 32 | func TestServiceInitializationWithCredentials(t *testing.T) { |
| 33 | t.Parallel() |
| 34 | synctest.Test(t, func(t *testing.T) { |
| 35 | mockClient := &MockHTTPClient{} |
| 36 | serviceURL := "ntfy://user:[email protected]/mytopic" |
| 37 | service := createTestService( |
| 38 | t, |
| 39 | serviceURL, |
| 40 | mockClient, |
| 41 | ) |
| 42 | |
| 43 | require.NotNil(t, service) |
| 44 | require.Equal(t, "ntfy", service.GetID()) |
| 45 | require.Equal(t, "user", service.Config.Username) |
| 46 | require.Equal(t, "pass", service.Config.Password) |
| 47 | |
| 48 | mockClient.AssertExpectations(t) |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | func TestServiceInitializationWithTopicPath(t *testing.T) { |
| 53 | t.Parallel() |
| 54 | synctest.Test(t, func(t *testing.T) { |
| 55 | mockClient := &MockHTTPClient{} |
| 56 | serviceURL := "ntfy://ntfy.example.com/alerts/critical" |
| 57 | service := createTestService( |
| 58 | t, |
| 59 | serviceURL, |
| 60 | mockClient, |
| 61 | ) |
| 62 | |
| 63 | require.NotNil(t, service) |
| 64 | require.Equal(t, "alerts/critical", service.Config.Topic) |
| 65 | |
| 66 | mockClient.AssertExpectations(t) |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | func TestServiceInitializationWithDisableTLS(t *testing.T) { |
| 71 | t.Parallel() |
| 72 | synctest.Test(t, func(t *testing.T) { |
| 73 | mockClient := &MockHTTPClient{} |
| 74 | serviceURL := "ntfy://ntfy.example.com/mytopic?disabletls=yes" |
| 75 | service := createTestService( |
| 76 | t, |
| 77 | serviceURL, |
| 78 | mockClient, |
| 79 | ) |
| 80 | |
| 81 | require.NotNil(t, service) |
| 82 | require.Equal(t, "http", service.Config.Scheme) |
| 83 | |
| 84 | mockClient.AssertExpectations(t) |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | func TestServiceInitializationWithDisableTLSVerification(t *testing.T) { |
| 89 | t.Parallel() |
| 90 | synctest.Test(t, func(t *testing.T) { |
| 91 | mockClient := &MockHTTPClient{} |
| 92 | serviceURL := "ntfy://ntfy.example.com/mytopic?disabletlsverification=yes" |
| 93 | service := createTestService( |
| 94 | t, |
| 95 | serviceURL, |
| 96 | mockClient, |
| 97 | ) |
| 98 | |
| 99 | require.NotNil(t, service) |
| 100 | require.True(t, service.Config.DisableTLSVerification) |
| 101 | |
| 102 | mockClient.AssertExpectations(t) |
| 103 | }) |
| 104 | } |
| 105 | |
| 106 | func TestServiceInitializationWithPriority(t *testing.T) { |
| 107 | t.Parallel() |
| 108 | synctest.Test(t, func(t *testing.T) { |
| 109 | mockClient := &MockHTTPClient{} |
| 110 | serviceURL := "ntfy://ntfy.example.com/mytopic?priority=Max" |
| 111 | service := createTestService( |
| 112 | t, |
| 113 | serviceURL, |
| 114 | mockClient, |
| 115 | ) |
| 116 | |
| 117 | require.NotNil(t, service) |
| 118 | require.Equal(t, ntfy.PriorityMax, service.Config.Priority) |
| 119 | |
| 120 | mockClient.AssertExpectations(t) |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | func TestServiceInitializationWithMarkdown(t *testing.T) { |
| 125 | t.Parallel() |
| 126 | synctest.Test(t, func(t *testing.T) { |
| 127 | mockClient := &MockHTTPClient{} |
| 128 | serviceURL := "ntfy://ntfy.example.com/mytopic?markdown=yes" |
| 129 | service := createTestService( |
| 130 | t, |
| 131 | serviceURL, |
| 132 | mockClient, |
| 133 | ) |
| 134 | |
| 135 | require.NotNil(t, service) |
| 136 | require.True(t, service.Config.Markdown) |
| 137 | |
| 138 | mockClient.AssertExpectations(t) |
| 139 | }) |
| 140 | } |
| 141 | |
| 142 | func TestServiceInitializationWithTags(t *testing.T) { |
| 143 | t.Parallel() |
| 144 | synctest.Test(t, func(t *testing.T) { |
| 145 | mockClient := &MockHTTPClient{} |
| 146 | serviceURL := "ntfy://ntfy.example.com/mytopic?tags=warning,skull" |
| 147 | service := createTestService( |
| 148 | t, |
| 149 | serviceURL, |
| 150 | mockClient, |
| 151 | ) |
| 152 | |
| 153 | require.NotNil(t, service) |
| 154 | require.Equal(t, []string{"warning", "skull"}, service.Config.Tags) |
| 155 | |
| 156 | mockClient.AssertExpectations(t) |
| 157 | }) |
| 158 | } |
| 159 | |
| 160 | func TestServiceInitializationWithTitle(t *testing.T) { |
| 161 | t.Parallel() |
| 162 | synctest.Test(t, func(t *testing.T) { |
| 163 | mockClient := &MockHTTPClient{} |
| 164 | serviceURL := "ntfy://ntfy.example.com/mytopic?title=Alert" |
| 165 | service := createTestService( |
| 166 | t, |
| 167 | serviceURL, |
| 168 | mockClient, |
| 169 | ) |
| 170 | |
| 171 | require.NotNil(t, service) |
| 172 | require.Equal(t, "Alert", service.Config.Title) |
| 173 | |
| 174 | mockClient.AssertExpectations(t) |
| 175 | }) |
| 176 | } |
| 177 | |
| 178 | func TestServiceInitializationMissingTopic(t *testing.T) { |
| 179 | t.Parallel() |
| 180 | synctest.Test(t, func(t *testing.T) { |
| 181 | service := &ntfy.Service{} |
| 182 | serviceURL := mustParseURL(t, "ntfy://ntfy.example.com/") |
| 183 | |
| 184 | err := service.Initialize(serviceURL, &mockLogger{}) |
| 185 | require.Error(t, err) |
| 186 | require.ErrorIs(t, err, ntfy.ErrTopicRequired) |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | func TestServiceInitializationWithUsernameOnly(t *testing.T) { |
| 191 | t.Parallel() |
| 192 | synctest.Test(t, func(t *testing.T) { |
| 193 | mockClient := &MockHTTPClient{} |
| 194 | serviceURL := "ntfy://[email protected]/mytopic" |
| 195 | service := createTestService( |
| 196 | t, |
| 197 | serviceURL, |
| 198 | mockClient, |
| 199 | ) |
| 200 | |
| 201 | require.NotNil(t, service) |
| 202 | require.Equal(t, "myuser", service.Config.Username) |
| 203 | require.Empty(t, service.Config.Password) |
| 204 | |
| 205 | mockClient.AssertExpectations(t) |
| 206 | }) |
| 207 | } |
| 208 | |
| 209 | func TestConfigDefaults(t *testing.T) { |
| 210 | t.Parallel() |
| 211 | synctest.Test(t, func(t *testing.T) { |
| 212 | service := &ntfy.Service{} |
| 213 | |
| 214 | parsedURL, err := url.Parse("ntfy://ntfy.example.com/mytopic") |
| 215 | require.NoError(t, err) |
| 216 | |
| 217 | err = service.Initialize(parsedURL, &mockLogger{}) |
| 218 | require.NoError(t, err) |
| 219 | |
| 220 | require.Equal(t, "https", service.Config.Scheme) |
| 221 | require.Equal(t, "ntfy.example.com", service.Config.Host) |
| 222 | require.Equal(t, "mytopic", service.Config.Topic) |
| 223 | require.Empty(t, service.Config.Username) |
| 224 | require.Empty(t, service.Config.Password) |
| 225 | require.Empty(t, service.Config.Title) |
| 226 | require.Equal(t, "Default", service.Config.Priority.String()) |
| 227 | require.False(t, service.Config.Markdown) |
| 228 | require.True(t, service.Config.Cache) |
| 229 | require.True(t, service.Config.Firebase) |
| 230 | require.False(t, service.Config.DisableTLSVerification) |
| 231 | require.False(t, service.Config.DisableTLS) |
| 232 | }) |
| 233 | } |
| 234 | |
| 235 | func TestConfigURLRoundTrip(t *testing.T) { |
| 236 | t.Parallel() |
| 237 | synctest.Test(t, func(t *testing.T) { |
| 238 | mockClient := &MockHTTPClient{} |
| 239 | service := createTestService( |
| 240 | t, |
| 241 | "ntfy://user:[email protected]/mytopic?priority=High&tags=warning,skull&markdown=yes", |
| 242 | mockClient, |
| 243 | ) |
| 244 | |
| 245 | require.NotNil(t, service) |
| 246 | |
| 247 | reconstructed := service.Config.GetURL() |
| 248 | require.Equal(t, "user", reconstructed.User.Username()) |
| 249 | password, _ := reconstructed.User.Password() |
| 250 | require.Equal(t, "pass", password) |
| 251 | require.Equal(t, "ntfy.example.com", reconstructed.Host) |
| 252 | require.Equal(t, "/mytopic", reconstructed.Path) |
| 253 | require.Equal(t, "High", reconstructed.Query().Get("priority")) |
| 254 | require.Equal(t, "warning,skull", reconstructed.Query().Get("tags")) |
| 255 | require.Equal(t, "Yes", reconstructed.Query().Get("markdown")) |
| 256 | |
| 257 | mockClient.AssertExpectations(t) |
| 258 | }) |
| 259 | } |
| 260 | |
| 261 | func mustParseURL(t *testing.T, raw string) *url.URL { |
| 262 | t.Helper() |
| 263 | |
| 264 | parsed, err := url.Parse(raw) |
| 265 | require.NoError(t, err) |
| 266 | |
| 267 | return parsed |
| 268 | } |