def test_post_landings_dryrun_no_access_token(api_url): landoapi = LandoAPIClient(api_url, auth0_access_token=None) with requests_mock.mock() as m: m.post(api_url + '/landings/dryrun', additional_matcher=match_landing_request('D1', 1), json=canned_landoapi.POST_LANDINGS_DRYRUN_SUCCESS) with pytest.raises(AssertionError): landoapi.post_landings_dryrun('D1', 1)
def test_post_landings_dryrun_connection_error(api_url): landoapi = LandoAPIClient(api_url, auth0_access_token='access_token') with requests_mock.mock() as m: m.post(api_url + '/landings/dryrun', additional_matcher=match_landing_request('D1', 1), exc=requests.exceptions.ConnectTimeout) with pytest.raises(UIError) as exc_info: landoapi.post_landings_dryrun('D1', 1) assert 'Failed to connect to Lando API.' in exc_info.value.title
def test_post_landings_dryrun_http_error_without_valid_response(api_url): landoapi = LandoAPIClient(api_url, auth0_access_token='access_token') with requests_mock.mock() as m: m.post(api_url + '/landings/dryrun', additional_matcher=match_landing_request('D1', 1), status_code=400) with pytest.raises(UIError) as exc_info: landoapi.post_landings_dryrun('D1', 1) assert 'Lando API did not respond successfully.' == exc_info.value.title assert 400 == exc_info.value.status_code
def test_post_landings_dryrun_http_error_with_valid_response(api_url): landoapi = LandoAPIClient(api_url, auth0_access_token='access_token') with requests_mock.mock() as m: m.post(api_url + '/landings/dryrun', additional_matcher=match_landing_request('D1', 1), json=canned_landoapi.PROBLEM_BAD_REQUEST, status_code=400) with pytest.raises(UIError) as exc_info: landoapi.post_landings_dryrun('D1', 1) assert 'Bad Request' in exc_info.value.title assert 'Bad Request Detail' in exc_info.value.message assert 400 == exc_info.value.status_code
def test_phabricator_api_token_in_request_header(api_url): landoapi = LandoAPIClient( api_url, auth0_access_token='access_token', phabricator_api_token='api_token' ) with requests_mock.mock() as m: m.get( api_url + '/revisions/D1', request_headers={'X-Phabricator-API-Key': 'api_token'}, status_code=404 ) with pytest.raises(RevisionNotFound): landoapi.get_revision('D1') m.get( api_url + '/landings', request_headers={'X-Phabricator-API-Key': 'api_token'}, status_code=404 ) with pytest.raises(RevisionNotFound): landoapi.get_landings('D1') m.post( api_url + '/landings', request_headers={ 'Authorization': 'Bearer access_token', 'X-Phabricator-API-Key': 'api_token' }, status_code=404 ) with pytest.raises(LandingSubmissionError) as exc_info: landoapi.post_landings('D1', '1', None) assert 'Lando API did not respond successfull' in exc_info.value.error m.post( api_url + '/landings/dryrun', request_headers={ 'Authorization': 'Bearer access_token', 'X-Phabricator-API-Key': 'api_token' }, status_code=404 ) with pytest.raises(UIError) as exc_info: landoapi.post_landings_dryrun('D1', '1') assert 404 == exc_info.value.status_code
def test_post_landings_dryrun_success(api_url): landoapi = LandoAPIClient(api_url, auth0_access_token='access_token') with requests_mock.mock() as m: m.post(api_url + '/landings/dryrun', additional_matcher=match_landing_request('D1', 1), json=canned_landoapi.POST_LANDINGS_DRYRUN_SUCCESS) result = landoapi.post_landings_dryrun('D1', 1) assert canned_landoapi.POST_LANDINGS_DRYRUN_SUCCESS == result
def revisions_handler(revision_id, diff_id=None): landoapi = LandoAPIClient( landoapi_url=current_app.config['LANDO_API_URL'], phabricator_api_token=get_phabricator_api_token(), auth0_access_token=session.get('access_token')) # Loads the submitted form if POST or creates a new one if GET form = RevisionForm() errors = [] # Submit the landing request if this is a POST if form.is_submitted(): if not is_user_authenticated(): errors.append('You must be logged in to land a revision.') elif form.validate(): try: # Returns True or raises a LandingSubmissionError if landoapi.post_landings(revision_id, form.diff_id.data, form.confirmation_token.data): redirect_url = ( '/revisions/{revision_id}/{diff_id}/'.format( revision_id=revision_id, diff_id=diff_id)) return redirect(redirect_url) except LandingSubmissionError as e: errors.append(e.error) else: for field, field_errors in form.errors.items(): for error in field_errors: errors.append(error) # If this is a GET or the POST fails, load data to display revision page. revision = landoapi.get_revision(revision_id, diff_id) diff_id = diff_id or revision['diff']['id'] landing_statuses = landoapi.get_landings(revision_id) dryrun_result = {} if is_user_authenticated(): dryrun_result = landoapi.post_landings_dryrun(revision_id, diff_id) form.confirmation_token.data = dryrun_result.get('confirmation_token') form.diff_id.data = diff_id return render_template('revision/revision.html', revision=revision, landing_statuses=landing_statuses, form=form, warnings=dryrun_result.get('warnings', []), blockers=dryrun_result.get('blockers', []), errors=errors)