Skip to content

Commit

Permalink
@uppy/companion: switch to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Oct 5, 2023
1 parent e1e892b commit ae8a49a
Show file tree
Hide file tree
Showing 18 changed files with 436 additions and 989 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
extends: ['transloadit', 'prettier'],
env: {
es6: true,
jest: true,
jest: false,
node: true,
// extra:
browser: true,
Expand Down
7 changes: 4 additions & 3 deletions packages/@uppy/companion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@
"@types/request": "2.48.8",
"@types/webpack": "^5.28.0",
"@types/ws": "8.5.3",
"expect": "^28.1.3",
"into-stream": "^6.0.0",
"jest": "^29.0.0",
"jest-mock": "^29.0.0",
"nock": "^13.1.3",
"supertest": "6.2.4",
"supertest": "6.3.3",
"typescript": "~4.8"
},
"files": [
Expand All @@ -110,7 +111,7 @@
"deploy": "kubectl apply -f infra/kube/companion-kube.yml",
"prepublishOnly": "yarn run build",
"start": "node ./lib/standalone/start-server.js",
"test": "jest"
"test": "node --test"
},
"engines": {
"node": "^14.19.0 || ^16.15.0 || >=18.0.0"
Expand Down
25 changes: 23 additions & 2 deletions packages/@uppy/companion/test/__tests__/callback.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
const { describe, test } = require('node:test')
const expect = require('expect').default
const mockOauthState = require('../mockoauthstate')()

// eslint-disable-next-line import/order
const request = require('supertest')
const tokenService = require('../../src/server/helpers/jwt')
const { getServer, grantToken } = require('../mockserver')

jest.mock('../../src/server/helpers/oauth-state', () => ({
...jest.requireActual('../../src/server/helpers/oauth-state'),
function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
// eslint-disable-next-line import/no-dynamic-require,global-require
const actual = require(package)
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(actual),
},
resetFn: { __proto__: null, value: replacer.bind(null, actual) },
})
}
}
mock('../../src/server/helpers/oauth-state', (actual) => ({
...actual,
...mockOauthState,
}))

Expand Down
35 changes: 28 additions & 7 deletions packages/@uppy/companion/test/__tests__/companion.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
const { it, describe, it:test, after:afterAll } = require('node:test')
const expect = require('expect').default

const nock = require('nock')
const request = require('supertest')

const mockOauthState = require('../mockoauthstate')
const { version } = require('../../package.json')
const { nockGoogleDownloadFile } = require('../fixtures/drive')

jest.mock('tus-js-client')
jest.mock('../../src/server/helpers/oauth-state', () => ({
...jest.requireActual('../../src/server/helpers/oauth-state'),
...mockOauthState(),

function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
// eslint-disable-next-line import/no-dynamic-require,global-require
const actual = require(package)
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(actual),
},
resetFn: { __proto__: null, value: replacer.bind(null, actual) },
})
}
}
mock('tus-js-client')
mock('../../src/server/helpers/oauth-state', (actual) => ({
...actual,
...mockOauthState,
}))

const fakeLocalhost = 'localhost.com'

jest.mock('node:dns', () => {
const actual = jest.requireActual('node:dns')
mock('node:dns', (actual) => {
return {
...actual,
lookup: (hostname, options, callback) => {
Expand Down
8 changes: 5 additions & 3 deletions packages/@uppy/companion/test/__tests__/cors.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
const { test, describe } = require('node:test')
const expect = require('expect').default
const { cors } = require('../../src/server/middlewares')

function testWithMock ({ corsOptions, get = () => {}, origin = 'https://localhost:1234' } = {}) {
const res = {
get,
getHeader: get,
setHeader: jest.fn(),
end: jest.fn(),
setHeader: Function.prototype,
end: Function.prototype,
}
const req = {
method: 'OPTIONS',
headers: {
origin,
},
}
const next = jest.fn()
const next = Function.prototype
cors(corsOptions)(req, res, next)
return { res }
}
Expand Down
54 changes: 54 additions & 0 deletions packages/@uppy/companion/test/__tests__/credentials.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
const { after: afterAll, test, describe } = require('node:test')
const expect = require('expect').default

function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
// eslint-disable-next-line import/no-dynamic-require,global-require
const actual = require(package)
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(actual),
},
resetFn: { __proto__: null, value: replacer.bind(null, actual) },
})
}
}
// mocking request module used to fetch custom oauth credentials
mock('request', () => {
// eslint-disable-next-line global-require
const { remoteZoomKey, remoteZoomSecret, remoteZoomVerificationToken } = require('../fixtures/zoom').expects

return {
post: (url, options, done) => {
if (url === 'http://localhost:2111/zoom-keys') {
const { body } = options
if (body.provider !== 'zoom') {
return done(new Error('wrong provider'))
}

if (body.parameters !== 'ZOOM-CREDENTIALS-PARAMS') {
return done(new Error('wrong params'))
}

const respBody = {
credentials: {
key: remoteZoomKey,
secret: remoteZoomSecret,
verificationToken: remoteZoomVerificationToken,
},
}
return done(null, { statusCode: 200, body: respBody }, respBody)
}

return done(new Error('unsupported request with mock function'))
},
}
})

const request = require('supertest')
const nock = require('nock')
const tokenService = require('../../src/server/helpers/jwt')
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/companion/test/__tests__/deauthorization.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { after: afterAll, test, describe } = require('node:test')
const nock = require('nock')
const request = require('supertest')
const { getServer } = require('../mockserver')
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/companion/test/__tests__/header-blacklist.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { test, describe } = require('node:test')
const expect = require('expect').default
const headerSanitize = require('../../src/server/header-blacklist')

describe('Header black-list testing', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/@uppy/companion/test/__tests__/http-agent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { after: afterAll, test, describe } = require('node:test')
const expect = require('expect').default
const nock = require('nock')
const { getRedirectEvaluator, FORBIDDEN_IP_ADDRESS, FORBIDDEN_RESOLVED_IP_ADDRESS } = require('../../src/server/helpers/request')
const { getProtectedGot } = require('../../src/server/helpers/request')
Expand All @@ -17,18 +19,16 @@ describe('test getRedirectEvaluator', () => {
},
}

test('when original URL has "https:" as protocol', (done) => {
test('when original URL has "https:" as protocol', () => {
const shouldRedirectHttps = getRedirectEvaluator(httpsURL, true)
expect(shouldRedirectHttps(httpsRedirectResp)).toEqual(true)
expect(shouldRedirectHttps(httpRedirectResp)).toEqual(false)
done()
})

test('when original URL has "http:" as protocol', (done) => {
test('when original URL has "http:" as protocol', () => {
const shouldRedirectHttp = getRedirectEvaluator(httpURL, true)
expect(shouldRedirectHttp(httpRedirectResp)).toEqual(true)
expect(shouldRedirectHttp(httpsRedirectResp)).toEqual(false)
done()
})
})

Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/companion/test/__tests__/logger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { it: test, describe, before: beforeAll } = require('node:test')
const expect = require('expect').default
const chalk = require('chalk')
const logger = require('../../src/server/logger')

Expand Down
21 changes: 20 additions & 1 deletion packages/@uppy/companion/test/__tests__/preauth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
jest.mock('../../src/server/helpers/jwt', () => {
function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(),
},
resetFn: { __proto__: null, value: replacer },
})
}
}
mock('../../src/server/helpers/jwt', () => {
return {
generateEncryptedToken: () => 'dummy token',
verifyEncryptedToken: () => '',
Expand All @@ -7,6 +24,8 @@ jest.mock('../../src/server/helpers/jwt', () => {
}
})

const { test, describe } = require('node:test')
const expect = require('expect').default
const request = require('supertest')
const { getServer } = require('../mockserver')
// the order in which getServer is called matters because, once an env is passed,
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/companion/test/__tests__/provider-manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { beforeEach, describe, it:test } = require('node:test')
const expect = require('expect').default
const providerManager = require('../../src/server/provider')
const { getCompanionOptions } = require('../../src/standalone/helper')
const { setDefaultEnv } = require('../mockserver')
Expand Down
37 changes: 33 additions & 4 deletions packages/@uppy/companion/test/__tests__/providers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
const { before: beforeAll, after: afterAll, describe, it } = require('node:test')
const expect = require('expect').default
const request = require('supertest')
const nock = require('nock')

const mockOauthState = require('../mockoauthstate')
function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(),
},
resetFn: { __proto__: null, value: replacer },
})
}
}

jest.mock('tus-js-client')
jest.mock('../../src/server/helpers/request', () => {
mock('tus-js-client')
mock('../../src/server/helpers/request', () => {
return {
getURLMeta: () => Promise.resolve({ size: 758051 }),
}
})
jest.mock('../../src/server/helpers/oauth-state', () => mockOauthState())
// eslint-disable-next-line global-require
mock('../../src/server/helpers/oauth-state', () => require('../mockoauthstate')())

const test = {
each (iterable) {
return (message, fn) => {
for (const val of iterable) {
it(message, fn.bind(null, val))
}
}
},
}

const fixtures = require('../fixtures')
const { nockGoogleDownloadFile } = require('../fixtures/drive')
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/companion/test/__tests__/subpath.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { it, test } = require('node:test')
const request = require('supertest')
const { getServer } = require('../mockserver')

Expand Down
14 changes: 11 additions & 3 deletions packages/@uppy/companion/test/__tests__/uploader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict'

jest.mock('tus-js-client')
require.cache[require.resolve('tus-js-client')] = require('../__mocks__/tus-js-client')

const intoStream = require('into-stream')
const fs = require('node:fs')
const path = require('node:path')
const { Readable } = require('node:stream')
const { describe, test, after: afterAll } = require('node:test')
const expect = require('expect').default
const jest = require('jest-mock')
const nock = require('nock')

const Uploader = require('../../src/server/Uploader')
Expand All @@ -16,10 +20,14 @@ afterAll(() => {
nock.restore()
})

process.env.COMPANION_DATADIR = './test/output'
process.env.COMPANION_DATADIR = path.join(__dirname, '..', 'output')
process.env.COMPANION_DOMAIN = 'localhost:3020'
const { companionOptions } = standalone()

function intoStream (buffer) {
return Readable.from(buffer, { objectMode: false })
}

describe('uploader with tus protocol', () => {
test('uploader respects uploadUrls', async () => {
const opts = {
Expand Down
Loading

0 comments on commit ae8a49a

Please sign in to comment.