123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import pytest
- import requests
- import json
- OK = 200
- CREATED = 201
- NOT_FOUND = 404
- CONFLICT = 409
- WEBSITE_PREFIX = 'http://localhost:8080'
- def test_multiple_exam_apis():
- new_exam = {
- 'name': '期中考试',
- 'description': '可以回家做',
- 'duration': 86400, # seconds
- }
- resp = create_exam(new_exam)
- assert(resp.status_code == OK)
- examId = json.loads(resp.text)['id']
- resp = all_exams()
- assert(resp.status_code == OK)
- exams = json.loads(resp.text)
- assert(len(exams) == 1)
- non_exist_user = {
- 'userName': 'fake'
- }
- resp = all_exams(non_exist_user) # show the exams available to this given user
- exams = json.loads(resp.text)
- assert(len(exams) == 0)
- resp = get_exam(examId)
- assert(resp.status_code == OK)
- exam = json.loads(resp.text)
- assert(exam['description'] == '可以回家做')
- assert(exam['duration'] == 86400)
- resp = edit_exam(examId, {
- 'duration': 100
- })
- assert(resp.status_code == OK)
- assert(json.loads(resp.text)['duration'] == 100)
- from user_test import register_user, delete_user, get_user
- resp = register_user('exam-lover', 'iloveexam')
- assert(resp.status_code == CREATED)
- resp = get_user('exam-lover')
- user = json.loads(resp.text)
- question = {
- 'userId': user['userName'],
- 'content': '天上什么星星最亮?',
- 'answers': ['太阳', '小天狼星', '北极星'],
- 'finalAnswer': ['小天狼星'],
- 'type': 'DanXuan'
- }
- resp = create_question(question)
- assert(resp.status_code == OK)
- qid = json.loads(resp.text)['id']
- resp = get_question(qid)
- assert(resp.status_code == OK)
- created_question = json.loads(resp.text)
- assert(created_question['userId'] == user['userName'])
- assert(created_question['finalAnswer'][0] == '小天狼星')
- resp = edit_question(qid, {
- 'finalAnswer': ['太阳']
- })
- updated_question = json.loads(resp.text)
- assert(updated_question['finalAnswer'][0] == '太阳')
- resp = add_question_to_exam(examId, [updated_question])
- assert(resp.status_code == OK)
- resp = start_exam(examId, user)
- assert(resp.status_code == OK)
- questions = json.loads(resp.text)
- assert(len(questions) == 1) # 1 question is under this exam
- resp = end_exam(examId, {
- 'startTime': '2021-01-01T17:09:42.411',
- 'endTime': '2021-01-01T17:09:50.411',
- 'userId': user['userName'],
- 'points': 5
- })
- assert(resp.status_code == OK)
- resp = remove_question_from_exam(examId, updated_question)
- resp = start_exam(examId, user)
- assert(resp.status_code == OK)
- questions = json.loads(resp.text)
- assert(len(questions) == 0) # 0 question is under this exam after removal
- delete_question(updated_question['id'])
- remove_exam(examId)
- delete_user(user['userName'])
- def create_exam(exam):
- url = WEBSITE_PREFIX + '/admin/exams'
- return requests.post(url, json=exam)
- def get_exam(examId):
- url = WEBSITE_PREFIX + '/exams/' + examId
- return requests.get(url)
- def edit_exam(examId, updatedExam):
- url = WEBSITE_PREFIX + '/exams/' + examId
- return requests.put(url, json=updatedExam)
- def remove_exam(examId):
- url = WEBSITE_PREFIX + '/exams/' + examId
- return requests.delete(url)
- def start_exam(examId, user):
- url = WEBSITE_PREFIX + '/exams/' + examId + '/start'
- return requests.post(url, json=user)
- def end_exam(examId, examHistory):
- url = WEBSITE_PREFIX + '/exams/' + examId + '/end'
- return requests.post(url, json=examHistory)
- def all_exams(user = None):
- url = WEBSITE_PREFIX + '/exams'
- return requests.get(url, json=user)
- def get_question(qid):
- url = WEBSITE_PREFIX + '/questions/' + qid
- return requests.get(url)
- def create_question(question):
- url = WEBSITE_PREFIX + '/questions/'
- return requests.post(url, json=question)
- def edit_question(qid, updatedQuestion):
- url = WEBSITE_PREFIX + '/questions/' + qid
- return requests.put(url, json=updatedQuestion)
- def delete_question(qid):
- url = WEBSITE_PREFIX + '/questions/' + qid
- return requests.delete(url)
- def add_question_to_exam(examId, questions):
- url = WEBSITE_PREFIX + '/exams/' + examId + '/questions'
- return requests.post(url, json=questions)
- def remove_question_from_exam(examId, question):
- url = WEBSITE_PREFIX + '/exams/' + examId + '/questions'
- return requests.delete(url, json=question)
|