Skip to content

Commit

Permalink
add a way to test refresh tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi committed Oct 13, 2023
1 parent 0cc6bc5 commit aad9ae9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/@uppy/companion-client/src/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ export default class Provider extends RequestClient {
await this.#refreshingTokenPromise

try {
// throw Object.assign(new Error(), { isAuthError: true }) // testing simulate access token expired (to refresh token)
// A better way to test this is for example with Google Drive:
// to test simulate access token expired (leading to a token token refresh),
// see mockAccessTokenExpiredError in companion/drive.
// If you want to test refresh token *and* access token invalid, do this for example with Google Drive:
// While uploading, go to your google account settings,
// "Third-party apps & services", then click "Companion" and "Remove access".

Expand Down
19 changes: 19 additions & 0 deletions packages/@uppy/companion/src/server/provider/drive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const logger = require('../../logger')
const { VIRTUAL_SHARED_DIR, adaptData, isShortcut, isGsuiteFile, getGsuiteExportType } = require('./adapter')
const { withProviderErrorHandling } = require('../providerErrors')
const { prepareStream } = require('../../helpers/utils')
const { ProviderAuthError } = require('../error')


// For testing refresh token:
// first run a download with mockAccessTokenExpiredError = true
// then when you want to test expiry, set to mockAccessTokenExpiredError to the logged access token
// This will trigger companion/nodemon to restart, and it will respond with a simulated invalid token response
const mockAccessTokenExpiredError = undefined
// const mockAccessTokenExpiredError = true
// const mockAccessTokenExpiredError = ''

const DRIVE_FILE_FIELDS = 'kind,id,imageMediaMetadata,name,mimeType,ownedByMe,size,modifiedTime,iconLink,thumbnailLink,teamDriveId,videoMediaMetadata,shortcutDetails(targetId,targetMimeType)'
const DRIVE_FILES_FIELDS = `kind,nextPageToken,incompleteSearch,files(${DRIVE_FILE_FIELDS})`
Expand Down Expand Up @@ -117,6 +127,15 @@ class Drive extends Provider {
}

async download ({ id: idIn, token }) {
if (mockAccessTokenExpiredError != null) {
logger.warn(`Access token: ${token}`)

if (mockAccessTokenExpiredError === token) {
logger.warn('Mocking expired access token!')
throw new ProviderAuthError()
}
}

return this.#withErrorHandling('provider.drive.download.error', async () => {
const client = getClient({ token })

Expand Down

0 comments on commit aad9ae9

Please sign in to comment.