From 9fbd18b53c50e70de3053dd2f8b7a50fdd6c1a3f Mon Sep 17 00:00:00 2001 From: Cory Slep Date: Thu, 10 May 2018 17:25:48 +0200 Subject: [PATCH] PostOutbox Follow tests --- pub/fed.go | 1 - pub/fed_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/pub/fed.go b/pub/fed.go index e7dadae..ae08d51 100644 --- a/pub/fed.go +++ b/pub/fed.go @@ -543,7 +543,6 @@ func (f *federator) handleClientFollow(c context.Context, deliverable *bool) fun if s.LenObject() == 0 { return ErrObjectRequired } - // Nothing extra to do. return f.ClientCallbacker.Follow(c, s) } } diff --git a/pub/fed_test.go b/pub/fed_test.go index 6f78cfd..264ac01 100644 --- a/pub/fed_test.go +++ b/pub/fed_test.go @@ -70,6 +70,7 @@ var ( testClientUpdateNote *vocab.Update testClientExpectedUpdateNote *vocab.Update testClientExpectedDeleteNote *vocab.Delete + testClientExpectedFollow *vocab.Follow ) func init() { @@ -319,6 +320,11 @@ func init() { testClientExpectedDeleteNote.AddActorObject(sallyActor) testClientExpectedDeleteNote.AddObject(testNote) testClientExpectedDeleteNote.AddToObject(samActor) + testClientExpectedFollow = &vocab.Follow{} + testClientExpectedFollow.SetId(*testNewIRI) + testClientExpectedFollow.AddActorObject(sallyActor) + testClientExpectedFollow.AddObject(samActor) + testClientExpectedFollow.AddToObject(samActor) } func Must(l *time.Location, e error) *time.Location { @@ -3672,11 +3678,73 @@ func TestPostOutbox_Delete_IsDelivered(t *testing.T) { } func TestPostOutbox_Follow_CallsCallback(t *testing.T) { - // TODO: Implement + app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p := NewPubberTest(t) + PreparePostOutboxTest(t, app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p) + resp := httptest.NewRecorder() + req := ActivityPubRequest(httptest.NewRequest("POST", testOutboxURI, bytes.NewBuffer(MustSerialize(testFollow)))) + gotCallback := 0 + var gotCallbackObject *streams.Follow + socialCb.follow = func(c context.Context, s *streams.Follow) error { + gotCallback++ + gotCallbackObject = s + return nil + } + handled, err := p.PostOutbox(context.Background(), resp, req) + if err != nil { + t.Fatal(err) + } else if !handled { + t.Fatalf("expected handled, got !handled") + } else if gotCallback != 1 { + t.Fatalf("expected %d, got %d", 1, gotCallback) + } else if err := PubObjectEquals(gotCallbackObject.Raw(), testClientExpectedFollow); err != nil { + t.Fatalf("unexpected callback object: %s", err) + } } func TestPostOutbox_Follow_IsDelivered(t *testing.T) { - // TODO: Implement + app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p := NewPubberTest(t) + PreparePostOutboxTest(t, app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p) + resp := httptest.NewRecorder() + req := ActivityPubRequest(httptest.NewRequest("POST", testOutboxURI, bytes.NewBuffer(MustSerialize(testFollow)))) + socialCb.follow = func(c context.Context, s *streams.Follow) error { + return nil + } + gotHttpDo := 0 + var httpDeliveryRequest *http.Request + httpClient.do = func(req *http.Request) (*http.Response, error) { + gotHttpDo++ + if gotHttpDo == 1 { + actorResp := &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewBuffer(samActorJSON)), + } + return actorResp, nil + } else if gotHttpDo == 2 { + actorResp := &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewBuffer(sallyActorJSON)), + } + return actorResp, nil + } else if gotHttpDo == 3 { + httpDeliveryRequest = req + okResp := &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewBuffer([]byte{})), + } + return okResp, nil + } + return nil, nil + } + handled, err := p.PostOutbox(context.Background(), resp, req) + if err != nil { + t.Fatal(err) + } else if !handled { + t.Fatalf("expected handled, got !handled") + } else if httpDeliveryRequest.Method != "POST" { + t.Fatalf("expected %s, got %s", "POST", httpDeliveryRequest.Method) + } else if s := httpDeliveryRequest.URL.String(); s != samIRIInboxString { + t.Fatalf("expected %s, got %s", samIRIInboxString, s) + } } func TestPostOutbox_Accept_CallsCallback(t *testing.T) {