Exemplo n.º 1
0
    def test_process_request_to_decos_join(self, mock_requests_get):
        """
        Is request url as expected
        """

        MOCK_URL = "https://test/"

        expected_header = {
            "Accept": "application/itemdata",
            "content-type": "application/json",
            "Authorization": "Basic 12345678",
        }
        expected_params = {
            "headers": expected_header,
            "timeout": 30,
        }

        decos_request = DecosJoinRequest()

        decos_request._process_request_to_decos_join(MOCK_URL)

        mock_requests_get.assert_called()

        mock_requests_get.assert_called_with(
            MOCK_URL,
            **expected_params,
        )
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        dt = datetime.today()
        if form.is_valid():
            if form.cleaned_data.get("search_url"):
                response = DecosJoinRequest().get(
                    form.cleaned_data.get("search_url"))
            elif (form.cleaned_data.get("bag_id")
                  and form.cleaned_data.get("response_type") == "raw"):
                response = DecosJoinRequest().get_decos_object_with_bag_id(
                    form.cleaned_data.get("bag_id"))
            elif (form.cleaned_data.get("bag_id")
                  and form.cleaned_data.get("response_type") == "checkmarks"):
                response = DecosJoinRequest().get_decos_entry_by_bag_id(
                    form.cleaned_data.get("bag_id"), dt)
                serializer = DecosSerializer(data=response)
                if serializer.is_valid():
                    response = serializer.data
                else:
                    response = {"serializer_error": "ERROR"}

            else:
                response = DecosJoinRequest().get("")

        context = self.get_context_data(**kwargs)
        context["form"] = form
        context["decos_data"] = json.dumps(response, indent=4)

        return self.render_to_response(context)
Exemplo n.º 3
0
    def test_get_decos_folder_fail(self):
        """
        Test failed when trying to get folder without proper decos object
        """

        MOCK_DECOS_OBJECT = {}

        decos_request = DecosJoinRequest()

        folder_result = decos_request._get_decos_folder(MOCK_DECOS_OBJECT)

        self.assertEqual(folder_result, False)
Exemplo n.º 4
0
    def test_get_decos_folder_succeeded(self, mock_get_folders_with_object_id):
        """
        Test succeeded when trying to get folder with proper decos object
        """

        MOCK_RESULT = {"count": 42}
        MOCK_DECOS_OBJECT = {"content": [{"key": "1234"}]}
        mock_get_folders_with_object_id.return_value = MOCK_RESULT

        decos_request = DecosJoinRequest()

        folder_result = decos_request._get_decos_folder(MOCK_DECOS_OBJECT)

        self.assertEqual(folder_result, MOCK_RESULT)
Exemplo n.º 5
0
    def test_get_folders_with_object_id(self,
                                        mock_process_request_to_decos_join):
        """
        Is request url as expected
        """

        MOCK_OBJECT_ID = "42"

        decos_request = DecosJoinRequest()

        decos_request.get_folders_with_object_id(MOCK_OBJECT_ID)

        mock_process_request_to_decos_join.assert_called()

        mock_process_request_to_decos_join.assert_called_with(
            settings.DECOS_JOIN_API + "items/42/FOLDERS/")
Exemplo n.º 6
0
    def permit_details(self, request, bag_id):
        response = DecosJoinRequest().get_permits_by_bag_id(bag_id)
        serializer = DecosPermitSerializer(data=response, many=True)

        if serializer.is_valid():
            return Response(serializer.data)
        return Response(serializer.initial_data)
Exemplo n.º 7
0
    def checkmarks(self, request, bag_id):
        response = DecosJoinRequest().get_checkmarks_by_bag_id(bag_id)

        serializer = PermitCheckmarkSerializer(data=response)

        if serializer.is_valid():
            return Response(serializer.data)
        return Response(serializer.initial_data)
Exemplo n.º 8
0
    def test_get_decos_object_with_bag_id(self,
                                          mock_process_request_to_decos_join):
        """
        Is request url as expected
        """

        MOCK_BAG_ID = "42"

        decos_request = DecosJoinRequest()

        decos_request.get_decos_object_with_bag_id(MOCK_BAG_ID)

        mock_process_request_to_decos_join.assert_called()

        mock_process_request_to_decos_join.assert_called_with(
            settings.DECOS_JOIN_API + "items/" +
            settings.DECOS_JOIN_BOOK_KNOWN_BAG_OBJECTS +
            "/COBJECTS?filter=PHONE3 eq '42'")
Exemplo n.º 9
0
    def test_get_decos_object_with_address(self,
                                           mock_process_request_to_decos_join):
        """
        Is request url as expected
        """

        MOCK_ADDRESS = "Duckstraat 42"

        decos_request = DecosJoinRequest()

        decos_request.get_decos_object_with_address(MOCK_ADDRESS)

        mock_process_request_to_decos_join.assert_called()

        mock_process_request_to_decos_join.assert_called_with(
            settings.DECOS_JOIN_API + "items/" +
            settings.DECOS_JOIN_BOOK_KNOWN_BAG_OBJECTS +
            "/COBJECTS?filter=SUBJECT1 eq 'Duckstraat 42'")
Exemplo n.º 10
0
    def permit_details(self, request, bag_id, dt=None):
        if not dt:
            dt = datetime.today()
        response = DecosJoinRequest().get_decos_entry_by_bag_id(bag_id, dt)
        serializer = DecosPermitSerializer(data=response, many=True)

        if serializer.is_valid():
            return Response(serializer.data)
        return Response(serializer.initial_data)
Exemplo n.º 11
0
    def check_status(self):
        from apps.permits.api_queries_decos_join import DecosJoinRequest

        try:
            # The address doesn't matter, as long an authenticated request is succesful.
            response = DecosJoinRequest().get_decos_object_with_address("foo")
            assert response, "Could not reach Decos Join"
        except Exception as e:
            self.add_error(ServiceUnavailable("Failed"), e)
 def check_status(self):
     logger.debug("Checking status of Decos Join API url...")
     response = DecosJoinRequest().get()
     if not response:
         self.add_error(
             ServiceUnavailable(
                 "Unable to connect to Decos Join: Connection was refused.")
         )
     else:
         logger.debug("Decos Join API connection established.")
Exemplo n.º 13
0
    def get_decos_entry_by_bag_id(self, request):
        bag_id = request.GET.get("bag_id")
        dt = datetime.strptime(
            request.GET.get("date",
                            datetime.today().strftime("%Y-%m-%d")), "%Y-%m-%d")

        if not bag_id:
            raise Http404

        response = DecosJoinRequest().get_decos_entry_by_bag_id(bag_id, dt)

        serializer = DecosSerializer(data=response)

        if serializer.is_valid(raise_exception=True):
            return Response(serializer.data)
        return Response(serializer.initial_data)