Skip to content

Commit

Permalink
CollectionsQuery #27 Now responds correctly to record not found
Browse files Browse the repository at this point in the history
  • Loading branch information
mzeroopenreserve committed Jan 22, 2023
1 parent bfdb8cf commit 000d2ed
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 17 deletions.
12 changes: 6 additions & 6 deletions go/applications/dwn/service/api/collections/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ func CollectionsQuery(ctx context.Context, collSvcClient services.CollectionServ

// First, make sure authorizations are valid and correct for this message
if !model.VerifyAuthorization(message) {
messageResultObj.Status = model.ResponseStatus{Code: http.StatusBadRequest, Detail: "Unable to verify authorization(s)."}
messageResultObj.Status = model.ResponseStatus{Code: http.StatusUnauthorized, Detail: "Unable to verify authorization(s)."}
return messageResultObj
}

// Next, find the schema and make sure it has been registered
schemaUri := message.Descriptor.Schema
schemaUri := message.Descriptor.Filter.Schema
if schemaUri == "" {
messageResultObj.Status = model.ResponseStatus{Code: http.StatusBadRequest, Detail: "Schema URI is required for a CollectionsWrite"}
messageResultObj.Status = model.ResponseStatus{Code: http.StatusBadRequest, Detail: "Schema URI is required for a CollectionsQuery"}
return messageResultObj
}

// Is this for a specific record, or all records since context?
if message.RecordID == "" {
if message.Descriptor.Filter.RecordID == "" {
// TODO: For now we are only going to allow single message access
messageResultObj.Status = model.ResponseStatus{Code: http.StatusBadRequest, Detail: "TODO: For now we are only going to allow single message access"}
return messageResultObj
Expand All @@ -40,8 +40,8 @@ func CollectionsQuery(ctx context.Context, collSvcClient services.CollectionServ
// Get the collection item
req := services.FindCollectionRequest{
QueryType: services.QueryType_SINGLE_COLLECTION_BY_ID_SCHEMA_URI,
RecordId: message.RecordID,
SchemaURI: message.Descriptor.Schema,
RecordId: message.Descriptor.Filter.RecordID,
SchemaURI: message.Descriptor.Filter.Schema,
RequestorDID: message.Processing.AuthorDID,
}

Expand Down
16 changes: 8 additions & 8 deletions go/model/request_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ type Descriptor struct {
Filter CollectionsQueryFilter `json:"filter,omitempty"`

// CollectionsWrite, Delete, Commit per https://identity.foundation/decentralized-web-node/spec/#collectionswrite
ParentID string `json:"parentId,omitempty" bson:"parent_id"`
Protocol string `json:"protocol,omitempty" bson:"protocol"`
ProtocolVersion string `json:"protocolVersion,omitempty" bson:"protocol_version"`
Schema string `json:"schema,omitempty" bson:"schema"`
CommitStrategy string `json:"commitStrategy,omitempty" bson:"commit_strategy"`
Published bool `json:"published,omitempty" bson:"published"`
DateCreated time.Time `json:"dateCreated,omitempty" bson:"date_created"`
DatePublished time.Time `json:"datePublished,omitempty" bson:"date_published"`
ParentID string `json:"parentId,omitempty" bson:"parent_id"`
Protocol string `json:"protocol,omitempty" bson:"protocol"`
ProtocolVersion string `json:"protocolVersion,omitempty" bson:"protocol_version"`
Schema string `json:"schema,omitempty" bson:"schema"`
CommitStrategy string `json:"commitStrategy,omitempty" bson:"commit_strategy"`
Published bool `json:"published,omitempty" bson:"published"`
DateCreated time.Time `json:"dateCreated,omitempty" bson:"date_created"`
DatePublished *time.Time `json:"datePublished,omitempty" bson:"date_published"`
}

type MessageProcessing struct {
Expand Down
79 changes: 79 additions & 0 deletions integration-tests/collections/query_collection_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
package collections_test

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/json"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/openreserveio/dwn/go/model"
"github.com/openreserveio/dwn/integration-tests/testutils"
"net/http"
)

var _ = Describe("Query For A Collection", func() {

authorPrivateKey, _ := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
authorPublicKey := authorPrivateKey.PublicKey
authorDID, _ := testutils.CreateKeyDID(&authorPublicKey)

recipientPrivateKey, _ := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
recipientPublicKey := recipientPrivateKey.PublicKey
recipientDID, _ := testutils.CreateKeyDID(&recipientPublicKey)

Describe("Query for a collection that doesn't exist", func() {

descriptor := model.Descriptor{
Method: model.METHOD_COLLECTIONS_QUERY,
Filter: model.CollectionsQueryFilter{
RecordID: "DOES NOT EXIST",
Schema: "https://openreserve.io/schemas/test.json",
},
}

messageProcessing := model.MessageProcessing{
Nonce: uuid.NewString(),
AuthorDID: authorDID,
RecipientDID: recipientDID,
}

message := model.Message{
ContextID: "",
Processing: messageProcessing,
Descriptor: descriptor,
}

authorization := model.CreateAuthorization(&message, *authorPrivateKey)
attestation := model.CreateAttestation(&message, *authorPrivateKey)
message.Attestation = attestation
message.Authorization = authorization

It("Queries the DWN for an entry that does not exist", func() {

ro := model.RequestObject{}
ro.Messages = append(ro.Messages, message)

res, err := resty.New().R().
SetBody(ro).
SetHeader("Content-Type", "application/json").
Post("http://localhost:8080/")

Expect(err).To(BeNil())
Expect(res).ToNot(BeNil())
Expect(res.StatusCode()).To(Equal(http.StatusOK))

var responseObject model.ResponseObject
err = json.Unmarshal(res.Body(), &responseObject)
Expect(err).To(BeNil())

Expect(responseObject.Status.Code).To(Equal(http.StatusOK))
Expect(len(responseObject.Replies)).To(Equal(1))
Expect(responseObject.Replies[0].Status.Code).To(Equal(http.StatusNotFound))

})

})

})
6 changes: 3 additions & 3 deletions integration-tests/collections/write_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var _ = Describe("Write Collection", func() {
CommitStrategy: "",
Published: false,
DateCreated: time.Now(),
DatePublished: time.Now(),
DatePublished: nil,
}

processing := model.MessageProcessing{
Expand Down Expand Up @@ -110,7 +110,7 @@ var _ = Describe("Write Collection", func() {
CommitStrategy: "",
Published: false,
DateCreated: time.Now(),
DatePublished: time.Now(),
DatePublished: nil,
}

secondProcessing := model.MessageProcessing{
Expand Down Expand Up @@ -170,7 +170,7 @@ var _ = Describe("Write Collection", func() {
CommitStrategy: "",
Published: false,
DateCreated: time.Now(),
DatePublished: time.Now(),
DatePublished: nil,
}

commitProcessing := model.MessageProcessing{
Expand Down

0 comments on commit 000d2ed

Please sign in to comment.