def mock_response(headers, body): content = body.encode() r = Response() r.raw = urllib3.response.HTTPResponse(body=content) r._content = content r.headers = CaseInsensitiveDict(headers) return CoroutineMock(return_value=r)
async def test_get_permissions_for_username_missing( api_client: Client, mocker: MockFixture, mock_get_token_for_install: None ) -> None: not_found = Response() not_found.status_code = 404 mocker.patch("kodiak.queries.http.Session.get", return_value=wrap_future(not_found)) async with api_client as api_client: res = await api_client.get_permissions_for_username("_invalid_username") assert res == Permission.NONE
async def test_get_permissions_for_username_read( api_client: Client, mocker: MockFixture, mock_get_token_for_install: None ) -> None: response = Response() response.status_code = 200 response._content = PERMISSION_OK_READ_USER_RESPONSE mocker.patch("kodiak.queries.http.Session.get", return_value=wrap_future(response)) async with api_client as api_client: res = await api_client.get_permissions_for_username("ghost") assert res == Permission.READ
def generate_page_of_prs(numbers: Iterable[int]) -> Response: """ Create a fake page for the list-pull-requests API. This is used by get_open_pull_requests. """ response = Response() response.status_code = 200 prs = [{"number": number, "base": {"ref": "main"}} for number in numbers] response._content = json.dumps(prs).encode() return response
def _load(cls: Type[T], res: Response) -> T: if cls == Any: return res.json() else: if typing_inspect.get_origin(cls) == list: cls, = typing_inspect.get_args(cls) many = True else: many = False schema = get_schema(cls, many=many) obj = schema.load(res.json()) return cast(T, obj)
async def test_pr_update_ok( mocker: MockFixture, event_response: queries.EventInfoResponse, pr: PR ) -> None: """ Update should return true on success """ mocker.patch.object(PR, "get_event", return_value=wrap_future(event_response)) res = Response() res.status_code = 200 mocker.patch( "kodiak.pull_request.queries.Client.merge_branch", return_value=wrap_future(res) ) res = await pr.update() assert res, "should be true when we have a successful call"
def exception_from_response(response: requests.Response) -> MosregException: n_json = None try: n_json = response.json() except ValueError: pass return MosregException(n_json, response.text, response.status_code)
async def test_pr_update_bad_merge( mocker: MockFixture, event_response: queries.EventInfoResponse, pr: PR ) -> None: """ Update should return false on an error """ mocker.patch.object(PR, "get_event", return_value=wrap_future(event_response)) res = Response() res.status_code = 409 res._content = b"{}" mocker.patch( "kodiak.pull_request.queries.Client.merge_branch", return_value=wrap_future(res) ) res = await pr.update() assert not res
def _raise_for_status(response: Response): try: response.raise_for_status() except HTTPError as e: # type: HTTPError raise HTTPException(response.status_code) # type: HTTPException