Skip to content

Commit

Permalink
Merge pull request #71 from henilp105/main
Browse files Browse the repository at this point in the history
fix: CI, tests, bugs
  • Loading branch information
henilp105 authored Mar 24, 2024
2 parents 405d055 + 190153a commit 9f7f35b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 53 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/actions.yml

This file was deleted.

34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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/[email protected]
with:
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: |
echo "${{ secrets.TEST_ENV_FILE }}" > backend/.env
- name: Build the Docker image and run tests
run: cd backend && python -m unittest discover -s tests
36 changes: 17 additions & 19 deletions backend/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(package)

# Get the package author from id.
package_author = db.users.find_one({"_id": package_obj.author})
Expand Down Expand Up @@ -891,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"]},
Expand Down Expand Up @@ -970,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"]},
Expand Down
15 changes: 8 additions & 7 deletions backend/tests/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,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.
Expand All @@ -51,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:
Expand All @@ -60,7 +60,7 @@ def login(self):
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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -475,12 +476,12 @@ 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",
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")

Expand Down Expand Up @@ -542,7 +543,7 @@ 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
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")
Expand Down

0 comments on commit 9f7f35b

Please sign in to comment.