Add more unit tests for new id generation.

Fix major bugs: was using IsExtendedBy checks instead of IsOrExtends
from the streams package.

For example, if checking for an OrderedCollection, was
checking its child types and failing if it was an OrderedCollection. Now
this is fixed, so OrderedCollection and child types are accepted.
このコミットが含まれているのは:
Cory Slep 2019-03-13 21:19:38 +01:00
コミット 1ea8e2c7f3
7個のファイルの変更112行の追加22行の削除

ファイルの表示

@ -312,7 +312,7 @@ func (b *baseActor) PostOutbox(c context.Context, w http.ResponseWriter, r *http
}
// If the value is not an Activity or type extending from Activity, then
// we need to wrap it in a Create Activity.
if !IsAnActivityType(asValue) {
if !streams.IsOrExtendsActivityStreamsActivity(asValue) {
asValue, err = b.delegate.WrapInCreate(c, asValue, r.URL)
if err != nil {
return true, err

ファイルの表示

@ -515,7 +515,7 @@ func (w FederatingWrappedCallbacks) accept(c context.Context, a vocab.ActivitySt
return fmt.Errorf("cannot handle federated create: object is neither a value nor IRI")
}
// Ensure it is a Follow.
if !streams.ActivityStreamsFollowIsExtendedBy(t) {
if !streams.IsOrExtendsActivityStreamsFollow(t) {
continue
}
follow, ok := t.(Activity)
@ -567,7 +567,7 @@ func (w FederatingWrappedCallbacks) accept(c context.Context, a vocab.ActivitySt
if err != nil {
return err
}
if !streams.ActivityStreamsFollowIsExtendedBy(t) {
if !streams.IsOrExtendsActivityStreamsFollow(t) {
return fmt.Errorf("peer gave an Accept wrapping a Follow but provided a non-Follow id")
}
follow, ok := t.(Activity)

ファイルの表示

@ -88,7 +88,7 @@ func NewActivityStreamsHandler(authFn AuthenticateFunc, db Database, clock Clock
// Construct the response.
addResponseHeaders(w.Header(), clock, raw)
// Write the response.
if streams.ActivityStreamsTombstoneIsExtendedBy(t) {
if streams.IsOrExtendsActivityStreamsTombstone(t) {
w.WriteHeader(http.StatusGone)
} else {
w.WriteHeader(http.StatusOK)

ファイルの表示

@ -28,6 +28,7 @@ const (
testNoteId2 = "https://example.com/note/2"
testNewActivityIRI = "https://example.com/new/1"
testNewActivityIRI2 = "https://example.com/new/2"
testNewActivityIRI3 = "https://example.com/new/3"
testToIRI = "https://maybe.example.com/to/1"
testToIRI2 = "https://maybe.example.com/to/2"
testCcIRI = "https://maybe.example.com/cc/1"
@ -77,8 +78,10 @@ var (
testErr = errors.New("test error")
// testNote is a test Note from a federated peer.
testFederatedNote vocab.ActivityStreamsNote
// testNote is a test Note owned by this server.
// testMyNote is a test Note owned by this server.
testMyNote vocab.ActivityStreamsNote
// testMyNoteNoId is a test Note owned by this server.
testMyNoteNoId vocab.ActivityStreamsNote
// testMyCreate is a test Create Activity.
testMyCreate vocab.ActivityStreamsCreate
// testCreate is a test Create Activity.
@ -110,6 +113,8 @@ var (
testOrderedCollectionWithFederatedId vocab.ActivityStreamsOrderedCollectionPage
// testMyListen is a test Listen C2S Activity.
testMyListen vocab.ActivityStreamsListen
// testMyListenNoId is a test Listen C2S Activity without an id.
testMyListenNoId vocab.ActivityStreamsListen
// testListen is a test Listen Activity.
testListen vocab.ActivityStreamsListen
// testOrderedCollectionWithFederatedId2 has the second federated
@ -159,6 +164,16 @@ func setupData() {
id.Set(mustParse(testNoteId1))
testMyNote.SetActivityStreamsId(id)
}()
// testMyNoteNoId
func() {
testMyNoteNoId = streams.NewActivityStreamsNote()
name := streams.NewActivityStreamsNameProperty()
name.AppendXMLSchemaString("My Note")
testMyNoteNoId.SetActivityStreamsName(name)
content := streams.NewActivityStreamsContentProperty()
content.AppendXMLSchemaString("This is a simple note of mine.")
testMyNoteNoId.SetActivityStreamsContent(content)
}()
// testMyCreate
func() {
testMyCreate = streams.NewActivityStreamsCreate()
@ -269,6 +284,13 @@ func setupData() {
op.AppendActivityStreamsNote(testMyNote)
testMyListen.SetActivityStreamsObject(op)
}()
// testMyListenNoId
func() {
testMyListenNoId = streams.NewActivityStreamsListen()
op := streams.NewActivityStreamsObjectProperty()
op.AppendActivityStreamsNote(testMyNoteNoId)
testMyListenNoId.SetActivityStreamsObject(op)
}()
// testListen
func() {
testListen = streams.NewActivityStreamsListen()

ファイルの表示

@ -225,14 +225,14 @@ func (a *sideEffectActor) InboxForwarding(c context.Context, inboxIRI *url.URL,
if err != nil {
return err
}
if streams.ActivityStreamsOrderedCollectionIsExtendedBy(t) {
if streams.IsOrExtendsActivityStreamsOrderedCollection(t) {
if im, ok := t.(orderedItemser); ok {
oCol[iri.String()] = im
colIRIs = append(colIRIs, iri)
} else {
a.db.Unlock(c, iri)
}
} else if streams.ActivityStreamsCollectionIsExtendedBy(t) {
} else if streams.IsOrExtendsActivityStreamsCollection(t) {
if im, ok := t.(itemser); ok {
col[iri.String()] = im
colIRIs = append(colIRIs, iri)
@ -338,7 +338,7 @@ func (a *sideEffectActor) AddNewIds(c context.Context, activity Activity) error
activityId := streams.NewActivityStreamsIdProperty()
activityId.Set(id)
activity.SetActivityStreamsId(activityId)
if streams.ActivityStreamsCreateIsExtendedBy(activity) {
if streams.IsOrExtendsActivityStreamsCreate(activity) {
o, ok := activity.(objecter)
if !ok {
return fmt.Errorf("cannot add new id for Create: %T has no object property", activity)

ファイルの表示

@ -989,14 +989,88 @@ func TestPostOutbox(t *testing.T) {
// TestAddNewIds ensures that new 'id' properties are set on an activity and all
// of its 'object' property values if it is a Create activity.
func TestAddNewIds(t *testing.T) {
t.Run("AddsIdToActivity", func(t *testing.T) {
t.Errorf("Not yet implemented.")
ctx := context.Background()
setupFn := func(ctl *gomock.Controller) (c *MockCommonBehavior, fp *MockFederatingProtocol, sp *MockSocialProtocol, db *MockDatabase, cl *MockClock, a DelegateActor) {
setupData()
c = NewMockCommonBehavior(ctl)
fp = NewMockFederatingProtocol(ctl)
sp = NewMockSocialProtocol(ctl)
db = NewMockDatabase(ctl)
cl = NewMockClock(ctl)
a = &sideEffectActor{
common: c,
s2s: fp,
c2s: sp,
db: db,
clock: cl,
}
return
}
t.Run("AddsIdToActivityWithoutId", func(t *testing.T) {
// Setup
ctl := gomock.NewController(t)
defer ctl.Finish()
_, _, _, db, _, a := setupFn(ctl)
db.EXPECT().NewId(ctx, testMyListenNoId).Return(mustParse(testNewActivityIRI2), nil)
// Run
err := a.AddNewIds(ctx, testMyListenNoId)
// Verify
assertEqual(t, err, nil)
resultId := testMyListenNoId.GetActivityStreamsId()
assertNotEqual(t, resultId, nil)
assertEqual(t, resultId.Get().String(), mustParse(testNewActivityIRI2).String())
})
t.Run("AddsIdToActivityWithId", func(t *testing.T) {
// Setup
ctl := gomock.NewController(t)
defer ctl.Finish()
_, _, _, db, _, a := setupFn(ctl)
db.EXPECT().NewId(ctx, testMyListen).Return(mustParse(testNewActivityIRI2), nil)
// Run
err := a.AddNewIds(ctx, testMyListen)
// Verify
assertEqual(t, err, nil)
resultId := testMyListen.GetActivityStreamsId()
assertNotEqual(t, resultId, nil)
assertEqual(t, resultId.Get().String(), mustParse(testNewActivityIRI2).String())
})
t.Run("AddsIdsToObjectsIfCreateActivity", func(t *testing.T) {
t.Errorf("Not yet implemented.")
// Setup
ctl := gomock.NewController(t)
defer ctl.Finish()
_, _, _, db, _, a := setupFn(ctl)
db.EXPECT().NewId(ctx, testMyCreate).Return(mustParse(testNewActivityIRI2), nil)
db.EXPECT().NewId(ctx, testMyNote).Return(mustParse(testNewActivityIRI3), nil)
// Run
err := a.AddNewIds(ctx, testMyCreate)
// Verify
assertEqual(t, err, nil)
op := testMyCreate.GetActivityStreamsObject()
assertNotEqual(t, op, nil)
assertEqual(t, op.Len(), 1)
n := op.At(0).GetActivityStreamsNote()
assertNotEqual(t, n, nil)
noteId := n.GetActivityStreamsId()
assertNotEqual(t, noteId, nil)
assertEqual(t, noteId.Get().String(), mustParse(testNewActivityIRI3).String())
})
t.Run("DoesNotAddIdsToObjectsIfNotCreateActivity", func(t *testing.T) {
t.Errorf("Not yet implemented.")
// Setup
ctl := gomock.NewController(t)
defer ctl.Finish()
_, _, _, db, _, a := setupFn(ctl)
db.EXPECT().NewId(ctx, testMyListenNoId).Return(mustParse(testNewActivityIRI2), nil)
// Run
err := a.AddNewIds(ctx, testMyListenNoId)
// Verify
assertEqual(t, err, nil)
op := testMyListenNoId.GetActivityStreamsObject()
assertNotEqual(t, op, nil)
assertEqual(t, op.Len(), 1)
n := op.At(0).GetActivityStreamsNote()
assertNotEqual(t, n, nil)
noteId := n.GetActivityStreamsId()
assertEqual(t, noteId, nil)
})
}

ファイルの表示

@ -16,12 +16,6 @@ import (
"time"
)
// IsAnActivityType returns true if the ActivityStreams value is an Activity or
// extends from the Activity type.
func IsAnActivityType(value vocab.Type) bool {
return value.GetTypeName() == streams.ActivityStreamsActivityName || streams.ActivityStreamsActivityIsExtendedBy(value)
}
var (
// ErrObjectRequired indicates the activity needs its object property
// set. Can be returned by DelegateActor's PostInbox or PostOutbox so a
@ -875,7 +869,7 @@ func add(c context.Context,
if err != nil {
return err
}
if streams.ActivityStreamsOrderedCollectionIsExtendedBy(tp) {
if streams.IsOrExtendsActivityStreamsOrderedCollection(tp) {
oi, ok := tp.(orderedItemser)
if !ok {
return fmt.Errorf("type extending from OrderedCollection cannot convert to orderedItemser interface")
@ -888,7 +882,7 @@ func add(c context.Context,
for _, objId := range opIds {
oiProp.AppendIRI(objId)
}
} else if streams.ActivityStreamsCollectionIsExtendedBy(tp) {
} else if streams.IsOrExtendsActivityStreamsCollection(tp) {
i, ok := tp.(itemser)
if !ok {
return fmt.Errorf("type extending from Collection cannot convert to itemser interface")
@ -956,7 +950,7 @@ func remove(c context.Context,
if err != nil {
return err
}
if streams.ActivityStreamsOrderedCollectionIsExtendedBy(tp) {
if streams.IsOrExtendsActivityStreamsOrderedCollection(tp) {
oi, ok := tp.(orderedItemser)
if !ok {
return fmt.Errorf("type extending from OrderedCollection cannot convert to orderedItemser interface")
@ -975,7 +969,7 @@ func remove(c context.Context,
}
}
}
} else if streams.ActivityStreamsCollectionIsExtendedBy(tp) {
} else if streams.IsOrExtendsActivityStreamsCollection(tp) {
i, ok := tp.(itemser)
if !ok {
return fmt.Errorf("type extending from Collection cannot convert to itemser interface")