Serve tombstone with HTTP status Gone

This commit is contained in:
Cory Slep 2018-06-09 00:03:45 +02:00
parent 2764b67c7e
commit 1f9eeafca8
3 changed files with 91 additions and 4 deletions

View file

@ -125,7 +125,11 @@ func serveActivityPubObject(c context.Context, a Application, clock Clock, w htt
return
}
addResponseHeaders(w.Header(), clock, b)
w.WriteHeader(http.StatusOK)
if hasType(pObj, tombstone) {
w.WriteHeader(http.StatusGone)
} else {
w.WriteHeader(http.StatusOK)
}
n, err := w.Write(b)
if err != nil {
return

View file

@ -177,6 +177,37 @@ func TestServeActivityPubObject(t *testing.T) {
},
expectHandled: true,
},
{
name: "tombstone is status gone",
app: &MockApplication{
t: t,
get: func(c context.Context, id *url.URL, rw RWType) (PubObject, error) {
if rw != Read {
t.Fatalf("expected RWType of %d, got %d", Read, rw)
} 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,
},
}
for _, test := range tests {
t.Logf("Running table test case %q", test.name)
@ -556,11 +587,19 @@ func TestServeActivityPubObjectWithVerificationMethod(t *testing.T) {
name: "remove bto & bcc",
app: &MockApplication{
t: t,
get: func(c context.Context, id *url.URL, rw RWType) (PubObject, error) {
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 {
t.Fatalf("expected RWType of %d, got %d", Read, rw)
} else if s := id.String(); s != noteURIString {
t.Fatalf("expected %s, got %s", noteURIString, s)
} else if u := user.String(); u != samIRIString {
t.Fatalf("expected %s, got %s", samIRIString, u)
}
testNote = &vocab.Note{}
testNote.SetId(noteIRI)
@ -578,7 +617,7 @@ func TestServeActivityPubObjectWithVerificationMethod(t *testing.T) {
},
},
clock: &MockClock{now},
input: ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil)),
input: Sign(ActivityPubRequest(httptest.NewRequest("GET", noteURIString, nil))),
expectedCode: http.StatusOK,
expectedObjFn: func() vocab.Serializer {
testNote = &vocab.Note{}
@ -589,6 +628,45 @@ func TestServeActivityPubObjectWithVerificationMethod(t *testing.T) {
},
expectHandled: true,
},
{
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 {
t.Fatalf("expected RWType of %d, got %d", Read, rw)
} 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,
},
}
for _, test := range tests {
t.Logf("Running table test case %q", test.name)

View file

@ -208,7 +208,7 @@ func postToOutbox(c HttpClient, b []byte, to *url.URL, agent string, creds *cred
func (f *federator) addNewIds(c context.Context, a vocab.ActivityType) {
newId := f.App.NewId(c, a)
a.SetId(newId)
if hasType(a, "Create") {
if hasType(a, create) {
for i := 0; i < a.ObjectLen(); i++ {
if a.IsObject(i) {
obj := a.GetObject(i)
@ -2127,6 +2127,11 @@ func clearSensitiveFields(obj vocab.ObjectType) {
// TODO: Move this to vocab package.
var activityTypes = []string{"Accept", "Add", "Announce", "Arrive", "Block", "Create", "Delete", "Dislike", "Flag", "Follow", "Ignore", "Invite", "Join", "Leave", "Like", "Listen", "Move", "Offer", "Question", "Reject", "Read", "Remove", "TentativeReject", "TentativeAccept", "Travel", "Undo", "Update", "View"}
const (
tombstone = "Tombstone"
create = "Create"
)
func isActivityType(t Typer) bool {
hasType := make(map[string]bool, 1)
for i := 0; i < t.TypeLen(); i++ {