def test_sa_backbone_error(self, _aggregate_request, _g):
     """Check if 500 is raise upon invalid response from backbone server."""
     with open(
             str(Path(__file__).parent.parent.parent) +
             '/data/manifests/202/npmlist.json', 'rb') as fp:
         fs = FileStorage(stream=fp, filename='npmlist.json')
         sa_post_request = StackAnalysesPostRequest(manifest=fs,
                                                    file_path='/tmp/bin',
                                                    ecosystem='npm',
                                                    show_transitive=True)
         sa = StackAnalyses(sa_post_request)
         with pytest.raises(Exception) as exception:
             sa.post_request()
         self.assertIs(exception.type, BackboneServerException)
 def test_sa_invalid_manifest_file_unknown_error(self, _mock_depfinder):
     """Check if 400 is raise upon invalid manifest file."""
     with open(
             str(Path(__file__).parent.parent.parent) +
             '/data/manifests/400/npmlist.json', 'rb') as fp:
         fs = FileStorage(stream=fp, filename='npmlist.json')
         sa_post_request = StackAnalysesPostRequest(manifest=fs,
                                                    file_path='/tmp/bin',
                                                    ecosystem='npm',
                                                    show_transitive=True)
         sa = StackAnalyses(sa_post_request)
         with pytest.raises(Exception) as exception:
             sa.post_request()
         self.assertIs(exception.type, SAInvalidInputException)
 def test_sa_rdb_error(self, _post_request, _g):
     """Check if 500 is raise upon request save failure."""
     with open(
             str(Path(__file__).parent.parent.parent) +
             '/data/manifests/202/npmlist.json', 'rb') as fp:
         fs = FileStorage(stream=fp, filename='npmlist.json')
         sa_post_request = StackAnalysesPostRequest(manifest=fs,
                                                    file_path='/tmp/bin',
                                                    ecosystem='npm',
                                                    show_transitive=True)
         sa = StackAnalyses(sa_post_request)
         with pytest.raises(Exception) as exception:
             sa.post_request()
         self.assertIs(exception.type, RDBSaveException)
 def test_sa_mismatch_manifest_file_and_ecosystem(self):
     """Check if 400 is raise upon mismatch between manifest file content and ecosystem type."""
     with open(
             str(Path(__file__).parent.parent.parent) +
             '/data/manifests/202/npmlist.json', 'rb') as fp:
         fs = FileStorage(stream=fp, filename='npmlist.json')
         with pytest.raises(Exception) as exception:
             sa_post_request = StackAnalysesPostRequest(
                 manifest=fs,
                 file_path='/tmp/bin',
                 ecosystem='pypi',
                 show_transitive=True)
             sa = StackAnalyses(sa_post_request)
             sa.post_request()
         self.assertIs(exception.type, ValidationError)
 def test_sa_success(self, _post_request, _g):
     """Success stack analyses flow."""
     with open(
             str(Path(__file__).parent.parent.parent) +
             '/data/manifests/202/npmlist.json', 'rb') as fp:
         fs = FileStorage(stream=fp, filename='npmlist.json')
         sa_post_request = StackAnalysesPostRequest(manifest=fs,
                                                    file_path='/tmp/bin',
                                                    ecosystem='npm',
                                                    show_transitive=True)
         sa = StackAnalyses(sa_post_request)
         response = sa.post_request()
         self.assertIsInstance(response, dict)
         self.assertIn('status', response)
         self.assertEqual(response['status'], 'success')
         self.assertIn('id', response)
Esempio n. 6
0
def stack_analyses():
    """Handle request to trigger a new stack analyses report.

    GET method would raise error to provide missing request id to the user.
    """
    logger.debug('[%s] /stack-analyses accessed', request.method)
    start = time.time()
    if request.method == 'GET':
        raise HTTPError(400, error="Request id missing")

    sa_post_request = None
    try:
        # 1. Validate and build request object.
        sa_post_request = StackAnalysesPostRequest(**request.form,
                                                   **request.files)

    except ValidationError as e:
        # 2. Check of invalid params and raise exception.
        error_message = 'Validation error(s) in the request.'
        for error in e.errors():
            error_message += ' {}.'.format(error['msg'])
        logger.exception(error_message)
        raise HTTPError(400, error=error_message) from e

    # 3. Initiate stack analyses object
    sa = StackAnalyses(sa_post_request)

    # 4. Post request
    try:
        data = sa.post_request()
        logger.info('%s took %f seconds for [POST] stack-analyses', data['id'],
                    time.time() - start)
        return jsonify(data)
    except SAInvalidInputException as e:
        raise HTTPError(400, e.args[0]) from e
    except BackboneServerException as e:
        raise HTTPError(500, e.args[0])
    except RDBSaveException as e:
        raise HTTPError(500, e.args[0])