From 33b9753f1e3ff8d5a5f4264d3cfe6f250ce08f85 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 9 Oct 2024 02:49:54 +0800 Subject: [PATCH 1/4] feat: add test workflow --- .github/workflows/test.yml | 71 +++++++++++++++++++++++++++ apps/frontend/.env.example | 2 +- apps/question-service/.env.example | 1 + apps/user-service/.env.sample | 7 ++- apps/user-service/model/repository.js | 7 +-- apps/user-service/package-lock.json | 22 ++++----- apps/user-service/package.json | 2 +- services/question-service/.gitignore | 3 -- 8 files changed, 90 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/test.yml delete mode 100644 services/question-service/.gitignore diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000000..339d4eb64b --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,71 @@ +name: Run Tests + +on: + push: + branches: + - main + - staging + pull_request: + branches: + - main + - staging + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Compose + run: | + sudo apt-get update + sudo apt-get install -y docker-compose + + - name: Create Environment Files + env: + QUESTION_SERVICE_URL: ${{ vars.QUESTION_SERVICE_URL }} + USER_SERVICE_URL: ${{ vars.USER_SERVICE_URL }} + JWT_SECRET: ${{ secrets.JWT_SECRET }} + FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }} + DB_CLOUD_URI: ${{ secrets.USER_SERVICE_DB_CLOUD_URI }} + USER_SERVICE_PORT: ${{ vars.USER_SERVICE_PORT }} + run: | + cd ./apps/frontend + echo "NEXT_PUBLIC_QUESTION_SERVICE_URL=$QUESTION_SERVICE_URL" >> .env + echo "NEXT_PUBLIC_USER_SERVICE_URL=$USER_SERVICE_URL" >> .env + + cd ../question-service + echo "FIREBASE_CREDENTIAL_PATH=$FIREBASE_CREDENTIAL_PATH" >> .env + echo "JWT_SECRET=$JWT_SECRET" >> .env + + cd ../user-service + echo "DB_CLOUD_URI=$DB_CLOUD_URI" >> .env + echo "PORT=$USER_SERVICE_PORT" >> .env + echo "JWT_SECRET=$JWT_SECRET" >> .env + + - name: Create Database Credential Files + env: + FIREBASE_JSON: ${{ secrets.QUESTION_SERVICE_FIREBASE_CREDENTIAL }} + FIREBASE_CREDENTIAL_PATH: ${{ vars.QUESTION_SERVICE_FIREBASE_CREDENTIAL_PATH }} + run: | + cd ./apps/question-service + echo "$FIREBASE_JSON" > "./$FIREBASE_CREDENTIAL_PATH" + + - name: Build and Run Services + run: | + cd ./apps + docker-compose up --build -d + + - name: Run Tests + env: + FRONTEND_URL: ${{ vars.FRONTEND_URL }} + USER_SERVICE_URL: ${{ vars.USER_SERVICE_URL }} + QUESTION_SERVICE_URL: ${{ vars.QUESTION_SERVICE_URL }} + run: | + curl -sSL $QUESTION_SERVICE_URL + # curl -fsSL $FRONTEND_URL + # curl -fsSL l $USER_SERVICE_URL + + # We can add more tests here diff --git a/apps/frontend/.env.example b/apps/frontend/.env.example index e9374508e5..a6c274c271 100644 --- a/apps/frontend/.env.example +++ b/apps/frontend/.env.example @@ -1,3 +1,3 @@ -# Replace with the corresponding url, credentials and endpoint +# URL endpoints of the services NEXT_PUBLIC_QUESTION_SERVICE_URL="http://localhost:8080/" NEXT_PUBLIC_USER_SERVICE_URL="http://localhost:3001/" \ No newline at end of file diff --git a/apps/question-service/.env.example b/apps/question-service/.env.example index 2acc980209..88d758a7be 100644 --- a/apps/question-service/.env.example +++ b/apps/question-service/.env.example @@ -1,3 +1,4 @@ +# Path to the firebase credential json FIREBASE_CREDENTIAL_PATH=cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json # Secret for creating JWT signature diff --git a/apps/user-service/.env.sample b/apps/user-service/.env.sample index b3518e0224..e8f773b6d4 100644 --- a/apps/user-service/.env.sample +++ b/apps/user-service/.env.sample @@ -1,9 +1,8 @@ +# Mongo DB connection URL DB_CLOUD_URI= -DB_LOCAL_URI=mongodb://127.0.0.1:27017/peerprepUserServiceDB -PORT=3001 -# Will use cloud MongoDB Atlas database -ENV=PROD +# Port for the user-service +PORT=3001 # Secret for creating JWT signature JWT_SECRET=you-can-replace-this-with-your-own-secret diff --git a/apps/user-service/model/repository.js b/apps/user-service/model/repository.js index 5d56b91e71..feeb8ac1a9 100644 --- a/apps/user-service/model/repository.js +++ b/apps/user-service/model/repository.js @@ -3,12 +3,9 @@ import "dotenv/config"; import { connect } from "mongoose"; export async function connectToDB() { - let mongoDBUri = - process.env.ENV === "PROD" - ? process.env.DB_CLOUD_URI - : process.env.DB_LOCAL_URI; + let mongoDBUri = process.env.DB_CLOUD_URI; - await connect(mongoDBUri); + await connect(mongoDBUri) } export async function createUser(username, email, password) { diff --git a/apps/user-service/package-lock.json b/apps/user-service/package-lock.json index b78445f96c..65a36b2df8 100644 --- a/apps/user-service/package-lock.json +++ b/apps/user-service/package-lock.json @@ -14,7 +14,7 @@ "dotenv": "^16.4.5", "express": "^4.21.0", "jsonwebtoken": "^9.0.2", - "mongoose": "^8.5.4" + "mongoose": "^8.7.0" }, "devDependencies": { "nodemon": "^3.1.4" @@ -40,9 +40,9 @@ } }, "node_modules/@mongodb-js/saslprep": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.8.tgz", - "integrity": "sha512-qKwC/M/nNNaKUBMQ0nuzm47b7ZYWQHN3pcXq4IIcoSBc2hOIrflAxJduIvvqmhoz3gR2TacTAs8vlsCVPkiEdQ==", + "version": "1.1.9", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.9.tgz", + "integrity": "sha512-tVkljjeEaAhCqTzajSdgbQ6gE6f3oneVwa3iXR6csiEwXXOFsiC6Uh9iAjAhXPtqa/XMDHWjjeNH/77m/Yq2dw==", "dependencies": { "sparse-bitfield": "^3.0.3" } @@ -1157,9 +1157,9 @@ } }, "node_modules/mongodb": { - "version": "6.7.0", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.7.0.tgz", - "integrity": "sha512-TMKyHdtMcO0fYBNORiYdmM25ijsHs+Njs963r4Tro4OQZzqYigAzYQouwWRg4OIaiLRUEGUh/1UAcH5lxdSLIA==", + "version": "6.9.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.9.0.tgz", + "integrity": "sha512-UMopBVx1LmEUbW/QE0Hw18u583PEDVQmUmVzzBRH0o/xtE9DBRA5ZYLOjpLIa03i8FXjzvQECJcqoMvCXftTUA==", "dependencies": { "@mongodb-js/saslprep": "^1.1.5", "bson": "^6.7.0", @@ -1211,13 +1211,13 @@ } }, "node_modules/mongoose": { - "version": "8.5.4", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.5.4.tgz", - "integrity": "sha512-nG3eehhWf9l1q80WuHvp5DV+4xDNFpDWLE5ZgcFD5tslUV2USJ56ogun8gaZ62MKAocJnoStjAdno08b8U57hg==", + "version": "8.7.0", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.7.0.tgz", + "integrity": "sha512-rUCSF1mMYQXjXYdqEQLLlMD3xbcj2j1/hRn+9VnVj7ipzru/UoUZxlj/hWmteKMAh4EFnDZ+BIrmma9l/0Hi1g==", "dependencies": { "bson": "^6.7.0", "kareem": "2.6.3", - "mongodb": "6.7.0", + "mongodb": "6.9.0", "mpath": "0.9.0", "mquery": "5.0.0", "ms": "2.1.3", diff --git a/apps/user-service/package.json b/apps/user-service/package.json index 94174a115b..5d16cf9d59 100644 --- a/apps/user-service/package.json +++ b/apps/user-service/package.json @@ -21,6 +21,6 @@ "dotenv": "^16.4.5", "express": "^4.21.0", "jsonwebtoken": "^9.0.2", - "mongoose": "^8.5.4" + "mongoose": "^8.7.0" } } diff --git a/services/question-service/.gitignore b/services/question-service/.gitignore deleted file mode 100644 index 74b9fb5300..0000000000 --- a/services/question-service/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# please add all private keys here -cs3219-g24-staging-firebase-adminsdk-suafv-9c0d1b2299.json -cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json From 45b9b4bdd0f2b077a3fded8a206a813a397563c9 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 9 Oct 2024 02:53:55 +0800 Subject: [PATCH 2/4] fix: add test for user service --- .github/workflows/test.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 339d4eb64b..c704b6582a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -65,7 +65,6 @@ jobs: QUESTION_SERVICE_URL: ${{ vars.QUESTION_SERVICE_URL }} run: | curl -sSL $QUESTION_SERVICE_URL - # curl -fsSL $FRONTEND_URL - # curl -fsSL l $USER_SERVICE_URL + curl -fsSL $USER_SERVICE_URL # We can add more tests here From 9650fc6c3ae996d7df140ae960d99551193067b1 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 9 Oct 2024 03:06:04 +0800 Subject: [PATCH 3/4] fix: add sleep --- .github/workflows/test.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c704b6582a..d6f2a92c0f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -58,6 +58,9 @@ jobs: cd ./apps docker-compose up --build -d + - name: Wait for services to be ready + run: sleep 30 + - name: Run Tests env: FRONTEND_URL: ${{ vars.FRONTEND_URL }} @@ -66,5 +69,6 @@ jobs: run: | curl -sSL $QUESTION_SERVICE_URL curl -fsSL $USER_SERVICE_URL + curl -fsSL $FRONTEND_URL # We can add more tests here From bba2f314ba00da22d1f282285c7f363d9261bc63 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 9 Oct 2024 03:11:37 +0800 Subject: [PATCH 4/4] fix: suppress output of curl --- .github/workflows/test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d6f2a92c0f..11c92b7035 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -67,8 +67,8 @@ jobs: USER_SERVICE_URL: ${{ vars.USER_SERVICE_URL }} QUESTION_SERVICE_URL: ${{ vars.QUESTION_SERVICE_URL }} run: | - curl -sSL $QUESTION_SERVICE_URL - curl -fsSL $USER_SERVICE_URL - curl -fsSL $FRONTEND_URL + curl -sSL -o /dev/null $QUESTION_SERVICE_URL + curl -fsSL -o /dev/null $USER_SERVICE_URL + curl -fsSL -o /dev/null $FRONTEND_URL # We can add more tests here