예제 #1
0
async def test_ps_all_exits():
    expected = RsEmpty()
    rs = await PsAll(
        [
            PsFixed(IdentitySimple("urn:test:1")),
        ],
        identity=0,
    ).exit(expected, IdentitySimple("whatever"))
    assert rs is expected
예제 #2
0
 def __init__(self,
              rs: Response = RsEmpty(),
              code: int = 303,
              loc: str = "/"):
     self._origin: Response = RsWithHeaders(
         RsWithoutHeaders(RsWithStatus(code, response=rs), ("Location", )),
         {"Location": loc},
     )
     super(RsForward, self).__init__(code)
예제 #3
0
async def test_fk_methods_matches():
    expected_rs = RsEmpty()
    fk = FkMethods(
        "PUT",
        "GET",
        resp=expected_rs,
    )
    assert await fk.route(RqFake(method="GET", url="/foo/bar?baz=1")
                          ) is expected_rs
    assert await fk.route(RqFake(method="POST", url="/foo/bar?baz=1")) is None
예제 #4
0
async def test_fk_params_matches():
    expected_rs = RsEmpty()
    fk = FkParams(
        "foo",
        re.compile("[0-9]+"),
        resp=expected_rs,
    )
    assert await fk.route(RqFake(url="/bar?foo=1226")) is expected_rs
    assert await fk.route(RqFake(url="/bar?foo=baz")) is None
    assert await fk.route(RqFake(url="/bar")) is None
예제 #5
0
 async def enter(self, request: Request) -> Optional[Identity]:
     headers = await request.headers()
     if "authorization" not in headers:
         raise RsForward(
             RsWithHeaders(
                 RsEmpty(),
                 {"WWW-Authenticate": f'Basic realm="{self._realm}"'}),
             401,
             href(await request.uri()),
         )
     auth: str = base64.b64decode(
         headers["authorization"].split("Basic")[1].strip()).decode()
     user, password = auth.split(":", maxsplit=1)
     identity: Optional[Identity] = await self._entry.enter(user, password)
     if identity is None:
         raise RsForward(
             RsWithHeaders(
                 RsFlash("access denied", RsFlash.Level.WARNING),
                 {"WWW-Authenticate": f'Basic realm="{self._realm}"'},
             ),
             401,
             href(await request.uri()),
         )
     return identity
예제 #6
0
async def test_mg_secure_passes_authenticated_user():
    expected = RsEmpty()
    rs = await MgSecure(MgFixed(expected)).act(
        RqWithHeaders(RqFake(headers={}), {"MgAuth": "urn%3Atest%3A1"}))
    assert rs is expected
예제 #7
0
async def test_mg_secure_fails_on_anonymous():
    with pytest.raises(HttpException) as e:
        await MgSecure(MgFixed(RsEmpty())).act(RqFake())
    assert e.value.code() == 303
예제 #8
0
async def test_mg_measured_measured(delay):
    headers = await (await MgMeasured(MgDelayed(delay, MgFixed(RsEmpty()))
                                      ).act(RqFake(url="/bar"))).headers()
    seconds = float(headers.get("X-Muggle-Millis"))
    assert delay < seconds < delay + 0.01
예제 #9
0
 def __init__(self, status: int, reason: str = None, response: Response = None):
     if status < 100 or status > 999:
         raise TypeError("Bad status code: %r" % status)
     self._status: int = status
     self._reason: str = reason or self._codes[status]
     super(RsWithStatus, self).__init__(RsEmpty() if response is None else response)
예제 #10
0
async def test_rs_empty():
    rs = RsEmpty()
    assert await rs.headers() == {}
    assert await rs.status() == "204 No Content"
    async for _ in rs.body():
        assert False
예제 #11
0
async def test_mg_flash_does_nothing_if_no_flash_cookie():
    rs = await MgFlash(MgFixed(RsEmpty())).act(RqFake())
    assert "Set-Cookie" not in await rs.headers()
예제 #12
0
async def test_mg_flash_removes_flash_cookie():
    rs = await MgFlash(MgFixed(RsEmpty())).act(
        RqWithHeaders(RqFake(), headers={"Cookie": "RsFlash=Hello!"}))
    assert (await rs.headers()).getall("Set-Cookie") == [
        "RsFlash=deleted;Path=/;Expires=Thu, 01 Jan 1970 00:00:00 GMT;"
    ]
예제 #13
0
async def test_fk_authenticated_match():
    expected = RsEmpty()
    rs = await FkAuthenticated(MgFixed(expected)).route(
        RqWithHeaders(RqFake(headers={}), {"MgAuth": "urn%3Atest%3A1"})
    )
    assert rs is expected
예제 #14
0
async def test_fk_authenticated_no_match():
    rs = await FkAuthenticated(MgFixed(RsEmpty())).route(RqFake())
    assert rs is None