Debian Codegraph @

testing/integration/matrix/config_test.go

raw
1 package matrix_test
2
3 import (
4 "net/url"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix"
10 )
11
12 func TestConfigSetURL(t *testing.T) {
13 t.Parallel()
14
15 tests := []struct {
16 name string
17 serviceURL string
18 }{
19 {
20 name: "basic URL with password",
21 serviceURL: "matrix://user:[email protected]",
22 },
23 {
24 name: "URL with special characters in password",
25 serviceURL: "matrix://user:p%[email protected]",
26 },
27 {
28 name: "URL with port",
29 serviceURL: "matrix://user:[email protected]:8080",
30 },
31 }
32
33 for _, tt := range tests {
34 t.Run(tt.name, func(t *testing.T) {
35 t.Parallel()
36
37 config := &matrix.Config{}
38
39 parsedURL, err := url.Parse(tt.serviceURL)
40 require.NoError(t, err, "URL should be parseable for %s", tt.name)
41
42 err = config.SetURL(parsedURL)
43
44 require.NoError(t, err, "Expected no error for %s", tt.name)
45 require.NotEmpty(t, config.Password, "Password should be set for %s", tt.name)
46 })
47 }
48 }
49
50 func TestConfigSetURLErrors(t *testing.T) {
51 t.Parallel()
52
53 tests := []struct {
54 name string
55 serviceURL string
56 errMsg string
57 }{
58 {
59 name: "missing password",
60 serviceURL: "matrix://matrix.example.com",
61 errMsg: "password or access token is required",
62 },
63 {
64 name: "empty host",
65 serviceURL: "matrix://user:token@",
66 errMsg: "host is required",
67 },
68 }
69
70 for _, tt := range tests {
71 t.Run(tt.name, func(t *testing.T) {
72 t.Parallel()
73
74 config := &matrix.Config{}
75
76 parsedURL, err := url.Parse(tt.serviceURL)
77 require.NoError(t, err, "URL should be parseable for %s", tt.name)
78
79 err = config.SetURL(parsedURL)
80
81 require.Error(t, err, "Expected error for %s", tt.name)
82 require.Contains(t, err.Error(), tt.errMsg,
83 "Error message should contain %q", tt.errMsg)
84 })
85 }
86 }
87
88 func TestConfigGetURL(t *testing.T) {
89 t.Parallel()
90
91 tests := []struct {
92 name string
93 serviceURL string
94 expectedHost string
95 expectedUser string
96 expectedPass string
97 }{
98 {
99 name: "basic config",
100 serviceURL: "matrix://user:[email protected]",
101 expectedHost: "matrix.example.com",
102 expectedUser: "user",
103 expectedPass: "token",
104 },
105 }
106
107 for _, tt := range tests {
108 t.Run(tt.name, func(t *testing.T) {
109 t.Parallel()
110
111 config := &matrix.Config{}
112
113 parsedURL, err := url.Parse(tt.serviceURL)
114 require.NoError(t, err, "URL should be parseable for %s", tt.name)
115
116 err = config.SetURL(parsedURL)
117 require.NoError(t, err, "Expected no error for %s", tt.name)
118
119 resultURL := config.GetURL()
120
121 require.Equal(t, tt.expectedHost, resultURL.Host,
122 "Host mismatch for %s", tt.name)
123
124 require.Equal(t, tt.expectedUser, resultURL.User.Username(),
125 "User mismatch for %s", tt.name)
126
127 actualPass, ok := resultURL.User.Password()
128 require.True(t, ok, "Password should be present for %s", tt.name)
129 require.Equal(t, tt.expectedPass, actualPass,
130 "Password mismatch for %s", tt.name)
131 })
132 }
133 }
134
135 func TestConfigQueryParams(t *testing.T) {
136 t.Parallel()
137
138 tests := []struct {
139 name string
140 serviceURL string
141 wantTitle string
142 wantTLS bool
143 }{
144 {
145 name: "with title",
146 serviceURL: "matrix://user:[email protected]?title=MyTitle",
147 wantTitle: "MyTitle",
148 wantTLS: false,
149 },
150 {
151 name: "with TLS disabled",
152 serviceURL: "matrix://user:[email protected]?disableTLS=true",
153 wantTitle: "",
154 wantTLS: true,
155 },
156 {
157 name: "with title and TLS",
158 serviceURL: "matrix://user:[email protected]?title=Alert&disableTLS=true",
159 wantTitle: "Alert",
160 wantTLS: true,
161 },
162 }
163
164 for _, tt := range tests {
165 t.Run(tt.name, func(t *testing.T) {
166 t.Parallel()
167
168 config := &matrix.Config{}
169
170 parsedURL, err := url.Parse(tt.serviceURL)
171 require.NoError(t, err, "URL should be parseable for %s", tt.name)
172
173 err = config.SetURL(parsedURL)
174 require.NoError(t, err, "Expected no error for %s", tt.name)
175
176 require.Equal(t, tt.wantTitle, config.Title,
177 "Title mismatch for %s", tt.name)
178 require.Equal(t, tt.wantTLS, config.DisableTLS,
179 "DisableTLS mismatch for %s", tt.name)
180 })
181 }
182 }
183
184 func TestConfigWithRoomsDirect(t *testing.T) {
185 t.Parallel()
186
187 // Test setting rooms directly on config (not via URL)
188 config := &matrix.Config{
189 Rooms: []string{"#room1:example.com", "!room2:example.com"},
190 }
191
192 require.Len(t, config.Rooms, 2)
193 require.Equal(t, "#room1:example.com", config.Rooms[0])
194 require.Equal(t, "!room2:example.com", config.Rooms[1])
195 }