2024-05-11 08:25:15 +00:00
|
|
|
package matrix
|
2024-05-10 06:42:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2024-05-11 08:25:15 +00:00
|
|
|
"github.com/thegeeklab/wp-matrix/matrix/mocks"
|
2024-05-10 06:42:59 +00:00
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
)
|
|
|
|
|
2024-05-11 08:25:15 +00:00
|
|
|
func TestMessageSend(t *testing.T) {
|
2024-05-10 06:42:59 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2024-05-11 08:25:15 +00:00
|
|
|
messageOpt MessageOptions
|
2024-05-10 06:42:59 +00:00
|
|
|
want event.MessageEventContent
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "plain text message",
|
2024-05-11 08:25:15 +00:00
|
|
|
messageOpt: MessageOptions{
|
2024-05-10 06:42:59 +00:00
|
|
|
RoomID: "test-room",
|
|
|
|
Message: "hello world",
|
|
|
|
},
|
|
|
|
want: event.MessageEventContent{
|
|
|
|
MsgType: "m.text",
|
|
|
|
Body: "hello world",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "markdown message",
|
2024-05-11 08:25:15 +00:00
|
|
|
messageOpt: MessageOptions{
|
2024-05-10 06:42:59 +00:00
|
|
|
RoomID: "test-room",
|
|
|
|
Message: "**hello world**",
|
|
|
|
},
|
|
|
|
want: event.MessageEventContent{
|
|
|
|
MsgType: "m.text",
|
|
|
|
Body: "**hello world**",
|
|
|
|
Format: "org.matrix.custom.html",
|
|
|
|
FormattedBody: "<strong>hello world</strong>",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "html message",
|
2024-05-11 08:25:15 +00:00
|
|
|
messageOpt: MessageOptions{
|
2024-05-10 06:42:59 +00:00
|
|
|
RoomID: "test-room",
|
|
|
|
Message: "hello<br>world",
|
|
|
|
TemplateUnsafe: true,
|
|
|
|
},
|
|
|
|
want: event.MessageEventContent{
|
|
|
|
MsgType: "m.text",
|
|
|
|
Body: "hello\nworld",
|
|
|
|
Format: "org.matrix.custom.html",
|
|
|
|
FormattedBody: "hello<br>world",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "safe html message",
|
2024-05-11 08:25:15 +00:00
|
|
|
messageOpt: MessageOptions{
|
2024-05-10 06:42:59 +00:00
|
|
|
RoomID: "test-room",
|
|
|
|
Message: "hello world<script>alert('XSS')</script>",
|
|
|
|
TemplateUnsafe: false,
|
|
|
|
},
|
|
|
|
want: event.MessageEventContent{
|
|
|
|
MsgType: "m.text",
|
|
|
|
Body: "hello world<script>alert('XSS')</script>",
|
|
|
|
Format: "org.matrix.custom.html",
|
|
|
|
FormattedBody: "hello world<script>alert('XSS')</script>",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unsafe html message",
|
2024-05-11 08:25:15 +00:00
|
|
|
messageOpt: MessageOptions{
|
2024-05-10 06:42:59 +00:00
|
|
|
RoomID: "test-room",
|
|
|
|
Message: "hello world<script>alert('XSS')</script>",
|
|
|
|
TemplateUnsafe: true,
|
|
|
|
},
|
|
|
|
want: event.MessageEventContent{
|
|
|
|
MsgType: "m.text",
|
|
|
|
Body: "hello world",
|
|
|
|
Format: "org.matrix.custom.html",
|
|
|
|
FormattedBody: "hello world",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2024-05-11 08:25:15 +00:00
|
|
|
mockClient := mocks.NewMockAPIClient(t)
|
|
|
|
m := &Message{
|
2024-05-10 06:42:59 +00:00
|
|
|
Opt: tt.messageOpt,
|
|
|
|
client: mockClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
mockClient.
|
2024-09-20 06:46:36 +00:00
|
|
|
On("SendMessageEvent", mock.Anything, tt.messageOpt.RoomID, event.EventMessage,
|
|
|
|
mock.MatchedBy(func(content event.MessageEventContent) bool {
|
|
|
|
tt.want.Mentions = &event.Mentions{}
|
|
|
|
|
|
|
|
return assert.EqualValues(t, tt.want, content)
|
|
|
|
})).
|
2024-05-10 06:42:59 +00:00
|
|
|
Return(&mautrix.RespSendEvent{}, nil)
|
|
|
|
|
|
|
|
err := m.Send(ctx)
|
|
|
|
assert.Equal(t, tt.wantErr, err != nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|