Skip to content

Latest commit

 

History

History
423 lines (346 loc) · 14 KB

testDoc.md

File metadata and controls

423 lines (346 loc) · 14 KB

TOC

decorated-google-drive:

initializing

should not throw an error.

function init() {
  drive = driveZ(google, request, keys, tokens);
}
init.should.not.throw();

drive should not be undefined.

assert.ok(!!drive);

drive.x should be an object.

assert.equal(typeof(drive.x), 'object');

drive.x.aboutMe

should return the test users email address.

return drive.x.aboutMe().then((info) => {
  assert.ok(info.user.emailAddress.endsWith("@gmail.com"));
});

should return a storageQuota object with properties limit, usage.

return drive.x.aboutMe().then((info) => {
  const quota = info.storageQuota;
  assert.ok(typeof(quota) === 'object');
  quota.should.have.properties('limit', 'usage');
});

drive.about.get still works, as well, and the outputs match.

return Promise.all([
  drive.x.aboutMe(),
  pify(drive.about.get)({ fields: 'user, storageQuota' })
]).then(([A, B]) => {
  A.should.deepEqual(B);
});

drive.x.appDataFolder.upload2: upload a string to appDataFolder

uploading the string to appDataFolder file myaccount should resolve with expected file metadata.

uploadResult.should.be.type("object");
uploadResult.should.have.properties('id', 'name', 'mimeType', 'isNew', 'parents');
uploadResult.name.should.equal("myaccount");
uploadResult.mimeType.should.equal("text/plain");
uploadResult.isNew.should.equal(true);

drive.x.appDataFolder.searcher should report there is exactly one myaccount file in the folder and it should match upload file id.

drive.x.appDataFolder.searcher({})('appDataFolder', 'myaccount').then((found) => {
  found.should.have.properties('parent', 'name', 'files');
  found.files.length.should.equal(1);
  found.files[0].id.should.equal(uploadResult.id);
});

drive.x.appDataFolder.contents should resolve to contents Hello-World-Test-1-2-3.

drive.x.appDataFolder.contents(uploadResult.id).then((contents) => {
  contents.should.be.type("string");
  contents.should.equal('Hello-World-Test-1-2-3');
});

drive.x.upload2: upload a file README.md to Drive folder /path/to/test/Files

uploading the README.md file to /path/to/test/Files/README.md should resolve with expected file metadata.

uploadResult.should.be.type("object");
uploadResult.should.have.properties('id', 'name', 'mimeType', 'isNew', 'parents');
uploadResult.id.length.should.be.above(1);
uploadResult.name.should.equal("README.md");
uploadResult.mimeType.should.equal("text/plain");
uploadResult.isNew.should.equal(true);
assert.ok(Array.isArray(uploadResult.parents), "uploadResult.parents should be an Array");
assert.ok(uploadResult.parents.length === 1, "uploadResult.parents.length should be 1");
uploadResult.parents[0].should.be.type('string');

the parents[0] folder should have the name 'Files'.

drive.files.get({ fileId: uploadResult.parents[0] }, function (e, data) {
  if (e) throw e;
  data.name.should.equal('Files');
  done();
});

searching the parents[0] folder for README.md find a file with matching id.

return (drive.x.searcher({ trashed: false })(uploadResult.parents[0], 'README.md')
  .then((info) => {
    assert.ok(Array.isArray(info.files), "info.files is array");
    info.files[0].name.should.equal('README.md');
    info.files[0].id.should.equal(uploadResult.id);
  })
);

after drive.x.upload2

searching root for anything should yield folder 'path' with .isFolder===true.

return (drive.x.searcher({ trashed: false })('root')
  .then((info) => {
    assert.ok(Array.isArray(info.files), "info.files is array");
    assert.ok(info.files.some((f) => ((f.mimeType === folderMimeType) && (f.name === 'path') && f.isFolder)), "info.files contains folder 'path'");
    assert.ok(!info.isNew, "info.isNew should be falsey or undefined");
  })
);

searching root for folders should yield folder 'path' with .isFolder===true.

return (drive.x.searcher({
  trashed: false,
  isFolder: true
})('root').then((info) => {
  assert.ok(Array.isArray(info.files), "info.files is array");
  assert.ok(info.files.some((f) => ((f.mimeType === folderMimeType) && (f.name === 'path') && f.isFolder)), "info.files contains folder 'path'");
  assert.ok(!info.isNew, "info.isNew should be falsey or undefined");
}));

searching root for non-folders should be empty .

return (drive.x.searcher({
  trashed: false,
  isFolder: false
})('root').then((info) => {
  assert.ok(Array.isArray(info.files), "info.files is array");
  assert.ok(info.files.length === 0, "info.files should be empty");
}));

searching all folders for any non-trashed file should be non-empty and include file README.md in results .

return (drive.x.searcher({
  trashed: false,
  isFolder: false
})().then((info) => {
  assert.ok(Array.isArray(info.files), "info.files is array");
  assert.ok(info.files.some((f) => ((f.name === 'README.md') && (!f.isFolder) && (!f.isNew))));
  assert.ok(info.files.length > 0, "info.files should be non-empty");
}));

searching all folders or a file with appProperties: { 'role': 'documentation' } should be empty .

return (drive.x.searcher({
  appProperties: {
    'role': 'documentation'
  }
})().then((info) => {
  assert.ok(info.files.length === 0, "info.files should be empty");
}));

checking existence of /path/to/test/Files/README.md with drive.x.findPath should yield expected file metadata.

return drive.x.findPath("/path/to/test/Files/README.md").then((info) => {
  info.should.have.properties('id', 'name', 'mimeType', 'isFolder');
  info.isFolder.should.equal(false);
  info.id.length.should.be.above(1);
  info.name.should.equal("README.md");
  info.mimeType.should.equal("text/plain");
  assert.ok(!info.isNew, "info.isNew should be falsey or undefined");
});

checking existence of /path/to/test should yield expected folder metadata.

return drive.x.findPath("/path/to/test").then((info) => {
  info.should.have.properties('id', 'name', 'mimeType', 'isFolder');
  info.isFolder.should.equal(true);
  info.id.length.should.be.above(1);
  info.name.should.equal("test");
  info.mimeType.should.equal(folderMimeType);
  assert.ok(!info.isNew, "info.isNew should be falsey or undefined");
});

checking existence on wrong path should throw Boom.notfound.

// note: folder names seem to ignore upper/lower case
return drive.x.findPath("/not/the/path/to/test/Files/README.md").then(
  () => { throw new Error("unexpected success"); },
  (e) => { if (e.isBoom && e.typeof === Boom.notFound) return Promise.resolve("ok, got Boom 404");throw e; }
);

downloading content with drive.x.download should yield contents string including 'License: MIT'.

return drive.x.download("/path/to/test/Files/README.md").then((contents) => {
  contents.should.be.type('string');
  assert.ok(contents.includes("License: MIT"));
});

updating README.md appProperties to {'role': 'documentation'} should succeed.

return (drive.x.findPath("/path/to/test/Files/README.md")
  .then((file) => (drive.x.updateMetadata(file.id, { appProperties: { role: 'documentation' }, description: "read this first" })))
  .then((info) => {
    // checks response from drive.x.updateMetadata
    info.appProperties.role.should.equal('documentation');
    info.description.should.equal('read this first');
    return pify(drive.files.get)({ fileId: info.id, fields: "id,name,description,appProperties" });
  }).then((info) => {
    // checks response from subsequent drive.files.get
    info.description.should.equal("read this first");
    info.appProperties.role.should.equal('documentation');
  })
);

searching all folders or a file with appProperties: { 'role': 'documentation' } should find README.md .

return (drive.x.searcher({
  appProperties: {
    'role': 'documentation'
  }
})().then((info) => {
  assert.ok(info.files.length === 1, "info.files should contain one file");
  assert.ok(info.files[0].name === "README.md", "info.files[0].name should be README.md");
  info.files[0].appProperties.role.should.equal('documentation');
  assert.ok(typeof(info.files[0].description) === 'undefined', "info.files[0].description should be undefined");
}));

drive.x.upload2 uploading the file again with {clobber:false} will throw Boom.conflict error because file already exists.

return drive.x.upload2({
  folderPath: '/path/to/test/Files/',
  name: 'README.md',
  stream: fs.createReadStream("./README.md"),
  mimeType: 'text/plain',
  createPath: true,
  clobber: false
}).then(() => { throw new Error("unexpected success"); }, (e) => { if (e.isBoom && e.typeof === Boom.conflict) return Promise.resolve('ok');throw e; });

drive.x.upload2: upload test/test.zip to Drive folder /path/to/test/Files

uploading the test.zip file to /path/to/test/Files/test.zip should resolve with expected file metadata and md5 match.

uploadResult.should.be.type("object");
uploadResult.should.have.properties('id', 'name', 'mimeType', 'md5Checksum', 'ourMD5', 'isNew', 'isFolder');
uploadResult.id.length.should.be.above(1);
uploadResult.name.should.equal("test.zip");
uploadResult.mimeType.should.equal("application/zip");
uploadResult.ourMD5.should.equal(uploadResult.md5Checksum);
uploadResult.ourMD5.should.equal(testMD5);
uploadResult.isNew.should.equal(true);
uploadResult.isFolder.should.equal(false);

create folder /path/to/test2

the resolved folder object should be an object with props id, name, mimeType, isFolder .

test2Folder.should.be.type("object");
test2Folder.should.have.properties('id', 'name', 'mimeType', 'isFolder', 'isNew', 'parents');

the folder.id should be a string with length >4 .

test2Folder.id.should.be.type('string');
test2Folder.id.length.should.be.above(4);

the folder.name should be "test2" .

test2Folder.name.should.equal('test2');

the mimeType should be application/vnd.google-apps.folder .

test2Folder.mimeType.should.equal(folderMimeType);

isNew should be true .

test2Folder.isNew.should.equal(true);

isFolder should be true .

test2Folder.isFolder.should.equal(true);

parents should be an Array containing 1 string .

assert.ok(Array.isArray(test2Folder.parents), "test2Folder.parents should be an Array");
assert.ok(test2Folder.parents.length === 1, "test2Folder.parents.length should be 1");
test2Folder.parents[0].should.be.type('string');

use folderId of /path/to/test2 to upload test.zip

uploading the test.zip file to /path/to/test2/test.zip should resolve with expected file metadata and md5 match.

uploadResult.should.be.type("object");
uploadResult.should.have.properties('id', 'name', 'mimeType', 'md5Checksum', 'ourMD5');
uploadResult.id.length.should.be.above(1);
uploadResult.name.should.equal("test.zip");
uploadResult.mimeType.should.equal("application/zip");
uploadResult.isNew.should.equal(true);
uploadResult.isFolder.should.equal(false);
uploadResult.ourMD5.should.equal(uploadResult.md5Checksum);
uploadResult.ourMD5.should.equal(testMD5);

cleanup via drive.x.janitor

janitor hopefully deletes the README.md file(s) OK and resolves correctly.

return drive.x.findPath("/path/to/test/Files/README.md").then(janitor).then((response) => {
  response.should.have.properties('deleted');
  assert.ok(response.deleted);
});

drive.x.findPath will throw Boom.notFound if the file was successfully deleted.

return drive.x.findPath("/path/to/test/Files/README.md").then(janitor).then(
  () => { throw new Error("unexpected success"); },
  (e) => { if (e.isBoom && e.typeof === Boom.notFound) return Promise.resolve("ok");throw e; }
);

janitor will throw an error if told to delete an invalid file.

return janitor([{ id: 'invalid' }]).then(
  () => { throw new Error("unexpected success"); },
  (e) => { if (e.toString().includes('not found')) return Promise.resolve('ok');throw e; }
);

janitor should not throw an error if given an empty filelist.

return janitor([]).then((response) => {
  response.should.have.property('deleted');
  assert.ok(!response.deleted);
});

final cleanup: delete the path folder and check non-existence.

return (drive.x.findPath("/path")
  .then(janitor)
  .then(() => (drive.x.findPath("/path")))
  .then(() => { throw new Error("unexpected success"); },
    (e) => { if (e.isBoom && e.typeof === Boom.notFound) return Promise.resolve("ok");throw e; }
  )
);