-
Notifications
You must be signed in to change notification settings - Fork 14
/
activity_test.go
128 lines (111 loc) · 4.34 KB
/
activity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package getstream_test
import (
"errors"
"testing"
getstream "github.com/GetStream/stream-go"
"github.com/pborman/uuid"
)
func TestActivityMarshallJson(t *testing.T) {
activity := &getstream.Activity{
Verb: "post",
ForeignID: uuid.New(),
Object: "flat:eric",
Actor: "flat:john",
}
_, err := activity.MarshalJSON()
if err != nil {
t.Fatal(err)
}
}
func TestActivityBadForeignKeyMarshall(t *testing.T) {
activity := &getstream.Activity{
Verb: "post",
ForeignID: "not a real foreign id",
Object: "flat:eric",
Actor: "flat:john",
}
_, err := activity.MarshalJSON()
if err != nil && err.Error() != "invalid ForeignID" {
t.Fatal(errors.New("Expected activity.MarshalJSON() to fail on non-UUID ForeignID, it failed because of this:" + err.Error()))
}
}
func TestActivityUnmarshall(t *testing.T) {
activity := &getstream.Activity{}
payload := []byte("{\"actor\":\"flat:john\",\"foreign_id\":\"82d2bb81-069d-427b-9238-8d822012e6d7\",\"object\":\"flat:eric\",\"origin\":\"\",\"time\":\"2016-09-22T21:44:58.821577\",\"verb\":\"post\"}")
err := activity.UnmarshalJSON(payload)
if err != nil {
t.Fatal(err)
}
}
func TestActivityUnmarshallEmptyPayload(t *testing.T) {
activity := &getstream.Activity{}
err := activity.UnmarshalJSON([]byte{})
if err == nil {
t.Fatal(err)
}
if err.Error() != "unexpected end of JSON input" {
t.Fatal(errors.New("Expected activity.UnmarshalJSON method to fail on a bad payload, it failed because of this:" + err.Error()))
}
}
func TestActivityUnmarshallBadPayloadTime(t *testing.T) {
var err error
activity := &getstream.Activity{}
// empty json value for "time" should still set "time" to nil
payload := []byte("{\"actor\":\"flat:john\",\"foreign_id\":\"82d2bb81-069d-427b-9238-8d822012e6d7\",\"object\":\"flat:eric\",\"origin\":\"\",\"time\":{},\"verb\":\"post\"}")
err = activity.UnmarshalJSON(payload)
if err != nil {
t.Fatal(err)
}
if activity.TimeStamp != nil {
t.Fatal("Expected TimeStamp to be nil if it was empty JSON {}")
}
// non-Time value should still parse fine and set "time" to nil
payload = []byte("{\"actor\":\"flat:john\",\"foreign_id\":\"82d2bb81-069d-427b-9238-8d822012e6d7\",\"object\":\"flat:eric\",\"origin\":\"\",\"time\":\"abc\",\"verb\":\"post\"}")
err = activity.UnmarshalJSON(payload)
if err != nil {
t.Fatal(err)
}
if activity.TimeStamp != nil {
t.Fatal("Expected TimeStamp to be nil if it was an unparseable time")
}
}
func TestActivityUnmarshallBadPayloadTo(t *testing.T) {
var err error
activity := &getstream.Activity{}
// empty json value for "to" should set "to" to nil
payload := []byte("{\"to\":null,\"actor\":\"flat:john\",\"foreign_id\":\"82d2bb81-069d-427b-9238-8d822012e6d7\",\"object\":\"flat:eric\",\"origin\":\"\",\"time\":\"2016-09-22T21:44:58.821577\",\"verb\":\"post\"}")
err = activity.UnmarshalJSON(payload)
if err != nil {
t.Fatal(err)
}
if activity.To != nil {
t.Fatal("To JSON was null, expected To to be nil afterward, got:", activity.To)
}
// empty json value for "to" should set "to" to nil
payload = []byte("{\"to\":{},\"actor\":\"flat:john\",\"foreign_id\":\"82d2bb81-069d-427b-9238-8d822012e6d7\",\"object\":\"flat:eric\",\"origin\":\"\",\"time\":\"2016-09-22T21:44:58.821577\",\"verb\":\"post\"}")
err = activity.UnmarshalJSON(payload)
if err != nil {
t.Fatal(err)
}
if activity.To != nil {
t.Fatal("To payload was bad JSON, expected To to be nil afterward, got:", activity.To)
}
// two-dimensional To should set To to ... something?
payload = []byte("{\"to\":[\"bob\"],\"actor\":\"flat:john\",\"foreign_id\":\"82d2bb81-069d-427b-9238-8d822012e6d7\",\"object\":\"flat:eric\",\"origin\":\"\",\"time\":\"2016-09-22T21:44:58.821577\",\"verb\":\"post\"}")
err = activity.UnmarshalJSON(payload)
if err != nil {
t.Fatal(err)
}
if len(activity.To) != 0 {
t.Fatal("To payload was bad JSON, expected To to be nil afterward, got:", activity.To)
}
// malformed To userID should null out To
payload = []byte("{\"to\":[{\"bob\"}],\"actor\":\"flat:john\",\"foreign_id\":\"82d2bb81-069d-427b-9238-8d822012e6d7\",\"object\":\"flat:eric\",\"origin\":\"\",\"time\":\"2016-09-22T21:44:58.821577\",\"verb\":\"post\"}")
err = activity.UnmarshalJSON(payload)
if err == nil {
t.Fatal(err)
}
if activity.To != nil {
t.Fatal("To payload was not a value feedslug:userid format, expected To to be nil afterward, got:", activity.To)
}
}