36
36
import attr
37
37
from typing_extensions import Literal
38
38
39
+ from twisted .test .proto_helpers import MemoryReactorClock
39
40
from twisted .web .resource import Resource
40
41
from twisted .web .server import Site
41
42
@@ -67,6 +68,7 @@ class RestHelper:
67
68
"""
68
69
69
70
hs : HomeServer
71
+ reactor : MemoryReactorClock
70
72
site : Site
71
73
auth_user_id : Optional [str ]
72
74
@@ -142,7 +144,7 @@ def create_room_as(
142
144
path = path + "?access_token=%s" % tok
143
145
144
146
channel = make_request (
145
- self .hs . get_reactor () ,
147
+ self .reactor ,
146
148
self .site ,
147
149
"POST" ,
148
150
path ,
@@ -216,7 +218,7 @@ def knock(
216
218
data ["reason" ] = reason
217
219
218
220
channel = make_request (
219
- self .hs . get_reactor () ,
221
+ self .reactor ,
220
222
self .site ,
221
223
"POST" ,
222
224
path ,
@@ -313,7 +315,7 @@ def change_membership(
313
315
data .update (extra_data or {})
314
316
315
317
channel = make_request (
316
- self .hs . get_reactor () ,
318
+ self .reactor ,
317
319
self .site ,
318
320
"PUT" ,
319
321
path ,
@@ -394,7 +396,7 @@ def send_event(
394
396
path = path + "?access_token=%s" % tok
395
397
396
398
channel = make_request (
397
- self .hs . get_reactor () ,
399
+ self .reactor ,
398
400
self .site ,
399
401
"PUT" ,
400
402
path ,
@@ -433,7 +435,7 @@ def get_event(
433
435
path = path + f"?access_token={ tok } "
434
436
435
437
channel = make_request (
436
- self .hs . get_reactor () ,
438
+ self .reactor ,
437
439
self .site ,
438
440
"GET" ,
439
441
path ,
@@ -488,7 +490,7 @@ def _read_write_state(
488
490
if body is not None :
489
491
content = json .dumps (body ).encode ("utf8" )
490
492
491
- channel = make_request (self .hs . get_reactor () , self .site , method , path , content )
493
+ channel = make_request (self .reactor , self .site , method , path , content )
492
494
493
495
assert channel .code == expect_code , "Expected: %d, got: %d, resp: %r" % (
494
496
expect_code ,
@@ -573,8 +575,8 @@ def upload_media(
573
575
image_length = len (image_data )
574
576
path = "/_matrix/media/r0/upload?filename=%s" % (filename ,)
575
577
channel = make_request (
576
- self .hs . get_reactor () ,
577
- FakeSite (resource , self .hs . get_reactor () ),
578
+ self .reactor ,
579
+ FakeSite (resource , self .reactor ),
578
580
"POST" ,
579
581
path ,
580
582
content = image_data ,
@@ -603,7 +605,7 @@ def whoami(
603
605
expect_code: The return code to expect from attempting the whoami request
604
606
"""
605
607
channel = make_request (
606
- self .hs . get_reactor () ,
608
+ self .reactor ,
607
609
self .site ,
608
610
"GET" ,
609
611
"account/whoami" ,
@@ -642,7 +644,7 @@ def login_via_oidc(
642
644
) -> Tuple [JsonDict , FakeAuthorizationGrant ]:
643
645
"""Log in (as a new user) via OIDC
644
646
645
- Returns the result of the final token login.
647
+ Returns the result of the final token login and the fake authorization grant .
646
648
647
649
Requires that "oidc_config" in the homeserver config be set appropriately
648
650
(TEST_OIDC_CONFIG is a suitable example) - and by implication, needs a
@@ -672,10 +674,28 @@ def login_via_oidc(
672
674
assert m , channel .text_body
673
675
login_token = m .group (1 )
674
676
675
- # finally, submit the matrix login token to the login API, which gives us our
676
- # matrix access token and device id.
677
+ return self .login_via_token (login_token , expected_status ), grant
678
+
679
+ def login_via_token (
680
+ self ,
681
+ login_token : str ,
682
+ expected_status : int = 200 ,
683
+ ) -> JsonDict :
684
+ """Submit the matrix login token to the login API, which gives us our
685
+ matrix access token and device id.Log in (as a new user) via OIDC
686
+
687
+ Returns the result of the token login.
688
+
689
+ Requires that "oidc_config" in the homeserver config be set appropriately
690
+ (TEST_OIDC_CONFIG is a suitable example) - and by implication, needs a
691
+ "public_base_url".
692
+
693
+ Also requires the login servlet and the OIDC callback resource to be mounted at
694
+ the normal places.
695
+ """
696
+
677
697
channel = make_request (
678
- self .hs . get_reactor () ,
698
+ self .reactor ,
679
699
self .site ,
680
700
"POST" ,
681
701
"/login" ,
@@ -684,7 +704,7 @@ def login_via_oidc(
684
704
assert (
685
705
channel .code == expected_status
686
706
), f"unexpected status in response: { channel .code } "
687
- return channel .json_body , grant
707
+ return channel .json_body
688
708
689
709
def auth_via_oidc (
690
710
self ,
@@ -805,7 +825,7 @@ def complete_oidc_auth(
805
825
with fake_serer .patch_homeserver (hs = self .hs ):
806
826
# now hit the callback URI with the right params and a made-up code
807
827
channel = make_request (
808
- self .hs . get_reactor () ,
828
+ self .reactor ,
809
829
self .site ,
810
830
"GET" ,
811
831
callback_uri ,
@@ -849,7 +869,7 @@ def initiate_sso_login(
849
869
# is the easiest way of figuring out what the Host header ought to be set to
850
870
# to keep Synapse happy.
851
871
channel = make_request (
852
- self .hs . get_reactor () ,
872
+ self .reactor ,
853
873
self .site ,
854
874
"GET" ,
855
875
uri ,
@@ -867,7 +887,7 @@ def get_location(channel: FakeChannel) -> str:
867
887
location = get_location (channel )
868
888
parts = urllib .parse .urlsplit (location )
869
889
channel = make_request (
870
- self .hs . get_reactor () ,
890
+ self .reactor ,
871
891
self .site ,
872
892
"GET" ,
873
893
urllib .parse .urlunsplit (("" , "" ) + parts [2 :]),
@@ -900,9 +920,7 @@ def initiate_sso_ui_auth(
900
920
+ urllib .parse .urlencode ({"session" : ui_auth_session_id })
901
921
)
902
922
# hit the redirect url (which will issue a cookie and state)
903
- channel = make_request (
904
- self .hs .get_reactor (), self .site , "GET" , sso_redirect_endpoint
905
- )
923
+ channel = make_request (self .reactor , self .site , "GET" , sso_redirect_endpoint )
906
924
# that should serve a confirmation page
907
925
assert channel .code == HTTPStatus .OK , channel .text_body
908
926
channel .extract_cookies (cookies )
0 commit comments