Skip to content

Commit

Permalink
wip: adding e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juligasa committed Jul 31, 2023
1 parent df28c8f commit 325a2d6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
17 changes: 11 additions & 6 deletions backend/daemon/api/documents/v1alpha/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (api *Server) GetPublication(ctx context.Context, in *documents.GetPublicat
eid := hyper.EntityID("hd://d/" + in.DocumentId)
version := hyper.Version(in.Version)

pub, err := api.loadPublication(ctx, eid, version)
pub, err := api.loadPublication(ctx, eid, version, in.TrustedOnly)
if err == nil {
return pub, nil
}
Expand All @@ -362,10 +362,10 @@ func (api *Server) GetPublication(ctx context.Context, in *documents.GetPublicat
return nil, status.Errorf(codes.NotFound, "failed to discover object %q at version %q", eid, version)
}

return api.loadPublication(ctx, eid, version)
return api.loadPublication(ctx, eid, version, in.TrustedOnly)
}

func (api *Server) loadPublication(ctx context.Context, docid hyper.EntityID, version hyper.Version) (docpb *documents.Publication, err error) {
func (api *Server) loadPublication(ctx context.Context, docid hyper.EntityID, version hyper.Version, trustedOnly bool) (docpb *documents.Publication, err error) {
var entity *hyper.Entity
if version != "" {
heads, err := hyper.Version(version).Parse()
Expand All @@ -378,7 +378,11 @@ func (api *Server) loadPublication(ctx context.Context, docid hyper.EntityID, ve
return nil, err
}
} else {
entity, err = api.blobs.LoadEntity(ctx, docid)
if trustedOnly {
entity, err = api.blobs.LoadTrustedEntity(ctx, docid)
} else {
entity, err = api.blobs.LoadEntity(ctx, docid)
}
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -443,8 +447,9 @@ func (api *Server) ListPublications(ctx context.Context, in *documents.ListPubli
for _, e := range entities {
docid := e.TrimPrefix("hd://d/")
pub, err := api.GetPublication(ctx, &documents.GetPublicationRequest{
DocumentId: docid,
LocalOnly: true,
DocumentId: docid,
LocalOnly: true,
TrustedOnly: in.TrustedOnly,
})
if err != nil {
continue
Expand Down
78 changes: 78 additions & 0 deletions backend/daemon/daemon_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,84 @@ func TestSite(t *testing.T) {
require.Error(t, err)
}

func TestTrustedChanges(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

t.Cleanup(func() {
cancel()
})

trusted := makeTestApp(t, "alice", makeTestConfig(t), true)
untrusted := makeTestApp(t, "bob", makeTestConfig(t), true)

// Both peers connect connect to each other, to exchange documents. Does not mean they trust each other.
_, err := trusted.RPC.Networking.Connect(ctx, &networking.ConnectRequest{Addrs: getAddrs(t, untrusted)})
require.NoError(t, err)

// Create a document.
sharedDocument := publishDocument(t, ctx, trusted)

// Sync the document with the untusted peer so it can modify it.
_, err = untrusted.RPC.Daemon.ForceSync(ctx, &daemon.ForceSyncRequest{})
require.NoError(t, err)
var publicationList *documents.ListPublicationsResponse
require.Eventually(t, func() bool {
// List all documents which by default includes untrusted changes.
publicationList, err = untrusted.RPC.Documents.ListPublications(ctx, &documents.ListPublicationsRequest{})
require.NoError(t, err)
return len(publicationList.Publications) == 1
}, 1*time.Second, 100*time.Millisecond, "peer should have synced the document")

// Check that the received version is the one the initial author created.
require.Equal(t, sharedDocument.Version, publicationList.Publications[0].Version)
require.Equal(t, sharedDocument.Document.Author, publicationList.Publications[0].Document.Author)
require.Equal(t, sharedDocument.Document.Id, publicationList.Publications[0].Document.Id)

// Add an untrusted change
const anotherTitle = "New Document title leading to a new version"
newVersion := updateDocumenTitle(t, ctx, untrusted, sharedDocument.Document.Id, anotherTitle)
require.Equal(t, sharedDocument.Document.Id, newVersion.Document.Id)

// Send the document back to the creator which the new untrusted changes.
_, err = trusted.RPC.Daemon.ForceSync(ctx, &daemon.ForceSyncRequest{})
require.NoError(t, err)

require.Eventually(t, func() bool {
// List all documents which by default includes untrusted changes.
publicationList, err = trusted.RPC.Documents.ListPublications(ctx, &documents.ListPublicationsRequest{})
require.NoError(t, err)
return len(publicationList.Publications) == 1
}, 1*time.Second, 100*time.Millisecond, "peer should have synced the document")

// Check that the version is the latest one (untrusted).
require.Equal(t, newVersion.Version, publicationList.Publications[0].Version)
require.Equal(t, newVersion.Document.Author, publicationList.Publications[0].Document.Author)
require.Equal(t, newVersion.Document.Id, publicationList.Publications[0].Document.Id)

// Now ask for trusted changes only.
publicationList, err = trusted.RPC.Documents.ListPublications(ctx, &documents.ListPublicationsRequest{
TrustedOnly: true,
})
require.NoError(t, err)
require.Len(t, publicationList.Publications, 1, "same document, different version")

// Check that the version is the initial one, without any untrusted changes.
require.Equal(t, sharedDocument.Version, publicationList.Publications[0].Version)
require.Equal(t, sharedDocument.Document.Author, publicationList.Publications[0].Document.Author)
require.Equal(t, sharedDocument.Document.Id, publicationList.Publications[0].Document.Id)

// But the untrusted peer (last editor) should get the latest version since it was him
// the one who wrote the latest changes and hi trust himself by default.
publicationList, err = untrusted.RPC.Documents.ListPublications(ctx, &documents.ListPublicationsRequest{
TrustedOnly: true,
})
require.NoError(t, err)
require.Len(t, publicationList.Publications, 1, "same document, different version")
require.Equal(t, newVersion.Version, publicationList.Publications[0].Version)
require.Equal(t, newVersion.Document.Author, publicationList.Publications[0].Document.Author)
require.Equal(t, newVersion.Document.Id, publicationList.Publications[0].Document.Id)
}

func TestGateway(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
Expand Down

0 comments on commit 325a2d6

Please sign in to comment.