Skip to content

Commit

Permalink
usage of enum and fixed sql
Browse files Browse the repository at this point in the history
  • Loading branch information
JibrilExe committed Mar 21, 2024
1 parent 6682bfc commit 9751348
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 43 deletions.
4 changes: 2 additions & 2 deletions backend/db_construct.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CREATE TYPE role_options AS ENUM ('student', 'teacher', 'admin');
CREATE TYPE role AS ENUM ('STUDENT', 'TEACHER', 'ADMIN');

CREATE TABLE users (
uid VARCHAR(255),
role role_options NOT NULL,
role role NOT NULL,
PRIMARY KEY(uid)
);

Expand Down
2 changes: 1 addition & 1 deletion backend/project/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class User(db.Model):

__tablename__ = "users"
uid: str = Column(String(255), primary_key=True)
role: Role = Column(EnumField(Role))
role: Role = Column(EnumField(Role), nullable=False)
10 changes: 6 additions & 4 deletions backend/project/utils/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from project import db

from project.models.user import User
from project.models.user import User,Role
from project.models.course import Course
from project.models.project import Project
from project.models.submission import Submission
Expand Down Expand Up @@ -62,9 +62,11 @@ def return_authenticated_user_id():

if user:
return auth_user_id
role = 'student'

# Use the Enum here
role = Role.STUDENT
if user_info["jobTitle"] != None:
role = 'teacher'
role = Role.TEACHER

# add user if not yet in database
try:
Expand All @@ -89,7 +91,7 @@ def is_teacher(auth_user_id):
"url": f"{API_URL}/users"}, 500
if not user: # should realistically never happen
abort(500, "A database error occured")
if user.role == 'teacher':
if user.role == Role.TEACHER:
return True
return False

Expand Down
10 changes: 5 additions & 5 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from project.sessionmaker import engine, Session
from project.db_in import db
from project.models.course import Course
from project.models.user import User
from project.models.user import User,Role
from project.models.project import Project
from project.models.course_relation import CourseStudent,CourseAdmin
from project.models.submission import Submission
Expand Down Expand Up @@ -34,10 +34,10 @@ def db_session():
def users():
"""Return a list of users to populate the database"""
return [
User(uid="brinkmann", role='admin'),
User(uid="laermans", role='admin'),
User(uid="student01", role='student'),
User(uid="student02", role='student')
User(uid="brinkmann", role=Role.ADMIN),
User(uid="laermans", role=Role.ADMIN),
User(uid="student01", role=Role.STUDENT),
User(uid="student02", role=Role.STUDENT)
]

def courses():
Expand Down
18 changes: 9 additions & 9 deletions backend/tests/endpoints/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from zoneinfo import ZoneInfo
import pytest
from sqlalchemy import create_engine
from project.models.user import User
from project.models.user import User,Role
from project.models.course import Course
from project.models.course_share_code import CourseShareCode
from project import create_app_with_db
Expand Down Expand Up @@ -46,7 +46,7 @@ def valid_user():
"""
return {
"uid": "w_student",
"role": 'student'
"role": Role.STUDENT
}

@pytest.fixture
Expand All @@ -73,10 +73,10 @@ def valid_user_entries(session):
Returns a list of users that are in the database
"""
users = [
User(uid="del", role='teacher'),
User(uid="pat", role='teacher'),
User(uid="u_get", role='teacher'),
User(uid="query_user", role='admin')]
User(uid="del", role=Role.TEACHER),
User(uid="pat", role=Role.TEACHER),
User(uid="u_get", role=Role.TEACHER),
User(uid="query_user", role=Role.ADMIN)]

session.add_all(users)
session.commit()
Expand Down Expand Up @@ -127,7 +127,7 @@ def app():
@pytest.fixture
def course_teacher_ad():
"""A user that's a teacher for testing"""
ad_teacher = User(uid="Gunnar", role='teacher')
ad_teacher = User(uid="Gunnar", role=Role.TEACHER)
return ad_teacher

@pytest.fixture
Expand Down Expand Up @@ -177,7 +177,7 @@ def client(app):
@pytest.fixture
def valid_teacher_entry(session):
"""A valid teacher for testing that's already in the db"""
teacher = User(uid="Bart", role='teacher')
teacher = User(uid="Bart", role=Role.TEACHER)
session.add(teacher)
session.commit()
return teacher
Expand All @@ -204,7 +204,7 @@ def valid_course_entry(session, valid_course):
def valid_students_entries(session):
"""Valid students for testing that are already in the db"""
students = [
User(uid=f"student_sel2_{i}", role='student')
User(uid=f"student_sel2_{i}", role=Role.STUDENT)
for i in range(3)
]
session.add_all(students)
Expand Down
34 changes: 17 additions & 17 deletions backend/tests/endpoints/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from project.models.user import User
from project.models.user import User,Role
from project.db_in import db
from tests import db_url

Expand All @@ -24,12 +24,12 @@ def user_db_session():
db.metadata.create_all(engine)
session = Session()
session.add_all(
[User(uid="del", role='teacher'),
User(uid="pat", role='teacher'),
User(uid="u_get", role='teacher'),
User(uid="query_user", role='admin')
]
)
[User(uid="del", role=Role.TEACHER),
User(uid="pat", role=Role.TEACHER),
User(uid="u_get", role=Role.TEACHER),
User(uid="query_user", role=Role.ADMIN)
]
)
session.commit()
yield session
session.rollback()
Expand Down Expand Up @@ -117,12 +117,12 @@ def test_get_one_user_wrong_authentication(self, client, valid_user_entry):
def test_patch_user(self, client, valid_user_entry):
"""Test updating a user."""

if valid_user_entry.role == 'teacher':
new_role = 'admin'
if valid_user_entry.role == 'admin':
new_role = 'student'
if valid_user_entry.role == Role.TEACHER:
new_role = Role.ADMIN
if valid_user_entry.role == Role.ADMIN:
new_role = Role.STUDENT
else:
new_role = 'teacher'
new_role = Role.TEACHER

response = client.patch(f"/users/{valid_user_entry.uid}", json={
'role': new_role
Expand All @@ -132,17 +132,17 @@ def test_patch_user(self, client, valid_user_entry):
def test_patch_non_existent(self, client):
"""Test updating a non-existent user."""
response = client.patch("/users/-20", json={
'role': 'teacher'
'role': Role.TEACHER
})
assert response.status_code == 403 # Patching is not allowed

def test_patch_non_json(self, client, valid_user_entry):
"""Test sending a non-JSON patch request."""
valid_user_form = asdict(valid_user_entry)
if valid_user_form["role"] == 'teacher':
valid_user_form["role"] = 'student'
if valid_user_form["role"] == Role.TEACHER:
valid_user_form["role"] = Role.STUDENT
else:
valid_user_form["role"] = 'teacher'
valid_user_form["role"] = Role.TEACHER
response = client.patch(f"/users/{valid_user_form['uid']}", data=valid_user_form)
assert response.status_code == 403 # Patching is not allowed

Expand All @@ -155,4 +155,4 @@ def test_get_users_with_query(self, client, valid_user_entries):
# Check that the response contains only the user that matches the query
users = response.json["data"]
for user in users:
assert user["role"] == 'admin'
assert user["role"] == Role.ADMIN
10 changes: 5 additions & 5 deletions backend/tests/models/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from pytest import raises, mark
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
from project.models.user import User
from project.models.user import User,Role

class TestUserModel:
"""Class to test the User model"""

def test_create_user(self, session: Session):
"""Test if a user can be created"""
user = User(uid="user01", role='student')
user = User(uid="user01", role=Role.STUDENT)
session.add(user)
session.commit()
assert session.get(User, "user01") is not None
Expand All @@ -21,14 +21,14 @@ def test_query_user(self, session: Session):
assert session.query(User).count() == 4
teacher = session.query(User).filter_by(uid="brinkmann").first()
assert teacher is not None
assert teacher.role == 'teacher'
assert teacher.role == Role.TEACHER

def test_update_user(self, session: Session):
"""Test if a user can be updated"""
student = session.query(User).filter_by(uid="student01").first()
student.is_admin = True
student.role = Role.ADMIN
session.commit()
assert session.get(User, "student01").is_admin
assert session.get(User, "student01").role == Role.ADMIN

def test_delete_user(self, session: Session):
"""Test if a user can be deleted"""
Expand Down

0 comments on commit 9751348

Please sign in to comment.