-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from pshenmic/feat/api-integration-tests
Add API integration tests
- Loading branch information
Showing
43 changed files
with
7,954 additions
and
255 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,14 +3,61 @@ name: Build and push packages | |
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
env: | ||
REGISTRY: ghcr.io | ||
IMAGE_NAME: ${{ github.repository }} | ||
|
||
jobs: | ||
test_api: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
# This is used to complete the identity challenge | ||
# with sigstore/fulcio when running outside of PRs. | ||
id-token: write | ||
|
||
services: | ||
postgres: | ||
image: postgres | ||
env: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
POSTGRES_DB: postgres | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
- 5432:5432 | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20.x' | ||
|
||
- name: NPM install | ||
run: cd packages/api && npm install | ||
|
||
- name: Run unit tests | ||
run: cd packages/api && npm run test:unit | ||
|
||
- name: Migrate DB | ||
run: cd packages/api && POSTGRES_HOST=127.0.0.1 POSTGRES_DB=postgres POSTGRES_USER=postgres POSTGRES_PASS=postgres POSTGRES_DB=postgres npm run db:migrate | ||
|
||
- name: Run integration tests | ||
run: cd packages/api && POSTGRES_HOST=127.0.0.1 POSTGRES_DB=postgres POSTGRES_USER=postgres POSTGRES_PASS=postgres POSTGRES_DB=postgres npm run test:integration | ||
|
||
build_api: | ||
runs-on: ubuntu-latest | ||
needs: test_api | ||
permissions: | ||
contents: read | ||
packages: write | ||
|
@@ -35,7 +82,8 @@ jobs: | |
- name: Build and push API package Docker image | ||
uses: docker/build-push-action@v4 | ||
with: | ||
context: ./packages/api | ||
context: ./ | ||
file: ./packages/api/Dockerfile | ||
push: ${{ github.event_name != 'pull_request' }} | ||
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:api | ||
build_indexer: | ||
|
@@ -70,6 +118,7 @@ jobs: | |
deploy: | ||
runs-on: ubuntu-latest | ||
needs: [build_api, build_indexer] | ||
if: github.event.pull_request.merged == true | ||
steps: | ||
- name: Deploy to the server | ||
uses: appleboy/[email protected] | ||
|
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
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 |
---|---|---|
@@ -1,87 +1,7 @@ | ||
require('dotenv').config() | ||
|
||
const Dash = require('dash') | ||
const Fastify = require('fastify') | ||
const cors = require('@fastify/cors') | ||
const Routes = require('./src/routes') | ||
const server = require('./src/server') | ||
|
||
const ServiceNotAvailableError = require("./src/errors/ServiceNotAvailableError"); | ||
const MainController = require("./src/controllers/MainController"); | ||
const TransactionsController = require("./src/controllers/TransactionsController"); | ||
const BlocksController = require("./src/controllers/BlocksController"); | ||
const DocumentsController = require("./src/controllers/DocumentsController"); | ||
const IdentitiesController = require("./src/controllers/IdentitiesController"); | ||
const packageVersion = require('./package.json').version | ||
const Worker = require('./src/worker/index') | ||
const {BLOCK_TIME} = require("./src/constants"); | ||
const DataContractsController = require("./src/controllers/DataContractsController"); | ||
|
||
function errorHandler(err, req, reply) { | ||
if (err instanceof ServiceNotAvailableError) { | ||
return reply.status(403).send({error: 'tenderdash backend is not available'}) | ||
} | ||
|
||
if (err?.constructor?.name === 'InvalidStateTransitionError') { | ||
const [error] = err.getErrors() | ||
const {code, message} = error | ||
|
||
return reply.status(500).send({error: message, code}) | ||
} | ||
|
||
console.error(err) | ||
reply.status(500) | ||
|
||
reply.send({error: err.message}) | ||
} | ||
|
||
let status; | ||
|
||
const init = async () => { | ||
const client = new Dash.Client() | ||
await client.platform.initialize() | ||
|
||
const worker = new Worker() | ||
|
||
worker.setHandler(async () => { | ||
try { | ||
status = true | ||
} catch (e) { | ||
} | ||
}) | ||
worker.start(BLOCK_TIME) | ||
|
||
const fastify = Fastify() | ||
|
||
await fastify.register(cors, { | ||
// put your options here | ||
}) | ||
|
||
const knex = require('knex')({ | ||
client: 'pg', | ||
connection: { | ||
host: process.env["POSTGRES_HOST"], | ||
port: process.env["POSTGRES_PORT"], | ||
user: process.env["POSTGRES_USER"], | ||
database: process.env["POSTGRES_NAME"], | ||
password: process.env["POSTGRES_PASS"], | ||
ssl: process.env["POSTGRES_SSL"] ? { rejectUnauthorized: false } : false, | ||
} | ||
}); | ||
|
||
await knex.raw('select 1+1'); | ||
|
||
const mainController = new MainController(knex) | ||
const blocksController = new BlocksController(knex) | ||
const transactionsController = new TransactionsController(client, knex) | ||
const dataContractsController = new DataContractsController(knex) | ||
const documentsController = new DocumentsController(knex) | ||
const identitiesController = new IdentitiesController(knex) | ||
|
||
Routes({fastify, mainController, blocksController, transactionsController, dataContractsController, documentsController, identitiesController}) | ||
|
||
fastify.setErrorHandler(errorHandler) | ||
fastify.listen({ host: "0.0.0.0", port: 3005, listenTextResolver: (address) => console.log(`Platform indexer API has started on the ${address}`)}); | ||
} | ||
|
||
|
||
init().then((`Platform Explorer backend v${packageVersion}`)) | ||
server.start() | ||
.then((_server) => server.listen(_server)) | ||
.then(() => console.log(`Platform Explorer API started`)) |
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
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 |
---|---|---|
@@ -1,3 +1,13 @@ | ||
module.exports = { | ||
BLOCK_TIME: 5000 | ||
BLOCK_TIME: 5000, | ||
StateTransitionEnum: { | ||
DATA_CONTRACT_CREATE: 0, | ||
DOCUMENTS_BATCH: 1, | ||
IDENTITY_CREATE: 2, | ||
IDENTITY_TOP_UP: 3, | ||
DATA_CONTRACT_UPDATE: 4, | ||
IDENTITY_UPDATE: 5, | ||
IDENTITY_CREDIT_WITHDRAWAL: 6, | ||
IDENTITY_CREDIT_TRANSFER: 7 | ||
} | ||
} |
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
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
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
Oops, something went wrong.