Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
207 additions
and
204 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
import { expect, describe, it, beforeAll, afterAll } from 'vitest'; | ||
import { server } from './server.js'; | ||
import fetch from 'node-fetch'; | ||
import { | ||
multipleMigrations, | ||
createActivityLog, | ||
environmentService, | ||
connectionService, | ||
createSync, | ||
createSyncJob, | ||
AuthCredentials, | ||
Connection, | ||
Sync, | ||
Job as SyncJob, | ||
SyncType, | ||
SyncStatus, | ||
Environment, | ||
db | ||
} from '@nangohq/shared'; | ||
|
||
describe('Persist API', () => { | ||
const port = 3096; | ||
const serverUrl = `http://localhost:${port}`; | ||
let seed: { | ||
env: Environment; | ||
activityLogId: number; | ||
connection: Connection; | ||
sync: Sync; | ||
syncJob: SyncJob; | ||
}; | ||
|
||
beforeAll(async () => { | ||
await multipleMigrations(); | ||
seed = await initDb(); | ||
server.listen(port); | ||
}); | ||
|
||
afterAll(async () => { | ||
await clearDb(); | ||
}); | ||
|
||
it('should server /health', async () => { | ||
const response = await fetch(`${serverUrl}/health`); | ||
expect(response.status).toEqual(200); | ||
expect(await response.json()).toEqual({ status: 'ok' }); | ||
}); | ||
|
||
it('should log', async () => { | ||
const response = await fetch(`${serverUrl}/environment/${seed.env.id}/log`, { | ||
method: 'POST', | ||
body: JSON.stringify({ activityLogId: seed.activityLogId, level: 'info', msg: 'Hello, world!' }), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
expect(response.status).toEqual(201); | ||
}); | ||
|
||
describe('save records', () => { | ||
it('should error if no records', async () => { | ||
const response = await fetch( | ||
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
model: 'MyModel', | ||
records: [], | ||
providerConfigKey: seed.connection.provider_config_key, | ||
connectionId: seed.connection.connection_id, | ||
lastSyncDate: new Date(), | ||
trackDeletes: false, | ||
softDelete: true | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
} | ||
); | ||
expect(response.status).toEqual(400); | ||
const respBody = (await response.json()) as any[]; | ||
expect(respBody[0]['errors']['issues'][0]['path'][0]).toEqual('records'); | ||
expect(respBody[0]['errors']['issues'][0]['message']).toEqual('Array must contain at least 1 element(s)'); | ||
}); | ||
|
||
it('should save records', async () => { | ||
const model = 'MyModel'; | ||
const records = [ | ||
{ id: 1, name: 'r1' }, | ||
{ id: 2, name: 'r2' } | ||
]; | ||
const response = await fetch( | ||
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`, | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
model, | ||
records: records, | ||
providerConfigKey: seed.connection.provider_config_key, | ||
connectionId: seed.connection.connection_id, | ||
activityLogId: seed.activityLogId, | ||
lastSyncDate: new Date(), | ||
trackDeletes: false | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
} | ||
); | ||
expect(response.status).toEqual(201); | ||
}); | ||
}); | ||
|
||
it('should delete records ', async () => { | ||
const model = 'MyModel'; | ||
const records = [ | ||
{ id: 1, name: 'r1' }, | ||
{ id: 2, name: 'r2' } | ||
]; | ||
const response = await fetch( | ||
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`, | ||
{ | ||
method: 'DELETE', | ||
body: JSON.stringify({ | ||
model, | ||
records: records, | ||
providerConfigKey: seed.connection.provider_config_key, | ||
connectionId: seed.connection.connection_id, | ||
activityLogId: seed.activityLogId, | ||
lastSyncDate: new Date(), | ||
trackDeletes: false | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
} | ||
); | ||
expect(response.status).toEqual(201); | ||
}); | ||
|
||
it('should update records ', async () => { | ||
const model = 'MyModel'; | ||
const records = [ | ||
{ id: 1, name: 'new1' }, | ||
{ id: 2, name: 'new2' } | ||
]; | ||
const response = await fetch( | ||
`${serverUrl}/environment/${seed.env.id}/connection/${seed.connection.id}/sync/${seed.sync.id}/job/${seed.syncJob.id}/records`, | ||
{ | ||
method: 'PUT', | ||
body: JSON.stringify({ | ||
model, | ||
records: records, | ||
providerConfigKey: seed.connection.provider_config_key, | ||
connectionId: seed.connection.connection_id, | ||
activityLogId: seed.activityLogId, | ||
lastSyncDate: new Date(), | ||
trackDeletes: false, | ||
softDelete: true | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
} | ||
); | ||
expect(response.status).toEqual(201); | ||
}); | ||
}); | ||
|
||
const initDb = async () => { | ||
const env = await environmentService.createEnvironment(0, 'testEnv'); | ||
if (!env) throw new Error('Environment not created'); | ||
const activityLogId = await createActivityLog({ | ||
environment_id: env.id, | ||
level: 'info', | ||
action: 'sync', | ||
success: null, | ||
timestamp: Date.now(), | ||
start: Date.now(), | ||
connection_id: null, | ||
provider_config_key: null | ||
}); | ||
if (!activityLogId) throw new Error('Activity log not created'); | ||
const connectionRes = await connectionService.upsertConnection(`conn-test`, `provider-test`, 'google', {} as AuthCredentials, {}, env.id, 0); | ||
const connectionId = connectionRes[0]?.id; | ||
if (!connectionId) throw new Error('Connection not created'); | ||
|
||
const connection = (await connectionService.getConnectionById(connectionId)) as Connection; | ||
if (!connection) throw new Error('Connection not found'); | ||
|
||
const sync = await createSync(connectionId, 'sync-test'); | ||
if (!sync?.id) throw new Error('Sync not created'); | ||
|
||
const syncJob = (await createSyncJob(sync.id, SyncType.INITIAL, SyncStatus.RUNNING, `job-test`, connection)) as SyncJob; | ||
if (!syncJob) throw new Error('Sync job not created'); | ||
|
||
return { | ||
env, | ||
activityLogId, | ||
connection, | ||
sync, | ||
syncJob | ||
}; | ||
}; | ||
|
||
const clearDb = async () => { | ||
await db.knex.raw(`DROP SCHEMA nango CASCADE`); | ||
}; |
Oops, something went wrong.