From 14955ff9e31d30b452d600cb049b4eb3507c25a6 Mon Sep 17 00:00:00 2001 From: Anand <51914102+Jupiter-is-BIG@users.noreply.github.com> Date: Wed, 20 Mar 2024 00:40:37 -0700 Subject: [PATCH] added enroll api and unit testing (#41) Co-authored-by: Anand --- app/routes/course.py | 72 +++++++++++++++++- app/routes/user.py | 2 +- ...test_fixtures.cpython-311-pytest-8.0.2.pyc | Bin 14386 -> 14386 bytes test/course/test_course_enrollment.py | 35 +++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 test/course/test_course_enrollment.py diff --git a/app/routes/course.py b/app/routes/course.py index bcbe894..a4bb81e 100644 --- a/app/routes/course.py +++ b/app/routes/course.py @@ -276,4 +276,74 @@ async def get_student_status_courses( comment=course.comment )) - return result \ No newline at end of file + return result + +@router.post( + "/enroll/{course_id}", + status_code=201 +) +async def enroll( + course_id: int, + user: user_schema.UserSignIn, + db: Session = Depends(get_db), +): + """ returns all student courses that are pending or rejected """ + + user = db.query(user_model.User).filter(and_(user_model.User.email == user.email,user_model.User.password == hash(user.password))).first() + + if not user: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Wrong email and password combination" + ) + + if not user.verified: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="User is not verified yet" + ) + + student = db.query(user_model.Student).filter(user_model.Student.user_id == user.id).first() + + if not student: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="User is not a student" + ) + + course = ( + db.query(course_model.Course) + .filter(and_(course_model.Course.id == course_id, course_model.Course.status == course_model.CourseStatusEnum.active)) + .first() + ) + + if not course: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="No such active course found" + ) + + check_enrollment = ( + db.query(course_model.Enrollment) + .filter(and_(course_model.Enrollment.student_id==student.id, course_model.Enrollment.course_id==course_id)) + .first() + ) + + if check_enrollment: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Enrollment already exists" + ) + + + enrollment_data = course_model.Enrollment( + comment="Enrollment request is under review. Please contact admin@korse.com for more information.", + course_id = course_id, + student_id = student.id + ) + + db.add(enrollment_data) + db.commit() + db.refresh(enrollment_data) + + return {"message": "user enrollment request registered successfully"} \ No newline at end of file diff --git a/app/routes/user.py b/app/routes/user.py index 683b401..6decad8 100644 --- a/app/routes/user.py +++ b/app/routes/user.py @@ -182,7 +182,7 @@ async def verification_status( user: user_schema.UserSignIn, db: Session = Depends(get_db), ): - """Logs a user in""" + """ returns list of unverified users """ user = db.query(user_model.User).filter(and_(user_model.User.email == user.email,user_model.User.password == hash(user.password))).first() diff --git a/test/__pycache__/test_fixtures.cpython-311-pytest-8.0.2.pyc b/test/__pycache__/test_fixtures.cpython-311-pytest-8.0.2.pyc index 846b2649c4931d5cef06e26ed3d045ce89664b9d..6c3ea52ff11e4ae2a419c7d8257ae6b9bc5d7b62 100644 GIT binary patch delta 20 acmdl~u&IE1IWI340}%Y~__dK+-2wne>ISm_ delta 20 acmdl~u&IE1IWI340}wph_kAO`x&;79j0VvF diff --git a/test/course/test_course_enrollment.py b/test/course/test_course_enrollment.py new file mode 100644 index 0000000..75a0299 --- /dev/null +++ b/test/course/test_course_enrollment.py @@ -0,0 +1,35 @@ +from test.test_fixtures import * + +def test_course_enrollment_submission(authorized_client, test_courses, test_verified_student_1): + res = authorized_client.post(f"/course/enroll/{test_courses[0].id}",json={'email': 'student1@korse.com', 'password': 'password'}) + res2 = authorized_client.post("/course/student_status",json={'email': 'student1@korse.com', 'password': 'password'}) + + assert res.status_code == 201 + assert res2.status_code == 200 + assert len(res2.json()) == 1 + +def test_course_enrollment_submission_with_preenrollment(authorized_client, test_courses, test_verified_student_1): + res = authorized_client.post(f"/course/enroll/{test_courses[0].id}",json={'email': 'student1@korse.com', 'password': 'password'}) + res2 = authorized_client.post(f"/course/enroll/{test_courses[0].id}",json={'email': 'student1@korse.com', 'password': 'password'}) + + assert res.status_code == 201 + assert res2.status_code == 409 + assert res2.json()["detail"]=="Enrollment already exists" + +def test_course_enrollment_submission_with_invalid_course(authorized_client, test_verified_student_1): + res = authorized_client.post(f"/course/enroll/1",json={'email': 'student1@korse.com', 'password': 'password'}) + + assert res.status_code == 404 + assert res.json()["detail"]=="No such active course found" + +def test_course_enrollment_submission_with_teacher(authorized_client, test_courses, test_verified_teacher): + res = authorized_client.post(f"/course/enroll/{test_courses[0].id}",json={'email': 'teacher@korse.com', 'password': 'password'}) + + assert res.status_code == 409 + assert res.json()["detail"]=="User is not a student" + +def test_course_enrollment_submission_with_unverfied_student(authorized_client, test_courses, test_unverified_user): + res = authorized_client.post(f"/course/enroll/{test_courses[0].id}",json={'email': 'student1@gmail.com', 'password': 'password'}) + + assert res.status_code == 409 + assert res.json()["detail"] == "User is not verified yet" \ No newline at end of file