Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Containerized the app-backend 🐳🐳🐳 #87

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
indent_size = 4
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ ADMIN_SECRET=keyboardcat
YOUTUBE_API_KEY=aaaaaaaaaaaaaaaaaaaaaaaaaaa
YOUTUBE_CHANNEL_ID=UCLNgu_OupwoeESgtab33CCw
PORT=3000
HOST=0.0.0.0
HOST=0.0.0.0
SETUP_TYPE=manual|docker
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Makefile
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:10.16.3

COPY package.json ./
COPY yarn.lock ./
COPY ./entrypoint.sh /entrypoint.sh

RUN npm i -g yarn

RUN yarn install

COPY . .

EXPOSE 3000

ENTRYPOINT ["bash", "/entrypoint.sh"]
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
project=-p new_app-backend

setup:
@docker-compose build

start:
@docker-compose -f docker-compose.yml $(project) up -d

test:
@docker-compose -f ./docker-compose-test.yml up

ssh:
@docker exec -it app-backend_dev bash

test_now:
@docker exec -it app-backend_dev yarn run test
27 changes: 27 additions & 0 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'
services:
app:
container_name: app-backend_test
build: .
volumes:
- .:/usr/src/app-backend
working_dir: /usr/src/app-backend
ports:
- '80:3000'
links:
- mongo
environment:
- NODE_ENV=test
- MONGO_URI=mongodb://mongo:27017/codinggardencommunity
- TEST_MONGO_URI=mongodb://mongo:27017/codinggardencommunity-test
- ADMIN_SECRET=${ADMIN_SECRET}
- YOUTUBE_API_KEY=${YOUTUBE_API_KEY}
- YOUTUBE_CHANNEL_ID=${YOUTUBE_CHANNEL_ID}
- PORT=${PORT}
- HOST=${HOST}

mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: '3'
services:
app:
container_name: app-backend_dev
restart: always
build: .
volumes:
- .:/usr/src/app-backend
working_dir: /usr/src/app-backend
ports:
- '80:3000'
links:
- mongo
environment:
- NODE_ENV=${NODE_ENV}
- MONGO_URI=mongodb://mongo:27017/codinggardencommunity
- TEST_MONGO_URI=mongodb://mongo:27017/codinggardencommunity-test
- ADMIN_SECRET=${ADMIN_SECRET}
- YOUTUBE_API_KEY=${YOUTUBE_API_KEY}
- YOUTUBE_CHANNEL_ID=${YOUTUBE_CHANNEL_ID}
- PORT=${PORT}
- HOST=${HOST}

mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'
33 changes: 33 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
read_var() {
if [ -z "$1" ]; then
echo "Environment variable name is required."
return
fi

local ENV_FILE='.env'
if [ ! -z "$2" ]; then
ENV_FILE="$2"
fi

local VAR=$(grep $1 "$ENV_FILE" | xargs)
IFS="=" read -ra VAR <<< "$VAR"
echo ${VAR[1]}
}

runPreCommitHook() {

local SETUP_TYPE=$(read_var NODE_ENV)

if [ $NODE_ENV == "development" ]
then
yarn run seed && yarn run dev:docker
elif [ $NODE_ENV == "test" ]
then
yarn install && yarn run lint && yarn run format && yarn run test
exit $?
else
echo "Oops, we have a problem here. NODE_ENV in .env must be 'development' or 'test'."
fi
}

runPreCommitHook
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"main": "src/index.js",
"jest": {
"testEnvironment": "node",
"preset": "jest-puppeteer",
"collectCoverageFrom": [
"!**node_modules**",
"src/api/**/*.js"
Expand All @@ -28,14 +27,17 @@
"deploy": "./deploy.sh",
"format": "prettier --write src/**/*.js",
"buildAPIDoc": "swagger-ui-watcher ./Swag/MainSwag.yaml --bundle=./docs/APIs.json",
"coverage:open": "node src/tasks/openCoverage.js"
"coverage:open": "node src/tasks/openCoverage.js",
"dev:docker": "cross-env NODE_ENV=development nodemon -L src/index.js",
"dev:docker:clean": "docker rm app-backend_dev && docker rmi app-backend_app",
"ttest": "sh preCommit.sh"
},
"repository": "https://github.com/CodingGardenCommunity/app-backend.git",
"author": "Coding Garden Community App",
"license": "MIT",
"husky": {
"hooks": {
"pre-commit": "yarn run lint && yarn run format && yarn run test"
"pre-commit": "yarn run ttest"
}
},
"dependencies": {
Expand All @@ -62,10 +64,8 @@
"eslint-plugin-promise": "^4.2.1",
"husky": "^3.0.5",
"jest": "^24.9.0",
"jest-puppeteer": "^4.3.0",
"nodemon": "^1.19.2",
"prettier": "^1.18.2",
"puppeteer": "^1.20.0",
"supertest": "^4.0.2"
}
}
40 changes: 40 additions & 0 deletions preCommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
read_var() {
if [ -z "$1" ]; then
echo "Environment variable name is required."
return
fi

local ENV_FILE='.env'
if [ ! -z "$2" ]; then
ENV_FILE="$2"
fi

local VAR=$(grep $1 "$ENV_FILE" | xargs)
IFS="=" read -ra VAR <<< "$VAR"
echo ${VAR[1]}
}

runPreCommitHook() {

local SETUP_TYPE=$(read_var SETUP_TYPE)

if [ $SETUP_TYPE == "docker" ]
then
echo "Running tests inside docker container.."
docker exec app-backend_dev pwd
docker exec app-backend_dev yarn run lint
docker exec app-backend_dev yarn run format
docker exec app-backend_dev yarn run test
elif [ $SETUP_TYPE == "manual" ]
then
echo "Running tests on local machine.."
yarn run lint
yarn run format
yarn run test
else
echo "Err: Oops, we have a problem here. SETUP_TYPE in .env file MUST be 'docker' or 'manual'."
fi
}


runPreCommitHook
62 changes: 14 additions & 48 deletions src/api/docs/docs.test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,18 @@
const puppeteer = require('puppeteer');
const { PORT } = require('../../config');
const app = require('../../app').listen(PORT);

let browser;
let page;
const BASE_URL = `http://localhost:${PORT}/docs`;

beforeAll(async () => {
browser = await puppeteer.launch();
page = await browser.newPage();
});

afterAll(async () => {
await browser.close();
app.close();
});

describe('GET /docs/', () => {
it('Should display "Available versions" text on page.', async () => {
await page.goto(`${BASE_URL}/versions`);
await expect(page).toMatch('Available versions');
});
const request = require('supertest');
const app = require('../../app');

describe('GET /docs/versions', () => {
it('Should respond with a 200 status code', done =>
request(app)
.get('/docs/versions')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect(200, done));
});

describe('GET /docs/v1', () => {
it('Should display "CodingGarden Community App APIs" text on page.', async () => {
await page.goto(`${BASE_URL}/v1`);
await expect(page).toMatch('CodingGarden Community App APIs');
});
});

const click = async (selector, p) => {
const element = await p.waitForSelector(selector);
await element.click();
};

describe('GET API response through API Doc', () => {
it('Should be able to query /contributors API from Doc page.', async () => {
await page.goto(BASE_URL);

await click('#operations-Contributors-getAllContributors', page);
await click('.opblock-body > .opblock-section > .opblock-section-header > .try-out > .btn', page);
await click('.opblock-body > .execute-wrapper > .btn', page);

await page.waitForSelector('.live-responses-table tbody .response-col_status');
const statusCodeText = await page.$eval('.live-responses-table tbody .response-col_status', el => el.textContent);
const statusCode = parseInt(statusCodeText, 10);

expect(statusCode).toBe(200);
});
it('Should respond with a 200 status code', done =>
request(app)
.get('/docs/v1')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, done));
});
Loading