2018-05-27 08:49:03 +09:00
|
|
|
package pub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"github.com/go-fed/activity/vocab"
|
|
|
|
"github.com/go-fed/httpsig"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServeActivityPubObject(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
app *MockApplication
|
|
|
|
clock *MockClock
|
|
|
|
input *http.Request
|
|
|
|
expectedCode int
|
|
|
|
expectedObjFn func() vocab.Serializer
|
|
|
|
expectHandled bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "unsigned request",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
get: func(c context.Context, id *url.URL, rw RWType) (PubObject, error) {
|
2018-05-28 05:57:43 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote, nil
|
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http signature request",
|
|
|
|
input: Sign(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
getPublicKey: func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) {
|
2018-05-27 08:49:03 +09:00
|
|
|
if publicKeyId != testPublicKeyId {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", testPublicKeyId, publicKeyId)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
2018-05-30 04:57:39 +09:00
|
|
|
return testPrivateKey.Public(), httpsig.RSA_SHA256, samIRI, nil
|
2018-05-27 08:49:03 +09:00
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
getAsVerifiedUser: func(c context.Context, id, user *url.URL, rw RWType) (PubObject, error) {
|
2018-05-28 05:57:43 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if u := user.String(); u != samIRIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", samIRIString, u)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote, nil
|
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not owned",
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCode: http.StatusNotFound,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not activitypub get",
|
|
|
|
input: httptest.NewRequest("GET", noteURIString, nil),
|
|
|
|
expectHandled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad http signature",
|
|
|
|
input: BadSignature(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
getPublicKey: func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) {
|
2018-05-27 08:49:03 +09:00
|
|
|
if publicKeyId != testPublicKeyId {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", testPublicKeyId, publicKeyId)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
2018-05-30 04:57:39 +09:00
|
|
|
return testPrivateKey.Public(), httpsig.RSA_SHA256, samIRI, nil
|
2018-05-27 08:49:03 +09:00
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCode: http.StatusForbidden,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
2018-06-09 06:45:25 +09:00
|
|
|
{
|
|
|
|
name: "remove bto & bcc",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
|
|
|
get: func(c context.Context, id *url.URL, rw RWType) (PubObject, error) {
|
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-06-09 06:45:25 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
|
|
|
testNote.SetId(noteIRI)
|
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
|
|
|
testNote.AppendBtoIRI(samIRI)
|
|
|
|
testNote.AppendBccIRI(sallyIRI)
|
|
|
|
return testNote, nil
|
|
|
|
},
|
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
|
|
|
testNote.SetId(noteIRI)
|
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
2018-06-09 07:03:45 +09:00
|
|
|
{
|
|
|
|
name: "tombstone is status gone",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
|
|
|
get: func(c context.Context, id *url.URL, rw RWType) (PubObject, error) {
|
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-06-09 07:03:45 +09:00
|
|
|
} else if s := id.String(); s != testNewIRIString {
|
|
|
|
t.Fatalf("expected %s, got %s", testNewIRIString, s)
|
|
|
|
}
|
|
|
|
tombstone := &vocab.Tombstone{}
|
|
|
|
tombstone.SetId(testNewIRI)
|
|
|
|
return tombstone, nil
|
|
|
|
},
|
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != testNewIRIString {
|
|
|
|
t.Fatalf("expected %s, got %s", testNewIRIString, s)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", testNewIRIString, nil)),
|
|
|
|
expectedCode: http.StatusGone,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
tombstone := &vocab.Tombstone{}
|
|
|
|
tombstone.SetId(testNewIRI)
|
|
|
|
return tombstone
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Logf("Running table test case %q", test.name)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-05-31 06:37:59 +09:00
|
|
|
fnUnderTest := ServeActivityPubObject(test.app, test.clock)
|
|
|
|
handled, err := fnUnderTest(context.Background(), resp, test.input)
|
2018-05-27 08:49:03 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("(%q) %s", test.name, err)
|
|
|
|
} else if handled != test.expectHandled {
|
|
|
|
t.Fatalf("(%q) expected %v, got %v", test.name, test.expectHandled, handled)
|
|
|
|
} else if test.expectedCode != 0 {
|
|
|
|
if resp.Code != test.expectedCode {
|
|
|
|
t.Fatalf("(%q) expected %d, got %d", test.name, test.expectedCode, resp.Code)
|
|
|
|
}
|
|
|
|
} else if test.expectedObjFn != nil {
|
|
|
|
if err := VocabEquals(resp.Body, test.expectedObjFn()); err != nil {
|
|
|
|
t.Fatalf("(%q) unexpected object: %s", test.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServeActivityPubObjectWithVerificationMethod(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
app *MockApplication
|
|
|
|
clock *MockClock
|
|
|
|
verifier *MockSocialAPIVerifier
|
|
|
|
input *http.Request
|
|
|
|
expectedCode int
|
|
|
|
expectedObjFn func() vocab.Serializer
|
|
|
|
expectHandled bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "unsigned request",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
get: func(c context.Context, id *url.URL, rw RWType) (PubObject, error) {
|
2018-05-28 05:57:43 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote, nil
|
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http signature request",
|
|
|
|
input: Sign(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
getPublicKey: func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) {
|
2018-05-27 08:49:03 +09:00
|
|
|
if publicKeyId != testPublicKeyId {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", testPublicKeyId, publicKeyId)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
2018-05-30 04:57:39 +09:00
|
|
|
return testPrivateKey.Public(), httpsig.RSA_SHA256, samIRI, nil
|
2018-05-27 08:49:03 +09:00
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
getAsVerifiedUser: func(c context.Context, id, user *url.URL, rw RWType) (PubObject, error) {
|
2018-05-28 05:57:43 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if u := user.String(); u != samIRIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", samIRIString, u)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote, nil
|
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not owned",
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCode: http.StatusNotFound,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not activitypub get",
|
|
|
|
input: httptest.NewRequest("GET", noteURIString, nil),
|
|
|
|
expectHandled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad http signature",
|
|
|
|
input: BadSignature(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
getPublicKey: func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) {
|
2018-05-27 08:49:03 +09:00
|
|
|
if publicKeyId != testPublicKeyId {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", testPublicKeyId, publicKeyId)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
2018-05-30 04:57:39 +09:00
|
|
|
return testPrivateKey.Public(), httpsig.RSA_SHA256, samIRI, nil
|
2018-05-27 08:49:03 +09:00
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCode: http.StatusForbidden,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unsigned request passes verifier",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
getAsVerifiedUser: func(c context.Context, id, user *url.URL, rw RWType) (PubObject, error) {
|
2018-05-28 05:57:43 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if u := user.String(); u != samIRIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", samIRIString, u)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote, nil
|
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
verifier: &MockSocialAPIVerifier{
|
|
|
|
t: t,
|
|
|
|
verify: func(r *http.Request) (*url.URL, bool, bool, error) {
|
|
|
|
return samIRI, true, true, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http signature request passes verifier",
|
|
|
|
input: Sign(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
getAsVerifiedUser: func(c context.Context, id, user *url.URL, rw RWType) (PubObject, error) {
|
2018-05-28 05:57:43 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if u := user.String(); u != samIRIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", samIRIString, u)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote, nil
|
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
verifier: &MockSocialAPIVerifier{
|
|
|
|
t: t,
|
|
|
|
verify: func(r *http.Request) (*url.URL, bool, bool, error) {
|
|
|
|
return samIRI, true, true, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "verifier authed unauthz",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
verifier: &MockSocialAPIVerifier{
|
|
|
|
t: t,
|
|
|
|
verify: func(r *http.Request) (*url.URL, bool, bool, error) {
|
|
|
|
return samIRI, true, false, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusForbidden,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "verifier unauthed unauthz",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
verifier: &MockSocialAPIVerifier{
|
|
|
|
t: t,
|
|
|
|
verify: func(r *http.Request) (*url.URL, bool, bool, error) {
|
|
|
|
return nil, false, false, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusBadRequest,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "verifier unauthed authz unsigned fails",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
verifier: &MockSocialAPIVerifier{
|
|
|
|
t: t,
|
|
|
|
verify: func(r *http.Request) (*url.URL, bool, bool, error) {
|
|
|
|
return nil, false, true, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusBadRequest,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "verifier unauthed authz signed success",
|
|
|
|
input: Sign(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
getPublicKey: func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) {
|
2018-05-27 08:49:03 +09:00
|
|
|
if publicKeyId != testPublicKeyId {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", testPublicKeyId, publicKeyId)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
2018-05-30 04:57:39 +09:00
|
|
|
return testPrivateKey.Public(), httpsig.RSA_SHA256, samIRI, nil
|
2018-05-27 08:49:03 +09:00
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
getAsVerifiedUser: func(c context.Context, id, user *url.URL, rw RWType) (PubObject, error) {
|
2018-05-28 05:57:43 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-30 04:57:39 +09:00
|
|
|
} else if u := user.String(); u != samIRIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", samIRIString, u)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote, nil
|
|
|
|
},
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
verifier: &MockSocialAPIVerifier{
|
|
|
|
t: t,
|
|
|
|
verify: func(r *http.Request) (*url.URL, bool, bool, error) {
|
|
|
|
return nil, false, true, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
2018-05-30 04:57:39 +09:00
|
|
|
testNote.SetId(noteIRI)
|
2018-05-28 00:07:40 +09:00
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
2018-05-27 08:49:03 +09:00
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "verifier unauthed authz unsigned fails with bad impl returning user",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-05-30 04:57:39 +09:00
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
2018-05-31 04:33:05 +09:00
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
verifier: &MockSocialAPIVerifier{
|
|
|
|
t: t,
|
|
|
|
verify: func(r *http.Request) (*url.URL, bool, bool, error) {
|
|
|
|
return samIRI, false, true, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
|
|
|
|
expectedCode: http.StatusBadRequest,
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
2018-06-09 06:45:25 +09:00
|
|
|
{
|
|
|
|
name: "remove bto & bcc",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
2018-06-09 07:03:45 +09:00
|
|
|
getPublicKey: func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) {
|
|
|
|
if publicKeyId != testPublicKeyId {
|
|
|
|
t.Fatalf("expected %s, got %s", testPublicKeyId, publicKeyId)
|
|
|
|
}
|
|
|
|
return testPrivateKey.Public(), httpsig.RSA_SHA256, samIRI, nil
|
|
|
|
},
|
|
|
|
getAsVerifiedUser: func(c context.Context, id, user *url.URL, rw RWType) (PubObject, error) {
|
2018-06-09 06:45:25 +09:00
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-06-09 06:45:25 +09:00
|
|
|
} else if s := id.String(); s != noteURIString {
|
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
2018-06-09 07:03:45 +09:00
|
|
|
} else if u := user.String(); u != samIRIString {
|
|
|
|
t.Fatalf("expected %s, got %s", samIRIString, u)
|
2018-06-09 06:45:25 +09:00
|
|
|
}
|
|
|
|
testNote = &vocab.Note{}
|
|
|
|
testNote.SetId(noteIRI)
|
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
|
|
|
testNote.AppendBtoIRI(samIRI)
|
|
|
|
testNote.AppendBccIRI(sallyIRI)
|
|
|
|
return testNote, nil
|
|
|
|
},
|
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != noteURIString {
|
|
|
|
t.Fatalf("expected %s, got %s", noteURIString, s)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
2018-06-09 07:03:45 +09:00
|
|
|
input: Sign(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
|
2018-06-09 06:45:25 +09:00
|
|
|
expectedCode: http.StatusOK,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
testNote = &vocab.Note{}
|
|
|
|
testNote.SetId(noteIRI)
|
|
|
|
testNote.AppendNameString(noteName)
|
|
|
|
testNote.AppendContentString("This is a simple note")
|
|
|
|
return testNote
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
2018-06-09 07:03:45 +09:00
|
|
|
{
|
|
|
|
name: "tombstone is status gone",
|
|
|
|
app: &MockApplication{
|
|
|
|
t: t,
|
|
|
|
getPublicKey: func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) {
|
|
|
|
if publicKeyId != testPublicKeyId {
|
|
|
|
t.Fatalf("expected %s, got %s", testPublicKeyId, publicKeyId)
|
|
|
|
}
|
|
|
|
return testPrivateKey.Public(), httpsig.RSA_SHA256, samIRI, nil
|
|
|
|
},
|
|
|
|
getAsVerifiedUser: func(c context.Context, id, user *url.URL, rw RWType) (PubObject, error) {
|
|
|
|
if rw != Read {
|
2018-08-20 01:31:45 +09:00
|
|
|
t.Fatalf("expected RWType of %v, got %v", Read, rw)
|
2018-06-09 07:03:45 +09:00
|
|
|
} else if s := id.String(); s != testNewIRIString {
|
|
|
|
t.Fatalf("expected %s, got %s", testNewIRIString, s)
|
|
|
|
} else if u := user.String(); u != samIRIString {
|
|
|
|
t.Fatalf("expected %s, got %s", samIRIString, u)
|
|
|
|
}
|
|
|
|
tombstone := &vocab.Tombstone{}
|
|
|
|
tombstone.SetId(testNewIRI)
|
|
|
|
return tombstone, nil
|
|
|
|
},
|
|
|
|
owns: func(c context.Context, id *url.URL) bool {
|
|
|
|
if s := id.String(); s != testNewIRIString {
|
|
|
|
t.Fatalf("expected %s, got %s", testNewIRIString, s)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clock: &MockClock{now},
|
|
|
|
input: Sign(ActivityPubRequest(httptest.NewRequest("GET", testNewIRIString, nil))),
|
|
|
|
expectedCode: http.StatusGone,
|
|
|
|
expectedObjFn: func() vocab.Serializer {
|
|
|
|
tombstone := &vocab.Tombstone{}
|
|
|
|
tombstone.SetId(testNewIRI)
|
|
|
|
return tombstone
|
|
|
|
},
|
|
|
|
expectHandled: true,
|
|
|
|
},
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Logf("Running table test case %q", test.name)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-05-31 06:37:59 +09:00
|
|
|
var fnUnderTest HandlerFunc
|
2018-05-27 08:49:03 +09:00
|
|
|
if test.verifier != nil {
|
2018-05-31 06:37:59 +09:00
|
|
|
verifierFn := func(c context.Context) SocialAPIVerifier {
|
|
|
|
return test.verifier
|
|
|
|
}
|
|
|
|
fnUnderTest = ServeActivityPubObjectWithVerificationMethod(test.app, test.clock, verifierFn)
|
2018-05-27 08:49:03 +09:00
|
|
|
} else {
|
2018-05-31 06:37:59 +09:00
|
|
|
fnUnderTest = ServeActivityPubObjectWithVerificationMethod(test.app, test.clock, nil)
|
2018-05-27 08:49:03 +09:00
|
|
|
}
|
2018-05-31 06:37:59 +09:00
|
|
|
handled, err := fnUnderTest(context.Background(), resp, test.input)
|
2018-05-27 08:49:03 +09:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("(%q) %s", test.name, err)
|
|
|
|
} else if handled != test.expectHandled {
|
|
|
|
t.Fatalf("(%q) expected %v, got %v", test.name, test.expectHandled, handled)
|
|
|
|
} else if test.expectedCode != 0 {
|
|
|
|
if resp.Code != test.expectedCode {
|
|
|
|
t.Fatalf("(%q) expected %d, got %d", test.name, test.expectedCode, resp.Code)
|
|
|
|
}
|
|
|
|
} else if test.expectedObjFn != nil {
|
|
|
|
if err := VocabEquals(resp.Body, test.expectedObjFn()); err != nil {
|
|
|
|
t.Fatalf("(%q) unexpected object: %s", test.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|