def test_delete_faq_method_handles_not_found(self): with self.app.app_context(): faq = FaqFactory.create(is_deleted=True) faq_controller = FaqController(self.request_context) response = faq_controller.delete_faq(faq.id) self.assertEqual(response.status_code, 404) self.assertEqual(response.get_json()['msg'], 'FAQ Not Found')
def test_list_faq_method_returns_faqs(self): with self.app.app_context(): new_faq = FaqFactory.create() faq_controller = FaqController(self.request_context) response = faq_controller.list_faqs() self.assertEqual(response.status_code, 200) self.assertEqual(response.get_json()['msg'], 'OK') self.assertEqual(response.get_json()['payload']['faqs'][0]['id'], new_faq.id)
def test_delete_faq_method_succeeds(self): with self.app.app_context(): faq = FaqFactory() faq_controller = FaqController(self.request_context) response = faq_controller.delete_faq(faq.id) self.assertEqual(response.status_code, 200) self.assertEqual(response.get_json()['msg'], 'FAQ deleted successfully')
def test_list_faq_method_captures_invalid_dates(self): with self.app.app_context(): faq_controller = FaqController(self.request_context) response = faq_controller.list_faqs( created_at='2019-04-04-invalid') self.assertEqual(response.status_code, 400) self.assertEqual( response.get_json()['msg'], 'Bad Request - created_at should be valid date. Format: YYYY-MM-DD' )
def test_list_faq_method_captures_invalid_category_values( self, mock_filter_by): with self.app.app_context(): mock_filter_by.side_effect = DataError('statement', 'params', 'orig') faq_controller = FaqController(self.request_context) response = faq_controller.list_faqs(category='2019-04-04-invalid') self.assertEqual(response.status_code, 400) self.assertEqual( response.get_json()['msg'], "Category should be one of these values ['user_faq', 'admin_faq']" )
def test_update_faq_method_handle_not_found(self, mock_request_params_dict): with self.app.app_context(): faq = FaqFactory() update_faq_info = FaqFactory.build() mock_request_params_dict.return_value = { 'question': update_faq_info.question } faq_controller = FaqController(self.request_context) response = faq_controller.update_faq(1000) self.assertEqual(response.status_code, 404) self.assertEqual(response.get_json()['msg'], 'FAQ Not Found')
def test_create_faq_method_handles_duplicate_faq_creation( self, mock_request_params): with self.app.app_context(): faq = FaqFactory() mock_request_params.return_value = [ faq.category, faq.question, faq.answer ] faq_controller = FaqController(self.request_context) response = faq_controller.create_faq() self.assertEqual(response.status_code, 400) self.assertEqual( response.get_json()['msg'], "Question '{}' already exists".format(faq.question))
def test_update_faq_method_succeeds(self, mock_request_params_dict): with self.app.app_context(): faq = FaqFactory() update_faq_info = FaqFactory.build() mock_request_params_dict.return_value = { 'question': update_faq_info.question } faq_controller = FaqController(self.request_context) response = faq_controller.update_faq(faq.id) self.assertEqual(response.status_code, 201) self.assertEqual(response.get_json()['msg'], 'OK') self.assertEqual(response.get_json()['payload']['faq']['question'], update_faq_info.question)
def test_create_faq_method_succeeds(self, mock_request_params): with self.app.app_context(): faq = FaqFactory.build() mock_request_params.return_value = [ faq.category, faq.question, faq.answer ] faq_controller = FaqController(self.request_context) response = faq_controller.create_faq() self.assertEqual(response.status_code, 201) self.assertEqual(response.get_json()['msg'], 'OK') self.assertEqual(response.get_json()['payload']['faq']['question'], faq.question) self.assertEqual(response.get_json()['payload']['faq']['answer'], faq.answer)
'''A module of FAQ blueprint''' from app.blueprints.base_blueprint import Blueprint, BaseBlueprint, request, Auth from app.controllers.faq_controller import FaqController from app.utils.security import Security from app.models import Faq from flasgger import swag_from faq_blueprint = Blueprint('faq', __name__, url_prefix='{}/faqs'.format(BaseBlueprint.base_url_prefix)) faq_controller = FaqController(request) @faq_blueprint.route('/', methods=['GET']) @Security.validate_query_params(Faq) @swag_from('documentation/get_faqs.yml') def list_faqs(): kwargs = faq_controller.get_params_dict() return faq_controller.list_faqs(**kwargs) @faq_blueprint.route('/', methods=['POST']) @Auth.has_role('admin') @Security.validator(['category|required:enum_FaqCategoryType', 'question|required', 'answer|required']) @swag_from('documentation/create_faq.yml') def create_faq(): return faq_controller.create_faq()