Skip to content

Commit

Permalink
fixed linter
Browse files Browse the repository at this point in the history
  • Loading branch information
warreprovoost committed Feb 25, 2024
1 parent f072cfc commit 3006e43
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
57 changes: 30 additions & 27 deletions backend/project/endpoints/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def get(self):
It should return all users from the database.
"""
users = UserModel.query.all()
users_list = [{"uid": user.uid, "is_teacher": user.is_teacher, "is_admin": user.is_admin} for user in users]
users_list = [{"uid": user.uid, "is_teacher": user.is_teacher, "is_admin": user.is_admin}
for user in users]
return users_list

def post(self):
Expand Down Expand Up @@ -47,31 +48,33 @@ def post(self):
return {"Message": "User created successfully!"}

def patch(self):
"""
Update the user's information.
Returns:
dict: A dictionary containing the message indicating the success or failure of the update.
"""
uid = request.json.get('uid')
is_teacher = request.json.get('is_teacher')
is_admin = request.json.get('is_admin')
if uid is None:
return {"Message": "User ID is required!"}, 400

user = UserModel.query.get(uid)
if user is None:
return {"Message": "User not found!"}, 404

if is_teacher is not None:
user.is_teacher = is_teacher
if is_admin is not None:
user.is_admin = is_admin

# Save the changes to the database
db.session.commit()
return {"Message": "User updated successfully!"}

"""
Update the user's information.
Returns:
dict: A dictionary containing the message indicating the success
or failure of the update.
"""
uid = request.json.get('uid')
is_teacher = request.json.get('is_teacher')
is_admin = request.json.get('is_admin')
if uid is None:
return {"Message": "User ID is required!"}, 400


user = db.session.get(UserModel,uid)
if user is None:
return {"Message": "User not found!"}, 404

if is_teacher is not None:
user.is_teacher = is_teacher
if is_admin is not None:
user.is_admin = is_admin

# Save the changes to the database
db.session.commit()
return {"Message": "User updated successfully!"}

def delete(self):
"""
This function will respond to DELETE requests made to /users.
Expand All @@ -82,7 +85,7 @@ def delete(self):
if uid is None:
return {"Message": "User ID is required!"}, 400

user = UserModel.query.get(uid)
user = db.session.get(UserModel, uid)
if user is None:
return {"Message": "User not found!"}, 404

Expand Down
12 changes: 11 additions & 1 deletion backend/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"""
This module is used to create a SQLAlchemy URL object for a PostgreSQL database.
It uses environment variables to get the necessary database configuration details:
- 'POSTGRES_DB': The name of the database.
- 'POSTGRES_USER': The username to connect to the database.
- 'POSTGRES_PASSWORD': The password to connect to the database.
- 'POSTGRES_HOST': The host where the database is located.
"""
import os
from sqlalchemy.engine.url import URL
from dotenv import load_dotenv
Expand All @@ -13,4 +23,4 @@
host=DATABASE_HOST,
database=DATABSE_NAME,
password=DATABASE_PASSWORD
)
)
2 changes: 1 addition & 1 deletion backend/tests/endpoints/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Configuration for pytest, Flask, and the test client."""
import pytest
from project import create_app_with_db
from sqlalchemy import create_engine
from project import create_app_with_db
from project import db
from tests import db_url

Expand Down
13 changes: 11 additions & 2 deletions backend/tests/endpoints/user_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
"""
This module tests user management endpoints.
- test_post_delete_user: Tests user creation, deletion, and error handling for deletion
of non-existent user.
- test_get_users: Tests retrieval of all users, ensuring the response is a list.
- test_patch_user: Tests user update functionality and error handling for updating
non-existent user.
"""
def test_post_delete_user(client):
"""Test whether the users page is accessible"""
response = client.post("/users", json={
Expand All @@ -9,7 +18,7 @@ def test_post_delete_user(client):
# Delete the user
response = client.delete("/users", json={'uid': 'del'})
assert response.status_code == 200
assert response.json == {"Message": f"User with id: del deleted successfully!"}
assert response.json == {"Message": "User with id: del deleted successfully!"}

# Try to delete the user again
response = client.delete("/users", json={'uid': 'del'})
Expand Down Expand Up @@ -53,4 +62,4 @@ def test_patch_user(client):
'is_teacher': False,
'is_admin': True
})
assert response.status_code == 404
assert response.status_code == 404
3 changes: 0 additions & 3 deletions backend/tests/models/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
Configuration for the models tests. Contains all the fixtures needed for multiple models tests.
"""

import os
from datetime import datetime
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine.url import URL
from dotenv import load_dotenv
import pytest
from project import db
from project.models.courses import Courses
Expand Down

0 comments on commit 3006e43

Please sign in to comment.