-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added enroll api and unit testing (#41)
Co-authored-by: Anand <[email protected]>
- Loading branch information
1 parent
16c6898
commit 14955ff
Showing
4 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
test/__pycache__/test_fixtures.cpython-311-pytest-8.0.2.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |