Skip to content

Commit

Permalink
added enroll api and unit testing (#41)
Browse files Browse the repository at this point in the history
Co-authored-by: Anand <[email protected]>
  • Loading branch information
Jupiter-is-BIG and Anand authored Mar 20, 2024
1 parent 16c6898 commit 14955ff
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
72 changes: 71 additions & 1 deletion app/routes/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,74 @@ async def get_student_status_courses(
comment=course.comment
))

return result
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 [email protected] 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"}
2 changes: 1 addition & 1 deletion app/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Binary file modified test/__pycache__/test_fixtures.cpython-311-pytest-8.0.2.pyc
Binary file not shown.
35 changes: 35 additions & 0 deletions test/course/test_course_enrollment.py
Original file line number Diff line number Diff line change
@@ -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': '[email protected]', 'password': 'password'})
res2 = authorized_client.post("/course/student_status",json={'email': '[email protected]', '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': '[email protected]', 'password': 'password'})
res2 = authorized_client.post(f"/course/enroll/{test_courses[0].id}",json={'email': '[email protected]', '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': '[email protected]', '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': '[email protected]', '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': '[email protected]', 'password': 'password'})

assert res.status_code == 409
assert res.json()["detail"] == "User is not verified yet"

0 comments on commit 14955ff

Please sign in to comment.