mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-03 22:08:04 +00:00 
			
		
		
		
	Merge pull request #87 from northernSage/add-filter-tests
Add tests for filters.go
This commit is contained in:
		
						commit
						ceba4d9f75
					
				
					 2 changed files with 89 additions and 110 deletions
				
			
		
							
								
								
									
										110
									
								
								events_test.go
									
										
									
									
									
								
							
							
						
						
									
										110
									
								
								events_test.go
									
										
									
									
									
								
							| 
						 | 
					@ -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)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										89
									
								
								filter_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								filter_test.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -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 {
 | 
				
			||||||
 | 
							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)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue