Best Python code snippet using autotest_python
test_lesson_routers.py
Source:test_lesson_routers.py
1from app.main import app2from app.tests.create_requirements import create_semester, create_user_and_login3from fastapi.testclient import TestClient4with TestClient(app) as client:5 class TestUser:6 user_id = None7 token = None8 semester_id = None9 lesson_id = None10 test_user = TestUser()11 def test_prepare_test_data():12 test_user.user_id, test_user.token = create_user_and_login(13 client, "lesson_routers@test.com", "test"14 )15 test_user.semester_id = create_semester(16 client, test_user.user_id, test_user.token17 )18 def test_create_lesson():19 """Test creating lesson"""20 lesson = client.post(21 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons",22 headers={"Authorization": f"Bearer {test_user.token}"},23 json={24 "name": "Test Lesson",25 "instructor": "Test Instructor",26 "absenceLimit": 0,27 "slots": [28 {"day": 2, "hour": 7, "isLab": 0},29 {"day": 2, "hour": 8, "isLab": 0},30 ],31 },32 )33 assert lesson.status_code == 20134 assert lesson.json()["message"] == "Lesson created"35 test_user.lesson_id = lesson.json()["_id"]36 def test_create_lesson_with_another_user_id():37 """Test creating lesson with a user id other than auth user"""38 user_id = "non_exists_user_id"39 lesson = client.post(40 f"/users/{user_id}/semesters/{test_user.semester_id}/lessons",41 headers={"Authorization": f"Bearer {test_user.token}"},42 json={43 "name": "Test Lesson",44 "instructor": "Test Instructor",45 "absenceLimit": 0,46 "slots": [47 {"day": 2, "hour": 7, "isLab": 0},48 {"day": 2, "hour": 8, "isLab": 0},49 ],50 },51 )52 assert lesson.status_code == 40353 assert lesson.json()["message"] == "No right to access"54 def test_create_lesson_with_invalid_semester_id():55 """Test creating lesson with an invalid semester id"""56 semester_id = "non_exists_semester_id"57 lesson = client.post(58 f"/users/{test_user.user_id}/semesters/{semester_id}/lessons",59 headers={"Authorization": f"Bearer {test_user.token}"},60 json={61 "name": "Test Lesson",62 "instructor": "Test Instructor",63 "absenceLimit": 0,64 "slots": [65 {"day": 2, "hour": 7, "isLab": 0},66 {"day": 2, "hour": 8, "isLab": 0},67 ],68 },69 )70 assert lesson.status_code == 40471 assert lesson.json()["message"] == "Lesson could not be created"72 def test_get_lesson():73 """Test getting lesson"""74 lesson = client.get(75 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",76 headers={"Authorization": f"Bearer {test_user.token}"},77 )78 assert lesson.status_code == 20079 assert lesson.json() == {80 "_id": test_user.lesson_id,81 "name": "Test Lesson",82 "instructor": "Test Instructor",83 "absenceLimit": 0,84 "slots": [[2, 7, 0], [2, 8, 0]],85 "absences": [],86 }87 def test_get_lesson_with_another_user_id():88 """Test getting lesson with a user id other than auth user"""89 user_id = "non_exists_user_id"90 lesson = client.get(91 f"/users/{user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",92 headers={"Authorization": f"Bearer {test_user.token}"},93 )94 assert lesson.status_code == 40395 assert lesson.json()["message"] == "No right to access"96 def test_get_lesson_with_invalid_semester_id():97 """Test getting lesson with an invalid semester id"""98 semester_id = "non_exists_semester_id"99 lesson = client.get(100 f"/users/{test_user.user_id}/semesters/{semester_id}/lessons/{test_user.lesson_id}",101 headers={"Authorization": f"Bearer {test_user.token}"},102 )103 assert lesson.status_code == 404104 assert lesson.json()["message"] == "Lesson not found"105 def test_get_lesson_with_invalid_lesson_id():106 """Test getting lesson with an invalid lesson id"""107 lesson_id = "non_exists_lesson_id"108 lesson = client.get(109 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{lesson_id}",110 headers={"Authorization": f"Bearer {test_user.token}"},111 )112 assert lesson.status_code == 404113 assert lesson.json()["message"] == "Lesson not found"114 def test_update_lesson():115 """Test updating lesson"""116 lesson = client.put(117 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",118 headers={"Authorization": f"Bearer {test_user.token}"},119 json={120 "name": "Test Lesson 2",121 "instructor": "Test Instructor 2",122 "absenceLimit": 0,123 "slots": [124 {"day": 3, "hour": 5, "isLab": 0},125 {"day": 3, "hour": 6, "isLab": 1},126 ],127 },128 )129 assert lesson.status_code == 200130 assert lesson.json()["message"] == "Lesson updated"131 def test_update_lesson_with_another_user_id():132 """Test updating lesson with a user id other than auth user"""133 user_id = "non_exists_user_id"134 lesson = client.put(135 f"/users/{user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",136 headers={"Authorization": f"Bearer {test_user.token}"},137 json={138 "name": "Test Lesson 2",139 "instructor": "Test Instructor 2",140 "absenceLimit": 0,141 "slots": [142 {"day": 3, "hour": 5, "isLab": 0},143 {"day": 3, "hour": 6, "isLab": 1},144 ],145 },146 )147 assert lesson.status_code == 403148 assert lesson.json()["message"] == "No right to access"149 def test_update_lesson_with_invalid_semester_id():150 """Test updating lesson with an invalid semester id"""151 semester_id = "non_exists_semester_id"152 lesson = client.put(153 f"/users/{test_user.user_id}/semesters/{semester_id}/lessons/{test_user.lesson_id}",154 headers={"Authorization": f"Bearer {test_user.token}"},155 json={156 "name": "Test Lesson 2",157 "instructor": "Test Instructor 2",158 "absenceLimit": 0,159 "slots": [160 {"day": 3, "hour": 5, "isLab": 0},161 {"day": 3, "hour": 6, "isLab": 1},162 ],163 },164 )165 assert lesson.status_code == 404166 assert lesson.json()["message"] == "Lesson not found"167 def test_update_lesson_with_invalid_lesson_id():168 """Test updating lesson with an invalid lesson id"""169 lesson_id = "non_exists_lesson_id"170 lesson = client.put(171 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{lesson_id}",172 headers={"Authorization": f"Bearer {test_user.token}"},173 json={174 "name": "Test Lesson 2",175 "instructor": "Test Instructor 2",176 "absenceLimit": 0,177 "slots": [178 {"day": 3, "hour": 5, "isLab": 0},179 {"day": 3, "hour": 6, "isLab": 1},180 ],181 },182 )183 assert lesson.status_code == 404184 assert lesson.json()["message"] == "Lesson not found"185 def test_list_lessons():186 """Test listing lessons"""187 lessons = client.get(188 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons",189 headers={"Authorization": f"Bearer {test_user.token}"},190 )191 assert lessons.status_code == 200192 # lesson.json() only includes our last lesson that created and updated193 assert lessons.json() == [194 {195 "_id": test_user.lesson_id,196 "name": "Test Lesson 2",197 "instructor": "Test Instructor 2",198 "absenceLimit": 0,199 "slots": [[3, 5, 0], [3, 6, 1]],200 "absences": [],201 }202 ]203 def test_list_lessons_with_another_user_id():204 """Test listing lessons with a user id other than auth user"""205 user_id = "non_exists_user_id"206 lessons = client.get(207 f"/users/{user_id}/semesters/{test_user.semester_id}/lessons",208 headers={"Authorization": f"Bearer {test_user.token}"},209 )210 assert lessons.status_code == 403211 assert lessons.json()["message"] == "No right to access"212 def test_list_lessons_with_invalid_semester_id():213 """Test listing lessons with an invalid semester id"""214 semester_id = "non_exists_semester_id"215 lessons = client.get(216 f"/users/{test_user.user_id}/semesters/{semester_id}/lessons",217 headers={"Authorization": f"Bearer {test_user.token}"},218 )219 assert lessons.status_code == 404220 assert lessons.json()["message"] == "Semester not found"221 def test_create_second_lesson():222 """Test creating second lesson"""223 lesson = client.post(224 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons",225 headers={"Authorization": f"Bearer {test_user.token}"},226 json={227 "name": "Test Lesson 3",228 "instructor": "Test Instructor 3",229 "absenceLimit": 0,230 "slots": [231 {"day": 2, "hour": 7, "isLab": 0},232 {"day": 2, "hour": 8, "isLab": 0},233 ],234 },235 )236 assert lesson.status_code == 201237 assert lesson.json()["message"] == "Lesson created"238 def test_list_lessons_with_two_lessons():239 """Test listing lessons with two lessons"""240 lessons = client.get(241 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons",242 headers={"Authorization": f"Bearer {test_user.token}"},243 )244 assert lessons.status_code == 200245 assert lessons.json() == [246 {247 "_id": lessons.json()[0]["_id"],248 "name": "Test Lesson 2",249 "instructor": "Test Instructor 2",250 "absenceLimit": 0,251 "slots": [[3, 5, 0], [3, 6, 1]],252 "absences": [],253 },254 {255 "_id": lessons.json()[1]["_id"],256 "name": "Test Lesson 3",257 "instructor": "Test Instructor 3",258 "absenceLimit": 0,259 "slots": [[2, 7, 0], [2, 8, 0]],260 "absences": [],261 },262 ]263 def test_create_absence():264 """Test creating absence"""265 absence = client.post(266 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}/absences",267 headers={"Authorization": f"Bearer {test_user.token}"},268 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},269 )270 assert absence.status_code == 201271 assert absence.json()["message"] == "Absence created"272 def test_get_lesson_after_adding_absence():273 """Test getting lesson after adding absence"""274 lesson = client.get(275 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",276 headers={"Authorization": f"Bearer {test_user.token}"},277 )278 assert lesson.status_code == 200279 assert lesson.json() == {280 "_id": test_user.lesson_id,281 "name": "Test Lesson 2",282 "instructor": "Test Instructor 2",283 "absenceLimit": 0,284 "slots": [[3, 5, 0], [3, 6, 1]],285 "absences": [[0, 3, 5, 0]],286 }287 def test_create_same_absence():288 """Test creating same absence"""289 absence = client.post(290 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}/absences",291 headers={"Authorization": f"Bearer {test_user.token}"},292 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},293 )294 assert absence.status_code == 400295 assert absence.json()["message"] == "Absence already exists"296 def test_create_absence_with_another_user_id():297 """Test creating absence with a user id other than auth user"""298 user_id = "non_exists_user_id"299 absence = client.post(300 f"/users/{user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}/absences",301 headers={"Authorization": f"Bearer {test_user.token}"},302 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},303 )304 assert absence.status_code == 403305 assert absence.json()["message"] == "No right to access"306 def test_create_absence_with_invalid_semester_id():307 """Test creating absence with an invalid semester id"""308 semester_id = "non_exists_semester_id"309 absence = client.post(310 f"/users/{test_user.user_id}/semesters/{semester_id}/lessons/{test_user.lesson_id}/absences",311 headers={"Authorization": f"Bearer {test_user.token}"},312 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},313 )314 assert absence.status_code == 404315 assert absence.json()["message"] == "Absence could not be created"316 def test_create_absence_with_invalid_lesson_id():317 """Test creating absence with an invalid lesson id"""318 lesson_id = "non_exists_lesson_id"319 absence = client.post(320 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{lesson_id}/absences",321 headers={"Authorization": f"Bearer {test_user.token}"},322 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},323 )324 assert absence.status_code == 404325 assert absence.json()["message"] == "Absence could not be created"326 def test_delete_absence():327 """Test deleting absence"""328 absence = client.delete(329 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}/absences",330 headers={"Authorization": f"Bearer {test_user.token}"},331 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},332 )333 assert absence.status_code == 200334 assert absence.json()["message"] == "Absence deleted"335 def test_get_lesson_after_deleting_absence():336 """Test getting lesson after deleting absence"""337 lesson = client.get(338 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",339 headers={"Authorization": f"Bearer {test_user.token}"},340 )341 assert lesson.status_code == 200342 assert lesson.json() == {343 "_id": test_user.lesson_id,344 "name": "Test Lesson 2",345 "instructor": "Test Instructor 2",346 "absenceLimit": 0,347 "slots": [[3, 5, 0], [3, 6, 1]],348 "absences": [],349 }350 def test_delete_same_absence():351 """Test deleting same absence"""352 absence = client.delete(353 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}/absences",354 headers={"Authorization": f"Bearer {test_user.token}"},355 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},356 )357 assert absence.status_code == 404358 assert absence.json()["message"] == "Absence not found"359 def test_delete_absence_with_another_user_id():360 """Test deleting absence with a user id other than auth user"""361 user_id = "non_exists_user_id"362 absence = client.delete(363 f"/users/{user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}/absences",364 headers={"Authorization": f"Bearer {test_user.token}"},365 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},366 )367 assert absence.status_code == 403368 assert absence.json()["message"] == "No right to access"369 def test_delete_absence_with_invalid_semester_id():370 """Test deleting absence with an invalid semester id"""371 semester_id = "non_exists_semester_id"372 absence = client.delete(373 f"/users/{test_user.user_id}/semesters/{semester_id}/lessons/{test_user.lesson_id}/absences",374 headers={"Authorization": f"Bearer {test_user.token}"},375 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},376 )377 assert absence.status_code == 404378 assert absence.json()["message"] == "Absence not found"379 def test_delete_absence_with_invalid_lesson_id():380 """Test deleting absence with an invalid lesson id"""381 lesson_id = "non_exists_lesson_id"382 absence = client.delete(383 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{lesson_id}/absences",384 headers={"Authorization": f"Bearer {test_user.token}"},385 json={"absence": {"week": 0, "day": 3, "hour": 5, "isLab": 0}},386 )387 assert absence.status_code == 404388 assert absence.json()["message"] == "Absence not found"389 def test_delete_lesson():390 """Test deleting lesson"""391 lesson = client.delete(392 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",393 headers={"Authorization": f"Bearer {test_user.token}"},394 )395 assert lesson.status_code == 200396 assert lesson.json()["message"] == "Lesson deleted"397 def test_delete_same_lesson():398 """Test deleting same lesson"""399 lesson = client.delete(400 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",401 headers={"Authorization": f"Bearer {test_user.token}"},402 )403 assert lesson.status_code == 404404 assert lesson.json()["message"] == "Lesson not found"405 def test_delete_lesson_with_another_user_id():406 """Test deleting lesson with a user id other than auth user"""407 user_id = "non_exists_user_id"408 lesson = client.delete(409 f"/users/{user_id}/semesters/{test_user.semester_id}/lessons/{test_user.lesson_id}",410 headers={"Authorization": f"Bearer {test_user.token}"},411 )412 assert lesson.status_code == 403413 assert lesson.json()["message"] == "No right to access"414 def test_delete_lesson_with_invalid_semester_id():415 """Test deleting lesson with an invalid semester id"""416 semester_id = "non_exists_semester_id"417 lesson = client.delete(418 f"/users/{test_user.user_id}/semesters/{semester_id}/lessons/{test_user.lesson_id}",419 headers={"Authorization": f"Bearer {test_user.token}"},420 )421 assert lesson.status_code == 404422 assert lesson.json()["message"] == "Lesson not found"423 def test_delete_lesson_with_invalid_lesson_id():424 """Test deleting lesson with an invalid lesson id"""425 lesson_id = "non_exists_lesson_id"426 lesson = client.delete(427 f"/users/{test_user.user_id}/semesters/{test_user.semester_id}/lessons/{lesson_id}",428 headers={"Authorization": f"Bearer {test_user.token}"},429 )430 assert lesson.status_code == 404...
test_uni_cur_lesson_routers.py
Source:test_uni_cur_lesson_routers.py
1from app.main import app, settings2from app.tests.create_requirements import (3 create_professor_and_login,4 create_user_and_login,5 login_admin_user,6 create_university,7 create_department,8 create_curriculum,9 create_curriculum_semester,10)11from fastapi.testclient import TestClient12with TestClient(app) as client:13 class TestUser:14 user_id = None15 token = None16 university_id = None17 department_id = None18 curriculum_id = None19 curriculum_semester_id = None20 curriculum_lesson_id = None21 second_curriculum_lesson_id = None22 test_user = TestUser()23 default_user = TestUser()24 def test_prepare_test_data():25 admin_token = login_admin_user(client, settings)26 test_user.user_id, test_user.token = create_professor_and_login(27 client, admin_token, "professor_uni_cur_lesson_routers@test.com", "test"28 )29 test_user.university_id = create_university(30 client, test_user.token, "Test University for Uni Cur Lesson Routers"31 )32 test_user.department_id = create_department(33 client,34 test_user.token,35 test_user.university_id,36 "Test Department for Uni Cur Lesson Routers",37 )38 test_user.curriculum_id = create_curriculum(39 client,40 test_user.token,41 test_user.university_id,42 test_user.department_id,43 "Test Curriculum for Uni Cur Lesson Routers",44 )45 test_user.curriculum_semester_id = create_curriculum_semester(46 client,47 test_user.token,48 test_user.university_id,49 test_user.department_id,50 test_user.curriculum_id,51 1,52 )53 default_user.user_id, default_user.token = create_user_and_login(54 client, "default_user_uni_cur@test.com", "test"55 )56 def test_create_curriculum_lesson():57 """Test creating a curriculum lesson"""58 curriculum_lesson = client.post(59 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",60 headers={"Authorization": f"Bearer {test_user.token}"},61 json={62 "name": "Test Curriculum Lesson",63 "code": "TEST_CUR_LESSON",64 "lessonType": "default",65 },66 )67 assert curriculum_lesson.status_code == 20168 assert curriculum_lesson.json()["message"] == "Curriculum lesson created"69 test_user.curriculum_lesson_id = curriculum_lesson.json()["_id"]70 def test_get_curriculum_lesson():71 """Test getting a curriculum lesson"""72 curriculum_lesson = client.get(73 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",74 headers={"Authorization": f"Bearer {test_user.token}"},75 )76 assert curriculum_lesson.status_code == 20077 assert curriculum_lesson.json() == {78 "_id": test_user.curriculum_lesson_id,79 "name": "Test Curriculum Lesson",80 "code": "TEST_CUR_LESSON",81 "lessonType": "default",82 }83 def test_get_curriculum_lesson_with_default_user():84 """Test getting a curriculum lesson with default user"""85 curriculum_lesson = client.get(86 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",87 headers={"Authorization": f"Bearer {default_user.token}"},88 )89 assert curriculum_lesson.status_code == 20090 assert curriculum_lesson.json() == {91 "_id": test_user.curriculum_lesson_id,92 "name": "Test Curriculum Lesson",93 "code": "TEST_CUR_LESSON",94 "lessonType": "default",95 }96 def test_get_curriculum_lesson_with_invalid_lesson_id():97 """Test getting a curriculum lesson with invalid lesson id"""98 curriculum_lesson = client.get(99 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/invalid_lesson_id",100 headers={"Authorization": f"Bearer {test_user.token}"},101 )102 assert curriculum_lesson.status_code == 404103 assert curriculum_lesson.json()["message"] == "Curriculum lesson not found"104 def test_create_same_curriculum_lesson():105 """Test creating a curriculum lesson with the same name"""106 curriculum_lesson = client.post(107 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",108 headers={"Authorization": f"Bearer {test_user.token}"},109 json={110 "name": "Test Curriculum Lesson",111 "code": "TEST_CUR_LESSON",112 "lessonType": "default",113 },114 )115 assert curriculum_lesson.status_code == 409116 assert curriculum_lesson.json()["message"] == "Curriculum lesson already exists"117 def test_create_curriculum_lesson_with_invalid_semester_id():118 """Test creating a curriculum lesson with an invalid semester id"""119 curriculum_semester_id = "non_exists_curriculum_semester_id"120 curriculum_lesson = client.post(121 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{curriculum_semester_id}/lessons",122 headers={"Authorization": f"Bearer {test_user.token}"},123 json={124 "name": "Test Curriculum Lesson",125 "code": "TEST_CUR_LESSON",126 "lessonType": "default",127 },128 )129 assert curriculum_lesson.status_code == 404130 assert curriculum_lesson.json()["message"] == "Curriculum semester not found"131 def test_create_curriculum_lesson_with_invalid_university_id():132 """Test creating a curriculum lesson with an invalid university id"""133 university_id = "non_exists_university_id"134 curriculum_lesson = client.post(135 f"/universities/{university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",136 headers={"Authorization": f"Bearer {test_user.token}"},137 json={138 "name": "Test Curriculum Lesson",139 "code": "TEST_CUR_LESSON",140 "lessonType": "default",141 },142 )143 assert curriculum_lesson.status_code == 404144 assert curriculum_lesson.json()["message"] == "Curriculum semester not found"145 def test_create_curriculum_lesson_with_default_user():146 """Test creating a curriculum lesson with a default user"""147 curriculum_lesson = client.post(148 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",149 headers={"Authorization": f"Bearer {default_user.token}"},150 json={151 "name": "Test Curriculum Lesson",152 "code": "TEST_CUR_LESSON",153 "lessonType": "default",154 },155 )156 assert curriculum_lesson.status_code == 403157 assert curriculum_lesson.json()["message"] == "No right to access"158 def test_list_curriculum_lessons():159 """Test listing curriculum lessons"""160 curriculum_lessons = client.get(161 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",162 headers={"Authorization": f"Bearer {test_user.token}"},163 )164 assert curriculum_lessons.status_code == 200165 assert curriculum_lessons.json() == [166 {167 "_id": test_user.curriculum_lesson_id,168 "name": "Test Curriculum Lesson",169 "code": "TEST_CUR_LESSON",170 "lessonType": "default",171 }172 ]173 def test_list_curriculum_lessons_with_default_user():174 """Test listing curriculum lessons with a default user"""175 curriculum_lessons = client.get(176 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",177 headers={"Authorization": f"Bearer {default_user.token}"},178 )179 assert curriculum_lessons.status_code == 200180 assert curriculum_lessons.json() == [181 {182 "_id": test_user.curriculum_lesson_id,183 "name": "Test Curriculum Lesson",184 "code": "TEST_CUR_LESSON",185 "lessonType": "default",186 }187 ]188 def test_list_curriculum_lessons_with_invalid_semester_id():189 """Test listing curriculum lessons with an invalid semester id"""190 curriculum_semester_id = "non_exists_curriculum_semester_id"191 curriculum_lessons = client.get(192 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{curriculum_semester_id}/lessons",193 headers={"Authorization": f"Bearer {test_user.token}"},194 )195 assert curriculum_lessons.status_code == 404196 assert curriculum_lessons.json()["message"] == "Curriculum semester not found"197 def test_list_curriculum_lessons_with_invalid_university_id():198 """Test listing curriculum lessons with an invalid university id"""199 university_id = "non_exists_university_id"200 curriculum_lessons = client.get(201 f"/universities/{university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",202 headers={"Authorization": f"Bearer {test_user.token}"},203 )204 assert curriculum_lessons.status_code == 404205 assert curriculum_lessons.json()["message"] == "Curriculum semester not found"206 def test_update_curriculum_lesson():207 """Test updating a curriculum lesson"""208 curriculum_lesson = client.put(209 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",210 headers={"Authorization": f"Bearer {test_user.token}"},211 json={212 "name": "Test Curriculum Lesson Updated",213 "code": "TEST_CUR_LESSON",214 "lessonType": "default",215 },216 )217 assert curriculum_lesson.status_code == 200218 assert curriculum_lesson.json()["message"] == "Curriculum lesson updated"219 def test_create_second_curriculum_lesson():220 """Test creating a second curriculum lesson"""221 curriculum_lesson = client.post(222 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",223 headers={"Authorization": f"Bearer {test_user.token}"},224 json={225 "name": "Test Curriculum Lesson 2",226 "code": "TEST_CUR_LESSON_2",227 "lessonType": "default",228 },229 )230 assert curriculum_lesson.status_code == 201231 assert curriculum_lesson.json()["message"] == "Curriculum lesson created"232 test_user.second_curriculum_lesson_id = curriculum_lesson.json()["_id"]233 def test_list_curriculum_lessons_with_two_lessons():234 """Test listing curriculum lessons with two lessons"""235 curriculum_lessons = client.get(236 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons",237 headers={"Authorization": f"Bearer {test_user.token}"},238 )239 assert curriculum_lessons.status_code == 200240 assert curriculum_lessons.json() == [241 {242 "_id": test_user.curriculum_lesson_id,243 "name": "Test Curriculum Lesson Updated",244 "code": "TEST_CUR_LESSON",245 "lessonType": "default",246 },247 {248 "_id": test_user.second_curriculum_lesson_id,249 "name": "Test Curriculum Lesson 2",250 "code": "TEST_CUR_LESSON_2",251 "lessonType": "default",252 },253 ]254 def test_update_curriculum_lesson_with_invalid_lesson_id():255 """Test updating a curriculum lesson with an invalid lesson id"""256 curriculum_lesson_id = "non_exists_curriculum_lesson_id"257 curriculum_lesson = client.put(258 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{curriculum_lesson_id}",259 headers={"Authorization": f"Bearer {test_user.token}"},260 json={261 "name": "Test Curriculum Lesson Updated 3",262 "code": "TEST_CUR_LESSON_3",263 "lessonType": "default",264 },265 )266 assert curriculum_lesson.status_code == 400267 assert (268 curriculum_lesson.json()["message"]269 == "Curriculum lesson could not be updated"270 )271 def test_update_curriculum_lesson_with_exists_lesson_code():272 """Test updating a curriculum lesson with an exists lesson code"""273 curriculum_lesson = client.put(274 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",275 headers={"Authorization": f"Bearer {test_user.token}"},276 json={277 "name": "Test Curriculum Lesson Updated 4",278 "code": "TEST_CUR_LESSON_2",279 "lessonType": "default",280 },281 )282 assert curriculum_lesson.status_code == 409283 assert (284 curriculum_lesson.json()["message"]285 == "Lesson could not be found or there exists another lesson with given lesson code"286 )287 def test_udpate_curriculum_lesson_with_same_lesson_context():288 """Test updating a curriculum lesson with the same lesson context"""289 curriculum_lesson = client.put(290 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",291 headers={"Authorization": f"Bearer {test_user.token}"},292 json={293 "name": "Test Curriculum Lesson Updated",294 "code": "TEST_CUR_LESSON",295 "lessonType": "default",296 },297 )298 assert curriculum_lesson.status_code == 400299 assert (300 curriculum_lesson.json()["message"]301 == "Curriculum lesson could not be updated"302 )303 def test_update_curriculum_lesson_with_default_user():304 """Test updating a curriculum lesson with the default user"""305 curriculum_lesson = client.put(306 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",307 headers={"Authorization": f"Bearer {default_user.token}"},308 json={309 "name": "Test Curriculum Lesson Updated",310 "code": "TEST_CUR_LESSON_4",311 "lessonType": "default",312 },313 )314 assert curriculum_lesson.status_code == 403315 assert curriculum_lesson.json()["message"] == "No right to access"316 def test_delete_curriculum_lesson():317 """Test deleting a curriculum lesson"""318 curriculum_lesson = client.delete(319 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",320 headers={"Authorization": f"Bearer {test_user.token}"},321 )322 assert curriculum_lesson.status_code == 200323 assert curriculum_lesson.json()["message"] == "Curriculum lesson deleted"324 def test_delete_same_curriculum_lesson():325 """Test deleting a curriculum lesson"""326 curriculum_lesson = client.delete(327 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",328 headers={"Authorization": f"Bearer {test_user.token}"},329 )330 assert curriculum_lesson.status_code == 404331 assert curriculum_lesson.json()["message"] == "Curriculum lesson not found"332 def test_delete_same_curriculum_lesson_with_default_user():333 """Test deleting a curriculum lesson with the default user"""334 curriculum_lesson = client.delete(335 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}/lessons/{test_user.curriculum_lesson_id}",336 headers={"Authorization": f"Bearer {default_user.token}"},337 )338 assert curriculum_lesson.status_code == 403339 assert curriculum_lesson.json()["message"] == "No right to access"340 def test_delete_used_university():341 """Test deleting a university that has been used"""342 university = client.delete(343 f"/universities/{test_user.university_id}",344 headers={"Authorization": f"Bearer {test_user.token}"},345 )346 assert university.status_code == 200...
test_uni_cur_semester_routers.py
Source:test_uni_cur_semester_routers.py
1from app.main import app, settings2from app.tests.create_requirements import (3 create_professor_and_login,4 create_user_and_login,5 login_admin_user,6 create_university,7 create_department,8 create_curriculum,9)10from fastapi.testclient import TestClient11with TestClient(app) as client:12 class TestUser:13 user_id = None14 token = None15 university_id = None16 department_id = None17 curriculum_id = None18 curriculum_semester_id = None19 second_curriculum_semester_id = None20 test_user = TestUser()21 default_user = TestUser()22 def test_prepare_test_data():23 admin_token = login_admin_user(client, settings)24 test_user.user_id, test_user.token = create_professor_and_login(25 client, admin_token, "professor_uni_cur_semester_routers@test.com", "test"26 )27 test_user.university_id = create_university(28 client, test_user.token, "Test University for Uni Cur Semester Routers"29 )30 test_user.department_id = create_department(31 client,32 test_user.token,33 test_user.university_id,34 "Test Department for Uni Cur Semester Routers",35 )36 test_user.curriculum_id = create_curriculum(37 client,38 test_user.token,39 test_user.university_id,40 test_user.department_id,41 "Test Curriculum for Uni Cur Lesson Routers",42 )43 default_user.user_id, default_user.token = create_user_and_login(44 client, "default_user_uni_cur_semester@test.com", "test"45 )46 def test_create_curriculum_semester():47 """Test creating a curriculum semester"""48 curriculum_semester = client.post(49 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",50 headers={"Authorization": f"Bearer {test_user.token}"},51 json={52 "semester": 1,53 },54 )55 assert curriculum_semester.status_code == 20156 assert curriculum_semester.json()["message"] == "Curriculum semester created"57 test_user.curriculum_semester_id = curriculum_semester.json()["_id"]58 def test_get_curriculum_semester():59 """Test getting a curriculum semester"""60 curriculum_semester = client.get(61 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",62 headers={"Authorization": f"Bearer {test_user.token}"},63 )64 assert curriculum_semester.status_code == 20065 assert curriculum_semester.json() == {66 "_id": test_user.curriculum_semester_id,67 "semester": 1,68 "lessons": [],69 }70 def test_get_curriculum_semester_with_default_user():71 """Test getting a curriculum semester with default user"""72 curriculum_semester = client.get(73 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",74 headers={"Authorization": f"Bearer {default_user.token}"},75 )76 assert curriculum_semester.status_code == 20077 assert curriculum_semester.json() == {78 "_id": test_user.curriculum_semester_id,79 "semester": 1,80 "lessons": [],81 }82 def test_get_curriculum_semester_with_invalid_semester_id():83 """Test getting a curriculum semester with invalid semester id"""84 curriculum_semester = client.get(85 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/invalid_semester_id",86 headers={"Authorization": f"Bearer {test_user.token}"},87 )88 assert curriculum_semester.status_code == 40489 assert curriculum_semester.json()["message"] == "Curriculum semester not found"90 def test_create_same_curriculum_semester():91 """Test creating a curriculum semester with the same name"""92 curriculum_semester = client.post(93 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",94 headers={"Authorization": f"Bearer {test_user.token}"},95 json={96 "semester": 1,97 },98 )99 assert curriculum_semester.status_code == 409100 assert (101 curriculum_semester.json()["message"]102 == "Curriculum semester already exists"103 )104 def test_create_curriculum_semester_with_invalid_university_id():105 """Test creating a curriculum semester with an invalid university id"""106 university_id = "non_exists_university_id"107 curriculum_semester = client.post(108 f"/universities/{university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",109 headers={"Authorization": f"Bearer {test_user.token}"},110 json={111 "semester": 1,112 },113 )114 assert curriculum_semester.status_code == 404115 assert (116 curriculum_semester.json()["message"]117 == "University or department or curriculum not found"118 )119 def test_create_curriculum_semester_with_invalid_department_id():120 """Test creating a curriculum semester with an invalid department id"""121 department_id = "non_exists_department_id"122 curriculum_semester = client.post(123 f"/universities/{test_user.university_id}/departments/{department_id}/curriculums/{test_user.curriculum_id}/semesters",124 headers={"Authorization": f"Bearer {test_user.token}"},125 json={126 "semester": 1,127 },128 )129 assert curriculum_semester.status_code == 404130 assert (131 curriculum_semester.json()["message"]132 == "University or department or curriculum not found"133 )134 def test_create_curriculum_semester_with_invalid_curriculum_id():135 """Test creating a curriculum semester with an invalid university id"""136 curriculum_id = "non_exists_curriculum_id"137 curriculum_semester = client.post(138 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{curriculum_id}/semesters",139 headers={"Authorization": f"Bearer {test_user.token}"},140 json={141 "semester": 1,142 },143 )144 assert curriculum_semester.status_code == 404145 assert (146 curriculum_semester.json()["message"]147 == "University or department or curriculum not found"148 )149 def test_create_curriculum_semester_with_default_user():150 """Test creating a curriculum semester with a default user"""151 curriculum_semester = client.post(152 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",153 headers={"Authorization": f"Bearer {default_user.token}"},154 json={155 "semester": 1,156 },157 )158 assert curriculum_semester.status_code == 403159 assert curriculum_semester.json()["message"] == "No right to access"160 def test_list_curriculum_semesters():161 """Test listing curriculum semesters"""162 curriculum_semesters = client.get(163 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",164 headers={"Authorization": f"Bearer {test_user.token}"},165 )166 assert curriculum_semesters.status_code == 200167 assert curriculum_semesters.json() == [168 {169 "_id": test_user.curriculum_semester_id,170 "semester": 1,171 "lessons": [],172 }173 ]174 def test_list_curriculum_semesters_with_default_user():175 """Test listing curriculum semesters with a default user"""176 curriculum_semesters = client.get(177 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",178 headers={"Authorization": f"Bearer {default_user.token}"},179 )180 assert curriculum_semesters.status_code == 200181 assert curriculum_semesters.json() == [182 {183 "_id": test_user.curriculum_semester_id,184 "semester": 1,185 "lessons": [],186 }187 ]188 def test_list_curriculum_semesters_with_invalid_university_id():189 """Test listing curriculum semesters with an invalid university id"""190 university_id = "non_exists_university_id"191 curriculum_semesters = client.get(192 f"/universities/{university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",193 headers={"Authorization": f"Bearer {test_user.token}"},194 )195 assert curriculum_semesters.status_code == 404196 assert curriculum_semesters.json()["message"] == "Curriculum semester not found"197 def test_list_curriculum_semesters_with_invalid_department_id():198 """Test listing curriculum semesters with an invalid department id"""199 department_id = "non_exists_department_id"200 curriculum_semesters = client.get(201 f"/universities/{test_user.university_id}/departments/{department_id}/curriculums/{test_user.curriculum_id}/semesters",202 headers={"Authorization": f"Bearer {test_user.token}"},203 )204 assert curriculum_semesters.status_code == 404205 assert curriculum_semesters.json()["message"] == "Curriculum semester not found"206 def test_list_curriculum_semesters_with_invalid_curriculum_id():207 """Test listing curriculum semesters with an invalid curriculum id"""208 curriculum_id = "non_exists_curriculum_id"209 curriculum_semesters = client.get(210 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{curriculum_id}/semesters",211 headers={"Authorization": f"Bearer {test_user.token}"},212 )213 assert curriculum_semesters.status_code == 404214 assert curriculum_semesters.json()["message"] == "Curriculum semester not found"215 def test_update_curriculum_semester():216 """Test updating a curriculum semester"""217 curriculum_semester = client.put(218 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",219 headers={"Authorization": f"Bearer {test_user.token}"},220 json={221 "semester": 2,222 },223 )224 assert curriculum_semester.status_code == 200225 assert curriculum_semester.json()["message"] == "Curriculum semester updated"226 def test_create_second_curriculum_semester():227 """Test creating a second curriculum semester"""228 curriculum_semester = client.post(229 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",230 headers={"Authorization": f"Bearer {test_user.token}"},231 json={232 "semester": 3,233 },234 )235 assert curriculum_semester.status_code == 201236 assert curriculum_semester.json()["message"] == "Curriculum semester created"237 test_user.second_curriculum_semester_id = curriculum_semester.json()["_id"]238 def test_list_curriculum_semesters_with_two_semester():239 """Test listing curriculum semesters with two semesters"""240 curriculum_semesters = client.get(241 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters",242 headers={"Authorization": f"Bearer {test_user.token}"},243 )244 assert curriculum_semesters.status_code == 200245 assert curriculum_semesters.json() == [246 {247 "_id": test_user.curriculum_semester_id,248 "semester": 2,249 "lessons": [],250 },251 {252 "_id": test_user.second_curriculum_semester_id,253 "semester": 3,254 "lessons": [],255 },256 ]257 def test_update_curriculum_semester_with_exists_semester_name():258 """Test updating a curriculum semester with an exists semester code"""259 curriculum_semester = client.put(260 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",261 headers={"Authorization": f"Bearer {test_user.token}"},262 json={263 "semester": 3,264 },265 )266 assert curriculum_semester.status_code == 400267 assert (268 curriculum_semester.json()["message"]269 == "Curriculum semester already exists"270 )271 def test_update_curriculum_semester_with_default_user():272 """Test updating a curriculum semester with the default user"""273 curriculum_semester = client.put(274 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",275 headers={"Authorization": f"Bearer {default_user.token}"},276 json={277 "semester": 4,278 },279 )280 assert curriculum_semester.status_code == 403281 assert curriculum_semester.json()["message"] == "No right to access"282 def test_delete_curriculum_semester():283 """Test deleting a curriculum semester"""284 curriculum_semester = client.delete(285 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",286 headers={"Authorization": f"Bearer {test_user.token}"},287 )288 assert curriculum_semester.status_code == 200289 assert curriculum_semester.json()["message"] == "Curriculum semester deleted"290 def test_delete_same_curriculum_semester():291 """Test deleting a curriculum semester"""292 curriculum_semester = client.delete(293 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",294 headers={"Authorization": f"Bearer {test_user.token}"},295 )296 assert curriculum_semester.status_code == 404297 assert curriculum_semester.json()["message"] == "Curriculum semester not found"298 def test_delete_same_curriculum_semester_with_default_user():299 """Test deleting a curriculum semester with the default user"""300 curriculum_semester = client.delete(301 f"/universities/{test_user.university_id}/departments/{test_user.department_id}/curriculums/{test_user.curriculum_id}/semesters/{test_user.curriculum_semester_id}",302 headers={"Authorization": f"Bearer {default_user.token}"},303 )304 assert curriculum_semester.status_code == 403305 assert curriculum_semester.json()["message"] == "No right to access"306 def test_delete_used_university():307 """Test deleting a university that has been used"""308 university = client.delete(309 f"/universities/{test_user.university_id}",310 headers={"Authorization": f"Bearer {test_user.token}"},311 )312 assert university.status_code == 200...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!