From a25e113277468717a1c7f2495016389e23174e63 Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 01:37:41 +0530 Subject: [PATCH 01/14] fix: tests --- .github/workflows/actions.yml | 27 --------------------------- .github/workflows/tests.yml | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 27 deletions(-) delete mode 100644 .github/workflows/actions.yml create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml deleted file mode 100644 index b9be9a64..00000000 --- a/.github/workflows/actions.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Run Tests for Backend - -on: - push: - branches: ["main"] - pull_request: - branches: ["main"] - workflow_dispatch: - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout page source - uses: actions/checkout@v4 - - - name: Build the Docker image and run tests - run: docker compose -f "backend/compose.test.yaml" up -d - - - name: Print Logs for Container - run: | - export CONTAINER_ID=$(docker ps -aqf "name=backend-test_ci-1") - docker logs -f $CONTAINER_ID 2>&1 | grep -i "FAILED" && exit 1 || exit 0 - - - name: Stop and remove the container - run: docker compose -f "backend/compose.test.yaml" down diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 00000000..c7654e24 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,28 @@ +name: Run Tests for Backend + +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout page source + uses: actions/checkout@v4 + + - name: MongoDB in GitHub Actions + uses: supercharge/mongodb-github-action@1.10.0 + with: + mongodb-port: 27017 + + - name: load env file + run: | + echo "${{ secrets.TEST_ENV_FILE }}" > backend/.env + + - name: Build the Docker image and run tests + run: cd backend && python -m unittest discover -s tests \ No newline at end of file From 0c39d5ee2798d6f2f97a45ecaac86ac0e4ac2dae Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 01:39:08 +0530 Subject: [PATCH 02/14] fix: tests --- .github/workflows/tests.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c7654e24..35363f6c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,7 +18,13 @@ jobs: - name: MongoDB in GitHub Actions uses: supercharge/mongodb-github-action@1.10.0 with: - mongodb-port: 27017 + mongodb-port: 27017 + + - name: Setup Python + uses: actions/setup-python@v5 + + - name: Install python libraries + run: pip3 install --user -r backend/requirements.txt - name: load env file run: | From 27af742a816c79155a0b6cbadebe60dab381dbe5 Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 02:03:59 +0530 Subject: [PATCH 03/14] fix: tests --- backend/packages.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/packages.py b/backend/packages.py index c5bee13b..3b95b11f 100644 --- a/backend/packages.py +++ b/backend/packages.py @@ -22,6 +22,7 @@ from models.user import User from models.package import Package from models.package import Version +from bson import json_util parameters = { @@ -604,7 +605,7 @@ def get_package_from_version(namespace_name, package_name, version): return jsonify({"message": "Package not found", "code": 404}), 404 else: - package_obj = Package.from_json(json.dumps(package)) + package_obj = Package.from_json(json_util.dumps(package)) # Get the package author from id. package_author = db.users.find_one({"_id": package_obj.author}) From aa93ef2f205cfe1c7256d333261006c58ebd10fc Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 10:38:29 +0530 Subject: [PATCH 04/14] fix: rating bugs --- backend/packages.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/backend/packages.py b/backend/packages.py index 3b95b11f..549739bb 100644 --- a/backend/packages.py +++ b/backend/packages.py @@ -892,19 +892,17 @@ def post_ratings(namespace, package): ) user = db.users.find_one({"uuid": uuid}) + if not user: + return jsonify({"code": 404, "message": "User not found"}), 404 + namespace_doc = db.namespaces.find_one({"namespace": namespace}) + if not namespace_doc: + return jsonify({"code": 404, "message": "Namespace not found"}), 404 package_doc = db.packages.find_one( {"name": package, "namespace": namespace_doc["_id"]} ) - - if not user or not namespace_doc or not package_doc: - error_message = { - "user": "User not found" if not user else None, - "namespace": "Namespace not found" if not namespace_doc else None, - "package": "Package not found" if not package_doc else None, - "code": 404 - } - return jsonify({"message": error_message}), 404 + if not package_doc: + return jsonify({"code": 404, "message": "Package not found"}), 404 db.packages.update_one( {"name": package, "namespace": namespace_doc["_id"]}, @@ -971,19 +969,18 @@ def post_malicious(namespace, package): ) user = db.users.find_one({"uuid": uuid}) + + if not user: + return jsonify({"code": 404, "message": "User not found"}), 404 + namespace_doc = db.namespaces.find_one({"namespace": namespace}) + if not namespace_doc: + return jsonify({"code": 404, "message": "Namespace not found"}), 404 package_doc = db.packages.find_one( {"name": package, "namespace": namespace_doc["_id"]} ) - - if not user or not namespace_doc or not package_doc: - error_message = { - "user": "User not found" if not user else None, - "namespace": "Namespace not found" if not namespace_doc else None, - "package": "Package not found" if not package_doc else None, - "code": 404 - } - return jsonify({"message": error_message}), 404 + if not package_doc: + return jsonify({"code": 404, "message": "Package not found"}), 404 package_version_doc = db.packages.update_one( {"name": package, "namespace": namespace_doc["_id"]}, From a7ab29bced257417f0869d73ee625d5e77bb05cf Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 10:42:46 +0530 Subject: [PATCH 05/14] fix: tests --- backend/tests/test_packages.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/tests/test_packages.py b/backend/tests/test_packages.py index 35ee39e1..6530752d 100644 --- a/backend/tests/test_packages.py +++ b/backend/tests/test_packages.py @@ -409,6 +409,7 @@ def test_successful_rating_submit(self): AssertionError: If the response code received from the server is not as expected. """ access_token = self.login() + upload_response = self.upload() response = self.client.post( f"/ratings/{self.test_namespace_data['namespace']}/{self.test_package_data['package_name']}", content_type="multipart/form-data", @@ -475,6 +476,7 @@ def test_successful_post_malicious(self): AssertionError: If the response code received from the server is not as expected. """ access_token = self.login() + upload_response = self.upload() response = self.client.post( f"/report/{self.test_namespace_data['namespace']}/{self.test_package_data['package_name']}", content_type="multipart/form-data", From 5502c97edcb53fd9a0c0488e0bbddc8520d7c7e7 Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 10:48:44 +0530 Subject: [PATCH 06/14] fix: tests --- backend/tests/test_packages.py | 5 +++-- backend/tests/test_signup.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/tests/test_packages.py b/backend/tests/test_packages.py index 6530752d..49ab533a 100644 --- a/backend/tests/test_packages.py +++ b/backend/tests/test_packages.py @@ -6,6 +6,7 @@ import random import os from dotenv import load_dotenv +from test_signup import test_successful_sudo_signup load_dotenv() @@ -482,7 +483,6 @@ def test_successful_post_malicious(self): content_type="multipart/form-data", data={"reason":"the package is found to be malicious"}, headers={"Authorization": f"Bearer {access_token}"}, ) - print("test_successful_post_malicious response", response.json) self.assertEqual(200, response.json["code"]) print("test_successful_post_malicious passed") @@ -544,7 +544,8 @@ def test_successful_fetch_malicious_reports(self): AssertionError: If the response code received from the server is not as expected. """ - access_token = self.login() # create a sudo user + test_successful_sudo_signup = test_successful_sudo_signup() # create a sudo user + access_token = test_successful_sudo_signup["access_token"] response = self.client.get("/report/view",headers={"Authorization": f"Bearer {access_token}"}) self.assertEqual(200, response.json["code"]) print("test_successful_fetch_malicious_reports passed") diff --git a/backend/tests/test_signup.py b/backend/tests/test_signup.py index 1bb8aece..25f959a5 100644 --- a/backend/tests/test_signup.py +++ b/backend/tests/test_signup.py @@ -132,4 +132,5 @@ def test_successful_sudo_signup(self): } response = self.client.post('/auth/signup', data=data) - self.assertEqual(200, response.status_code) \ No newline at end of file + self.assertEqual(200, response.status_code) + return response.json \ No newline at end of file From e9b2a6a9f761d16a05ea0f00c6477b8999b10a7c Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 10:53:24 +0530 Subject: [PATCH 07/14] fix: tests --- backend/tests/test_packages.py | 8 +++----- backend/tests/test_signup.py | 3 +-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/backend/tests/test_packages.py b/backend/tests/test_packages.py index 49ab533a..454f1402 100644 --- a/backend/tests/test_packages.py +++ b/backend/tests/test_packages.py @@ -6,7 +6,6 @@ import random import os from dotenv import load_dotenv -from test_signup import test_successful_sudo_signup load_dotenv() @@ -36,7 +35,7 @@ def setUp(self): "namespace_description": "Test namespace description", } - def login(self): + def login(self,is_sudo=False): """ Helper function to signup and login a user. @@ -52,7 +51,7 @@ def login(self): signup_data = { "email": self.email, - "password": self.password, + "password": self.password if not is_sudo else os.getenv("SUDO_PASSWORD"), "username": self.username, } if self.is_created: @@ -544,8 +543,7 @@ def test_successful_fetch_malicious_reports(self): AssertionError: If the response code received from the server is not as expected. """ - test_successful_sudo_signup = test_successful_sudo_signup() # create a sudo user - access_token = test_successful_sudo_signup["access_token"] + access_token = self.login(is_sudo=True) # create a sudo user response = self.client.get("/report/view",headers={"Authorization": f"Bearer {access_token}"}) self.assertEqual(200, response.json["code"]) print("test_successful_fetch_malicious_reports passed") diff --git a/backend/tests/test_signup.py b/backend/tests/test_signup.py index 25f959a5..1bb8aece 100644 --- a/backend/tests/test_signup.py +++ b/backend/tests/test_signup.py @@ -132,5 +132,4 @@ def test_successful_sudo_signup(self): } response = self.client.post('/auth/signup', data=data) - self.assertEqual(200, response.status_code) - return response.json \ No newline at end of file + self.assertEqual(200, response.status_code) \ No newline at end of file From 77562bc0306ed147373b96259784cc5d3497b7ee Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 11:01:41 +0530 Subject: [PATCH 08/14] fix: tests --- backend/tests/test_packages.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/tests/test_packages.py b/backend/tests/test_packages.py index 454f1402..9984b830 100644 --- a/backend/tests/test_packages.py +++ b/backend/tests/test_packages.py @@ -320,8 +320,8 @@ def test_get_existing_package_version(self): response = self.client.get( f"/packages/{self.test_namespace_data['namespace']}/{self.test_package_data['package_name']}/0.0.1" ) - print(response.json["message"]) - self.assertEqual(200, response.json["code"]) + + self.assertEqual(200, response.json["code"]) # TODO: Check after fixing : get_package_from_version 608 print("test_get_existing_package_version passed") def test_package_invalid_license(self): @@ -545,6 +545,7 @@ def test_successful_fetch_malicious_reports(self): access_token = self.login(is_sudo=True) # create a sudo user response = self.client.get("/report/view",headers={"Authorization": f"Bearer {access_token}"}) + print(response.json) self.assertEqual(200, response.json["code"]) print("test_successful_fetch_malicious_reports passed") From 6382512aafb45c43aeb47203b7f0b915db3a1e85 Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 11:03:58 +0530 Subject: [PATCH 09/14] fix: tests --- backend/tests/test_packages.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/tests/test_packages.py b/backend/tests/test_packages.py index 9984b830..7b5c4c32 100644 --- a/backend/tests/test_packages.py +++ b/backend/tests/test_packages.py @@ -60,7 +60,7 @@ def login(self,is_sudo=False): response_for_signup = self.client.post("/auth/signup", data=signup_data) self.assertEqual(200, response_for_signup.json["code"]) self.is_created = True - login_data = {"user_identifier": self.email, "password": self.password} + login_data = {"user_identifier": self.email, "password": self.password if not is_sudo else os.getenv("SUDO_PASSWORD")} # Login with the same user. response_for_login = self.client.post("/auth/login", data=login_data) @@ -545,7 +545,6 @@ def test_successful_fetch_malicious_reports(self): access_token = self.login(is_sudo=True) # create a sudo user response = self.client.get("/report/view",headers={"Authorization": f"Bearer {access_token}"}) - print(response.json) self.assertEqual(200, response.json["code"]) print("test_successful_fetch_malicious_reports passed") From d388e937fc1087e57dfd1fd84390fe3b617145eb Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 11:07:49 +0530 Subject: [PATCH 10/14] fix: tests --- backend/packages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/packages.py b/backend/packages.py index 549739bb..4b8b5908 100644 --- a/backend/packages.py +++ b/backend/packages.py @@ -605,7 +605,7 @@ def get_package_from_version(namespace_name, package_name, version): return jsonify({"message": "Package not found", "code": 404}), 404 else: - package_obj = Package.from_json(json_util.dumps(package)) + package_obj = Package.from_json(json.loads(json_util.dumps(package))) # Get the package author from id. package_author = db.users.find_one({"_id": package_obj.author}) From c0402f9f4ec47d642e22419e1b57ec523bcc6156 Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 11:10:00 +0530 Subject: [PATCH 11/14] fix: tests --- backend/packages.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/packages.py b/backend/packages.py index 4b8b5908..6e99376c 100644 --- a/backend/packages.py +++ b/backend/packages.py @@ -607,6 +607,7 @@ def get_package_from_version(namespace_name, package_name, version): else: package_obj = Package.from_json(json.loads(json_util.dumps(package))) + print(package_obj.to_json()) # Get the package author from id. package_author = db.users.find_one({"_id": package_obj.author}) From da121756363da1c07386c557d742d490ba714c1c Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 11:17:59 +0530 Subject: [PATCH 12/14] fix: tests --- backend/packages.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/packages.py b/backend/packages.py index 6e99376c..c571521b 100644 --- a/backend/packages.py +++ b/backend/packages.py @@ -605,6 +605,7 @@ def get_package_from_version(namespace_name, package_name, version): return jsonify({"message": "Package not found", "code": 404}), 404 else: + print(package) package_obj = Package.from_json(json.loads(json_util.dumps(package))) print(package_obj.to_json()) From 305959104c5e2988ff10c7cd30fa1e0042befbcc Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 11:51:08 +0530 Subject: [PATCH 13/14] fix: tests --- backend/packages.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/packages.py b/backend/packages.py index c571521b..b57802bb 100644 --- a/backend/packages.py +++ b/backend/packages.py @@ -606,7 +606,7 @@ def get_package_from_version(namespace_name, package_name, version): else: print(package) - package_obj = Package.from_json(json.loads(json_util.dumps(package))) + package_obj = Package.from_json(package) print(package_obj.to_json()) # Get the package author from id. From 9e198bf9c5599c0e6977b6e1aea1eee7e24bf9f2 Mon Sep 17 00:00:00 2001 From: Henil Panchal Date: Sun, 24 Mar 2024 11:59:56 +0530 Subject: [PATCH 14/14] fix: tests --- backend/packages.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/packages.py b/backend/packages.py index b57802bb..59a4d9c5 100644 --- a/backend/packages.py +++ b/backend/packages.py @@ -605,10 +605,8 @@ def get_package_from_version(namespace_name, package_name, version): return jsonify({"message": "Package not found", "code": 404}), 404 else: - print(package) package_obj = Package.from_json(package) - print(package_obj.to_json()) # Get the package author from id. package_author = db.users.find_one({"_id": package_obj.author})