def test_github_device_flow(requests_mock: RequestsMocker, monkeypatch: MonkeyPatch) -> None:
    device_code = '111'
    user_code = '123-456'
    verification_uri = 'https://github.com'
    expires_in = 900
    interval = 1
    access_token = 'aaa-ccc'

    requests_mock.post('https://github.com/login/device/code', text=urlencode({
        'device_code': device_code,
        'user_code': user_code,
        'verification_uri': verification_uri,
        'expires_in': expires_in,
        'interval': interval
    }))

    requests_mock.post('https://github.com/login/oauth/access_token', text=urlencode({
        'access_token': access_token,
    }))

    def noop(s: float) -> None:
        pass

    monkeypatch.setattr(time, "sleep", noop)

    def write_token(access_token: AccessToken) -> None:
        assert access_token

    with github_device_flow(write_token) as verif_codes:
        assert verif_codes
Example #2
0
def test_promote_launches_promote_drone_job(drone: Drone,
                                            requests_mock: Mocker,
                                            caplog: LogCaptureFixture) -> None:
    """
    Given: A Drone adapter.
    When: Using the promote method.
    Then: Calls the promote API method with the desired build number.
    """
    requests_mock.get(
        f"{drone.drone_url}/api/repos/owner/repository/builds/172",
        json={
            "id": 882,
            "number": 172,
            "status": "success",
            "after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e",
            "target": "master",
            "event": "push",
            "message": "updated README",
        },
    )
    promote_url = (
        f"{drone.drone_url}/api/repos/owner/repository/builds/172/promote"
        "?target=production")
    requests_mock.post(
        promote_url,
        json={
            "id": 100207,
            "number": 174,
            "parent": 172,
            "status": "pending",
            "event": "promote",
            "message": "updated README",
            "before": "e3320539a4c03ccfda992641646deb67d8bf98f3",
            "after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e",
            "source": "master",
            "target": "master",
            "author_login": "******",
            "author_name": "The Octocat",
            "sender": "bradrydzewski",
            "started": 0,
            "finished": 0,
            "stages": [],
        },
    )

    result = drone.promote("owner/repository", 172, "production")

    assert result == 174
    assert requests_mock.request_history[-1].method == "POST"
    assert requests_mock.request_history[-1].url == promote_url
    assert (
        "drode.adapters.drone",
        logging.INFO,
        "Job #174 has started.",
    ) in caplog.record_tuples
Example #3
0
def mock_jira(requests_mock: Mocker):
    requests_mock.get(
        '/rest/api/latest/worklog/updated?since=1614556800000',
        json=json.loads(py_rel_path('./data/worklogs.json').read_text()),
    )
    requests_mock.post('/rest/api/latest/worklog/list', json=worklog_callback)
    requests_mock.get(
        re.compile('/rest/api/latest/issue/.*'),
        json=json.loads(py_rel_path('./data/jira_issue.json').read_text()),
    )
    pass
def test_notes_api_create_note_response_status_exceptions(
    status_code: int,
    expectation: ContextManager,
    notes_api: NotesApi,
    requests_mock: RequestsMocker,
):
    requests_mock.post(
        f'https://{notes_api.hostname}/index.php/apps/notes/api/v1/notes',
        status_code=status_code,
    )

    with expectation:
        notes_api.create_note(Note())
def test_notes_api_create_note(example_note: Note, notes_api: NotesApi,
                               requests_mock: RequestsMocker):
    # Server sets this
    example_note.id = None
    example_note.modified = None

    server_note = example_note
    server_note.id = 1337
    server_note.update_modified()
    requests_mock.post(
        f'https://{notes_api.hostname}/index.php/apps/notes/api/v1/notes',
        json=server_note.to_dict(),
    )

    assert notes_api.create_note(example_note) == server_note
Example #6
0
def test_get_generates_post_request(drone: Drone,
                                    requests_mock: Mocker) -> None:
    """
    Given: A Drone adapter
    When: Using the get method with the post argument
    Then: a requests object is returned with the query result.
    """
    requests_mock.post("http://url", text="hi")

    result = drone.get("http://url", "post")

    assert result.text == "hi"
    assert requests_mock.request_history[0].method == "POST"
    assert (requests_mock.request_history[0].headers["Authorization"] ==
            "Bearer drone_token")