11# -*- coding: utf-8 -*-
22# Copyright 2018 New Vector Ltd
3+ # Copyright 2019 Matrix.org Federation C.I.C
34#
45# Licensed under the Apache License, Version 2.0 (the "License");
56# you may not use this file except in compliance with the License.
1617
1718from synapse .events import FrozenEvent
1819from synapse .federation .federation_server import server_matches_acl_event
20+ from synapse .rest import admin
21+ from synapse .rest .client .v1 import login , room
1922
2023from tests import unittest
2124
@@ -41,6 +44,66 @@ def test_block_ip_literals(self):
4144 self .assertTrue (server_matches_acl_event ("1:2:3:4" , e ))
4245
4346
47+ class StateQueryTests (unittest .FederatingHomeserverTestCase ):
48+
49+ servlets = [
50+ admin .register_servlets ,
51+ room .register_servlets ,
52+ login .register_servlets ,
53+ ]
54+
55+ def test_without_event_id (self ):
56+ """
57+ Querying v1/state/<room_id> without an event ID will return the current
58+ known state.
59+ """
60+ u1 = self .register_user ("u1" , "pass" )
61+ u1_token = self .login ("u1" , "pass" )
62+
63+ room_1 = self .helper .create_room_as (u1 , tok = u1_token )
64+ self .inject_room_member (room_1 , "@user:other.example.com" , "join" )
65+
66+ request , channel = self .make_request (
67+ "GET" , "/_matrix/federation/v1/state/%s" % (room_1 ,)
68+ )
69+ self .render (request )
70+ self .assertEquals (200 , channel .code , channel .result )
71+
72+ self .assertEqual (
73+ channel .json_body ["room_version" ],
74+ self .hs .config .default_room_version .identifier ,
75+ )
76+
77+ members = set (
78+ map (
79+ lambda x : x ["state_key" ],
80+ filter (
81+ lambda x : x ["type" ] == "m.room.member" , channel .json_body ["pdus" ]
82+ ),
83+ )
84+ )
85+
86+ self .assertEqual (members , set (["@user:other.example.com" , u1 ]))
87+ self .assertEqual (len (channel .json_body ["pdus" ]), 6 )
88+
89+ def test_needs_to_be_in_room (self ):
90+ """
91+ Querying v1/state/<room_id> requires the server
92+ be in the room to provide data.
93+ """
94+ u1 = self .register_user ("u1" , "pass" )
95+ u1_token = self .login ("u1" , "pass" )
96+
97+ room_1 = self .helper .create_room_as (u1 , tok = u1_token )
98+
99+ request , channel = self .make_request (
100+ "GET" , "/_matrix/federation/v1/state/%s" % (room_1 ,)
101+ )
102+ self .render (request )
103+ self .assertEquals (403 , channel .code , channel .result )
104+ self .assertEqual (channel .json_body ["errcode" ], "M_FORBIDDEN" )
105+
106+
44107def _create_acl_event (content ):
45108 return FrozenEvent (
46109 {
0 commit comments