From 4fa0103cc803d94be812e334796dbcd7148cd240 Mon Sep 17 00:00:00 2001 From: Cory Slep Date: Sat, 12 May 2018 21:47:55 +0200 Subject: [PATCH] PostOutbox Undo tests --- pub/fed_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/pub/fed_test.go b/pub/fed_test.go index 19077a1..3841018 100644 --- a/pub/fed_test.go +++ b/pub/fed_test.go @@ -76,6 +76,7 @@ var ( testClientExpectedAdd *vocab.Add testClientExpectedRemove *vocab.Remove testClientExpectedLike *vocab.Like + testClientExpectedUndo *vocab.Undo ) func init() { @@ -357,6 +358,11 @@ func init() { testClientExpectedLike.AddActorObject(sallyActor) testClientExpectedLike.AddObject(testNote) testClientExpectedLike.AddToObject(samActor) + testClientExpectedUndo = &vocab.Undo{} + testClientExpectedUndo.SetId(*testNewIRI) + testClientExpectedUndo.AddActorObject(sallyActor) + testClientExpectedUndo.AddObject(testLikeNote) + testClientExpectedUndo.AddToObject(samActor) } func Must(l *time.Location, e error) *time.Location { @@ -4589,11 +4595,73 @@ func TestPostOutbox_Like_IsDelivered(t *testing.T) { } func TestPostOutbox_Undo_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(testUndoLike)))) + gotCallback := 0 + var gotCallbackObject *streams.Undo + socialCb.undo = func(c context.Context, s *streams.Undo) 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(), testClientExpectedUndo); err != nil { + t.Fatalf("unexpected callback object: %s", err) + } } func TestPostOutbox_Undo_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(testUndoLike)))) + socialCb.undo = func(c context.Context, s *streams.Undo) 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_Block_CallsCallback(t *testing.T) {