2018-06-07 14:48:08 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-06 10:48:04 +00:00
|
|
|
a "github.com/mariusor/activitypub.go/activitypub"
|
|
|
|
j "github.com/mariusor/activitypub.go/jsonld"
|
2018-06-07 14:48:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const dir = "./mocks"
|
|
|
|
|
|
|
|
var stopOnFailure = false
|
|
|
|
|
|
|
|
type testPair struct {
|
|
|
|
path string
|
|
|
|
expected bool
|
|
|
|
blank a.ObjectOrLink
|
|
|
|
result a.ObjectOrLink
|
|
|
|
}
|
|
|
|
|
|
|
|
type tests map[string]testPair
|
|
|
|
|
2018-06-09 11:08:30 +00:00
|
|
|
var allTests = tests{
|
|
|
|
"empty": testPair{
|
2018-06-07 14:48:08 +00:00
|
|
|
path: "./mocks/empty.json",
|
|
|
|
expected: true,
|
2018-06-09 12:05:46 +00:00
|
|
|
blank: &a.Object{},
|
|
|
|
result: &a.Object{},
|
2018-06-09 11:08:30 +00:00
|
|
|
},
|
|
|
|
"link_simple": testPair{
|
|
|
|
path: "./mocks/link_simple.json",
|
|
|
|
expected: true,
|
|
|
|
blank: &a.Link{},
|
|
|
|
result: &a.Link{
|
|
|
|
Type: a.LinkType,
|
|
|
|
Href: a.URI("http://example.org/abc"),
|
|
|
|
HrefLang: a.LangRef("en"),
|
|
|
|
MediaType: a.MimeType("text/html"),
|
|
|
|
Name: a.NaturalLanguageValue{
|
|
|
|
a.LangRef("-"): "An example link",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-24 21:11:08 +00:00
|
|
|
"object_with_url": testPair{
|
|
|
|
path: "./mocks/object_with_url.json",
|
|
|
|
expected: true,
|
|
|
|
blank: &a.Object{},
|
|
|
|
result: &a.Object{
|
|
|
|
URL: a.URI("http://littr.git/api/accounts/system"),
|
|
|
|
},
|
|
|
|
},
|
2018-06-09 11:08:30 +00:00
|
|
|
"object_simple": testPair{
|
|
|
|
path: "./mocks/object_simple.json",
|
|
|
|
expected: true,
|
2018-06-09 12:05:46 +00:00
|
|
|
blank: &a.Object{},
|
|
|
|
result: &a.Object{
|
2018-06-09 11:08:30 +00:00
|
|
|
Type: a.ObjectType,
|
|
|
|
ID: a.ObjectID("http://www.test.example/object/1"),
|
|
|
|
Name: a.NaturalLanguageValue{
|
|
|
|
a.LangRef("-"): "A Simple, non-specific object",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-06 10:48:04 +00:00
|
|
|
//"activity_simple": testPair{
|
|
|
|
// path: "./mocks/activity_simple.json",
|
2018-07-24 21:11:08 +00:00
|
|
|
// expected: false,
|
2018-07-06 10:48:04 +00:00
|
|
|
// blank: &a.Activity{},
|
|
|
|
// result: &a.Activity{
|
|
|
|
// Type: a.ActivityType,
|
|
|
|
// Summary: a.NaturalLanguageValue{a.LangRef("-"): "Sally did something to a note"},
|
2018-07-24 21:11:08 +00:00
|
|
|
// Actor: a.Actor(a.Person{
|
|
|
|
// Type: a.PersonType,
|
|
|
|
// Name: a.NaturalLanguageValue{
|
|
|
|
// a.LangRef("-"): "Sally",
|
|
|
|
// },
|
|
|
|
// }),
|
2018-08-01 08:45:21 +00:00
|
|
|
// payloadWithContext: a.payloadWithContext{
|
2018-07-24 21:11:08 +00:00
|
|
|
// Type: a.NoteType,
|
|
|
|
// Name: a.NaturalLanguageValue{
|
|
|
|
// a.LangRef("-"): "A Note",
|
|
|
|
// },
|
|
|
|
// },
|
2018-07-06 10:48:04 +00:00
|
|
|
// },
|
|
|
|
//},
|
2018-07-24 21:11:08 +00:00
|
|
|
"person_with_outbox": testPair{
|
|
|
|
path: "./mocks/person_with_outbox.json",
|
|
|
|
expected: true,
|
|
|
|
blank: &a.Person{},
|
|
|
|
result: &a.Person{
|
|
|
|
ID: a.ObjectID("http://example.com/accounts/ana"),
|
|
|
|
Type: a.PersonType,
|
|
|
|
Name: a.NaturalLanguageValue{
|
|
|
|
a.LangRef("-"): "ana",
|
|
|
|
},
|
|
|
|
PreferredUsername: a.NaturalLanguageValue{
|
|
|
|
a.LangRef("-"): "Ana",
|
|
|
|
},
|
|
|
|
URL: a.URI("http://example.com/accounts/ana"),
|
2018-07-25 10:34:36 +00:00
|
|
|
Outbox: &a.OutboxStream{
|
2018-07-24 21:11:08 +00:00
|
|
|
ID: a.ObjectID("http://example.com/accounts/ana/outbox"),
|
|
|
|
Type: a.OrderedCollectionType,
|
|
|
|
URL: a.URI("http://example.com/outbox"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-06-07 14:48:08 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:08 +00:00
|
|
|
func getFileContents(path string) ([]byte, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-07 14:48:08 +00:00
|
|
|
|
|
|
|
data := make([]byte, 512)
|
|
|
|
io.ReadFull(f, data)
|
|
|
|
data = bytes.Trim(data, "\x00")
|
|
|
|
|
2018-07-24 21:11:08 +00:00
|
|
|
return data, nil
|
2018-06-07 14:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ActivityPubUnmarshall(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
var f = t.Errorf
|
|
|
|
if stopOnFailure {
|
|
|
|
f = t.Fatalf
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(allTests) == 0 {
|
|
|
|
t.Skip("No tests found")
|
|
|
|
}
|
|
|
|
|
2018-06-09 11:08:30 +00:00
|
|
|
for k, pair := range allTests {
|
2018-07-24 21:11:08 +00:00
|
|
|
var data []byte
|
|
|
|
data, err = getFileContents(pair.path)
|
|
|
|
if err != nil {
|
|
|
|
f("Error: %s for %s", err, pair.path)
|
|
|
|
continue
|
|
|
|
}
|
2018-06-07 14:48:08 +00:00
|
|
|
object := pair.blank
|
|
|
|
|
|
|
|
err = j.Unmarshal(data, object)
|
|
|
|
if err != nil {
|
2018-07-24 21:11:08 +00:00
|
|
|
f("Error: %s for %s", err, data)
|
2018-06-07 14:48:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
expLbl := ""
|
|
|
|
if !pair.expected {
|
|
|
|
expLbl = "not be "
|
|
|
|
}
|
|
|
|
if pair.expected != reflect.DeepEqual(object, pair.result) {
|
|
|
|
f("\n%#v\n should %sequal to\n%#v", object, expLbl, pair.result)
|
2018-07-24 21:11:08 +00:00
|
|
|
oj, e1 := j.Marshal(object)
|
|
|
|
if e1 != nil {
|
|
|
|
f(e1.Error())
|
|
|
|
}
|
|
|
|
tj, e2 := j.Marshal(pair.result)
|
|
|
|
if e2 != nil {
|
|
|
|
f(e2.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
f("\n%s\n should %sequal to\n%s", oj, expLbl, tj)
|
2018-06-07 14:48:08 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-07-24 21:11:08 +00:00
|
|
|
//fmt.Printf("%#v", object)
|
2018-06-07 14:48:08 +00:00
|
|
|
if err == nil {
|
2018-06-09 11:08:30 +00:00
|
|
|
fmt.Printf(" --- %s: %s\n %s\n", "PASS", k, pair.path)
|
2018-06-07 14:48:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|