def test_question_answer_as_admin(self): question = Question(text='Question?') db.session.add(question) db.session.commit() answer = Answer(question_id=question.id, text='Answer.', application_id=self.application.id) db.session.add(answer) db.session.commit() with self.assertRaises(ForbiddenException): get_answers(self.applicant.id, current_user=self.admin)
def api_user_answers(user_id=None): """ Get and put question answers for a given user. Args: user_id (int) if missing/None uses the logged in user answers (list) Given for PUT only. List of dict with keys 'question_id' and 'text'. Returns: response (dict): A dictionary whose keys are integer question ids and values are answer text, question text & user id. Error codes: Forbidden (403): If logged in user is not a senior recruiter, a recruiter who has claimed the given user, or the user themself, or if there is not an unsubmitted application for the user. """ if request.method == 'GET': user_id = user_id or current_user.id return jsonify(get_answers(user_id, current_user=current_user)) elif request.method == 'PUT': return jsonify( set_answers(user_id, answers=request.get_json()['answers'], current_user=current_user))
def test_three_questions_one_answer(self): for i in range(3): question = Question(text='Question {}?'.format(i)) db.session.add(question) db.session.commit() answer = Answer( question_id=question.id, text='My answer.', application_id=self.application.id, ) db.session.add(answer) db.session.commit() result = get_answers(self.applicant.id, current_user=self.recruiter) self.assertIsInstance(result, dict) self.assertEqual(len(result['questions']), 3) for question_id, answer_data in result['questions'].items(): self.assertIsInstance(question_id, int) self.assertIsInstance(answer_data['question'], str) self.assertIsInstance(answer_data['answer'], str) self.assertEqual(answer_data['user_id'], self.applicant.id) self.assertEqual(len(answer_data), 3, answer_data) if question_id != answer.question_id: self.assertEqual(answer_data['answer'], '', answer) self.assertEqual(result['questions'][answer.question_id]['answer'], 'My answer.')
def test_three_questions_no_answers(self): for i in range(3): question = Question(text='Question {}?'.format(i)) db.session.add(question) db.session.commit() result = get_answers(self.applicant.id, current_user=self.recruiter) self.assertIsInstance(result, dict) self.assertIn('questions', result) self.assertEqual(len(result['questions']), 3) for question_id, answer_data in result['questions'].items(): self.assertIsInstance(question_id, int) self.assertIsInstance(answer_data['question'], str) self.assertEqual(answer_data['user_id'], self.applicant.id) self.assertIsInstance(answer_data['answer'], str) self.assertEqual(answer_data['answer'], '') self.assertEqual(len(answer_data), 3, answer_data)
def test_question_answer_as_applicant(self): question = Question(text='Question?') db.session.add(question) db.session.commit() answer = Answer(question_id=question.id, text='Answer.', application_id=self.application.id) db.session.add(answer) db.session.commit() result = get_answers(self.applicant.id, current_user=self.applicant) self.assertIsInstance(result, dict) self.assertEqual(len(result['questions']), 1) self.assertEqual(result['questions'][question.id]['answer'], 'Answer.') self.assertEqual(result['questions'][question.id]['question'], 'Question?') self.assertEqual(result['questions'][question.id]['user_id'], self.applicant.id)
def test_set_answers_applicant_with_application_applicant(self): self.application.is_submitted = False db.session.commit() for i in range(3): question = Question(text='Question {}?'.format(i)) db.session.add(question) db.session.commit() result = get_questions(current_user=self.applicant) question_keys = [] answers = [] for k in result: question_keys.append(k) answers.append({'question_id': k, 'text': f'answer for {k}'}) set_answers(self.applicant.id, answers=answers, current_user=self.applicant) db.session.commit() after_set_result = get_answers(self.applicant.id, current_user=self.applicant) self.assertIsInstance(after_set_result, dict) self.assertEqual(len(after_set_result['questions']), 3) for k in question_keys: self.assertEqual(after_set_result['questions'][k]['answer'], f'answer for {k}')
def test_no_questions_answers(self): result = get_answers(self.applicant.id, current_user=self.recruiter) self.assertIsInstance(result, dict) self.assertIn('questions', result) self.assertEqual(len(result['questions']), 0)
def test_has_no_application(self): with self.assertRaises(BadRequestException): get_answers(self.not_applicant.id)
def test_has_application(self): result = get_answers(self.applicant.id) self.assertIsInstance(result, dict) self.assertIn('has_application', result) self.assertEqual(result['has_application'], True)