Beispiel #1
0
def test_get_revision_connection_failure(api_url):
    landoapi = LandoAPIClient(api_url)
    with requests_mock.mock() as m:
        m.get(api_url + '/revisions/D1',
              exc=requests.exceptions.ConnectTimeout)
        with pytest.raises(UIError) as exc_info:
            landoapi.get_revision('D1')

        assert 'Failed to reach Lando API' == exc_info.value.title
def test_get_revision_500_failure(api_url):
    landoapi = LandoAPIClient(api_url)
    with requests_mock.mock() as m:
        m.get(
            api_url + '/revisions/D1',
            status_code=500,
        )
        with pytest.raises(UIError) as exc_info:
            landoapi.get_revision('D1')

        assert 'Failed to reach Lando API' == exc_info.value.title
Beispiel #3
0
def test_get_revision_404(api_url):
    landoapi = LandoAPIClient(api_url)
    with requests_mock.mock() as m:
        m.get(api_url + '/revisions/D1',
              status_code=404,
              json=canned_landoapi.PROBLEM_REVISION_NOT_FOUND)

        with pytest.raises(RevisionNotFound) as exc_info:
            landoapi.get_revision('D1', 100)

        assert 'D1' in str(exc_info.value)
        assert 'Diff 100' in str(exc_info.value)
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
Beispiel #5
0
def test_get_revision_success(api_url):
    landoapi = LandoAPIClient(api_url)
    with requests_mock.mock() as m:
        m.get(api_url + '/revisions/D1',
              json=canned_landoapi.GET_REVISION_DEFAULT)
        revision = landoapi.get_revision('D1')
    assert revision == canned_landoapi.GET_REVISION_DEFAULT
Beispiel #6
0
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)