From f2de32a945e8b03e273046f6ea6671d2f07e634e Mon Sep 17 00:00:00 2001 From: Siebe Vlietinck <71773032+Vucis@users.noreply.github.com> Date: Thu, 18 Apr 2024 20:36:38 +0200 Subject: [PATCH] Addding a displayname to a user object (#221) --- backend/db_construct.sql | 1 + backend/project/models/user.py | 11 ++++++++--- backend/tests/conftest.py | 22 +++++++++++----------- backend/tests/endpoints/conftest.py | 13 +++++++------ 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/backend/db_construct.sql b/backend/db_construct.sql index cd35fe3f..f9a31e60 100644 --- a/backend/db_construct.sql +++ b/backend/db_construct.sql @@ -5,6 +5,7 @@ CREATE TYPE runner AS ENUM ('PYTHON', 'GENERAL', 'CUSTOM'); CREATE TABLE users ( uid VARCHAR(255), + display_name VARCHAR(255), role role NOT NULL, PRIMARY KEY(uid) ); diff --git a/backend/project/models/user.py b/backend/project/models/user.py index 7cd59fd1..7bc9ed30 100644 --- a/backend/project/models/user.py +++ b/backend/project/models/user.py @@ -13,12 +13,16 @@ class Role(Enum): @dataclass class User(db.Model): - """This class defines the users table, - a user has a uid and a role, a user - can be either a student,admin or teacher""" + """ + This class defines the users table + a user has a uid, + a display_name and a role, + this role can be either student, admin or teacher + """ __tablename__ = "users" uid: str = Column(String(255), primary_key=True) + display_name: str = Column(String(255)) role: Role = Column(EnumField(Role), nullable=False) def to_dict(self): """ @@ -27,4 +31,5 @@ def to_dict(self): return { 'uid': self.uid, 'role': self.role.name, # Convert the enum to a string + 'display_name': self.display_name } diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index c031805a..8404d35f 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -73,13 +73,13 @@ def auth_tokens(session: Session) -> None: """Add the authenticated users to the database""" session.add_all([ - User(uid="login", role=Role.STUDENT), - User(uid="student", role=Role.STUDENT), - User(uid="student_other", role=Role.STUDENT), - User(uid="teacher", role=Role.TEACHER), - User(uid="teacher_other", role=Role.TEACHER), - User(uid="admin", role=Role.ADMIN), - User(uid="admin_other", role=Role.ADMIN) + User(uid="login", role=Role.STUDENT, display_name="Login User"), + User(uid="student", role=Role.STUDENT, display_name="Student Person"), + User(uid="student_other", role=Role.STUDENT, display_name="Student Other Person"), + User(uid="teacher", role=Role.TEACHER, display_name="Teacher Person"), + User(uid="teacher_other", role=Role.TEACHER, display_name="Teacher Other"), + User(uid="admin", role=Role.ADMIN, display_name="Admin Man"), + User(uid="admin_other", role=Role.ADMIN, display_name="Admin Woman") ]) session.commit() @@ -117,10 +117,10 @@ def db_session(): def users(): """Return a list of users to populate the database""" return [ - User(uid="brinkmann", role=Role.ADMIN), - User(uid="laermans", role=Role.ADMIN), - User(uid="student01", role=Role.STUDENT), - User(uid="student02", role=Role.STUDENT) + User(uid="brinkmann", role=Role.ADMIN, display_name="Gunnar Brinkmann"), + User(uid="laermans", role=Role.ADMIN, display_name="Eric Laermans"), + User(uid="student01", role=Role.STUDENT, display_name="Student Zero One"), + User(uid="student02", role=Role.STUDENT, display_name="Student Zero Two") ] def courses(): diff --git a/backend/tests/endpoints/conftest.py b/backend/tests/endpoints/conftest.py index b6311936..27ff8be4 100644 --- a/backend/tests/endpoints/conftest.py +++ b/backend/tests/endpoints/conftest.py @@ -153,7 +153,8 @@ def valid_user(): """ return { "uid": "w_student", - "role": Role.STUDENT.name + "role": Role.STUDENT.name, + "display_name": "Valid User" } @pytest.fixture @@ -180,10 +181,10 @@ def valid_user_entries(session): Returns a list of users that are in the database """ users = [ - 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)] + User(uid="del", role=Role.TEACHER, display_name="Peter Deleter"), + User(uid="pat", role=Role.TEACHER, display_name="Patrick Patcher"), + User(uid="u_get", role=Role.TEACHER, display_name="User Getter"), + User(uid="query_user", role=Role.ADMIN, display_name="Quentin Query")] session.add_all(users) session.commit() @@ -222,7 +223,7 @@ def files(): @pytest.fixture def course_teacher_ad(): """A user that's a teacher for testing""" - ad_teacher = User(uid="Gunnar", role=Role.TEACHER) + ad_teacher = User(uid="Gunnar", role=Role.TEACHER, display_name="Gunnar Brinckmann") return ad_teacher @pytest.fixture