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_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_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)
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])
def test_get_flat_dependency_tree(self): """Test Get Flat Dependency tree.""" with open( str(Path(__file__).parent.parent.parent) + '/data/manifests/golist2.json', 'rb') as fp: fs = FileStorage(stream=fp, filename='golist.json') sa_post_request = StackAnalysesPostRequest(manifest=fs, file_path='/tmp/bin', ecosystem='golang', show_transitive=True) sa = StackAnalyses(sa_post_request) sa._manifest_file_info = { 'filename': sa.params.manifest.filename, 'filepath': sa.params.file_path, 'content': sa.params.manifest.read().decode('utf-8') } save_in_db, packages = sa._get_flat_dependency_tree() assert isinstance(save_in_db, dict) assert isinstance(save_in_db['result'], list) assert isinstance(save_in_db['result'][0]['details'], list) assert isinstance(save_in_db['result'][0]['details'][0], dict) assert isinstance( save_in_db['result'][0]['details'][0]['_resolved'], list) assert isinstance( save_in_db['result'][0]['details'][0]['_resolved'], list) assert isinstance( save_in_db['result'][0]['details'][0]['_resolved'][0], dict) assert save_in_db['result'][0]['details'][0]['_resolved'][0]['package'], \ 'github.com/thoughtworks/talisman' assert save_in_db['result'][0]['details'][0]['_resolved'][0][ 'version'], '0.3.3' assert isinstance( save_in_db['result'][0]['details'][0]['_resolved'][0]['deps'], list) assert save_in_db['result'][0]['details'][0]['_resolved'][0]['deps'][0]['package'], \ 'github.com/hashicorp/vault/vault' assert isinstance(packages, list)