Ejemplo n.º 1
0
    def test_extract_tender(self, mocked_error_handler, mocked_decode_path):
        mocked_error_handler.return_value = Exception("Oops.")
        mocked_decode_path.side_effect = [
            KeyError("Missing 'PATH_INFO'"),
            UnicodeDecodeError("UTF-8", "obj", 1, 10, "Hm..."),
            "/",
            "/api/2.3/tenders/{}".format(self.tender_data["id"]),
        ]
        tender_data = deepcopy(self.tender_data)
        tender_data["doc_type"] = "Tender"
        request = MagicMock()
        request.environ = {"PATH_INFO": "/"}
        request.registry.tender_procurementMethodTypes.get.return_value = Tender
        request.tender_from_data.return_value = tender_from_data(
            request, tender_data)
        request.registry.db = MagicMock()

        # Test with KeyError
        self.assertIs(extract_tender(request), None)

        # Test with UnicodeDecodeError
        with self.assertRaises(URLDecodeError) as e:
            extract_tender(request)
        self.assertEqual(e.exception.encoding, "UTF-8")
        self.assertEqual(e.exception.object, "obj")
        self.assertEqual(e.exception.start, 1)
        self.assertEqual(e.exception.end, 10)
        self.assertEqual(e.exception.reason, "Hm...")
        self.assertIsInstance(e.exception, URLDecodeError)

        # Test with path '/'
        self.assertIs(extract_tender(request), None)

        mocked_decode_path.side_effect = [
            "/api/2.3/tenders/{}".format(self.tender_data["id"])
        ] * 3

        # Test with extract_tender_adapter raise HTTP 410
        request.registry.db.get.return_value = {"doc_type": "tender"}
        with self.assertRaises(Exception) as e:
            extract_tender(request)
        self.assertEqual(request.errors.status, 410)
        request.errors.add.assert_called_once_with("url", "tender_id",
                                                   "Archived")

        # Test with extract_tender_adapter raise HTTP 404
        request.registry.db.get.return_value = {"doc_type": "notTender"}
        with self.assertRaises(Exception) as e:
            extract_tender(request)
        self.assertEqual(request.errors.status, 404)
        request.errors.add.assert_has_calls(
            [call("url", "tender_id", "Not Found")])

        # Test with extract_tender_adapter return Tender object
        request.registry.db.get.return_value = tender_data
        tender = extract_tender(request)
        serialized_tender = tender.serialize("draft")
        self.assertIsInstance(tender, Tender)
        for k in tender_data:
            self.assertEqual(tender_data[k], serialized_tender[k])
    def test_extract_tender(self, mocked_error_handler, mocked_decode_path):
        mocked_error_handler.return_value = Exception('Oops.')
        mocked_decode_path.side_effect = [
            KeyError('Missing \'PATH_INFO\''),
            UnicodeDecodeError('UTF-8', 'obj', 1, 10, 'Hm...'), '/',
            '/api/2.3/tenders/{}'.format(self.tender_data['id'])
        ]
        tender_data = deepcopy(self.tender_data)
        tender_data['doc_type'] = 'Tender'
        request = MagicMock()
        request.environ = {'PATH_INFO': '/'}
        request.registry.tender_procurementMethodTypes.get.return_value = \
            Tender
        request.tender_from_data.return_value = \
            tender_from_data(request, tender_data)
        request.registry.db = MagicMock()

        # Test with KeyError
        self.assertIs(extract_tender(request), None)

        # Test with UnicodeDecodeError
        with self.assertRaises(URLDecodeError) as e:
            extract_tender(request)
        self.assertEqual(e.exception.encoding, 'UTF-8')
        self.assertEqual(e.exception.object, 'obj')
        self.assertEqual(e.exception.start, 1)
        self.assertEqual(e.exception.end, 10)
        self.assertEqual(e.exception.reason, 'Hm...')
        self.assertIsInstance(e.exception, URLDecodeError)

        # Test with path '/'
        self.assertIs(extract_tender(request), None)

        mocked_decode_path.side_effect = [
            '/api/2.3/tenders/{}'.format(self.tender_data['id'])
        ] * 3

        # Test with extract_tender_adapter raise HTTP 410
        request.registry.db.get.return_value = {'doc_type': 'tender'}
        with self.assertRaises(Exception) as e:
            extract_tender(request)
        self.assertEqual(request.errors.status, 410)
        request.errors.add.assert_called_once_with('url', 'tender_id',
                                                   'Archived')

        # Test with extract_tender_adapter raise HTTP 404
        request.registry.db.get.return_value = {'doc_type': 'notTender'}
        with self.assertRaises(Exception) as e:
            extract_tender(request)
        self.assertEqual(request.errors.status, 404)
        request.errors.add.assert_has_calls(
            [call('url', 'tender_id', 'Not Found')])

        # Test with extract_tender_adapter return Tender object
        request.registry.db.get.return_value = tender_data
        tender = extract_tender(request)
        serialized_tender = tender.serialize('draft')
        self.assertIsInstance(tender, Tender)
        for k in tender_data:
            self.assertEqual(tender_data[k], serialized_tender[k])
Ejemplo n.º 3
0
    def test_tender_from_data(self, mocked_handler):
        mocked_handler.return_value = Exception("Mocked!")
        request = MagicMock()
        request.registry.tender_procurementMethodTypes.get.side_effect = [None, None, Tender, Tender]

        with self.assertRaises(Exception) as e:
            tender_from_data(request, self.tender_data)
        self.assertEqual(e.exception.message, "Mocked!")
        self.assertEqual(request.errors.status, 415)
        request.errors.add.assert_called_once_with("data", "procurementMethodType", "Not implemented")

        model = tender_from_data(request, self.tender_data, raise_error=False)
        self.assertIs(model, None)

        model = tender_from_data(request, self.tender_data, create=False)
        self.assertIs(model, Tender)

        model = tender_from_data(request, self.tender_data)
        self.assertIsInstance(model, Tender)
Ejemplo n.º 4
0
    def test_tender_from_data_revisions(self):
        request = MagicMock()
        request.registry.tender_procurementMethodTypes = {"belowThreshold": Tender}

        data = dict(**self.tender_data)
        data["revisions"] = [dict(rev=str(i), author="me") for i in range(10)]

        for method in ("GET", "POST", "PATCH", "PUT", "DELETE", "HEAD"):
            request.environ = {"REQUEST_METHOD": method}
            model = tender_from_data(request, data)

            self.assertEqual(len(model.revisions), 10)
            self.assertEqual(model.revisions[0]["rev"], "0")