Skip to content

Commit

Permalink
refactor: data routes renaming (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
MXPOL authored Nov 21, 2023
1 parent af76dd4 commit 49a50a1
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 70 deletions.
2 changes: 1 addition & 1 deletion apps/velo-external-db/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const initConnector = async(wixDataBaseUrl?: string, hooks?: Hooks) => {
export const createApp = async(wixDataBaseUrl?: string) => {
const app = express()
const initConnectorResponse = await initConnector(wixDataBaseUrl)
app.use(initConnectorResponse.externalDbRouter.router)
app.use('/v3/', initConnectorResponse.externalDbRouter.router)
const server = app.listen(8080, () => console.log('Connector listening on port 8080'))

return { server, ...initConnectorResponse, reload: () => initConnector(wixDataBaseUrl) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { dataSpi, convertersUtils } from '@wix-velo/velo-external-db-core'


const axiosInstance = axios.create({
baseURL: 'http://localhost:8080'
baseURL: 'http://localhost:8080/v3'
})

export const insertRequest = (collectionName: string, items: Item[]): dataSpi.InsertRequest => ({
Expand Down Expand Up @@ -40,12 +40,12 @@ export const queryRequest = (collectionName: string, sort: dataSpi.Sorting[], fi


export const queryCollectionAsArray = async(collectionName: string, sort: dataSpi.Sorting[], fields: string[], auth: any, filter?: dataSpi.Filter) =>
await axiosInstance.post('/data/query',
await axiosInstance.post('/items/query',
queryRequest(collectionName, sort, fields, filter), { transformRequest: auth.transformRequest }).then(res => res.data)


export const pagingMetadata = (count: number, total?: number) => ({ count, offset: 0, total })


export const givenItems = async(items: Item[], collectionName: string, auth: any) =>
await axiosInstance.post('/data/insert', insertRequest(collectionName, items), { transformRequest: auth.transformRequest })
await axiosInstance.post('/items/insert', insertRequest(collectionName, items), { transformRequest: auth.transformRequest })
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { schemaUtils } from '@wix-velo/velo-external-db-core'


const axiosClient = axios.create({
baseURL: 'http://localhost:8080'
baseURL: 'http://localhost:8080/v3'
})

export const givenCollection = async(name: string, columns: InputField[], auth: any) => {
Expand Down
15 changes: 9 additions & 6 deletions apps/velo-external-db/test/e2e/app.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { authOwner } from '@wix-velo/external-db-testkit'
import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName, env } from '../resources/e2e_resources'
import { givenHideAppInfoEnvIsTrue } from '../drivers/app_info_config_test_support'

import { CollectionCapability } from '@wix-velo/velo-external-db-core'
const axios = require('axios').create({
baseURL: 'http://localhost:8080'
baseURL: 'http://localhost:8080/v3'
})

describe(`Velo External DB: ${currentDbImplementationName()}`, () => {
Expand Down Expand Up @@ -34,10 +33,14 @@ describe(`Velo External DB: ${currentDbImplementationName()}`, () => {
})
test('answer capability', async() => {

expect((await axios.get('/capabilities', { }, authOwner)).data).toEqual(expect.objectContaining({
capabilities: {
collection: [CollectionCapability.CREATE]
}
expect((await axios.post('/capabilities/get', { }, authOwner)).data).toEqual(expect.objectContaining({
supportsCollectionModifications: true,
supportedFieldTypes: expect.toBeArray(),
supportsCollectionDisplayName: false,
supportsCollectionDisplayField: false,
supportsCollectionPermissions: false,
supportsCollectionFieldDisplayName: false,
supportsCollectionFieldDescription: false,
}))
})

Expand Down
8 changes: 4 additions & 4 deletions apps/velo-external-db/test/e2e/app_auth.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { authOwnerWithoutJwt, authOwnerWithWrongJwtPublicKey, authVisitor, authO
import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName } from '../resources/e2e_resources'

const axiosInstance = axios.create({
baseURL: 'http://localhost:8080'
baseURL: 'http://localhost:8080/v3'
})


Expand All @@ -27,15 +27,15 @@ describe(`Velo External DB authorization: ${currentDbImplementationName()}`, ()

describe('JWT authorization', () => {
test('should throw if the request is not singed with JWT token', async() => {
return expect(axiosInstance.post('data/insert', {}, authOwnerWithoutJwt)).rejects.toThrow('401')
return expect(axiosInstance.post('items/insert', {}, authOwnerWithoutJwt)).rejects.toThrow('401')
})

test('should throw if the request is singed with the wrong public key', async() => {
return expect(axiosInstance.post('data/insert', {}, authOwnerWithWrongJwtPublicKey)).rejects.toThrow('401')
return expect(axiosInstance.post('items/insert', {}, authOwnerWithWrongJwtPublicKey)).rejects.toThrow('401')
})

test('should throw if the request is signed with wrong app id', async() => {
return expect(axiosInstance.post('data/insert', {}, authOwnerWithWrongAppId)).rejects.toThrow('401')
return expect(axiosInstance.post('items/insert', {}, authOwnerWithWrongAppId)).rejects.toThrow('401')
})
})

Expand Down
36 changes: 18 additions & 18 deletions apps/velo-external-db/test/e2e/app_data.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { UpdateImmediately, DeleteImmediately, Truncate, Aggregate, FindWithSort,
const chance = Chance()

const axiosInstance = axios.create({
baseURL: 'http://localhost:8080'
baseURL: 'http://localhost:8080/v3'
})

describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, () => {
Expand Down Expand Up @@ -71,7 +71,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
test('insert api', async() => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)

const response = await axiosInstance.post('/data/insert', data.insertRequest(ctx.collectionName, ctx.items), authAdmin)
const response = await axiosInstance.post('/items/insert', data.insertRequest(ctx.collectionName, ctx.items), authAdmin)

expect(response.data).toEqual({ results: expect.toIncludeSameMembers(ctx.items) })

Expand All @@ -84,7 +84,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)

const response = await axiosInstance.post('/data/insert', data.insertRequest(ctx.collectionName, ctx.items), authAdmin)
const response = await axiosInstance.post('/items/insert', data.insertRequest(ctx.collectionName, ctx.items), authAdmin)


expect(response.data.results).toEqual([
Expand All @@ -109,7 +109,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()

await schema.givenCollection(ctx.collectionName, ctx.numberColumns, authOwner)
await data.givenItems([ctx.numberItem, ctx.anotherNumberItem], ctx.collectionName, authOwner)
const response = await axiosInstance.post('/data/aggregate', {
const response = await axiosInstance.post('/items/aggregate', {
collectionId: ctx.collectionName,
initialFilter: { _id: { $eq: ctx.numberItem._id } },
aggregation: {
Expand Down Expand Up @@ -145,7 +145,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems(ctx.items, ctx.collectionName, authAdmin)
await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
const response = await axiosInstance.post('/data/remove', {
const response = await axiosInstance.post('/items/remove', {
collectionId: ctx.collectionName, itemIds: ctx.items.map(i => i._id)
}, authAdmin)

Expand All @@ -160,7 +160,7 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems([ ctx.items[0] ], ctx.collectionName, authAdmin)

const response = await axiosInstance.post('/data/remove', {
const response = await axiosInstance.post('/items/remove', {
collectionId: ctx.collectionName, itemIds: ctx.items.map(i => i._id)
}, authAdmin)

Expand Down Expand Up @@ -227,7 +227,7 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
testIfSupportedOperationsIncludes(supportedOperations, [ UpdateImmediately ])('update api', async() => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems(ctx.items, ctx.collectionName, authAdmin)
const response = await axiosInstance.post('/data/update', data.updateRequest(ctx.collectionName, ctx.modifiedItems), authAdmin)
const response = await axiosInstance.post('/items/update', data.updateRequest(ctx.collectionName, ctx.modifiedItems), authAdmin)

expect(response.data.results).toEqual(ctx.modifiedItems)

Expand All @@ -240,7 +240,7 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
testIfSupportedOperationsIncludes(supportedOperations, [ UpdateImmediately ])('update api should return updated items if they exist and if they don\'t, it should return an error object', async() => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems(ctx.items.slice(0, ctx.items.length - 1), ctx.collectionName, authAdmin)
const response = await axiosInstance.post('/data/update', data.updateRequest(ctx.collectionName, ctx.modifiedItems), authAdmin)
const response = await axiosInstance.post('/items/update', data.updateRequest(ctx.collectionName, ctx.modifiedItems), authAdmin)

expect(response.data.results).toEqual([
...ctx.modifiedItems.slice(0, ctx.items.length - 1),
Expand All @@ -263,15 +263,15 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
test('count api', async() => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems([ctx.item, ctx.anotherItem], ctx.collectionName, authAdmin)
await expect( axiosInstance.post('/data/count', data.countRequest(ctx.collectionName), authAdmin) ).resolves.toEqual(
await expect( axiosInstance.post('/items/count', data.countRequest(ctx.collectionName), authAdmin) ).resolves.toEqual(
matchers.responseWith( { totalCount: 2 } ))
})


testIfSupportedOperationsIncludes(supportedOperations, [ Truncate ])('truncate api', async() => {
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems([ctx.item, ctx.anotherItem], ctx.collectionName, authAdmin)
await axiosInstance.post('/data/truncate', { collectionId: ctx.collectionName }, authAdmin)
await axiosInstance.post('/items/truncate', { collectionId: ctx.collectionName }, authAdmin)
await expect(data.queryCollectionAsArray(ctx.collectionName, [], undefined, authOwner)).resolves.toEqual({
items: [],
pagingMetadata: data.pagingMetadata(0, 0)
Expand All @@ -283,7 +283,7 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
delete ctx.numberItem[ctx.numberColumns[0].name]
delete ctx.numberItem[ctx.numberColumns[1].name]

await axiosInstance.post('/data/insert', data.insertRequest(ctx.collectionName, [ctx.numberItem]), authAdmin)
await axiosInstance.post('/items/insert', data.insertRequest(ctx.collectionName, [ctx.numberItem]), authAdmin)

await expect(data.queryCollectionAsArray(ctx.collectionName, [], undefined, authOwner)).resolves.toEqual({
items: expect.toIncludeSameMembers([{
Expand All @@ -302,7 +302,7 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
ctx.numberItem[ctx.numberColumns[0].name] = null
ctx.numberItem[ctx.numberColumns[1].name] = null

await axiosInstance.post('/data/update', data.updateRequest(ctx.collectionName, [ctx.numberItem]), authAdmin)
await axiosInstance.post('/items/update', data.updateRequest(ctx.collectionName, [ctx.numberItem]), authAdmin)


await expect(data.queryCollectionAsArray(ctx.collectionName, [], undefined, authOwner)).resolves.toEqual({
Expand Down Expand Up @@ -334,7 +334,7 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
await data.givenItems([ctx.item], ctx.collectionName, authAdmin)

const response = await axiosInstance.post('/data/insert', data.insertRequest(ctx.collectionName, [ctx.item]), authAdmin)
const response = await axiosInstance.post('/items/insert', data.insertRequest(ctx.collectionName, [ctx.item]), authAdmin)

expect(response.data).toEqual(expect.objectContaining({
results: [{
Expand All @@ -347,10 +347,10 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
})

each([
['update', '/data/update', data.updateRequest.bind(null, 'nonExistingCollection', [])],
['count', '/data/count', data.countRequest.bind(null, 'nonExistingCollection')],
['insert', '/data/insert', data.insertRequest.bind(null, 'nonExistingCollection', [], false)],
['query', '/data/query', data.queryRequest.bind(null, 'nonExistingCollection', [], undefined)],
['update', '/items/update', data.updateRequest.bind(null, 'nonExistingCollection', [])],
['count', '/items/count', data.countRequest.bind(null, 'nonExistingCollection')],
['insert', '/items/insert', data.insertRequest.bind(null, 'nonExistingCollection', [], false)],
['query', '/items/query', data.queryRequest.bind(null, 'nonExistingCollection', [], undefined)],
])
.test('%s api on non existing collection should fail with WDE0025, 404', async(_, api, request) => {
let error
Expand All @@ -371,7 +371,7 @@ await data.givenItems([ ctx.items[1] ], ctx.collectionName, authAdmin)
await schema.givenCollection(ctx.collectionName, [ctx.column], authOwner)
let error

await axiosInstance.post('/data/query', data.queryRequest(ctx.collectionName, [], undefined, { nonExistingColumn: { $eq: 'value' } }), authAdmin).catch(e => error = e)
await axiosInstance.post('/items/query', data.queryRequest(ctx.collectionName, [], undefined, { nonExistingColumn: { $eq: 'value' } }), authAdmin).catch(e => error = e)

expect(error).toBeDefined()
expect(error.response.status).toEqual(400)
Expand Down
Loading

0 comments on commit 49a50a1

Please sign in to comment.