def test_get_results_incorrect_uid(self, app): """ GET /api/questions/<question_id>/results: attempt to retrieve question with invalid uid """ uid_one = set_test_cookie(app) question_one_id = build_question("Question 1", uid_one)["_id"] uid_two = set_test_cookie(app) question_two_id = build_question("Question 2", uid_two)["_id"] res = app.get(f"/api/questions/{question_one_id}/results") assert res.status_code == 401 res = app.get(f"/api/questions/{question_two_id}/results") assert res.status_code == 200
def test_get_results_uid_accuracy(self, app): """ GET /api/questions/results: validate accuracy on retrieved results for a particular user """ uid = set_test_cookie(app) number_of_questions = 10 questions = build_questions(uid, number_of_questions) result_value = 5 questions = seed_questions_with_sample_results(questions, result_value) res = app.get(f"/api/questions/results") assert res.status_code == 200 results = res.json.get("results") assert results expected_avg = (number_of_questions * result_value) / number_of_questions expected_total = number_of_questions * result_value number_of_pauses = results.get("number_of_pauses") assert number_of_pauses["avg"] == expected_avg assert number_of_pauses["total"] == expected_total words_per_minute = results.get("words_per_minute") assert words_per_minute["avg"] == expected_avg assert words_per_minute["total"] == expected_total
def test_submit_answer_201(self, app): """ POST /api/questions/<question_id>/answer: submit proper file and check local directories """ uid = set_test_cookie(app) question_id = build_question("test question", uid)["_id"] webm_path = f"{TMP_DIR}/{question_id}{WEBM_EXT}" webm = generate_sample_webm(question_id) assert webm assert os.path.exists(webm_path) res = app.post( f"/api/questions/{question_id}/answer", data=dict(audio=webm), content_type="multipart/form-data", ) assert res.status_code == 201 question_response = res.json.get("question") assert question_response assert len(question_response) >= 1 blob_path = f"{uid}/{question_id}{WEBM_EXT}" blob_url = res.json.get("question").get("answer") assert blob_url assert blob_url == get_test_blob_url(blob_path) cleanup_webm(uid, question_id) assert os.path.exists(webm_path) is False
def test_get_results_uid_404(self, app): """ GET /api/questions/results: attempt to retrieve results without valid questions """ uid = set_test_cookie(app) res = app.get(f"/api/questions/results") assert res.status_code == 404
def test_get_results_404(self, app): """ GET /api/questions/<question_id>/results: provide invalid question_id """ uid = set_test_cookie(app) res = app.get(f"/api/questions/{new_id()}/results") assert res.status_code == 404
def test_get_results_uid_200(self, app): """ GET /api/questions/results: retrieve valid results """ uid = set_test_cookie(app) build_question("Question 1", uid) res = app.get(f"/api/questions/results") assert res.status_code == 200
def test_get_results_200(self, app): """ GET /api/questions/<question_id>/results: retrieve results for a valid [uid:question_id] combination """ uid = set_test_cookie(app) question_id = build_question("Question 1", uid)["_id"] res = app.get(f"/api/questions/{question_id}/results") assert res.status_code == 200 assert res.json.get("result") assert len(res.json.get("result")) >= 1
def test_submit_answer_400(self, app): """ POST /api/questions/<question_id>/answer: uses malformed file ending """ uid = set_test_cookie(app) file = dict(audio=(io.BytesIO(str.encode(uid)), "test.txt")) res = app.post( f"/api/questions/{new_id()}/answer", data=file, content_type="multipart/form-data", ) assert res.status_code == 400
def test_get_question_200(self, app): uid = set_test_cookie(app) question_doc = build_question("test question", uid) res = app.get(f"/api/questions/{question_doc['_id']}") assert res.status_code == 200 question = res.json.get("question") assert question question_doc = find_question_by_id(question["_id"]) assert question_doc assert question_doc["description"] == question["description"] assert question_doc["user_id"] == question["user_id"]
def test_add_questions_use_cookie(self, app): """ POST /api/questions: uses existing cookie sent with request """ uid = set_test_cookie(app) res = app.post("/api/questions", json=dict(questions=["question 1"])) assert res.status_code == 201 assert not res.headers.get("Set-Cookie") assert len(res.json["questions"]) == 1 question = find_question_by_id(res.json["questions"][0]) assert question assert question["user_id"] == uid
def test_get_questions_200(self, app): uid = set_test_cookie(app) question_docs = build_questions(uid, 10) res = app.get(f"/api/questions") assert res.status_code == 200 questions_response = res.json.get("questions") assert len(questions_response) == 10 for question in questions_response: question_doc = find(lambda q: q["_id"] == question["_id"], question_docs) assert question_doc assert question_doc["created_on"] == question["created_on"] assert question_doc["description"] == question["description"] assert question_doc["user_id"] == question["user_id"]
def test_get_questions_404(self, app): uid = set_test_cookie(app) res = app.get(f"/api/questions") assert res.status_code == 404