Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

matrix-org / synapse / 4532

23 Sep 2019 - 19:39 coverage decreased (-49.7%) to 17.596%
4532

Pull #6079

buildkite

Richard van der Hoff
update changelog
Pull Request #6079: Add submit_url response parameter to msisdn /requestToken

359 of 12986 branches covered (2.76%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 1 file covered. (0.0%)

18869 existing lines in 281 files now uncovered.

8809 of 39116 relevant lines covered (22.52%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.4
/synapse/replication/http/membership.py
1
# -*- coding: utf-8 -*-
2
# Copyright 2018 New Vector Ltd
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
import logging
1×
17

18
from twisted.internet import defer
1×
19

20
from synapse.http.servlet import parse_json_object_from_request
1×
21
from synapse.replication.http._base import ReplicationEndpoint
1×
22
from synapse.types import Requester, UserID
1×
23
from synapse.util.distributor import user_joined_room, user_left_room
1×
24

25
logger = logging.getLogger(__name__)
1×
26

27

28
class ReplicationRemoteJoinRestServlet(ReplicationEndpoint):
1×
29
    """Does a remote join for the given user to the given room
30

31
    Request format:
32

33
        POST /_synapse/replication/remote_join/:room_id/:user_id
34

35
        {
36
            "requester": ...,
37
            "remote_room_hosts": [...],
38
            "content": { ... }
39
        }
40
    """
41

42
    NAME = "remote_join"
1×
43
    PATH_ARGS = ("room_id", "user_id")
1×
44

45
    def __init__(self, hs):
1×
UNCOV
46
        super(ReplicationRemoteJoinRestServlet, self).__init__(hs)
!
47

UNCOV
48
        self.federation_handler = hs.get_handlers().federation_handler
!
UNCOV
49
        self.store = hs.get_datastore()
!
UNCOV
50
        self.clock = hs.get_clock()
!
51

52
    @staticmethod
1×
53
    def _serialize_payload(requester, room_id, user_id, remote_room_hosts, content):
54
        """
55
        Args:
56
            requester(Requester)
57
            room_id (str)
58
            user_id (str)
59
            remote_room_hosts (list[str]): Servers to try and join via
60
            content(dict): The event content to use for the join event
61
        """
62
        return {
!
63
            "requester": requester.serialize(),
64
            "remote_room_hosts": remote_room_hosts,
65
            "content": content,
66
        }
67

68
    @defer.inlineCallbacks
1×
69
    def _handle_request(self, request, room_id, user_id):
70
        content = parse_json_object_from_request(request)
!
71

72
        remote_room_hosts = content["remote_room_hosts"]
!
73
        event_content = content["content"]
!
74

75
        requester = Requester.deserialize(self.store, content["requester"])
!
76

77
        if requester.user:
Branches [[0, 78], [0, 80]] missed. !
78
            request.authenticated_entity = requester.user.to_string()
!
79

80
        logger.info("remote_join: %s into room: %s", user_id, room_id)
!
81

82
        yield self.federation_handler.do_invite_join(
!
83
            remote_room_hosts, room_id, user_id, event_content
84
        )
85

86
        return 200, {}
!
87

88

89
class ReplicationRemoteRejectInviteRestServlet(ReplicationEndpoint):
1×
90
    """Rejects the invite for the user and room.
91

92
    Request format:
93

94
        POST /_synapse/replication/remote_reject_invite/:room_id/:user_id
95

96
        {
97
            "requester": ...,
98
            "remote_room_hosts": [...],
99
        }
100
    """
101

102
    NAME = "remote_reject_invite"
1×
103
    PATH_ARGS = ("room_id", "user_id")
1×
104

105
    def __init__(self, hs):
1×
UNCOV
106
        super(ReplicationRemoteRejectInviteRestServlet, self).__init__(hs)
!
107

UNCOV
108
        self.federation_handler = hs.get_handlers().federation_handler
!
UNCOV
109
        self.store = hs.get_datastore()
!
UNCOV
110
        self.clock = hs.get_clock()
!
111

112
    @staticmethod
1×
113
    def _serialize_payload(requester, room_id, user_id, remote_room_hosts):
114
        """
115
        Args:
116
            requester(Requester)
117
            room_id (str)
118
            user_id (str)
119
            remote_room_hosts (list[str]): Servers to try and reject via
120
        """
121
        return {
!
122
            "requester": requester.serialize(),
123
            "remote_room_hosts": remote_room_hosts,
124
        }
125

126
    @defer.inlineCallbacks
1×
127
    def _handle_request(self, request, room_id, user_id):
128
        content = parse_json_object_from_request(request)
!
129

130
        remote_room_hosts = content["remote_room_hosts"]
!
131

132
        requester = Requester.deserialize(self.store, content["requester"])
!
133

134
        if requester.user:
Branches [[0, 135], [0, 137]] missed. !
135
            request.authenticated_entity = requester.user.to_string()
!
136

137
        logger.info("remote_reject_invite: %s out of room: %s", user_id, room_id)
!
138

139
        try:
!
140
            event = yield self.federation_handler.do_remotely_reject_invite(
!
141
                remote_room_hosts, room_id, user_id
142
            )
143
            ret = event.get_pdu_json()
!
144
        except Exception as e:
!
145
            # if we were unable to reject the exception, just mark
146
            # it as rejected on our end and plough ahead.
147
            #
148
            # The 'except' clause is very broad, but we need to
149
            # capture everything from DNS failures upwards
150
            #
151
            logger.warn("Failed to reject invite: %s", e)
!
152

153
            yield self.store.locally_reject_invite(user_id, room_id)
!
154
            ret = {}
!
155

156
        return 200, ret
!
157

158

159
class ReplicationUserJoinedLeftRoomRestServlet(ReplicationEndpoint):
1×
160
    """Notifies that a user has joined or left the room
161

162
    Request format:
163

164
        POST /_synapse/replication/membership_change/:room_id/:user_id/:change
165

166
        {}
167
    """
168

169
    NAME = "membership_change"
1×
170
    PATH_ARGS = ("room_id", "user_id", "change")
1×
171
    CACHE = False  # No point caching as should return instantly.
1×
172

173
    def __init__(self, hs):
1×
UNCOV
174
        super(ReplicationUserJoinedLeftRoomRestServlet, self).__init__(hs)
!
175

UNCOV
176
        self.registeration_handler = hs.get_registration_handler()
!
UNCOV
177
        self.store = hs.get_datastore()
!
UNCOV
178
        self.clock = hs.get_clock()
!
UNCOV
179
        self.distributor = hs.get_distributor()
!
180

181
    @staticmethod
1×
182
    def _serialize_payload(room_id, user_id, change):
183
        """
184
        Args:
185
            room_id (str)
186
            user_id (str)
187
            change (str): Either "joined" or "left"
188
        """
189
        assert change in ("joined", "left")
!
190

191
        return {}
!
192

193
    def _handle_request(self, request, room_id, user_id, change):
1×
194
        logger.info("user membership change: %s in %s", user_id, room_id)
!
195

196
        user = UserID.from_string(user_id)
!
197

198
        if change == "joined":
Branches [[0, 199], [0, 200]] missed. !
199
            user_joined_room(self.distributor, user, room_id)
!
200
        elif change == "left":
Branches [[0, 201], [0, 203]] missed. !
201
            user_left_room(self.distributor, user, room_id)
!
202
        else:
203
            raise Exception("Unrecognized change: %r", change)
!
204

205
        return 200, {}
!
206

207

208
def register_servlets(hs, http_server):
1×
UNCOV
209
    ReplicationRemoteJoinRestServlet(hs).register(http_server)
!
UNCOV
210
    ReplicationRemoteRejectInviteRestServlet(hs).register(http_server)
!
UNCOV
211
    ReplicationUserJoinedLeftRoomRestServlet(hs).register(http_server)
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC