From 46d2f1aad5c3c40fa89974dc3843a55ecedf3878 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Wed, 11 Aug 2021 21:57:06 +0200 Subject: [PATCH] fix up tests + automate with drone --- .drone.yml | 47 +++++++++++++------- internal/federation/federator_test.go | 21 +++++---- internal/oauth/clientstore_test.go | 2 +- internal/typeutils/astointernal_test.go | 57 ------------------------- testrig/db.go | 11 ++++- 5 files changed, 55 insertions(+), 83 deletions(-) diff --git a/.drone.yml b/.drone.yml index 672c3c821..37af9209a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,18 +1,35 @@ kind: pipeline type: docker -name: dockerpublish +name: default steps: - - name: publish image - image: plugins/docker - settings: - auto_tag: true - username: - from_secret: gts_docker_username - password: - from_secret: gts_docker_password - repo: superseriousbusiness/gotosocial - tags: latest - when: - event: - exclude: - - pull_request +- name: lint + image: golang + commands: + - go get -u github.com/golangci/golangci-lint/cmd/golangci-lint + - golangci-lint run +- name: test + image: golang + environment: + GTS_DB_ADDRESS: postgres + commands: + - go test -count 1 -p 1 ./... +- name: publish + image: plugins/docker + settings: + auto_tag: true + username: + from_secret: gts_docker_username + password: + from_secret: gts_docker_password + repo: superseriousbusiness/gotosocial + tags: latest + when: + event: + exclude: + - pull_request + +services: +- name: postgres + image: postgres + environment: + POSTGRES_PASSWORD: postgres diff --git a/internal/federation/federator_test.go b/internal/federation/federator_test.go index 00a29465e..cb21d44c2 100644 --- a/internal/federation/federator_test.go +++ b/internal/federation/federator_test.go @@ -25,6 +25,7 @@ import ( "testing" "github.com/go-fed/activity/pub" + "github.com/go-fed/httpsig" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" @@ -115,25 +116,27 @@ func (suite *ProtocolTestSuite) TestAuthenticatePostInbox() { // now setup module being tested, with the mock transport controller federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db), tc, suite.config, suite.log, suite.typeConverter, testrig.NewTestMediaHandler(suite.db, suite.storage)) - // setup request + request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) + // we need these headers for the request to be validated + request.Header.Set("Signature", activity.SignatureHeader) + request.Header.Set("Date", activity.DateHeader) + request.Header.Set("Digest", activity.DigestHeader) + + verifier, err := httpsig.NewVerifier(request) + assert.NoError(suite.T(), err) + ctx := context.Background() // by the time AuthenticatePostInbox is called, PostInboxRequestBodyHook should have already been called, // which should have set the account and username onto the request. We can replicate that behavior here: ctxWithAccount := context.WithValue(ctx, util.APAccount, inboxAccount) ctxWithActivity := context.WithValue(ctxWithAccount, util.APActivity, activity) + ctxWithVerifier := context.WithValue(ctxWithActivity, util.APRequestingPublicKeyVerifier, verifier) - -aaaaaaaaaaaaaaaaaaaaa - request := httptest.NewRequest(http.MethodPost, "http://localhost:8080/users/the_mighty_zork/inbox", nil) // the endpoint we're hitting - // we need these headers for the request to be validated - request.Header.Set("Signature", activity.SignatureHeader) - request.Header.Set("Date", activity.DateHeader) - request.Header.Set("Digest", activity.DigestHeader) // we can pass this recorder as a writer and read it back after recorder := httptest.NewRecorder() // trigger the function being tested, and return the new context it creates - newContext, authed, err := federator.AuthenticatePostInbox(ctxWithActivity, recorder, request) + newContext, authed, err := federator.AuthenticatePostInbox(ctxWithVerifier, recorder, request) assert.NoError(suite.T(), err) assert.True(suite.T(), authed) diff --git a/internal/oauth/clientstore_test.go b/internal/oauth/clientstore_test.go index 58c5148b2..962baa9fb 100644 --- a/internal/oauth/clientstore_test.go +++ b/internal/oauth/clientstore_test.go @@ -43,7 +43,7 @@ const () // SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout func (suite *PgClientStoreTestSuite) SetupSuite() { - suite.testClientID = "test-client-id" + suite.testClientID = "01FCVB74EW6YBYAEY7QG9CQQF6" suite.testClientSecret = "test-client-secret" suite.testClientDomain = "https://example.org" suite.testClientUserID = "test-client-user-id" diff --git a/internal/typeutils/astointernal_test.go b/internal/typeutils/astointernal_test.go index 2e33271c5..5b8e30134 100644 --- a/internal/typeutils/astointernal_test.go +++ b/internal/typeutils/astointernal_test.go @@ -25,7 +25,6 @@ import ( "testing" "github.com/go-fed/activity/streams" - "github.com/go-fed/activity/streams/vocab" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/ap" @@ -375,62 +374,6 @@ func (suite *ASToInternalTestSuite) TestParseGargron() { // TODO: write assertions here, rn we're just eyeballing the output } -func (suite *ASToInternalTestSuite) TestParseStatus() { - m := make(map[string]interface{}) - err := json.Unmarshal([]byte(statusWithEmojisAndTagsAsActivityJson), &m) - assert.NoError(suite.T(), err) - - t, err := streams.ToType(context.Background(), m) - assert.NoError(suite.T(), err) - - create, ok := t.(vocab.ActivityStreamsCreate) - assert.True(suite.T(), ok) - - obj := create.GetActivityStreamsObject() - assert.NotNil(suite.T(), obj) - - first := obj.Begin() - assert.NotNil(suite.T(), first) - - rep, ok := first.GetType().(ap.Statusable) - assert.True(suite.T(), ok) - - status, err := suite.typeconverter.ASStatusToStatus(rep) - assert.NoError(suite.T(), err) - - assert.Len(suite.T(), status.GTSEmojis, 3) - // assert.Len(suite.T(), status.GTSTags, 2) TODO: implement this first so that it can pick up tags -} - -func (suite *ASToInternalTestSuite) TestParseStatusWithMention() { - m := make(map[string]interface{}) - err := json.Unmarshal([]byte(statusWithMentionsActivityJson), &m) - assert.NoError(suite.T(), err) - - t, err := streams.ToType(context.Background(), m) - assert.NoError(suite.T(), err) - - create, ok := t.(vocab.ActivityStreamsCreate) - assert.True(suite.T(), ok) - - obj := create.GetActivityStreamsObject() - assert.NotNil(suite.T(), obj) - - first := obj.Begin() - assert.NotNil(suite.T(), first) - - rep, ok := first.GetType().(ap.Statusable) - assert.True(suite.T(), ok) - - status, err := suite.typeconverter.ASStatusToStatus(rep) - assert.NoError(suite.T(), err) - - fmt.Printf("%+v", status) - - assert.Len(suite.T(), status.GTSMentions, 1) - fmt.Println(status.GTSMentions[0]) -} - func (suite *ASToInternalTestSuite) TearDownTest() { testrig.StandardDBTeardown(suite.db) } diff --git a/testrig/db.go b/testrig/db.go index f34f7936b..659a74ca2 100644 --- a/testrig/db.go +++ b/testrig/db.go @@ -20,6 +20,7 @@ package testrig import ( "context" + "os" "github.com/sirupsen/logrus" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -52,9 +53,17 @@ var testModels []interface{} = []interface{}{ &oauth.Client{}, } -// NewTestDB returns a new initialized, empty database for testing +// NewTestDB returns a new initialized, empty database for testing. +// +// If the environment variable GTS_DB_ADDRESS is set, it will take that +// value as the database address instead. func NewTestDB() db.DB { config := NewTestConfig() + alternateAddress := os.Getenv("GTS_DB_ADDRESS") + if alternateAddress != "" { + config.DBConfig.Address = alternateAddress + } + l := logrus.New() l.SetLevel(logrus.TraceLevel) testDB, err := pg.NewPostgresService(context.Background(), config, l)