Skip to content

Commit

Permalink
InitiateUpload unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 7, 2021
1 parent 14a8846 commit 8b89bab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/storage/fs/nextcloud/nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,17 @@ func (nc *StorageDriver) ListFolder(ctx context.Context, ref *provider.Reference

// InitiateUpload as defined in the storage.FS interface
func (nc *StorageDriver) InitiateUpload(ctx context.Context, ref *provider.Reference, uploadLength int64, metadata map[string]string) (map[string]string, error) {
bodyStr, _ := json.Marshal(ref)
type paramsObj struct {
Ref provider.Reference `json:"ref"`
UploadLength int64 `json:"uploadLength"`
Metadata map[string]string `json:"metadata"`
}
bodyObj := &paramsObj{
Ref: *ref,
UploadLength: uploadLength,
Metadata: metadata,
}
bodyStr, _ := json.Marshal(bodyObj)
log := appctx.GetLogger(ctx)
log.Info().Msgf("InitiateUpload %s", bodyStr)

Expand Down
1 change: 1 addition & 0 deletions pkg/storage/fs/nextcloud/nextcloud_server_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ var responses = map[string]Response{
`POST /apps/sciencemesh/~tester/api/Move {"from":{"resource_id":{"storage_id":"storage-id-1","opaque_id":"opaque-id-1"},"path":"/some/old/path"},"to":{"resource_id":{"storage_id":"storage-id-2","opaque_id":"opaque-id-2"},"path":"/some/new/path"}}`: {200, ``, serverStateEmpty},
`POST /apps/sciencemesh/~tester/api/GetMD {"ref":{"resource_id":{"storage_id":"storage-id","opaque_id":"opaque-id"},"path":"/some/path"},"mdKeys":["val1","val2","val3"]}`: {200, `{ "size": 1, "path":"/some/path", "metadata": { "foo": "bar" }, "etag": "in-json-etag", "mimetype": "in-json-mimetype" }`, serverStateEmpty},
`POST /apps/sciencemesh/~tester/api/ListFolder {"ref":{"resource_id":{"storage_id":"storage-id","opaque_id":"opaque-id"},"path":"/some/path"},"mdKeys":["val1","val2","val3"]}`: {200, `[{ "size": 1, "path":"/some/path", "metadata": { "foo": "bar" }, "etag": "in-json-etag", "mimetype": "in-json-mimetype" }]`, serverStateEmpty},
`POST /apps/sciencemesh/~tester/api/InitiateUpload {"ref":{"resource_id":{"storage_id":"storage-id","opaque_id":"opaque-id"},"path":"/some/path"},"uploadLength":12345,"metadata":{"key1":"val1","key2":"val2","key3":"val3"}}`: {200, `{ "not":"sure", "what": "should be", "returned": "here" }`, serverStateEmpty},
}

// GetNextcloudServerMock returns a handler that pretends to be a remote Nextcloud server
Expand Down
36 changes: 36 additions & 0 deletions pkg/storage/fs/nextcloud/nextcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,42 @@ var _ = Describe("Nextcloud", func() {
})

// InitiateUpload(ctx context.Context, ref *provider.Reference, uploadLength int64, metadata map[string]string) (map[string]string, error)
Describe("InitiateUpload", func() {
It("calls the ListFolder endpoint", func() {
nc, _ := nextcloud.NewStorageDriver(&nextcloud.StorageDriverConfig{
EndPoint: "http://mock.com/apps/sciencemesh/",
MockHTTP: true,
})
called := make([]string, 0)
h := nextcloud.GetNextcloudServerMock(&called)
mock, teardown := nextcloud.TestingHTTPClient(h)
defer teardown()
nc.SetHTTPClient(mock)
// https://github.com/cs3org/go-cs3apis/blob/970eec3/cs3/storage/provider/v1beta1/resources.pb.go#L550-L561
ref := &provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: "storage-id",
OpaqueId: "opaque-id",
},
Path: "/some/path",
}
uploadLength := int64(12345)
metadata := map[string]string{
"key1": "val1",
"key2": "val2",
"key3": "val3",
}
results, err := nc.InitiateUpload(ctx, ref, uploadLength, metadata)
Expect(err).ToNot(HaveOccurred())
Expect(results).To(Equal(map[string]string{
"not": "sure",
"what": "should be",
"returned": "here",
}))
Expect(called[0]).To(Equal("POST /apps/sciencemesh/~tester/api/InitiateUpload {\"ref\":{\"resource_id\":{\"storage_id\":\"storage-id\",\"opaque_id\":\"opaque-id\"},\"path\":\"/some/path\"},\"uploadLength\":12345,\"metadata\":{\"key1\":\"val1\",\"key2\":\"val2\",\"key3\":\"val3\"}}"))
})
})

// Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) error
// Download(ctx context.Context, ref *provider.Reference) (io.ReadCloser, error)
// ListRevisions(ctx context.Context, ref *provider.Reference) ([]*provider.FileVersion, error)
Expand Down

0 comments on commit 8b89bab

Please sign in to comment.