test with activity
This commit is contained in:
parent
8b1d96ec52
commit
501c383c02
|
@ -86,7 +86,6 @@ func (f *Federator) PostInboxRequestBodyHook(ctx context.Context, r *http.Reques
|
||||||
"url": r.URL.String(),
|
"url": r.URL.String(),
|
||||||
"aptype": activity.GetTypeName(),
|
"aptype": activity.GetTypeName(),
|
||||||
})
|
})
|
||||||
l.Debugf("received inbox post request %+v", activity)
|
|
||||||
|
|
||||||
if !util.IsInboxPath(r.URL) {
|
if !util.IsInboxPath(r.URL) {
|
||||||
err := fmt.Errorf("url %s did not corresponding to inbox path", r.URL.String())
|
err := fmt.Errorf("url %s did not corresponding to inbox path", r.URL.String())
|
||||||
|
|
|
@ -19,23 +19,30 @@
|
||||||
package federation_test
|
package federation_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/go-fed/activity/pub"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
||||||
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||||
"github.com/superseriousbusiness/gotosocial/testrig"
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProtocolTestSuite struct {
|
type ProtocolTestSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
config *config.Config
|
config *config.Config
|
||||||
db db.DB
|
db db.DB
|
||||||
log *logrus.Logger
|
log *logrus.Logger
|
||||||
federator *federation.Federator
|
federator *federation.Federator
|
||||||
|
activities map[string]pub.Activity
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
|
// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout
|
||||||
|
@ -44,6 +51,7 @@ func (suite *ProtocolTestSuite) SetupSuite() {
|
||||||
suite.config = testrig.NewTestConfig()
|
suite.config = testrig.NewTestConfig()
|
||||||
suite.db = testrig.NewTestDB()
|
suite.db = testrig.NewTestDB()
|
||||||
suite.log = testrig.NewTestLog()
|
suite.log = testrig.NewTestLog()
|
||||||
|
suite.activities = testrig.NewTestActivities()
|
||||||
|
|
||||||
// setup module being tested
|
// setup module being tested
|
||||||
suite.federator = federation.NewFederator(suite.db, suite.log, suite.config).(*federation.Federator)
|
suite.federator = federation.NewFederator(suite.db, suite.log, suite.config).(*federation.Federator)
|
||||||
|
@ -58,26 +66,32 @@ func (suite *ProtocolTestSuite) TearDownTest() {
|
||||||
testrig.StandardDBTeardown(suite.db)
|
testrig.StandardDBTeardown(suite.db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure PostInboxRequestBodyHook properly sets the inbox username and activity on the context
|
||||||
func (suite *ProtocolTestSuite) TestPostInboxRequestBodyHook() {
|
func (suite *ProtocolTestSuite) TestPostInboxRequestBodyHook() {
|
||||||
|
|
||||||
|
activity := suite.activities["dm_for_zork"]
|
||||||
|
|
||||||
// setup
|
// setup
|
||||||
// recorder := httptest.NewRecorder()
|
ctx := context.Background()
|
||||||
// ctx := context.Background()
|
request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) // the endpoint we're hitting
|
||||||
// request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) // the endpoint we're hitting
|
|
||||||
|
|
||||||
// activity :=
|
newContext, err := suite.federator.PostInboxRequestBodyHook(ctx, request, activity)
|
||||||
|
assert.NoError(suite.T(), err)
|
||||||
|
assert.NotNil(suite.T(), newContext)
|
||||||
|
|
||||||
// _, err := suite.federator.PostInboxRequestBodyHook(ctx, request, nil)
|
usernameI := newContext.Value(util.APUsernameKey)
|
||||||
// assert.NoError(suite.T(), err)
|
assert.NotNil(suite.T(), usernameI)
|
||||||
|
username, ok := usernameI.(string)
|
||||||
// check response
|
assert.True(suite.T(), ok)
|
||||||
// suite.EqualValues(http.StatusOK, recorder.Code)
|
assert.NotEmpty(suite.T(), username)
|
||||||
|
assert.Equal(suite.T(), "the_mighty_zork", username)
|
||||||
// result := recorder.Result()
|
|
||||||
// defer result.Body.Close()
|
|
||||||
// b, err := ioutil.ReadAll(result.Body)
|
|
||||||
// assert.NoError(suite.T(), err)
|
|
||||||
|
|
||||||
|
activityI := newContext.Value(util.APActivityKey)
|
||||||
|
assert.NotNil(suite.T(), activityI)
|
||||||
|
returnedActivity, ok := activityI.(pub.Activity)
|
||||||
|
assert.True(suite.T(), ok)
|
||||||
|
assert.NotNil(suite.T(), returnedActivity)
|
||||||
|
assert.EqualValues(suite.T(), activity, returnedActivity)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProtocolTestSuite(t *testing.T) {
|
func TestProtocolTestSuite(t *testing.T) {
|
||||||
|
|
|
@ -22,8 +22,12 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"net"
|
"net"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-fed/activity/pub"
|
||||||
|
"github.com/go-fed/activity/streams"
|
||||||
|
"github.com/go-fed/activity/streams/vocab"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db/gtsmodel"
|
"github.com/superseriousbusiness/gotosocial/internal/db/gtsmodel"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
||||||
)
|
)
|
||||||
|
@ -993,3 +997,120 @@ func NewTestFaves() map[string]*gtsmodel.StatusFave {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewTestActivities() map[string]pub.Activity {
|
||||||
|
dmForZork := newNote(
|
||||||
|
URLMustParse("https://fossbros-anonymous.io/users/foss_satan/statuses/5424b153-4553-4f30-9358-7b92f7cd42f6"),
|
||||||
|
URLMustParse("https://fossbros-anonymous.io/@foss_satan/5424b153-4553-4f30-9358-7b92f7cd42f6"),
|
||||||
|
"hey zork here's a new private note for you",
|
||||||
|
"new note for zork",
|
||||||
|
URLMustParse("https://fossbros-anonymous.io/users/foss_satan"),
|
||||||
|
[]*url.URL{URLMustParse("http://localhost:8080/users/the_mighty_zork")},
|
||||||
|
nil,
|
||||||
|
true)
|
||||||
|
createDmForZork := wrapNoteInCreate(
|
||||||
|
URLMustParse("https://fossbros-anonymous.io/users/foss_satan/statuses/5424b153-4553-4f30-9358-7b92f7cd42f6/activity"),
|
||||||
|
URLMustParse("https://fossbros-anonymous.io/users/foss_satan"),
|
||||||
|
time.Now(),
|
||||||
|
dmForZork)
|
||||||
|
|
||||||
|
return map[string]pub.Activity{
|
||||||
|
"dm_for_zork": createDmForZork,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNote(
|
||||||
|
noteID *url.URL,
|
||||||
|
noteURL *url.URL,
|
||||||
|
noteContent string,
|
||||||
|
noteSummary string,
|
||||||
|
noteAttributedTo *url.URL,
|
||||||
|
noteTo []*url.URL,
|
||||||
|
noteCC []*url.URL,
|
||||||
|
noteSensitive bool) vocab.ActivityStreamsNote {
|
||||||
|
|
||||||
|
// create the note itself
|
||||||
|
note := streams.NewActivityStreamsNote()
|
||||||
|
|
||||||
|
// set id
|
||||||
|
if noteID != nil {
|
||||||
|
id := streams.NewJSONLDIdProperty()
|
||||||
|
id.Set(noteID)
|
||||||
|
note.SetJSONLDId(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set noteURL
|
||||||
|
if noteURL != nil {
|
||||||
|
url := streams.NewActivityStreamsUrlProperty()
|
||||||
|
url.AppendIRI(noteURL)
|
||||||
|
note.SetActivityStreamsUrl(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set noteContent
|
||||||
|
if noteContent != "" {
|
||||||
|
content := streams.NewActivityStreamsContentProperty()
|
||||||
|
content.AppendXMLSchemaString(noteContent)
|
||||||
|
note.SetActivityStreamsContent(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set noteSummary (aka content warning)
|
||||||
|
if noteSummary != "" {
|
||||||
|
summary := streams.NewActivityStreamsSummaryProperty()
|
||||||
|
summary.AppendXMLSchemaString(noteSummary)
|
||||||
|
note.SetActivityStreamsSummary(summary)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set noteAttributedTo (the url of the author of the note)
|
||||||
|
if noteAttributedTo != nil {
|
||||||
|
attributedTo := streams.NewActivityStreamsAttributedToProperty()
|
||||||
|
attributedTo.AppendIRI(noteAttributedTo)
|
||||||
|
note.SetActivityStreamsAttributedTo(attributedTo)
|
||||||
|
}
|
||||||
|
|
||||||
|
return note
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapNoteInCreate(createID *url.URL, createActor *url.URL, createPublished time.Time, createNote vocab.ActivityStreamsNote) vocab.ActivityStreamsCreate {
|
||||||
|
// create the.... create
|
||||||
|
create := streams.NewActivityStreamsCreate()
|
||||||
|
|
||||||
|
// set createID
|
||||||
|
if createID != nil {
|
||||||
|
id := streams.NewJSONLDIdProperty()
|
||||||
|
id.Set(createID)
|
||||||
|
create.SetJSONLDId(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set createActor
|
||||||
|
if createActor != nil {
|
||||||
|
actor := streams.NewActivityStreamsActorProperty()
|
||||||
|
actor.AppendIRI(createActor)
|
||||||
|
create.SetActivityStreamsActor(actor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set createPublished (time)
|
||||||
|
if !createPublished.IsZero() {
|
||||||
|
published := streams.NewActivityStreamsPublishedProperty()
|
||||||
|
published.Set(createPublished)
|
||||||
|
create.SetActivityStreamsPublished(published)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setCreateTo
|
||||||
|
if createNote.GetActivityStreamsTo() != nil {
|
||||||
|
create.SetActivityStreamsTo(createNote.GetActivityStreamsTo())
|
||||||
|
}
|
||||||
|
|
||||||
|
// setCreateCC
|
||||||
|
if createNote.GetActivityStreamsCc() != nil {
|
||||||
|
create.SetActivityStreamsCc(createNote.GetActivityStreamsCc())
|
||||||
|
}
|
||||||
|
|
||||||
|
// set createNote
|
||||||
|
if createNote != nil {
|
||||||
|
note := streams.NewActivityStreamsObjectProperty()
|
||||||
|
note.AppendActivityStreamsNote(createNote)
|
||||||
|
create.SetActivityStreamsObject(note)
|
||||||
|
}
|
||||||
|
|
||||||
|
return create
|
||||||
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,3 +63,13 @@ func CreateMultipartFormData(fieldName string, fileName string, extraFields map[
|
||||||
}
|
}
|
||||||
return b, w, nil
|
return b, w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URLMustParse tries to parse the given URL and panics if it can't.
|
||||||
|
// Should only be used in tests.
|
||||||
|
func URLMustParse(stringURL string) *url.URL {
|
||||||
|
u, err := url.Parse(stringURL)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue