From 1eaa10fe73b789ef59b5ec98e6b3db04d9ac1438 Mon Sep 17 00:00:00 2001 From: northernSage Date: Fri, 26 Feb 2021 11:04:23 -0300 Subject: [PATCH 1/3] add filter.go tests --- filter_test.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 filter_test.go diff --git a/filter_test.go b/filter_test.go new file mode 100644 index 0000000..ffe6c8e --- /dev/null +++ b/filter_test.go @@ -0,0 +1,89 @@ +package gomatrix + +import ( + "encoding/json" + "reflect" + "strings" + "testing" +) + +// sample from docs +var testFilter = ` +{ + "room": { + "state": { + "types": [ + "m.room.*" + ], + "not_rooms": [ + "!726s6s6q:example.com" + ] + }, + "timeline": { + "limit": 10, + "types": [ + "m.room.message" + ], + "not_rooms": [ + "!726s6s6q:example.com" + ], + "not_senders": [ + "@spam:example.com" + ] + }, + "ephemeral": { + "types": [ + "m.receipt", + "m.typing" + ], + "not_rooms": [ + "!726s6s6q:example.com" + ], + "not_senders": [ + "@spam:example.com" + ] + } + }, + "presence": { + "types": [ + "m.presence" + ], + "not_senders": [ + "@alice:example.com" + ] + }, + "event_format": "client", + "event_fields": [ + "type", + "content", + "sender" + ] +}` + +func TestFilterValidate(t *testing.T) { + var f Filter + err := json.NewDecoder(strings.NewReader(testFilter)).Decode(&f) + if err != nil { + t.Fatalf("TestFilterValidate: Failed to parse %s", testFilter) + } + // test validadtion success + if err = f.Validate(); err != nil { + t.Fatalf("TestFilterValidate: Filter validation has failed, event_format: '%s'", f.EventFormat) + } + // test validation fail + f.EventFormat = "unkown" + err = f.Validate() + if err == nil || err.Error() != "Bad event_format value. Must be one of [\"client\", \"federation\"]" { + t.Fatalf("TestFilterValidate: Filter validation false positive, event_format: '%s'", f.EventFormat) + } +} + +func TestDefaultFilter(t *testing.T) { + defaultFilter := DefaultFilter() + if reflect.TypeOf(defaultFilter) != reflect.TypeOf(Filter{}) { + t.Fatal("TestDefaultFilter: Invalid type for default filter") + } + if defaultFilter.EventFormat != "client" { + t.Fatalf("TestDefaultFilter: expected EventFormat %s, got %s", "client", defaultFilter.EventFormat) + } +} From 967f16432769479594fe09e1d2c4d2b5193632ca Mon Sep 17 00:00:00 2001 From: northernSage Date: Sat, 27 Feb 2021 00:28:53 -0300 Subject: [PATCH 2/3] add identifier tests --- identifier_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 identifier_test.go diff --git a/identifier_test.go b/identifier_test.go new file mode 100644 index 0000000..a6645b0 --- /dev/null +++ b/identifier_test.go @@ -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) + } +} From 759b7e81acc315f1f023b828902b36059b48b150 Mon Sep 17 00:00:00 2001 From: northernSage Date: Sat, 27 Feb 2021 00:39:39 -0300 Subject: [PATCH 3/3] fix PR duplication --- events_test.go | 110 ------------------------------------------------- filter_test.go | 89 --------------------------------------- 2 files changed, 199 deletions(-) delete mode 100644 events_test.go delete mode 100644 filter_test.go diff --git a/events_test.go b/events_test.go deleted file mode 100644 index 3ac5cca..0000000 --- a/events_test.go +++ /dev/null @@ -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": "This is an example text message" - }, - "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 = `
a

bc

d

efghi

j

k
lmno

pqrs
` - -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) - } -} diff --git a/filter_test.go b/filter_test.go deleted file mode 100644 index ffe6c8e..0000000 --- a/filter_test.go +++ /dev/null @@ -1,89 +0,0 @@ -package gomatrix - -import ( - "encoding/json" - "reflect" - "strings" - "testing" -) - -// sample from docs -var testFilter = ` -{ - "room": { - "state": { - "types": [ - "m.room.*" - ], - "not_rooms": [ - "!726s6s6q:example.com" - ] - }, - "timeline": { - "limit": 10, - "types": [ - "m.room.message" - ], - "not_rooms": [ - "!726s6s6q:example.com" - ], - "not_senders": [ - "@spam:example.com" - ] - }, - "ephemeral": { - "types": [ - "m.receipt", - "m.typing" - ], - "not_rooms": [ - "!726s6s6q:example.com" - ], - "not_senders": [ - "@spam:example.com" - ] - } - }, - "presence": { - "types": [ - "m.presence" - ], - "not_senders": [ - "@alice:example.com" - ] - }, - "event_format": "client", - "event_fields": [ - "type", - "content", - "sender" - ] -}` - -func TestFilterValidate(t *testing.T) { - var f Filter - err := json.NewDecoder(strings.NewReader(testFilter)).Decode(&f) - if err != nil { - t.Fatalf("TestFilterValidate: Failed to parse %s", testFilter) - } - // test validadtion success - if err = f.Validate(); err != nil { - t.Fatalf("TestFilterValidate: Filter validation has failed, event_format: '%s'", f.EventFormat) - } - // test validation fail - f.EventFormat = "unkown" - err = f.Validate() - if err == nil || err.Error() != "Bad event_format value. Must be one of [\"client\", \"federation\"]" { - t.Fatalf("TestFilterValidate: Filter validation false positive, event_format: '%s'", f.EventFormat) - } -} - -func TestDefaultFilter(t *testing.T) { - defaultFilter := DefaultFilter() - if reflect.TypeOf(defaultFilter) != reflect.TypeOf(Filter{}) { - t.Fatal("TestDefaultFilter: Invalid type for default filter") - } - if defaultFilter.EventFormat != "client" { - t.Fatalf("TestDefaultFilter: expected EventFormat %s, got %s", "client", defaultFilter.EventFormat) - } -}