Skip to content

Modern Docker Management, Designed for Everyone

8.4 KB
Raw Blame
package notifications

import (
	"errors"
	"testing"

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

func TestBuildSignalURL(t *testing.T) {
	tests := []struct {
		name   string
		config SignalConfig
		check  func(string) bool
	}{
		{
			name: "basic auth configuration",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				User:       "admin",
				Password:   "secret",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			check: func(url string) bool {
				return assert.Contains(t, url, "signal://admin:[email protected]:8080")
			},
		},
		{
			name: "token auth configuration",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "auth-token-123",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			check: func(url string) bool {
				return assert.Contains(t, url, "signal://signal.example.com:8080") &&
					assert.Contains(t, url, "token=auth-token-123")
			},
		},
		{
			name: "with DisableTLS",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "token123",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
				DisableTLS: true,
			},
			check: func(url string) bool {
				return assert.Contains(t, url, "disabletls=Yes")
			},
		},
		{
			name: "custom port",
			config: SignalConfig{
				Host:       "signal-api.local",
				Port:       9443,
				User:       "user",
				Password:   "pass",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			check: func(url string) bool {
				return assert.Contains(t, url, "signal://user:[email protected]:9443")
			},
		},
		{
			name: "NotifySelf defaults to true (omitted in shoutrrr URL)",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "token123",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			check: func(url string) bool {
				return assert.NotContains(t, url, "notifyself=")
			},
		},
		{
			name: "NotifySelf explicitly true (omitted in shoutrrr URL)",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "token123",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
				NotifySelf: new(true),
			},
			check: func(url string) bool {
				return assert.NotContains(t, url, "notifyself=")
			},
		},
		{
			name: "NotifySelf explicitly false (included as notifyself=No)",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "token123",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
				NotifySelf: new(false),
			},
			check: func(url string) bool {
				return assert.Contains(t, url, "notifyself=No")
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotURL, err := BuildSignalURL(tt.config)
			require.NoError(t, err)
			tt.check(gotURL)
		})
	}
}

func TestBuildSignalURL_AuthenticationMethods(t *testing.T) {
	tests := []struct {
		name     string
		config   SignalConfig
		wantAuth string
	}{
		{
			name: "basic auth only",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				User:       "testuser",
				Password:   "testpass",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantAuth: "testuser:testpass@",
		},
		{
			name: "token auth only",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "my-secure-token",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantAuth: "token=my-secure-token",
		},
		{
			name: "both basic and token auth provided",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				User:       "user",
				Password:   "pass",
				Token:      "token",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantAuth: "user:pass@",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotURL, err := BuildSignalURL(tt.config)
			require.NoError(t, err)
			assert.Contains(t, gotURL, tt.wantAuth)
		})
	}
}

func TestSendSignal_Validation(t *testing.T) {
	tests := []struct {
		name    string
		config  SignalConfig
		wantErr string
	}{
		{
			name: "missing host",
			config: SignalConfig{
				Port:       8080,
				Token:      "token",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantErr: "signal host is empty",
		},
		{
			name: "missing port",
			config: SignalConfig{
				Host:       "signal.example.com",
				Token:      "token",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantErr: "signal port is not set",
		},
		{
			name: "missing source",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "token",
				Recipients: []string{"+0987654321"},
			},
			wantErr: "signal source phone number is empty",
		},
		{
			name: "missing recipients",
			config: SignalConfig{
				Host:   "signal.example.com",
				Port:   8080,
				Token:  "token",
				Source: "+1234567890",
			},
			wantErr: "no signal recipients configured",
		},
		{
			name: "missing authentication - no user/password/token",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantErr: "signal requires either basic auth (user/password) or token authentication",
		},
		{
			name: "incomplete basic auth - user only",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				User:       "admin",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantErr: "signal requires either basic auth (user/password) or token authentication",
		},
		{
			name: "incomplete basic auth - password only",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Password:   "secret",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantErr: "signal requires either basic auth (user/password) or token authentication",
		},
		{
			name: "both auth methods provided",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				User:       "user",
				Password:   "pass",
				Token:      "token",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
			wantErr: "signal cannot use both basic auth and token authentication simultaneously",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Note: SendSignal will try to actually send, so we only test validation logic
			// by checking the error message before network call
			err := validateSignalConfig(tt.config)
			require.Error(t, err)
			assert.Contains(t, err.Error(), tt.wantErr)
		})
	}
}

func TestSendSignal_ValidConfigurations(t *testing.T) {
	tests := []struct {
		name   string
		config SignalConfig
	}{
		{
			name: "valid basic auth",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				User:       "admin",
				Password:   "secret",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
		},
		{
			name: "valid token auth",
			config: SignalConfig{
				Host:       "signal.example.com",
				Port:       8080,
				Token:      "token123",
				Source:     "+1234567890",
				Recipients: []string{"+0987654321"},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := validateSignalConfig(tt.config)
			assert.NoError(t, err)
		})
	}
}

// Helper function to extract validation logic for testing
func validateSignalConfig(config SignalConfig) error {
	if config.Host == "" {
		return errors.New("signal host is empty")
	}
	if config.Port == 0 {
		return errors.New("signal port is not set")
	}
	if config.Source == "" {
		return errors.New("signal source phone number is empty")
	}
	if len(config.Recipients) == 0 {
		return errors.New("no signal recipients configured")
	}

	// Validate authentication
	hasBasicAuth := config.User != "" && config.Password != ""
	hasTokenAuth := config.Token != ""
	if !hasBasicAuth && !hasTokenAuth {
		return errors.New("signal requires either basic auth (user/password) or token authentication")
	}
	if hasBasicAuth && hasTokenAuth {
		return errors.New("signal cannot use both basic auth and token authentication simultaneously")
	}

	return nil
}