diff --git a/.gitignore b/.gitignore index b33c328..a3d8243 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ lerna-debug.log* .env.dev .env.test .env.prod +!test/resources/.env.test # temp directory .temp .tmp diff --git a/scripts/run-e2e b/scripts/run-e2e new file mode 100755 index 0000000..82d84ab --- /dev/null +++ b/scripts/run-e2e @@ -0,0 +1,76 @@ +#!/bin/bash + +# Define constants +MARKER="\033[32m#\033[0m" + +# Define paths +ROOT_ENV=".env.test" +TEST_ENV="test/resources/.env.test" +BACKUP_ENV=".env.test.backup" +POSTGRES_COMPOSE_FILE="test/resources/postgresql.yml" + +get_env_var() { + local var_name="$1" + grep -E "^${var_name}=" "$ROOT_ENV" | cut -d '=' -f 2 +} + +title() { + echo -e "$MARKER $1" && sleep 1 +} + +linebreak() { + echo +} + +title "Check if the test .env file exists in the source directory" +if [ ! -f "$TEST_ENV" ]; then + echo "Error: $TEST_ENV does not exist. Aborting." + exit 1 +fi +linebreak + +title "Back up the existing .env.test file if it exists" +if [ -f "$ROOT_ENV" ]; then + echo "Backing up existing $ROOT_ENV to $BACKUP_ENV..." + cp "$ROOT_ENV" "$BACKUP_ENV" || { echo "Failed to create backup. Aborting."; exit 1; } +fi +linebreak + +title "Copy the new .env.test file to the root directory" +echo "Replacing $ROOT_ENV with $TEST_ENV..." +cp "$TEST_ENV" "$ROOT_ENV" || { echo "Failed to copy $TEST_ENV to $ROOT_ENV. Aborting."; exit 1; } +linebreak + +title "Starting PostgreSQL container..." +docker-compose --env-file "$ROOT_ENV" -f "$POSTGRES_COMPOSE_FILE" up -d || { echo "Failed to start Docker container. Aborting."; exit 1; } +linebreak + +title "Wait for PostgreSQL to be ready" +POSTGRES_USER=$(get_env_var "DB_USER") +until docker exec -it $(docker ps -q -f "name=postgres-e2e") pg_isready -U "$POSTGRES_USER"; do + echo "Waiting for PostgreSQL to be ready..." + sleep 2 +done +linebreak + +title "Run migration and the NestJS e2e tests" +export NODE_ENV="test" +yarn run migration:run && yarn run test:e2e +linebreak + +title "Stopping PostgreSQL container..." +docker-compose --env-file "$ROOT_ENV" -f "$POSTGRES_COMPOSE_FILE" down --volumes +linebreak + +title "Restore the original .env.test file if it existed" +if [ -f "$BACKUP_ENV" ]; then + echo "Restoring the original $ROOT_ENV from backup..." + mv "$BACKUP_ENV" "$ROOT_ENV" || { echo "Failed to restore original .env.test. Please check manually."; exit 1; } +else + # If no original .env.test existed, remove the temporary one after testing + echo "No original .env.test backup found. Cleaning up temporary file." + rm "$ROOT_ENV" +fi +linebreak + +title "Test run completed successfully." diff --git a/src/config/typeorm.config.ts b/src/config/typeorm.config.ts index c46faf8..82c5f74 100644 --- a/src/config/typeorm.config.ts +++ b/src/config/typeorm.config.ts @@ -1,3 +1,4 @@ +process.env.DOTENV_CONFIG_PATH = `${process.cwd()}/.env.${process.env.NODE_ENV ? `${process.env.NODE_ENV}` : 'dev'}`; import 'dotenv/config'; import { DataSource } from 'typeorm'; import { DictionaryWord } from '../dictionary-records/entities/dictionary-record.entity'; diff --git a/test/resources/.env.test b/test/resources/.env.test new file mode 100644 index 0000000..6e56164 --- /dev/null +++ b/test/resources/.env.test @@ -0,0 +1,19 @@ +# Rate Limit +# General purpose +THROTTLE_TTL=60000 +THROTTLE_LIMIT=120 + +# PostgreSQL connection properties +DB_HOST=localhost +DB_PORT=6432 +DB_USER=dictionary_api_e2e +DB_PASSWORD=dictionary_api_e2e +DB_DATABASE=dictionary_api_e2e + +# Google OAuth 2.0 +GOOGLE_CLIENT_ID=google-client-id +GOOGLE_CLIENT_SECRET=google-client-secret +GOOGLE_REDIRECT_URL=http://localhost:3000/auth/google/redirect + +# JWT Signing Secret +JWT_SECRET=secret diff --git a/test/resources/postgresql.yml b/test/resources/postgresql.yml new file mode 100644 index 0000000..2e007b4 --- /dev/null +++ b/test/resources/postgresql.yml @@ -0,0 +1,11 @@ +services: + postgres: + image: postgres:latest + container_name: postgres-e2e + restart: always + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: ${DB_DATABASE} + ports: + - "${DB_PORT}:5432"