Skip to content

Commit

Permalink
Merge pull request #2931 from OpenNeuroOrg/server/typescript-fixes
Browse files Browse the repository at this point in the history
Migrate server code to TypeScript
  • Loading branch information
nellh authored Nov 2, 2023
2 parents 70f7db8 + 359570e commit 0095eb6
Show file tree
Hide file tree
Showing 95 changed files with 178 additions and 431 deletions.
6 changes: 3 additions & 3 deletions packages/openneuro-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import Keyv from 'keyv'
import KeyvRedis from '@keyv/redis'
import cookieParser from 'cookie-parser'
import * as jwt from './libs/authentication/jwt'
import * as auth from './libs/authentication/states.js'
import { sitemapHandler } from './handlers/sitemap.js'
import { setupPassportAuth } from './libs/authentication/passport.js'
import * as auth from './libs/authentication/states'
import { sitemapHandler } from './handlers/sitemap'
import { setupPassportAuth } from './libs/authentication/passport'
import { redis } from './libs/redis'
import { version } from './lerna.json'
export { Express } from 'express-serve-static-core'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as changelog from '../changelog.js'
import { vi } from 'vitest'
import * as changelog from '../changelog'

vi.mock('ioredis')
vi.mock('../../config.js')
vi.mock('../../config.ts')

describe('changelog editing tools', () => {
describe('findVersion()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { vi } from 'vitest'
import request from 'superagent'
import { createDataset, datasetsFilter, testBlacklist } from '../dataset.js'
import { createDataset, datasetsFilter, testBlacklist } from '../dataset'
import { getDatasetWorker } from '../../libs/datalad-service'
import { connect } from 'mongoose'

// Mock requests to Datalad service
vi.mock('superagent')
vi.mock('ioredis')
vi.mock('../../libs/redis.js')
vi.mock('../../config.js')
vi.mock('../../libs/notifications.js')
vi.mock('../../libs/redis')
vi.mock('../../config.ts')
vi.mock('../../libs/notifications')

describe('dataset model operations', () => {
describe('createDataset()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { vi } from 'vitest'
import {
getDescriptionObject,
repairDescriptionTypes,
appendSeniorAuthor,
} from '../description.js'
} from '../description'

// Mock requests to Datalad service
vi.mock('ioredis')
vi.mock('../../config.js')
vi.mock('../../config.ts')

describe('datalad dataset descriptions', () => {
describe('appendSeniorAuthor', () => {
Expand Down Expand Up @@ -92,6 +93,7 @@ describe('datalad dataset descriptions', () => {
global.fetch = vi.fn()
})
it('returns the parsed dataset_description.json object', async () => {
// @ts-expect-error Fetch mock includes mockResolvedValue
fetch.mockResolvedValue({
json: () =>
Promise.resolve({ Name: 'Balloon Analog Risk-taking Task' }),
Expand All @@ -105,6 +107,7 @@ describe('datalad dataset descriptions', () => {
})
it('handles a corrupted response', async () => {
global.fetch = vi.fn()
// @ts-expect-error Fetch mock includes mockResolvedValue
fetch.mockResolvedValue({
json: () => Promise.reject('JSON could not be parsed'),
headers: {
Expand All @@ -120,6 +123,7 @@ describe('datalad dataset descriptions', () => {
})
it('throws an error when nothing is returned', async () => {
global.fetch = vi.fn()
// @ts-expect-error Fetch mock includes mockResolvedValue
fetch.mockResolvedValue({
json: () => Promise.reject('JSON could not be parsed'),
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import {
encodeFilePath,
decodeFilePath,
Expand All @@ -6,7 +7,7 @@ import {
} from '../files'

vi.mock('ioredis')
vi.mock('../../config.js')
vi.mock('../../config.ts')

const filename = 'sub-01/anat/sub-01_T1w.nii.gz'

Expand Down Expand Up @@ -69,6 +70,7 @@ describe('datalad files', () => {
size: 1945682,
},
]
// @ts-expect-error Test is mocking this
expect(computeTotalSize(mockFileSizes)).toBe(1957206)
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { vi } from 'vitest'
vi.mock('ioredis')
import request from 'superagent'
import { createDataset } from '../dataset.js'
import { createSnapshot } from '../snapshots.js'
import { createDataset } from '../dataset'
import { createSnapshot } from '../snapshots'
import { getDatasetWorker } from '../../libs/datalad-service'
import { connect } from 'mongoose'

Expand All @@ -17,11 +17,11 @@ vi.mock('../../libs/redis.js', () => ({
},
}))
// Mock draft files calls
vi.mock('../draft.js', () => ({
vi.mock('../draft.ts', () => ({
updateDatasetRevision: () => () => Promise.resolve(),
}))
vi.mock('../../config.js')
vi.mock('../../libs/notifications.js')
vi.mock('../../config.ts')
vi.mock('../../libs/notifications.ts')

describe('snapshot model operations', () => {
describe('createSnapshot()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import format from 'date-fns/format'
import { addFileString, commitFiles } from './dataset.js'
import { addFileString, commitFiles } from './dataset'
import { getDatasetWorker } from '../libs/datalad-service'

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import request from 'superagent'
import requestNode from 'request'
import objectHash from 'object-hash'
import { Readable } from 'stream'
import * as Mongoose from 'mongoose'
import config from '../config'
import * as subscriptions from '../handlers/subscriptions.js'
import * as subscriptions from '../handlers/subscriptions'
import { generateDataladCookie } from '../libs/authentication/jwt'
import { redis } from '../libs/redis'
import CacheItem, { CacheType } from '../cache/item'
import { updateDatasetRevision } from './draft.js'
import { updateDatasetRevision } from './draft'
import { fileUrl, getFileName, encodeFilePath, filesUrl } from './files'
import { getAccessionNumber } from '../libs/dataset.js'
import { getAccessionNumber } from '../libs/dataset'
import Dataset from '../models/dataset'
import Metadata from '../models/metadata'
import Permission from '../models/permission'
Expand Down Expand Up @@ -119,7 +120,7 @@ export const cacheDatasetConnection = options => connectionArguments => {
* @param {object} match MongoDB $match aggregate
* @returns {Array<object>} Array of MongoDB aggregate pipelines
*/
const aggregateArraySetup = match => [{ $match: match }]
const aggregateArraySetup = (match): Mongoose.Expression => [{ $match: match }]

/**
* Add any filter steps based on the filterBy options provided
Expand Down Expand Up @@ -157,7 +158,7 @@ export const datasetsFilter = options => match => {
)
return aggregates
}
const filterMatch = {}
const filterMatch: Mongoose.Expression = {}
if ('filterBy' in options) {
const filters = options.filterBy
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
import config from '../config'
import request from 'superagent'
import { redis } from '../libs/redis.js'
import { commitFiles } from './dataset.js'
import { redis } from '../libs/redis'
import { commitFiles } from './dataset'
import { fileUrl } from './files'
import { generateDataladCookie } from '../libs/authentication/jwt'
import { getDatasetWorker } from '../libs/datalad-service'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import request from 'superagent'
import Dataset from '../models/dataset'
import publishDraftUpdate from '../graphql/utils/publish-draft-update.js'
import { getDatasetWorker } from '../libs/datalad-service'

export const getDraftRevision = async datasetId => {
Expand All @@ -19,9 +18,7 @@ export const updateDatasetRevision = (datasetId, gitRef) => {
/**
* Update the revision modified time in a draft on changes
*/
return Dataset.updateOne({ id: datasetId }, { modified: new Date() })
.exec()
.then(() => publishDraftUpdate(datasetId, gitRef))
return Dataset.updateOne({ id: datasetId }, { modified: new Date() }).exec()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import request from 'superagent'
import { reindexDataset } from '../elasticsearch/reindex-dataset'
import { redis, redlock } from '../libs/redis'
import CacheItem, { CacheType } from '../cache/item'
import config from '../config.js'
import config from '../config'
import {
updateDatasetName,
snapshotCreationComparison,
} from '../graphql/resolvers/dataset.js'
import { description } from '../graphql/resolvers/description.js'
import doiLib from '../libs/doi/index.js'
} from '../graphql/resolvers/dataset'
import { description } from '../graphql/resolvers/description'
import doiLib from '../libs/doi/index'
import { getFiles } from './files'
import { generateDataladCookie } from '../libs/authentication/jwt'
import notifications from '../libs/notifications'
import Dataset from '../models/dataset'
import Snapshot from '../models/snapshot'
import { updateDatasetRevision } from './draft.js'
import Snapshot, { SnapshotDocument } from '../models/snapshot'
import { updateDatasetRevision } from './draft'
import { getDatasetWorker } from '../libs/datalad-service'
import { join } from 'path'

Expand Down Expand Up @@ -94,7 +94,7 @@ const postSnapshot = async (
* @param {string} datasetId Dataset accession number
* @returns {Promise<import('../models/snapshot').SnapshotDocument[]>}
*/
export const getSnapshots = datasetId => {
export const getSnapshots = (datasetId): Promise<SnapshotDocument[]> => {
const url = `${getDatasetWorker(datasetId)}/datasets/${datasetId}/snapshots`
return request
.get(url)
Expand Down Expand Up @@ -194,7 +194,10 @@ export const deleteSnapshot = (datasetId, tag) => {
* @param {string} commitRef Tag name to retrieve
* @returns {Promise<import('../models/snapshot').SnapshotDocument>}
*/
export const getSnapshot = (datasetId, commitRef) => {
export const getSnapshot = (
datasetId,
commitRef,
): Promise<SnapshotDocument> => {
const url = `${getDatasetWorker(
datasetId,
)}/datasets/${datasetId}/snapshots/${commitRef}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from '../config.js'
import config from '../config'
import { Client } from '@elastic/elasticsearch'

const elasticConfig = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from '../config.js'
import config from '../config'
import { indexDataset, queryForIndex, indexingToken } from '@openneuro/search'
import { elasticClient } from './elastic-client'
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`resolver permissions helpers > checkDatasetAdmin() > resolves to false for anonymous users 1`] = `"You do not have admin access to this dataset."`;

exports[`resolver permissions helpers > checkDatasetWrite() > resolves to false for anonymous users 1`] = `"You do not have access to modify this dataset."`;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import {
datasetReadQuery,
checkPermissionLevel,
Expand Down Expand Up @@ -95,12 +96,17 @@ describe('resolver permissions helpers', () => {
describe('checkDatasetAdmin()', () => {
it('resolves to false for anonymous users', () => {
return expect(
checkDatasetAdmin('ds000001', null, null, false),
checkDatasetAdmin('ds000001', null, null, { checkExists: false }),
).rejects.toThrowErrorMatchingSnapshot()
})
it('resolves to true for admins', () => {
return expect(
checkDatasetAdmin('ds000001', '1234', { admin: true }, false),
checkDatasetAdmin(
'ds000001',
'1234',
{ admin: true },
{ checkExists: false },
),
).resolves.toBe(true)
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import config from '../config.js'
import config from '../config'
import { GraphQLError } from 'graphql'
import Permission from '../models/permission'
import Dataset from '../models/dataset'
Expand Down
5 changes: 0 additions & 5 deletions packages/openneuro-server/src/graphql/pubsub.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { vi } from 'vitest'
import {
encodeCursor,
decodeCursor,
elasticRelayConnection,
} from '../dataset-search'

vi.mock('ioredis')
vi.mock('../../../elasticsearch/elastic-client.js')
vi.mock('../../../config.js')
vi.mock('../../../elasticsearch/elastic-client.ts')
vi.mock('../../../config.ts')

describe('dataset search resolvers', () => {
describe('encodeCursor()', () => {
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('dataset search resolvers', () => {
hasPreviousPage: false,
},
}
// @ts-expect-error Mock version does not use all arguments
const connection = await elasticRelayConnection(emptyApiResponse, {
dataset: vi.fn(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as ds from '../dataset'

vi.mock('superagent')
vi.mock('ioredis')
vi.mock('../../../config.js')
vi.mock('../../../libs/notifications.js')
vi.mock('../../../config.ts')
vi.mock('../../../libs/notifications.ts')

describe('dataset resolvers', () => {
beforeAll(() => {
Expand Down Expand Up @@ -40,9 +40,9 @@ describe('dataset resolvers', () => {
})
it('sorts array of objects by the "created" property as strings', () => {
const testArray = [
{ id: 2, created: '2018-11-20T00:05:43.473Z' },
{ id: 1, created: '2018-11-19T00:05:43.473Z' },
{ id: 3, created: '2018-11-23T00:05:43.473Z' },
{ id: 2, created: '2018-11-20T00:05:43.473Z', tag: '2.0.0' },
{ id: 1, created: '2018-11-19T00:05:43.473Z', tag: '1.0.0' },
{ id: 3, created: '2018-11-23T00:05:43.473Z', tag: '3.0.0' },
]
const sorted = testArray.sort(ds.snapshotCreationComparison)
expect(sorted[0].id).toBe(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import { updatePermissions } from '../permissions'

vi.mock('ioredis')
Expand All @@ -22,7 +23,7 @@ describe('permissions resolvers', () => {
await updatePermissions(
{},
{ datasetId: 'ds01234', userEmail: 'fake@test.com' },
{},
{ user: '1234', userInfo: { id: '1234' } },
)
} catch (err) {
error = err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { matchKnownObjects, filterLatestSnapshot } from '../snapshots.js'
import { vi } from 'vitest'
import { matchKnownObjects, filterLatestSnapshot } from '../snapshots'

vi.mock('ioredis')
vi.mock('../../../config.js')
vi.mock('../../../config.ts')

describe('snapshot resolvers', () => {
describe('matchKnownObjects()', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi } from 'vitest'
import { users } from '../user.js'

vi.mock('ioredis')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Comment from '../../models/comment'
import notifications from '../../libs/notifications.js'
import notifications from '../../libs/notifications'
import { user } from './user.js'
import { checkAdmin } from '../permissions'

Expand Down
Loading

0 comments on commit 0095eb6

Please sign in to comment.