def search_questions(request): """ search questions based on parameters. return Nested Json of Questions with answers and users """ try: service = Services() tenant_api_key = request.META.get('HTTP_API_KEY') if tenant_api_key : service.validate_uuid4(tenant_api_key) tenant = service.check_valid_api_key(tenant_api_key) else: raise CustomApiException("Please provide API key", status.HTTP_400_BAD_REQUEST) title = request.GET.get('title', None) offset = request.GET.get('offset', 0) limit = request.GET.get('limit', 10) questions, count = service.service_search_questions(title, offset, limit) question_serializer = QuestionSerializer(questions, many=True) HttpResponse.status_code = status.HTTP_200_OK return JsonResponse({'count': count,'questions': question_serializer.data}) except CustomApiException as err: HttpResponse.status_code = err.status_code return JsonResponse({'status_code': err.status_code, 'message': err.detail}) except Exception, e: HttpResponse.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR return JsonResponse({'status_code': status.HTTP_500_INTERNAL_SERVER_ERROR, 'message': str(e)})
def validate_uuid4(self, api_key): try: val = UUID(api_key, version=4) except: raise CustomApiException("Tenant API key is not valid UUID", status.HTTP_400_BAD_REQUEST) return val.hex == api_key.replace('-', '')
def test_unit_search_question_not_found(self, db, django_db_setup, mocker): questions_list = Question.objects.filter(Q(private = False) & Q(title__icontains = 'My Question'))[self.offset : self.limit] count = Question.objects.filter(Q(private = False) & Q(title__icontains = 'My Question')).count() with pytest.raises(CustomApiException) as exc_info: CustomApiException("Questions not found based on criteria", status.HTTP_404_NOT_FOUND) self.service_instance.service_search_questions('My Question', self.offset, self.limit)
def check_valid_api_key(self, api_key): try: tenant = Tenant.objects.get(api_key=api_key) tenant.api_requests_count += 1 tenant.request_date_time = timezone.now() tenant.daily_api_requests_count += 1 tenant.save() return tenant except Tenant.DoesNotExist as e: raise CustomApiException("Please provide valid API key", status.HTTP_400_BAD_REQUEST)
def test_unit_questions_not_found(self, db, django_db_setup, mocker): """ This method will tests search questions in views assert status is 404 and questions not found message is returned """ params = {'title': 'myquestion', 'offset': '0', 'limit' : '10'} request = self.request_factory.get(self.search_questions_api_url, params, HTTP_API_KEY = "6e762d97-2d46-48cc-99b6-58cc0942d514") mock_service_search_questions = mocker.patch.object(Services, 'service_search_questions') mock_service_search_questions.side_effect = CustomApiException("Questions not found based on criteria", status.HTTP_404_NOT_FOUND) response = search_questions(request) Services.service_search_questions.assert_called_with(params['title'], params['offset'], params['limit']) json_response = json.loads(response.content) assert response.status_code == status.HTTP_404_NOT_FOUND assert json_response['message'] == "Questions not found based on criteria"
def service_search_questions(self, title, offset, limit): #depending on the parameters fire query and get results #based on pagination questions_list = [] count = 0 if not title: questions_list = Question.objects.filter( private=False)[offset:limit] count = Question.objects.filter(private=False).count() if title: questions_list = Question.objects.filter( Q(private=False) & Q(title__icontains=title))[offset:limit] count = Question.objects.filter( Q(private=False) & Q(title__icontains=title)).count() if not questions_list: raise CustomApiException("Questions not found based on criteria", status.HTTP_404_NOT_FOUND) return questions_list, count
def test_unit_check_valid_api_key_error_case(self, db, django_db_setup, mocker): with pytest.raises(CustomApiException) as exc_info: CustomApiException("Please provide valid API key", status.HTTP_400_BAD_REQUEST) self.service_instance.check_valid_api_key("20ca9a9b-50f3-47e5-bb77-cee5575060d0")