From 59a7579c535aa59e0d5f3e9a6d8e3153e93c56f9 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 10:07:39 -0800 Subject: [PATCH 01/14] docs: update comments on development env --- backend/.env | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/backend/.env b/backend/.env index b1057599..474debf5 100644 --- a/backend/.env +++ b/backend/.env @@ -1,24 +1,27 @@ +# For development environment, the project is ready to run after cloning and installing the dependencies. + # Node environment NODE_ENV=development -# Development environment +# Development database environment DEV_DB_USERNAME=user123 DEV_DB_PASSWORD=password123 DEV_DB_NAME=onboarding_db DEV_DB_HOST=db DEV_DB_PORT=5432 +# Enable email service EMAIL_ENABLE=false +# Email service configuration, for the email service to run correctly, the user should add their own email credentials +EMAIL_HOST=your_email_host +EMAIL_PORT=your_email_port +EMAIL=your_email +APP_PASSWORD=your_app_password # JWT Secret Key JWT_SECRET="NKrbO2lpCsOpVAlqAPsjZ0tZXzIoKru7gAmYZ7XlHn0=qqwqeq" -TEST_DB_USERNAME=user123 -TEST_DB_PASSWORD=password123 -TEST_DB_NAME=onboarding_db_test -TEST_DB_HOST=localhost -TEST_DB_PORT=5432 - +# Enable IP check for the API ENABLE_IP_CHECK=false # Allowed IP range for the API "baseIp/rangeStart-rangeEnd" (e.g. 192.168.1/1-255) separated by comma ALLOWED_IP_RANGE=11.22.33/10-200, 192.168.65/1-255 From e467b01067add544f5ccb34053234ca0406e8b9c Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 10:08:00 -0800 Subject: [PATCH 02/14] docs: update comments on test env --- backend/.env.test | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/.env.test b/backend/.env.test index 7507a9c6..5d13be81 100644 --- a/backend/.env.test +++ b/backend/.env.test @@ -1,12 +1,13 @@ # Test Environment Configuration -# Database Configuration +# Test database environment TEST_DB_USERNAME=user123 TEST_DB_PASSWORD=password123 TEST_DB_NAME=onboarding_db_test TEST_DB_HOST=localhost TEST_DB_PORT=5432 +# Credentials for test postgres database POSTGRES_USER=user123 POSTGRES_PASSWORD=password123 POSTGRES_DB=onboarding_db_test @@ -14,6 +15,7 @@ POSTGRES_DB=onboarding_db_test # JWT Secret Key JWT_SECRET=your_test_jwt_secret_key_here +# Enable IP check for the API ENABLE_IP_CHECK=false # Allowed IP range for the API "baseIp/rangeStart-rangeEnd" (e.g. 192.168.1/1-255) separated by comma ALLOWED_IP_RANGE=11.22.33/10-200, 192.168.65/1-255 From 6bc5b5cae0df84b6e7422e5b3ce996bac1f8ded8 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 10:08:21 -0800 Subject: [PATCH 03/14] docs: update variables and add comments on prod env --- backend/.env.production | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/backend/.env.production b/backend/.env.production index 7de5ae9b..c9c7319b 100644 --- a/backend/.env.production +++ b/backend/.env.production @@ -1,15 +1,22 @@ -# Production Environment Configuration - -# Database Configuration -PROD_DB_USERNAME=prod_user -PROD_DB_PASSWORD=prod_password -PROD_DB_NAME=prod_db -PROD_DB_HOST=localhost +# Production environment +PROD_DB_USERNAME=user123 +PROD_DB_PASSWORD=password123 +PROD_DB_NAME=onboarding_db +PROD_DB_HOST=db PROD_DB_PORT=5432 +# Enable email service +EMAIL_ENABLE=true +# Email service configuration, for the email service to run correctly, the user should add their own email credentials +EMAIL_HOST=your_email_host +EMAIL_PORT=your_email_port +EMAIL=your_email +APP_PASSWORD=your_app_password + # JWT Secret Key JWT_SECRET=your_prod_jwt_secret_key_here +# Enable IP check for the API ENABLE_IP_CHECK=false # Allowed IP range for the API "baseIp/rangeStart-rangeEnd" (e.g. 192.168.1/1-255) separated by comma ALLOWED_IP_RANGE=11.22.33/10-200 From bed19c59669932836687f06861b45cd54e873c81 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 10:27:00 -0800 Subject: [PATCH 04/14] docs: update readme --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index 2906a297..9139d8f9 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,54 @@ server { ``cd ~/bluewave-onboarding docker compose up -d`` +## Environment variables + +In order to the project to run safely and correctly, the user should add their own environment variables. They can be added to the .env file in the root directory of the project. The following is the list of environment variables that should be added and its description: + +1. Database credentials +``` +DEV_DB_USERNAME - Development database username +DEV_DB_PASSWORD - Development database password +DEV_DB_NAME - Development database name +DEV_DB_HOST - Development database host +DEV_DB_PORT - Development database port +TEST_DB_USERNAME - Test database username +TEST_DB_PASSWORD - Test database password +TEST_DB_NAME - Test database name +TEST_DB_HOST - Test database host +TEST_DB_PORT - Test database port +PROD_DB_USERNAME - Production database username +PROD_DB_PASSWORD - Production database password +PROD_DB_NAME - Production database name +PROD_DB_HOST - Production database host +PROD_DB_PORT - Production database port +``` + +2. Email service configuration +For the email service to run correctly, the user should add their own email credentials +``` +EMAIL_ENABLE - Enable email service (boolean) +EMAIL_HOST - Email host +EMAIL_PORT - Email port +EMAIL - Email +APP_PASSWORD - Email password +``` + +3. JWT Secret Key +``` +JWT_SECRET - secret key to sign the JWT token +``` + +4. Enable IP check for the API +If the ENABLE_IP_CHECK is set to true, but the ALLOWED_IP_RANGE and ALLOWED_IPS are not set, the API will work for all IP addresses. +``` +ENABLE_IP_CHECK - Enable IP check for the API (boolean) +ALLOWED_IP_RANGE - Allowed IP range for the API with the format "baseIp/rangeStart-rangeEnd" (e.g. 192.168.1/1-255) separated by comma +ALLOWED_IPS - Allowed IP addresses for the API separated by comma +``` + +For development and testing purposes, the application is ready to go after cloning and dependencies installation. + ## Contributing Here's how you can contribute to the Onboarding product. From 859470fae479b80a53b8c4e04ba259ebe6457ae8 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 10:33:54 -0800 Subject: [PATCH 05/14] docs: update with coderabbit suggestions --- README.md | 79 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 9139d8f9..50567d89 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ # BlueWave Onboarding -BlueWave Onboarding helps app owners build knowledge and user-experience oriented apps. It includes the following features: +BlueWave Onboarding helps app owners build knowledge and user-experience oriented apps. It includes the following features: - Welcome tours - Product hints @@ -21,7 +21,6 @@ This is a work-in-progress application. The source code is available under GNU A ![Main dashboard](https://github.com/bluewave-labs/bluewave-onboarding/blob/master/Dashboard.png) - ## Tech stack - [ReactJs](https://react.dev/) @@ -29,7 +28,6 @@ This is a work-in-progress application. The source code is available under GNU A - [Node.js](https://nodejs.org/en) - [PostgreSQL](https://postgresql.org) - ## Installation 1. Make sure Docker is installed to your machine where the server will run. @@ -48,14 +46,14 @@ cd bluewave-onboarding Open the Nginx configuration file: -``sudo nano /etc/nginx/sites-available/onboarding-demo`` +`sudo nano /etc/nginx/sites-available/onboarding-demo` Add the following configuration. Change YOUR_DOMAIN_NAME with your domain name: ```server { listen 80; server_name YOUR_DOMAIN_NAME; - return 301 https://$host$request_uri; + return 301 https://$host$request_uri; } server { @@ -84,38 +82,38 @@ server { } ``` - 6. Create a symbolic link to enable the configuration: -``sudo ln -s /etc/nginx/sites-available/onboarding-demo /etc/nginx/sites-enabled/`` +`sudo ln -s /etc/nginx/sites-available/onboarding-demo /etc/nginx/sites-enabled/` 7. Install Certbot and its Nginx plugin: -``sudo apt install certbot python3-certbot-nginx`` +`sudo apt install certbot python3-certbot-nginx` 8. Obtain SSL Certificate. Run Certbot to obtain a certificate for your domain: -``sudo certbot --nginx`` +`sudo certbot --nginx` 9. Verify the Nginx configuration: -``sudo nginx -t`` +`sudo nginx -t` 10. Restart Nginx to apply the changes: -``sudo systemctl restart nginx`` +`sudo systemctl restart nginx` 11. Start the project -``cd ~/bluewave-onboarding -docker compose up -d`` +`cd ~/bluewave-onboarding +docker compose up -d` ## Environment variables In order to the project to run safely and correctly, the user should add their own environment variables. They can be added to the .env file in the root directory of the project. The following is the list of environment variables that should be added and its description: 1. Database credentials -``` + +```env DEV_DB_USERNAME - Development database username DEV_DB_PASSWORD - Development database password DEV_DB_NAME - Development database name @@ -134,8 +132,9 @@ PROD_DB_PORT - Production database port ``` 2. Email service configuration -For the email service to run correctly, the user should add their own email credentials -``` + For the email service to run correctly, the user should add their own email credentials + +```env EMAIL_ENABLE - Enable email service (boolean) EMAIL_HOST - Email host EMAIL_PORT - Email port @@ -143,29 +142,65 @@ EMAIL - Email APP_PASSWORD - Email password ``` -3. JWT Secret Key +Example configuration: + +```env +EMAIL_ENABLE=true +EMAIL_HOST=smtp.gmail.com +EMAIL_PORT=587 +EMAIL=your-email@example.com +APP_PASSWORD=your-app-specific-password ``` + +Note: When using Gmail, you'll need to enable 2-factor authentication and generate an App Password. + +3. JWT Secret Key + +```env JWT_SECRET - secret key to sign the JWT token ``` +Security requirements: + +- Use a strong, random secret key (minimum 32 characters) +- Never commit the actual secret to version control +- Rotate the secret periodically + 4. Enable IP check for the API -If the ENABLE_IP_CHECK is set to true, but the ALLOWED_IP_RANGE and ALLOWED_IPS are not set, the API will work for all IP addresses. -``` + If the ENABLE_IP_CHECK is set to true, but the ALLOWED_IP_RANGE and ALLOWED_IPS are not set, the API will work for all IP addresses. + +```env ENABLE_IP_CHECK - Enable IP check for the API (boolean) ALLOWED_IP_RANGE - Allowed IP range for the API with the format "baseIp/rangeStart-rangeEnd" (e.g. 192.168.1/1-255) separated by comma ALLOWED_IPS - Allowed IP addresses for the API separated by comma ``` +Example configuration: + +```env +ENABLE_IP_CHECK=true +ALLOWED_IP_RANGE=192.168.1/1-255,10.0.0/1-100 +ALLOWED_IPS=203.0.113.1,203.0.113.2 +``` + +Note: For security reasons, it's recommended to always set either ALLOWED_IP_RANGE or ALLOWED_IPS when ENABLE_IP_CHECK is true. + +5. In .env.test file, the user should have the following environment variables, so the postgres container can run correctly: + +```env +POSTGRES_USER - Test database username (The same as TEST_DB_USERNAME) +POSTGRES_PASSWORD - Test database password (The same as TEST_DB_PASSWORD) +POSTGRES_DB - Test database name (The same as TEST_DB_NAME) +``` + For development and testing purposes, the application is ready to go after cloning and dependencies installation. ## Contributing -Here's how you can contribute to the Onboarding product. +Here's how you can contribute to the Onboarding product. - Check [Contributor's guideline](https://github.com/bluewave-labs/bluewave-onboarding/blob/master/CONTRIBUTING.md) - Have a look at our Figma designs [here](https://www.figma.com/design/MLPbP1HM2L9ON6f88pHTee/Onboarding?node-id=0-1&t=iwgz015l5QWbWRqU-1). We encourage you to copy to your own Figma page, then work on it as it is read-only. - Open an issue if you believe you've encountered a bug - Make a pull request to add new features/make quality-of-life improvements/fix bugs. - Make sure your send your PRs to **develop** branch. - - From 27b7184666106f6f2112c91ec2a77c808697855e Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 10:48:34 -0800 Subject: [PATCH 06/14] docs: add test variables to .rnv --- backend/.env | 7 +++++++ backend/.env.production | 2 +- backend/.env.test | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/.env b/backend/.env index 474debf5..66128484 100644 --- a/backend/.env +++ b/backend/.env @@ -21,6 +21,13 @@ APP_PASSWORD=your_app_password # JWT Secret Key JWT_SECRET="NKrbO2lpCsOpVAlqAPsjZ0tZXzIoKru7gAmYZ7XlHn0=qqwqeq" +# Test database environment +TEST_DB_USERNAME=user123 +TEST_DB_PASSWORD=password123 +TEST_DB_NAME=onboarding_db_test +TEST_DB_HOST=localhost +TEST_DB_PORT=5432 + # Enable IP check for the API ENABLE_IP_CHECK=false # Allowed IP range for the API "baseIp/rangeStart-rangeEnd" (e.g. 192.168.1/1-255) separated by comma diff --git a/backend/.env.production b/backend/.env.production index c9c7319b..d5efbf21 100644 --- a/backend/.env.production +++ b/backend/.env.production @@ -6,7 +6,7 @@ PROD_DB_HOST=db PROD_DB_PORT=5432 # Enable email service -EMAIL_ENABLE=true +EMAIL_ENABLE=false # Email service configuration, for the email service to run correctly, the user should add their own email credentials EMAIL_HOST=your_email_host EMAIL_PORT=your_email_port diff --git a/backend/.env.test b/backend/.env.test index 5d13be81..008330e2 100644 --- a/backend/.env.test +++ b/backend/.env.test @@ -12,6 +12,9 @@ POSTGRES_USER=user123 POSTGRES_PASSWORD=password123 POSTGRES_DB=onboarding_db_test +# Enable email service +EMAIL_ENABLE=false + # JWT Secret Key JWT_SECRET=your_test_jwt_secret_key_here From 5d6736b4f25d34d63991a0f5f28da6b2ac51b9f1 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 12:48:32 -0800 Subject: [PATCH 07/14] chore: update config to different .env files --- backend/config/config.js | 63 ++++++++++++++++++++++++++------------ backend/package.json | 4 +-- backend/posttest-script.sh | 14 +++++++++ backend/pretest-script.sh | 27 ++++++++++++++++ 4 files changed, 86 insertions(+), 22 deletions(-) create mode 100755 backend/posttest-script.sh create mode 100755 backend/pretest-script.sh diff --git a/backend/config/config.js b/backend/config/config.js index 312759a0..e9619c8a 100644 --- a/backend/config/config.js +++ b/backend/config/config.js @@ -1,31 +1,54 @@ -require("dotenv").config(); +const dotenv = require('dotenv'); +const envSuffix = process.env.NODE_ENV && process.env.NODE_ENV !== 'development' ? `.${process.env.NODE_ENV}` : ''; +const env = `.env${envSuffix}`; + +dotenv.config({ path: `./${env}` }); + +const { + DEV_DB_HOST, + DEV_DB_NAME, + DEV_DB_PASSWORD, + DEV_DB_PORT, + DEV_DB_USERNAME, + TEST_DB_HOST, + TEST_DB_NAME, + TEST_DB_PASSWORD, + TEST_DB_PORT, + TEST_DB_USERNAME, + PROD_DB_HOST, + PROD_DB_NAME, + PROD_DB_PASSWORD, + PROD_DB_PORT, + PROD_DB_USERNAME, +} = process.env; + module.exports = { - defaultTeamName: "My Organisation", + defaultTeamName: 'My Organisation', development: { - username: process.env.DEV_DB_USERNAME, - password: process.env.DEV_DB_PASSWORD, - database: process.env.DEV_DB_NAME, - host: process.env.DEV_DB_HOST, - dialect: "postgres", - port: process.env.DEV_DB_PORT, + username: DEV_DB_USERNAME, + password: DEV_DB_PASSWORD, + database: DEV_DB_NAME, + host: DEV_DB_HOST, + dialect: 'postgres', + port: DEV_DB_PORT, logging: false, }, test: { - username: process.env.TEST_DB_USERNAME, - password: process.env.TEST_DB_PASSWORD, - database: process.env.TEST_DB_NAME, - host: process.env.TEST_DB_HOST, - dialect: "postgres", - port: process.env.TEST_DB_PORT, + username: TEST_DB_USERNAME, + password: TEST_DB_PASSWORD, + database: TEST_DB_NAME, + host: TEST_DB_HOST, + dialect: 'postgres', + port: TEST_DB_PORT, logging: false, }, production: { - username: process.env.PROD_DB_USERNAME, - password: process.env.PROD_DB_PASSWORD, - database: process.env.PROD_DB_NAME, - host: process.env.PROD_DB_HOST, - dialect: "postgres", - port: process.env.PROD_DB_PORT, + username: PROD_DB_USERNAME, + password: PROD_DB_PASSWORD, + database: PROD_DB_NAME, + host: PROD_DB_HOST, + dialect: 'postgres', + port: PROD_DB_PORT, logging: false, }, }; diff --git a/backend/package.json b/backend/package.json index 4b001fc0..4aff0a97 100644 --- a/backend/package.json +++ b/backend/package.json @@ -5,8 +5,8 @@ "main": "index.js", "scripts": { "start": "node index.js", - "pretest": "NODE_ENV=test docker rm -f test-postgres || true && docker run --name test-postgres --env-file .env.test -p 5432:5432 -d postgres && npx wait-on tcp:5432 && npx sequelize-cli db:create --env test || true && npx sequelize-cli db:migrate --env test", - "posttest": "docker stop test-postgres && docker rm test-postgres", + "pretest": "bash pretest-script.sh", + "posttest": "bash posttest-script.sh", "test": "NODE_ENV=test nyc mocha --extension js,mjs 'src/test/**/*.test.*'", "test:e2e": "npm run pretest && NODE_ENV=test mocha 'src/test/e2e/**/*.test.mjs'", "test:unit": "NODE_ENV=test mocha 'src/test/unit/**/*.test.js' --watch", diff --git a/backend/posttest-script.sh b/backend/posttest-script.sh new file mode 100755 index 00000000..1129f736 --- /dev/null +++ b/backend/posttest-script.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# stop container +docker stop test-postgres + +# remove container +docker rm -f test-postgres + +# reset NODE_ENV to default +if [[ "$OSTYPE" == "msys" ]]; then + set NODE_ENV=development +else + export NODE_ENV=development +fi \ No newline at end of file diff --git a/backend/pretest-script.sh b/backend/pretest-script.sh new file mode 100755 index 00000000..9caddaf9 --- /dev/null +++ b/backend/pretest-script.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# set node env to test, if windows use set instead of export +if [[ "$OSTYPE" == "msys" ]]; then + set NODE_ENV=test +else + export NODE_ENV=test +fi + +# remove test-postgres container if it exists +if [[ "$(docker ps -a -q -f name=test-postgres)" ]]; then + docker rm -f test-postgres +fi + +# run test-postgres container +docker run --name test-postgres --env-file .env.test -p 5432:5432 -d postgres + +# wait for postgres to start +npx wait-on tcp:5432 + +# create test database if it doesn't exist +if [[ ! "$(psql -U postgres -lqt | cut -d \| -f 1 | grep -w test_db)" ]]; then + npx sequelize-cli db:create --env test +fi + +# run migrations +npx sequelize-cli db:migrate --env test From 5c6a66d766d680de334fd0719a3c46c69cc72437 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 12:53:03 -0800 Subject: [PATCH 08/14] chore: remove test var from dev .env --- backend/.env | 7 ------- 1 file changed, 7 deletions(-) diff --git a/backend/.env b/backend/.env index 66128484..474debf5 100644 --- a/backend/.env +++ b/backend/.env @@ -21,13 +21,6 @@ APP_PASSWORD=your_app_password # JWT Secret Key JWT_SECRET="NKrbO2lpCsOpVAlqAPsjZ0tZXzIoKru7gAmYZ7XlHn0=qqwqeq" -# Test database environment -TEST_DB_USERNAME=user123 -TEST_DB_PASSWORD=password123 -TEST_DB_NAME=onboarding_db_test -TEST_DB_HOST=localhost -TEST_DB_PORT=5432 - # Enable IP check for the API ENABLE_IP_CHECK=false # Allowed IP range for the API "baseIp/rangeStart-rangeEnd" (e.g. 192.168.1/1-255) separated by comma From 1e53cc16f213ca3847e7fcf8b0cdeaa979ebcca8 Mon Sep 17 00:00:00 2001 From: erenfn <81182796+erenfn@users.noreply.github.com> Date: Mon, 6 Jan 2025 23:14:34 +0100 Subject: [PATCH 09/14] Update README.md --- README.md | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 50567d89..01653d6f 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,25 @@ This is a work-in-progress application. The source code is available under GNU A - [Node.js](https://nodejs.org/en) - [PostgreSQL](https://postgresql.org) -## Installation +## For Local Use + +Make sure docker and git is installed + +1. Clone the project + +`git clone https://github.com/bluewave-labs/guidefox.git` + +2. Navigate to the project directory + +`cd guidefox` + +3. Run docker + +`docker compose build` + +`docker compose up` + +## Server Installation 1. Make sure Docker is installed to your machine where the server will run. 2. Make sure git is installed to your machine Git. @@ -160,11 +178,7 @@ Note: When using Gmail, you'll need to enable 2-factor authentication and genera JWT_SECRET - secret key to sign the JWT token ``` -Security requirements: - - Use a strong, random secret key (minimum 32 characters) -- Never commit the actual secret to version control -- Rotate the secret periodically 4. Enable IP check for the API If the ENABLE_IP_CHECK is set to true, but the ALLOWED_IP_RANGE and ALLOWED_IPS are not set, the API will work for all IP addresses. From a6e024c71892edbf53a657fe7989855f8a38171a Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 14:17:06 -0800 Subject: [PATCH 10/14] chore: update scripts --- backend/posttest-script.sh | 16 ++++++++++++---- backend/pretest-script.sh | 9 ++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/backend/posttest-script.sh b/backend/posttest-script.sh index 1129f736..bd3fb436 100755 --- a/backend/posttest-script.sh +++ b/backend/posttest-script.sh @@ -1,14 +1,22 @@ #!/bin/bash -# stop container -docker stop test-postgres +# stop container, if it exists + +if [[ "$(docker ps -a -q -f name=test-postgres)" ]]; then + docker stop test-postgres +fi # remove container -docker rm -f test-postgres +if [[ "$(docker ps -a -q -f name=test-postgres)" ]]; then + docker rm -f test-postgres +fi # reset NODE_ENV to default if [[ "$OSTYPE" == "msys" ]]; then - set NODE_ENV=development + # For Windows CMD: set "NODE_ENV=development" + # For PowerShell: $env:NODE_ENV="development" + set "NODE_ENV=development" + $env:NODE_ENV="development" else export NODE_ENV=development fi \ No newline at end of file diff --git a/backend/pretest-script.sh b/backend/pretest-script.sh index 9caddaf9..99259159 100755 --- a/backend/pretest-script.sh +++ b/backend/pretest-script.sh @@ -2,7 +2,10 @@ # set node env to test, if windows use set instead of export if [[ "$OSTYPE" == "msys" ]]; then - set NODE_ENV=test + # For Windows CMD: set "NODE_ENV=test" + # For PowerShell: $env:NODE_ENV="test" + set "NODE_ENV=test" + $env:NODE_ENV="test" else export NODE_ENV=test fi @@ -19,6 +22,10 @@ docker run --name test-postgres --env-file .env.test -p 5432:5432 -d postgres npx wait-on tcp:5432 # create test database if it doesn't exist +if ! command -v psql &> /dev/null; then + echo "Error: psql command not found" + exit 1 +fi if [[ ! "$(psql -U postgres -lqt | cut -d \| -f 1 | grep -w test_db)" ]]; then npx sequelize-cli db:create --env test fi From 3d2aaba931d8ad8c7cd961d61ba552a61895608f Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 14:27:32 -0800 Subject: [PATCH 11/14] docs: add psql check to script --- backend/pretest-script.sh | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/pretest-script.sh b/backend/pretest-script.sh index 99259159..78acbba0 100755 --- a/backend/pretest-script.sh +++ b/backend/pretest-script.sh @@ -23,11 +23,19 @@ npx wait-on tcp:5432 # create test database if it doesn't exist if ! command -v psql &> /dev/null; then - echo "Error: psql command not found" - exit 1 -fi -if [[ ! "$(psql -U postgres -lqt | cut -d \| -f 1 | grep -w test_db)" ]]; then - npx sequelize-cli db:create --env test + # Check if database exists inside Docker container + if ! docker exec -it test-postgres psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='test_db'" | grep -q 1; then + npx sequelize-cli db:create --env test + else + echo "Database 'test_db' already exists in Docker." + fi +else + # Check if database exists locally + if ! psql -U postgres -lqt | cut -d \| -f 1 | grep -qw test_db; then + npx sequelize-cli db:create --env test + else + echo "Database 'test_db' already exists locally." + fi fi # run migrations From 454ca17ef9a7c5111f2ac0c5132359e76b2c9c6a Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Mon, 6 Jan 2025 14:29:16 -0800 Subject: [PATCH 12/14] docs: fix script logic --- backend/posttest-script.sh | 11 +++++++---- backend/pretest-script.sh | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/backend/posttest-script.sh b/backend/posttest-script.sh index bd3fb436..47b6340a 100755 --- a/backend/posttest-script.sh +++ b/backend/posttest-script.sh @@ -13,10 +13,13 @@ fi # reset NODE_ENV to default if [[ "$OSTYPE" == "msys" ]]; then - # For Windows CMD: set "NODE_ENV=development" - # For PowerShell: $env:NODE_ENV="development" - set "NODE_ENV=development" - $env:NODE_ENV="development" + if [ -n "$PSModulePath" ]; then + # PowerShell + echo '$env:NODE_ENV="development"' | powershell -Command - + else + # CMD + cmd.exe /C "set NODE_ENV=development" + fi else export NODE_ENV=development fi \ No newline at end of file diff --git a/backend/pretest-script.sh b/backend/pretest-script.sh index 78acbba0..590285a1 100755 --- a/backend/pretest-script.sh +++ b/backend/pretest-script.sh @@ -2,10 +2,13 @@ # set node env to test, if windows use set instead of export if [[ "$OSTYPE" == "msys" ]]; then - # For Windows CMD: set "NODE_ENV=test" - # For PowerShell: $env:NODE_ENV="test" - set "NODE_ENV=test" - $env:NODE_ENV="test" + if [ -n "$PSModulePath" ]; then + # PowerShell + echo '$env:NODE_ENV="test"' | powershell -Command - + else + # CMD + cmd.exe /C "set NODE_ENV=test" + fi else export NODE_ENV=test fi From 839c41987c38925ab62aa722d48844a65c6ea991 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Tue, 7 Jan 2025 10:26:41 -0800 Subject: [PATCH 13/14] docs: update pretest script --- backend/pretest-script.sh | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/backend/pretest-script.sh b/backend/pretest-script.sh index 590285a1..4041fa93 100755 --- a/backend/pretest-script.sh +++ b/backend/pretest-script.sh @@ -3,15 +3,14 @@ # set node env to test, if windows use set instead of export if [[ "$OSTYPE" == "msys" ]]; then if [ -n "$PSModulePath" ]; then - # PowerShell echo '$env:NODE_ENV="test"' | powershell -Command - else - # CMD cmd.exe /C "set NODE_ENV=test" fi else export NODE_ENV=test fi +echo "NODE_ENV=$NODE_ENV" # remove test-postgres container if it exists if [[ "$(docker ps -a -q -f name=test-postgres)" ]]; then @@ -19,26 +18,29 @@ if [[ "$(docker ps -a -q -f name=test-postgres)" ]]; then fi # run test-postgres container -docker run --name test-postgres --env-file .env.test -p 5432:5432 -d postgres -# wait for postgres to start -npx wait-on tcp:5432 +docker run --name test-postgres --env-file ./.env.test -p 5432:5432 -d postgres -# create test database if it doesn't exist -if ! command -v psql &> /dev/null; then - # Check if database exists inside Docker container - if ! docker exec -it test-postgres psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='test_db'" | grep -q 1; then - npx sequelize-cli db:create --env test +# wait for postgres to start +echo "Waiting for PostgreSQL to be ready..." +for i in {1..10}; do + if docker exec test-postgres pg_isready -U user123 >/dev/null 2>&1; then + echo "PostgreSQL is ready." + break else - echo "Database 'test_db' already exists in Docker." + echo "PostgreSQL is not ready. Retrying ($i/10)..." + sleep 2 fi +done +echo "PostgreSQL is ready." + +# create test database if it doesn't exist +exists=$(docker exec test-postgres psql -U user123 -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='onboarding_db_test'") +echo "exists=$exists" +if [[ -z "$exists" || "$exists" != *"1"* ]]; then + npx sequelize-cli db:create --env test else - # Check if database exists locally - if ! psql -U postgres -lqt | cut -d \| -f 1 | grep -qw test_db; then - npx sequelize-cli db:create --env test - else - echo "Database 'test_db' already exists locally." - fi + echo "Database 'onboarding_db_test' already exists in Docker." fi # run migrations From 4ba4da930f4a0f0ad39d6d44f047008c1a1d0547 Mon Sep 17 00:00:00 2001 From: Debora Serra Date: Tue, 7 Jan 2025 20:44:30 -0800 Subject: [PATCH 14/14] chore: add error treatment to config --- backend/config/config.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/config/config.js b/backend/config/config.js index e9619c8a..0e97d633 100644 --- a/backend/config/config.js +++ b/backend/config/config.js @@ -2,7 +2,12 @@ const dotenv = require('dotenv'); const envSuffix = process.env.NODE_ENV && process.env.NODE_ENV !== 'development' ? `.${process.env.NODE_ENV}` : ''; const env = `.env${envSuffix}`; -dotenv.config({ path: `./${env}` }); +const result = dotenv.config({ path: `./${env}` }); + +if (result.error) { + console.error(`Failed to load environment file: ${env}`); + process.exit(1); +} const { DEV_DB_HOST,