Factor out FakeResponse from test_oidc

pull/8858/head
Richard van der Hoff 2020-12-02 18:22:01 +00:00
parent 7ea85302f3
commit 76469898ee
2 changed files with 28 additions and 16 deletions

View File

@ -17,30 +17,15 @@ from urllib.parse import parse_qs, urlparse
from mock import Mock, patch from mock import Mock, patch
import attr
import pymacaroons import pymacaroons
from twisted.python.failure import Failure
from twisted.web._newclient import ResponseDone
from synapse.handlers.oidc_handler import OidcError, OidcMappingProvider from synapse.handlers.oidc_handler import OidcError, OidcMappingProvider
from synapse.handlers.sso import MappingException from synapse.handlers.sso import MappingException
from synapse.types import UserID from synapse.types import UserID
from tests.test_utils import FakeResponse
from tests.unittest import HomeserverTestCase, override_config from tests.unittest import HomeserverTestCase, override_config
@attr.s
class FakeResponse:
code = attr.ib()
body = attr.ib()
phrase = attr.ib()
def deliverBody(self, protocol):
protocol.dataReceived(self.body)
protocol.connectionLost(Failure(ResponseDone()))
# These are a few constants that are used as config parameters in the tests. # These are a few constants that are used as config parameters in the tests.
ISSUER = "https://issuer/" ISSUER = "https://issuer/"
CLIENT_ID = "test-client-id" CLIENT_ID = "test-client-id"

View File

@ -22,6 +22,11 @@ import warnings
from asyncio import Future from asyncio import Future
from typing import Any, Awaitable, Callable, TypeVar from typing import Any, Awaitable, Callable, TypeVar
import attr
from twisted.python.failure import Failure
from twisted.web.client import ResponseDone
TV = TypeVar("TV") TV = TypeVar("TV")
@ -80,3 +85,25 @@ def setup_awaitable_errors() -> Callable[[], None]:
sys.unraisablehook = unraisablehook # type: ignore sys.unraisablehook = unraisablehook # type: ignore
return cleanup return cleanup
@attr.s
class FakeResponse:
"""A fake twisted.web.IResponse object
there is a similar class at treq.test.test_response, but it lacks a `phrase`
attribute, and didn't support deliverBody until recently.
"""
# HTTP response code
code = attr.ib(type=int)
# HTTP response phrase (eg b'OK' for a 200)
phrase = attr.ib(type=bytes)
# body of the response
body = attr.ib(type=bytes)
def deliverBody(self, protocol):
protocol.dataReceived(self.body)
protocol.connectionLost(Failure(ResponseDone()))