| 1 |
package xmpp_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/require" |
| 7 |
|
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/xmpp" |
| 9 |
) |
| 10 |
|
| 11 |
func TestConfigValidURLParsing(t *testing.T) { |
| 12 |
t.Parallel() |
| 13 |
|
| 14 |
tests := []struct { |
| 15 |
name string |
| 16 |
url string |
| 17 |
wantHost string |
| 18 |
wantPort uint16 |
| 19 |
wantUser string |
| 20 |
wantTo []string |
| 21 |
wantRooms []string |
| 22 |
wantNick string |
| 23 |
wantTLS bool |
| 24 |
wantSkipVerify bool |
| 25 |
}{ |
| 26 |
{ |
| 27 |
name: "xmpp scheme with chat target", |
| 28 |
url: chatURL, |
| 29 |
wantHost: "xmpp.example.com", |
| 30 |
wantPort: xmpp.DefaultPort, |
| 31 |
wantUser: "alice", |
| 32 |
wantTo: []string{"[email protected]"}, |
| 33 |
wantTLS: true, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "xmpps scheme defaults to 5223", |
| 37 |
url: "xmpps://alice:[email protected]/[email protected]", |
| 38 |
wantHost: "xmpp.example.com", |
| 39 |
wantPort: xmpp.DefaultTLSPort, |
| 40 |
wantUser: "alice", |
| 41 |
wantTo: []string{"[email protected]"}, |
| 42 |
wantTLS: true, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "full auth JID distinct from dial host", |
| 46 |
url: "xmpp://alice%40jabber.example.com:[email protected]/[email protected]", |
| 47 |
wantHost: "xmpp.example.com", |
| 48 |
wantPort: xmpp.DefaultPort, |
| 49 |
wantUser: "[email protected]", |
| 50 |
wantRooms: []string{"[email protected]"}, |
| 51 |
wantTLS: true, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "disabletls on xmpp", |
| 55 |
url: "xmpp://alice:[email protected]/[email protected]&disabletls=yes", |
| 56 |
wantHost: "xmpp.example.com", |
| 57 |
wantPort: xmpp.DefaultPort, |
| 58 |
wantUser: "alice", |
| 59 |
wantTo: []string{"[email protected]"}, |
| 60 |
wantTLS: false, |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "skiptlsverify and custom port", |
| 64 |
url: "xmpps://alice:[email protected]:5224/[email protected]&skiptlsverify=yes", |
| 65 |
wantHost: "xmpp.example.com", |
| 66 |
wantPort: 5224, |
| 67 |
wantUser: "alice", |
| 68 |
wantTo: []string{"[email protected]"}, |
| 69 |
wantTLS: true, |
| 70 |
wantSkipVerify: true, |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "nick and roompassword", |
| 74 |
url: bothURL, |
| 75 |
wantHost: "xmpp.example.com", |
| 76 |
wantPort: xmpp.DefaultPort, |
| 77 |
wantUser: "alice", |
| 78 |
wantTo: []string{"[email protected]"}, |
| 79 |
wantRooms: []string{"[email protected]"}, |
| 80 |
wantNick: "bot", |
| 81 |
wantTLS: true, |
| 82 |
}, |
| 83 |
} |
| 84 |
|
| 85 |
for _, tt := range tests { |
| 86 |
t.Run(tt.name, func(t *testing.T) { |
| 87 |
t.Parallel() |
| 88 |
|
| 89 |
service := createTestService(t, tt.url, &mockSession{}) |
| 90 |
|
| 91 |
require.Equal(t, tt.wantHost, service.Config.Host) |
| 92 |
require.Equal(t, tt.wantPort, service.Config.Port) |
| 93 |
require.Equal(t, tt.wantUser, service.Config.User) |
| 94 |
require.Equal(t, tt.wantTo, service.Config.To) |
| 95 |
require.Equal(t, tt.wantRooms, service.Config.Rooms) |
| 96 |
require.Equal(t, tt.wantNick, service.Config.Nick) |
| 97 |
require.Equal(t, tt.wantTLS, !service.Config.DisableTLS) |
| 98 |
require.Equal(t, tt.wantSkipVerify, service.Config.SkipTLSVerify) |
| 99 |
require.Equal(t, xmpp.Scheme, service.GetID()) |
| 100 |
}) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestConfigURLRoundTrip(t *testing.T) { |
| 105 |
t.Parallel() |
| 106 |
|
| 107 |
service := createTestService(t, chatURL, &mockSession{}) |
| 108 |
parsed := &xmpp.Config{} |
| 109 |
require.NoError(t, parsed.SetURL(service.Config.GetURL())) |
| 110 |
require.Equal(t, service.Config.Host, parsed.Host) |
| 111 |
require.Equal(t, service.Config.User, parsed.User) |
| 112 |
require.Equal(t, service.Config.To, parsed.To) |
| 113 |
require.Equal(t, xmpp.Scheme, service.Config.GetURL().Scheme) |
| 114 |
} |
| 115 |
|
| 116 |
func TestConfigInvalidURLs(t *testing.T) { |
| 117 |
t.Parallel() |
| 118 |
|
| 119 |
tests := []struct { |
| 120 |
name string |
| 121 |
url string |
| 122 |
wantErr error |
| 123 |
}{ |
| 124 |
{ |
| 125 |
name: "disabletls on xmpps", |
| 126 |
url: "xmpps://alice:[email protected]/[email protected]&disabletls=yes", |
| 127 |
wantErr: xmpp.ErrDisableTLSOnXMPPS, |
| 128 |
}, |
| 129 |
{ |
| 130 |
name: "missing targets", |
| 131 |
url: "xmpp://alice:[email protected]/", |
| 132 |
wantErr: xmpp.ErrMissingTargets, |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "missing password", |
| 136 |
url: "xmpp://[email protected]/[email protected]", |
| 137 |
wantErr: xmpp.ErrMissingPassword, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "invalid recipient", |
| 141 |
url: "xmpp://alice:[email protected]/?to=not-a-jid", |
| 142 |
wantErr: xmpp.ErrInvalidRecipientJID, |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "invalid room", |
| 146 |
url: "xmpp://alice:[email protected]/?rooms=not-a-jid", |
| 147 |
wantErr: xmpp.ErrInvalidRoomJID, |
| 148 |
}, |
| 149 |
} |
| 150 |
|
| 151 |
for _, tt := range tests { |
| 152 |
t.Run(tt.name, func(t *testing.T) { |
| 153 |
t.Parallel() |
| 154 |
|
| 155 |
service := &xmpp.Service{} |
| 156 |
err := service.Initialize(mustParse(t, tt.url), &mockLogger{}) |
| 157 |
require.ErrorIs(t, err, tt.wantErr) |
| 158 |
}) |
| 159 |
} |
| 160 |
} |