This commit is contained in:
Gabriel Fioravante 2021-03-24 16:38:07 +00:00 committed by GitHub
commit 6154cb20aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 110 deletions

View File

@ -1,110 +0,0 @@
package gomatrix
import (
"encoding/json"
"strings"
"testing"
)
// example events from docs
var testEvents = map[string]string{
"withFields": `{
"content": {
"body": "eventbody123",
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"formatted_body": "<b>This is an example text message</b>"
},
"type": "m.room.message",
"event_id": "$143273582443PhrSn:example.org",
"room_id": "!726s6s6q:example.com",
"sender": "@example:example.org",
"origin_server_ts": 1432735824653,
"unsigned": {
"age": 1234
}
}`,
"withoutFields": `{
"content": {
"avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
"displayname": "Alice Margatroid",
"membership": "join"
},
"event_id": "$143273582443PhrSn:example.org",
"origin_server_ts": 1432735824653,
"room_id": "!jEsUZKDJdhlrceRyVU:example.org",
"sender": "@example:example.org",
"state_key": "@alice:example.org",
"type": "m.room.member",
"unsigned": {
"age": 1234
}
}`,
}
func TestEventWithBody(t *testing.T) {
var e Event
err := json.NewDecoder(strings.NewReader(testEvents["withFields"])).Decode(&e)
if err != nil {
t.Fatalf("TestFetchEventBody: Something went wrong while parsing: %s", testEvents["withFields"])
}
body, ok := e.Body()
if !ok || body != "eventbody123" {
t.Fatal("TestEventWithBody: Failed to fetch value of 'body' key in event content")
}
}
func TestEventWithoutBody(t *testing.T) {
var e Event
err := json.NewDecoder(strings.NewReader(testEvents["withoutFields"])).Decode(&e)
if err != nil {
t.Fatalf("TestEventWithoutBody: Something went wrong while parsing: %s", testEvents["withFields"])
}
body, ok := e.Body()
if ok || body != "" {
t.Fatal("TestEventWithoutBody: Failed on 'Event.Body' call for event without a 'body' key")
}
}
func TestEventWithMessageType(t *testing.T) {
var e Event
err := json.NewDecoder(strings.NewReader(testEvents["withFields"])).Decode(&e)
if err != nil {
t.Fatalf("TestEventWithMessageType: Something went wrong while parsing: %s", testEvents["withFields"])
}
msgtype, ok := e.MessageType()
if !ok || msgtype != "m.text" {
t.Fatal("TestEventWithMessageType: Failed to fetch value of 'msgtype' key in event content")
}
}
func TestEventWithoutMessageType(t *testing.T) {
var e Event
err := json.NewDecoder(strings.NewReader(testEvents["withoutFields"])).Decode(&e)
if err != nil {
t.Fatalf("TestEventWithMessageType: Something went wrong while parsing: %s", testEvents["withFields"])
}
msgtype, ok := e.MessageType()
if ok || msgtype != "" {
t.Fatal("TestEventWithoutBody: Failed on 'Event.Body' call for event without a 'msgtype' key")
}
}
var testHTML = `<div>a<h1>bc</h1>d<p>e<i>fg</i>hi</p>j<p>k<br/>l<b>m</b>no</p>p<small>q</small>rs</div>`
func TestGetHTMLMessage(t *testing.T) {
msg := GetHTMLMessage("m.text", testHTML)
if expected := "abcdefghijklmnopqrs"; msg.Body != expected {
t.Fatalf("TestGetHTMLMessage: got '%s', expected '%s'", msg.Body, expected)
}
if msg.FormattedBody != testHTML {
t.Fatalf("TestGetHTMLMessage: got '%s', expected '%s'", msg.FormattedBody, testHTML)
}
if msg.MsgType != "m.text" {
t.Fatalf("TestGetHTMLMessage: got '%s', expected 'm.text'", msg.FormattedBody)
}
if expected := "org.matrix.custom.html"; msg.Format != expected {
t.Fatalf("TestGetHTMLMessage: got '%s', expected '%s'", msg.Format, expected)
}
}

51
identifier_test.go Normal file
View File

@ -0,0 +1,51 @@
package gomatrix
import (
"testing"
)
func TestUserIDType(t *testing.T) {
var userID UserIdentifier
if idType := userID.Type(); idType != "m.id.user" {
t.Fatalf("TestUserIDType: expected ID type '%s', got '%s'", "m.id.user", idType)
}
}
func TestNewUserID(t *testing.T) {
userID := NewUserIdentifier("user0001")
if userID.User != "user0001" || userID.IDType != "m.id.user" {
t.Fatalf(
"TestNewUserID: invalid UserIdentifier returned, IDType: '%s' User: '%s'",
userID.IDType,
userID.User,
)
}
}
func TestThirdPartyIDType(t *testing.T) {
var tpID ThirdpartyIdentifier
if idType := tpID.Type(); idType != "m.id.thirdparty" {
t.Fatalf("TestUserIDType: expected ID type '%s', got '%s'", "m.id.user", idType)
}
}
func TestNewThirdPartyID(t *testing.T) {
tpID := NewThirdpartyIdentifier("m01", "addr01")
if tpID.Medium != "m01" || tpID.Address != "addr01" || tpID.IDType != "m.id.thirdparty" {
t.Fatalf("NewThirdpartyIdentifier: invalid ThirdPartyIdentifier returned, Medium: '%s', Address: '%s', IDType: '%s'", tpID.Medium, tpID.Address, tpID.IDType)
}
}
func TestPhoneIDType(t *testing.T) {
var phoneID PhoneIdentifier
if idType := phoneID.Type(); idType != "m.id.phone" {
t.Fatalf("TestPhoneIDType: expected ID type '%s', got '%s'", "m.id.phone", idType)
}
}
func TestNewPhoneID(t *testing.T) {
phoneID := NewPhoneIdentifier("country01", "(11)111-111")
if phoneID.Country != "country01" || phoneID.Phone != "(11)111-111" || phoneID.IDType != "m.id.phone" {
t.Fatalf("TestNewPhoneID: invalid PhoneIdentifier returned, country: '%s', phone: '%s', IDType: '%s'", phoneID.Country, phoneID.Phone, phoneID.IDType)
}
}