示例#1
0
    def test_bad_overrides(self):
        "ensure a number of bad cases for overrides fail"
        cases = [
            "title", # no pipe
            "title|", # pipe, but no value
            "title|bar", # invalid value (should be json: "bar")
            "|bar", # invalid key
        ]
        xml_fname = 'elife-16695-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        with open(xml_fixture, 'rb') as fh:
            for override in cases:
                resp = self.client.post('/xml?id=16695&version=1', **{
                    'buffered': True,
                    'content_type': 'multipart/form-data',
                    'data': { # culprit lies in the payload here somewhere
                        'xml': fh,
                        'override': [override],
                    }
                })

                expected_resp = {
                    'status': conf.ERROR,
                    'code': conf.BAD_OVERRIDES,
                    # 'message': '...',
                    # 'trace': '...',
                }

                self.assertEqual(resp.status_code, 400)
                self.assertTrue(utils.partial_match(expected_resp, resp.json))
                self.assertTrue(resp.json['trace'].startswith('Traceback (most recent call last):'))
                self.assertTrue(resp.json['message']) # one exists and isn't empty
示例#2
0
    def test_upload_invalid(self):
        "the response we expect when the scraped article-json is invalid"
        xml_fname = 'elife-00666-v1.xml.invalid'
        xml_upload_fname = 'elife-00666-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        with patch('adaptor.call_lax', side_effect=AssertionError("test shouldn't make it this far!")):
            resp = self.client.post('/xml?id=666&version=1', **{
                'buffered': True,
                'content_type': 'multipart/form-data',
                'data': {
                    'xml': (open(xml_fixture, 'rb'), xml_upload_fname),
                }
            })
        self.assertEqual(resp.status_code, 400) # bad request data

        # ensure xml uploaded
        expected_path = join(self.temp_dir, xml_upload_fname)
        self.assertTrue(os.path.exists(expected_path))

        # ensure ajson scraped
        expected_ajson = join(self.temp_dir, xml_upload_fname) + '.json'
        self.assertTrue(os.path.exists(expected_ajson))

        expected_resp = {
            'status': conf.INVALID,
            'code': conf.ERROR_INVALID,
            # 'message': '...', # will probably change
            # 'trace': '...', # stacktrace
        }
        self.assertTrue(utils.partial_match(expected_resp, resp.json))
        self.assertTrue(resp.json['message'])
        # title is missing
        self.assertTrue(resp.json['trace'].startswith("None is not of type 'string'"), "actual trace: %s" % resp.json['trace'])
示例#3
0
    def test_bad_upload_filename(self):
        "the response we expect when the xml fails to upload"
        xml_fname = 'elife-16695-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        # msid doesn't match
        # filename doesn't match pattern 'elife-msid-vN.xml'
        bad_path = '/var/www/html/_default/cms/cms-0.9.40-alpha/ecs_includes/packageCreator/19942-v1.xml'
        resp = self.client.post('/xml?id=16695&version=1', **{
            'buffered': True,
            'content_type': 'multipart/form-data',
            'data': {
                'xml': (open(xml_fixture, 'rb'), bad_path),
            }
        })

        expected_lax_resp = {
            'status': conf.ERROR,
            'code': conf.BAD_SCRAPE,
            # 'message': '...', # we just care that a message exists
            # 'trace': '...', # same again, just that a trace exists
        }
        self.assertEqual(resp.status_code, 400)
        self.assertTrue(utils.partial_match(expected_lax_resp, resp.json))
        self.assertTrue(resp.json['trace'].startswith('Traceback (most recent call last):'))
        # self.assertTrue(resp.json['message']) # one exists and isn't empty
        self.assertTrue(resp.json['message'].startswith('not enough values to unpack')) # todo: gotta fix this filename parsing
示例#4
0
 def test_request_lax_style(self):
     "dictionaries of article information can be fed in"
     paths = [{
         'msid':
         16695,
         'version':
         1,
         'location':
         'https://s3-external-1.amazonaws.com/elife-publishing-expanded/16695.1/9c2cabd8-a25a-4d76-9f30-1c729755480b/elife-16695-v1.xml',
     }]
     expected_lax_response = {
         "id": "16695",
         "requested-action": 'ingest',
         "token": 'pants-party',
         "status": conf.INGESTED,
         "message": '...?',
         "datetime": utils.ymdhms(datetime.now())
     }
     expected = {
         'valid': [expected_lax_response],
         'errors': [],
         'invalid': []
     }
     with mock.patch('adaptor.call_lax',
                     autospec=True,
                     specset=True,
                     return_value=expected_lax_response):
         with mock.patch('adaptor.http_download',
                         autospec=True,
                         return_value=open(self.small_doc, 'r')):
             actual = bfup.do_paths(paths)
             self.assertTrue(partial_match(expected, actual))
示例#5
0
    def test_bad_upload_ext(self):
        "the response we expect when the xml fails to upload"
        xml_fname = 'elife-16695-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        bad_ext = '.pants'
        resp = self.client.post('/xml?id=16695&version=1', **{
            'buffered': True,
            'content_type': 'multipart/form-data',
            'data': {
                'xml': (open(xml_fixture, 'rb'), xml_fname + bad_ext),
            }
        })

        expected_lax_resp = {
            'status': conf.ERROR,
            'code': conf.BAD_UPLOAD,
            # 'message': '...', # we just care that a message exists
            # 'trace': '...', # same again, just that a trace exists
        }
        self.assertEqual(resp.status_code, 400)

        self.assertTrue(utils.partial_match(expected_lax_resp, resp.json))
        self.assertTrue(resp.json['trace'].startswith('Traceback (most recent call last):'))
        self.assertTrue(resp.json['message']) # one exists and isn't empty
示例#6
0
    def test_request_multiple_valid_paths(self):
        "sends multiple articles to lax given paths to xml files"
        paths = [self.small_doc] * 3  # send the same article three times

        expected_lax_response = {
            # "id": '09560',
            "id": "16695",
            "requested-action": 'ingest',
            "token": 'pants-party',  # set by fs-adaptor
            "status": conf.INGESTED,
            "message": '...?',
            "datetime": utils.ymdhms(datetime.now())
        }
        expected = {
            'valid': [expected_lax_response] * 3,
            'errors': [],
            'invalid': []
        }

        with mock.patch('adaptor.call_lax',
                        autospec=True,
                        specset=True,
                        return_value=expected_lax_response):
            actual = bfup.do_paths(paths)
            self.assertTrue(partial_match(expected, actual))
示例#7
0
    def test_broken_glencoe_response(self):
        "the response we expect when the glencoe code fails"

        err_message = "informative error message"

        expected_resp = {
            'status': conf.ERROR,
            'code': conf.BAD_SCRAPE,
            'message': err_message,
            #'trace': '...' # super long, can't predict, especially when mocking
        }
        with patch('glencoe.validate_gc_data',
                   side_effect=AssertionError(err_message)):
            resp = self.client.post(
                '/xml', **{
                    'buffered': True,
                    'content_type': 'multipart/form-data',
                    'data': {
                        'xml':
                        open(join(self.fixtures_dir, 'elife-00666-v1.xml'),
                             'rb'),
                    },
                })

        self.assertEqual(resp.status_code, 400)  # bad request
        self.assertTrue(utils.partial_match(expected_resp, resp.json))
        self.assertTrue(resp.json['trace'].startswith('Traceback (most'))
示例#8
0
    def test_bad_overrides(self):
        "ensure a number of bad cases for overrides fail"
        cases = [
            "title",  # no pipe
            "title|",  # pipe, but no value
            "title|bar",  # invalid value (should be json: "bar")
            "|bar",  # invalid key
        ]
        xml_fname = 'elife-16695-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        with open(xml_fixture, 'rb') as fh:
            for override in cases:
                resp = self.client.post('/xml', **{
                    'buffered': True,
                    'content_type': 'multipart/form-data',
                    'data': { # culprit lies in the payload here somewhere
                        'xml': fh,
                        'override': [override],
                    }
                })

                expected_resp = {
                    'status': conf.ERROR,
                    'code': conf.BAD_OVERRIDES,
                    #'message': '...',
                    #'trace': '...',
                }

                self.assertEqual(resp.status_code, 400)
                self.assertTrue(utils.partial_match(expected_resp, resp.json))
                self.assertTrue(resp.json['trace'].startswith(
                    'Traceback (most recent call last):'))
                self.assertTrue(
                    resp.json['message'])  # one exists and isn't empty
示例#9
0
    def test_bad_scrape(self):
        "the response we expect the xml fails to scrape"
        xml_fname = 'elife-16695-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        with patch('main.main', side_effect=AssertionError('meow')):
            resp = self.client.post(
                '/xml', **{
                    'buffered': True,
                    'content_type': 'multipart/form-data',
                    'data': {
                        'xml': open(xml_fixture, 'rb'),
                    }
                })

        expected_lax_resp = {
            'status': conf.ERROR,
            'code': conf.BAD_SCRAPE,
            #'message': '...', # we just care that a message exists
            #'trace': '...', # same again, just that a trace exists
        }
        self.assertEqual(resp.status_code, 400)
        self.assertTrue(utils.partial_match(expected_lax_resp, resp.json))
        self.assertTrue(resp.json['trace'].startswith(
            'Traceback (most recent call last):'))
        self.assertTrue(resp.json['message'])  # one exists and isn't empty
示例#10
0
    def test_bad_upload(self):
        "the response we expect when the xml fails to upload"
        xml_fname = 'elife-16695-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        bad_ext = '.pants'
        resp = self.client.post(
            '/xml', **{
                'buffered': True,
                'content_type': 'multipart/form-data',
                'data': {
                    'xml': (open(xml_fixture, 'rb'), xml_fname + bad_ext),
                }
            })

        expected_lax_resp = {
            'status': conf.ERROR,
            'code': conf.BAD_UPLOAD,
            #'message': '...', # we just care that a message exists
            #'trace': '...', # same again, just that a trace exists
        }
        self.assertEqual(resp.status_code, 400)

        self.assertTrue(utils.partial_match(expected_lax_resp, resp.json))
        self.assertTrue(resp.json['trace'].startswith(
            'Traceback (most recent call last):'))
        self.assertTrue(resp.json['message'])  # one exists and isn't empty
示例#11
0
 def test_request_invalid_path(self):
     "something invalid that initially looks valid fails when we try to use it"
     paths = ['elife-09561-v1.xml'
              ]  # msid and version can be extracted, but it's not a path
     expected = {
         'valid': [],
         'errors': [{
             'status': 'error',
             'requested-action': 'ingest'
         }],
         'invalid': []
     }
     actual = bfup.do_paths(paths)
     self.assertTrue(partial_match(expected, actual))
示例#12
0
    def test_upload_invalid(self):
        "the response we expect when the scraped article-json is invalid"
        xml_fname = 'elife-00666-v1.xml.invalid'
        xml_upload_fname = 'elife-00666-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        with patch('adaptor.call_lax',
                   side_effect=AssertionError(
                       "test shouldn't make it this far!")):
            resp = self.client.post(
                '/xml', **{
                    'buffered': True,
                    'content_type': 'multipart/form-data',
                    'data': {
                        'xml': (open(xml_fixture, 'rb'), xml_upload_fname),
                    }
                })
        self.assertEqual(resp.status_code, 400)  # bad request data

        # ensure xml uploaded
        expected_path = join(self.temp_dir, xml_upload_fname)
        self.assertTrue(os.path.exists(expected_path))

        # ensure ajson scraped
        expected_ajson = join(self.temp_dir, xml_upload_fname) + '.json'
        self.assertTrue(os.path.exists(expected_ajson))

        expected_resp = {
            'status': conf.INVALID,
            'code': conf.ERROR_INVALID,
            #'message': '...', # will probably change
            #'trace': '...', # stacktrace
        }
        self.assertTrue(utils.partial_match(expected_resp, resp.json))
        self.assertTrue(resp.json['message'])
        # title is missing
        self.assertTrue(
            resp.json['trace'].startswith("None is not of type 'string'"),
            "actual trace: %s" % resp.json['trace'])
示例#13
0
    def test_broken_glencoe_response(self):
        "the response we expect when the glencoe code fails"

        err_message = "informative error message"

        expected_resp = {
            'status': conf.ERROR,
            'code': conf.BAD_SCRAPE,
            'message': err_message,
            # 'trace': '...' # super long, can't predict, especially when mocking
        }
        with patch('glencoe.validate_gc_data', side_effect=AssertionError(err_message)):
            resp = self.client.post('/xml?id=666&version=1', **{
                'buffered': True,
                'content_type': 'multipart/form-data',
                'data': {
                    'xml': open(join(self.fixtures_dir, 'elife-00666-v1.xml'), 'rb'),
                },
            })

        self.assertEqual(resp.status_code, 400) # bad request
        self.assertTrue(utils.partial_match(expected_resp, resp.json))
        self.assertTrue(resp.json['trace'].startswith('Traceback (most'))
示例#14
0
    def test_bad_scrape(self):
        "the response we expect the xml fails to scrape"
        xml_fname = 'elife-16695-v1.xml'
        xml_fixture = join(self.fixtures_dir, xml_fname)

        with patch('main.main', side_effect=AssertionError('meow')):
            resp = self.client.post('/xml?id=16695&version=1', **{
                'buffered': True,
                'content_type': 'multipart/form-data',
                'data': {
                    'xml': open(xml_fixture, 'rb'),
                }
            })

        expected_lax_resp = {
            'status': conf.ERROR,
            'code': conf.BAD_SCRAPE,
            # 'message': '...', # we just care that a message exists
            # 'trace': '...', # same again, just that a trace exists
        }
        self.assertEqual(resp.status_code, 400)
        self.assertTrue(utils.partial_match(expected_lax_resp, resp.json))
        self.assertTrue(resp.json['trace'].startswith('Traceback (most recent call last):'))
        self.assertTrue(resp.json['message']) # one exists and isn't empty