Skip to content

Modern Docker Management, Designed for Everyone

4.3 KB
Raw Blame
package notifications

import (
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestBuildNtfyURL(t *testing.T) {
	tests := []struct {
		name    string
		config  NtfyConfig
		wantErr bool
		check   func(string) bool
	}{
		{
			name: "basic config with default host",
			config: NtfyConfig{
				Topic:    "test-topic",
				Cache:    true,
				Firebase: true,
			},
			wantErr: false,
			check: func(url string) bool {
				return url == "ntfy://ntfy.sh/test-topic?cache=yes&firebase=yes&markdown=yes"
			},
		},
		{
			name: "config with custom host",
			config: NtfyConfig{
				Host:     "ntfy.example.com",
				Topic:    "alerts",
				Cache:    true,
				Firebase: true,
			},
			wantErr: false,
			check: func(url string) bool {
				return url == "ntfy://ntfy.example.com/alerts?cache=yes&firebase=yes&markdown=yes"
			},
		},
		{
			name: "config with port",
			config: NtfyConfig{
				Host:     "ntfy.example.com",
				Port:     8080,
				Topic:    "updates",
				Cache:    true,
				Firebase: true,
			},
			wantErr: false,
			check: func(url string) bool {
				return url == "ntfy://ntfy.example.com:8080/updates?cache=yes&firebase=yes&markdown=yes"
			},
		},
		{
			name: "config with auth",
			config: NtfyConfig{
				Host:     "ntfy.example.com",
				Port:     443,
				Topic:    "private",
				Username: "user",
				Password: "pass",
				Cache:    true,
				Firebase: true,
			},
			wantErr: false,
			check: func(url string) bool {
				return url == "ntfy://user:[email protected]:443/private?cache=yes&firebase=yes&markdown=yes"
			},
		},
		{
			name: "config with title priority and tags",
			config: NtfyConfig{
				Host:     "ntfy.sh",
				Topic:    "alerts",
				Title:    "Arcane Update",
				Priority: "high",
				Tags:     []string{"warning", "server"},
				Cache:    true,
				Firebase: true,
			},
			wantErr: false,
			check: func(url string) bool {
				return url == "ntfy://ntfy.sh/alerts?cache=yes&firebase=yes&markdown=yes&priority=high&tags=warning%2Cserver&title=Arcane+Update"
			},
		},
		{
			name: "missing topic",
			config: NtfyConfig{
				Host: "ntfy.sh",
			},
			wantErr: true,
		},
		{
			name: "config with all options",
			config: NtfyConfig{
				Host:                   "ntfy.example.com",
				Port:                   8080,
				Topic:                  "test",
				Username:               "user",
				Password:               "pass",
				Title:                  "Arcane Alert",
				Priority:               "max",
				Tags:                   []string{"urgent"},
				Icon:                   "https://example.com/icon.png",
				Cache:                  false,
				Firebase:               false,
				DisableTLSVerification: true,
			},
			wantErr: false,
			check: func(url string) bool {
				return url == "ntfy://user:[email protected]:8080/test?cache=no&disabletlsverification=yes&firebase=no&icon=htt"+
					"ps%3A%2F%2Fexample.com%2Ficon.png&markdown=yes&priority=max&tags=urgent&title=Arcane+Alert"
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotURL, err := BuildNtfyURL(tt.config)
			if tt.wantErr {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
				assert.True(t, tt.check(gotURL), "URL mismatch: %s", gotURL)
			}
		})
	}
}

func TestBuildNtfyURLDisableTLSVerificationUsesCertificateVerificationFlag(t *testing.T) {
	gotURL, err := BuildNtfyURL(NtfyConfig{
		Host:                   "ntfy.example.com",
		Topic:                  "alerts",
		Cache:                  true,
		Firebase:               true,
		DisableTLSVerification: true,
	})
	require.NoError(t, err)

	parsedURL, err := url.Parse(gotURL)
	require.NoError(t, err)

	query := parsedURL.Query()
	assert.Equal(t, "yes", query.Get("markdown"))
	assert.Equal(t, "yes", query.Get("disabletlsverification"))
	assert.Empty(t, query.Get("disabletls"))
}

func TestBuildNtfyURLDisableTLSUsesPlainHTTPFlag(t *testing.T) {
	gotURL, err := BuildNtfyURL(NtfyConfig{
		Host:       "ntfy.example.com",
		Topic:      "alerts",
		Cache:      true,
		Firebase:   true,
		DisableTLS: true,
	})
	require.NoError(t, err)

	parsedURL, err := url.Parse(gotURL)
	require.NoError(t, err)

	query := parsedURL.Query()
	assert.Equal(t, "yes", query.Get("markdown"))
	assert.Equal(t, "yes", query.Get("disabletls"))
	assert.Empty(t, query.Get("disabletlsverification"))
}