diff --git a/server/list.js b/server/list.js index 3eac74e4..44dfd624 100644 --- a/server/list.js +++ b/server/list.js @@ -108,7 +108,7 @@ function getOptions(id) { if (driveType === 'folder') { return { ...exports.commonListOptions.folder, - q: id.map((id) => `'${id}' in parents`).join(' or '), + q: `(${id.map((id) => `'${id}' in parents`).join(' or ')}) AND trashed = false`, fields } } diff --git a/test/unit/list.test.js b/test/unit/list.test.js index 25d08c5a..b971f6a7 100644 --- a/test/unit/list.test.js +++ b/test/unit/list.test.js @@ -1,6 +1,8 @@ 'use strict' const {expect} = require('chai') +const {google} = require('googleapis') +const sinon = require('sinon') const list = require('../../server/list') const {allFilenames} = require('../utils') @@ -58,3 +60,48 @@ describe('Filename Listing', () => { expect(filenames).to.include(...allFilenames) }) }) + +describe('File Filtering', () => { + it('should filter out trashed documents in team drive type', () => { + const options = list.commonListOptions.team + expect(options.q).to.include('trashed = false') + }) + + describe('when fetching files from shared folder', () => { + let listFilesSpy + let originalDriveType + + beforeAll(() => { + originalDriveType = process.env.DRIVE_TYPE + process.env.DRIVE_TYPE = 'folder' + + listFilesSpy = sinon.spy(() => ({ + data: { + files: [], + nextPageToken: null + } + })) + + google.drive = () => { + return { + files: { + list: listFilesSpy + } + } + } + }) + + afterAll(() => { + process.env.DRIVE_TYPE = originalDriveType + }) + + it('should include trashed = false in query for folder type', async () => { + await list.getTree() + + const callArgs = listFilesSpy.args[0] + if (callArgs && callArgs[0] && callArgs[0].q) { + expect(callArgs[0].q).to.include('trashed = false') + } + }) + }) +})