| 1 |
package xmpp_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/xmpp" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 |
) |
| 14 |
|
| 15 |
func TestSendWithDialError(t *testing.T) { |
| 16 |
t.Parallel() |
| 17 |
|
| 18 |
service := createTestService(t, chatURL, &mockSession{}) |
| 19 |
service.SetDialContext(func(context.Context, string, string) (net.Conn, error) { |
| 20 |
return nil, errors.New("connection refused") |
| 21 |
}) |
| 22 |
|
| 23 |
err := service.Send("hello", nil) |
| 24 |
require.Error(t, err) |
| 25 |
require.Contains(t, err.Error(), "dialing") |
| 26 |
} |
| 27 |
|
| 28 |
func TestSendWithSessionError(t *testing.T) { |
| 29 |
t.Parallel() |
| 30 |
|
| 31 |
service := createTestService(t, chatURL, &mockSession{}) |
| 32 |
service.SetSessionFactory(func(context.Context, *xmpp.Config, net.Conn) (xmpp.Session, error) { |
| 33 |
return nil, errors.New("negotiate failed") |
| 34 |
}) |
| 35 |
|
| 36 |
err := service.Send("hello", nil) |
| 37 |
require.EqualError(t, err, "negotiate failed") |
| 38 |
} |
| 39 |
|
| 40 |
func TestSendWithChatError(t *testing.T) { |
| 41 |
t.Parallel() |
| 42 |
|
| 43 |
session := &mockSession{chatErr: errors.New("chat failed")} |
| 44 |
service := createTestService(t, chatURL, session) |
| 45 |
|
| 46 |
require.ErrorIs(t, service.Send("hello", nil), session.chatErr) |
| 47 |
} |
| 48 |
|
| 49 |
func TestSendWithMUCError(t *testing.T) { |
| 50 |
t.Parallel() |
| 51 |
|
| 52 |
session := &mockSession{roomErr: errors.New("join failed")} |
| 53 |
service := createTestService(t, mucURL, session) |
| 54 |
|
| 55 |
require.ErrorIs(t, service.Send("hello", nil), session.roomErr) |
| 56 |
} |
| 57 |
|
| 58 |
func TestSendWithInvalidParams(t *testing.T) { |
| 59 |
t.Parallel() |
| 60 |
|
| 61 |
service := createTestService(t, chatURL, &mockSession{}) |
| 62 |
err := service.Send("hello", &types.Params{"notakey": "1"}) |
| 63 |
require.Error(t, err) |
| 64 |
require.Contains(t, err.Error(), "updating config from params") |
| 65 |
} |
| 66 |
|
| 67 |
func TestSendWithParamsClearingTargets(t *testing.T) { |
| 68 |
t.Parallel() |
| 69 |
|
| 70 |
service := createTestService(t, chatURL, &mockSession{}) |
| 71 |
err := service.Send("hello", &types.Params{"to": ""}) |
| 72 |
require.ErrorIs(t, err, xmpp.ErrMissingTargets) |
| 73 |
} |