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

24.27
/synapse/rest/client/v1/pusher.py
1
# -*- coding: utf-8 -*-
2
# Copyright 2014-2016 OpenMarket 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.api.errors import Codes, StoreError, SynapseError
1×
21
from synapse.http.server import finish_request
1×
22
from synapse.http.servlet import (
1×
23
    RestServlet,
24
    assert_params_in_dict,
25
    parse_json_object_from_request,
26
    parse_string,
27
)
28
from synapse.push import PusherConfigException
1×
29
from synapse.rest.client.v2_alpha._base import client_patterns
1×
30

31
logger = logging.getLogger(__name__)
1×
32

33

34
class PushersRestServlet(RestServlet):
1×
35
    PATTERNS = client_patterns("/pushers$", v1=True)
1×
36

37
    def __init__(self, hs):
1×
UNCOV
38
        super(PushersRestServlet, self).__init__()
!
UNCOV
39
        self.hs = hs
!
UNCOV
40
        self.auth = hs.get_auth()
!
41

42
    @defer.inlineCallbacks
1×
43
    def on_GET(self, request):
UNCOV
44
        requester = yield self.auth.get_user_by_req(request)
!
UNCOV
45
        user = requester.user
!
46

UNCOV
47
        pushers = yield self.hs.get_datastore().get_pushers_by_user_id(user.to_string())
!
48

UNCOV
49
        allowed_keys = [
!
50
            "app_display_name",
51
            "app_id",
52
            "data",
53
            "device_display_name",
54
            "kind",
55
            "lang",
56
            "profile_tag",
57
            "pushkey",
58
        ]
59

UNCOV
60
        for p in pushers:
Branches [[0, 61], [0, 65]] missed. !
UNCOV
61
            for k, v in list(p.items()):
Branches [[0, 60], [0, 62]] missed. !
UNCOV
62
                if k not in allowed_keys:
Branches [[0, 61], [0, 63]] missed. !
UNCOV
63
                    del p[k]
!
64

UNCOV
65
        return 200, {"pushers": pushers}
!
66

67
    def on_OPTIONS(self, _):
1×
68
        return 200, {}
!
69

70

71
class PushersSetRestServlet(RestServlet):
1×
72
    PATTERNS = client_patterns("/pushers/set$", v1=True)
1×
73

74
    def __init__(self, hs):
1×
UNCOV
75
        super(PushersSetRestServlet, self).__init__()
!
UNCOV
76
        self.hs = hs
!
UNCOV
77
        self.auth = hs.get_auth()
!
UNCOV
78
        self.notifier = hs.get_notifier()
!
UNCOV
79
        self.pusher_pool = self.hs.get_pusherpool()
!
80

81
    @defer.inlineCallbacks
1×
82
    def on_POST(self, request):
UNCOV
83
        requester = yield self.auth.get_user_by_req(request)
!
UNCOV
84
        user = requester.user
!
85

UNCOV
86
        content = parse_json_object_from_request(request)
!
87

UNCOV
88
        if (
Branches [[0, 94], [0, 99]] missed. !
89
            "pushkey" in content
90
            and "app_id" in content
91
            and "kind" in content
92
            and content["kind"] is None
93
        ):
UNCOV
94
            yield self.pusher_pool.remove_pusher(
!
95
                content["app_id"], content["pushkey"], user_id=user.to_string()
96
            )
UNCOV
97
            return 200, {}
!
98

UNCOV
99
        assert_params_in_dict(
!
100
            content,
101
            [
102
                "kind",
103
                "app_id",
104
                "app_display_name",
105
                "device_display_name",
106
                "pushkey",
107
                "lang",
108
                "data",
109
            ],
110
        )
111

UNCOV
112
        logger.debug("set pushkey %s to kind %s", content["pushkey"], content["kind"])
!
UNCOV
113
        logger.debug("Got pushers request with body: %r", content)
!
114

UNCOV
115
        append = False
!
UNCOV
116
        if "append" in content:
Branches [[0, 117], [0, 119]] missed. !
117
            append = content["append"]
!
118

UNCOV
119
        if not append:
Branches [[0, 120], [0, 126]] missed. !
UNCOV
120
            yield self.pusher_pool.remove_pushers_by_app_id_and_pushkey_not_user(
!
121
                app_id=content["app_id"],
122
                pushkey=content["pushkey"],
123
                not_user_id=user.to_string(),
124
            )
125

UNCOV
126
        try:
!
UNCOV
127
            yield self.pusher_pool.add_pusher(
!
128
                user_id=user.to_string(),
129
                access_token=requester.access_token_id,
130
                kind=content["kind"],
131
                app_id=content["app_id"],
132
                app_display_name=content["app_display_name"],
133
                device_display_name=content["device_display_name"],
134
                pushkey=content["pushkey"],
135
                lang=content["lang"],
136
                data=content["data"],
137
                profile_tag=content.get("profile_tag", ""),
138
            )
139
        except PusherConfigException as pce:
!
140
            raise SynapseError(
!
141
                400, "Config Error: " + str(pce), errcode=Codes.MISSING_PARAM
142
            )
143

UNCOV
144
        self.notifier.on_new_replication_data()
!
145

UNCOV
146
        return 200, {}
!
147

148
    def on_OPTIONS(self, _):
1×
149
        return 200, {}
!
150

151

152
class PushersRemoveRestServlet(RestServlet):
1×
153
    """
154
    To allow pusher to be delete by clicking a link (ie. GET request)
155
    """
156

157
    PATTERNS = client_patterns("/pushers/remove$", v1=True)
1×
158
    SUCCESS_HTML = b"<html><body>You have been unsubscribed</body><html>"
1×
159

160
    def __init__(self, hs):
1×
UNCOV
161
        super(PushersRemoveRestServlet, self).__init__()
!
UNCOV
162
        self.hs = hs
!
UNCOV
163
        self.notifier = hs.get_notifier()
!
UNCOV
164
        self.auth = hs.get_auth()
!
UNCOV
165
        self.pusher_pool = self.hs.get_pusherpool()
!
166

167
    @defer.inlineCallbacks
1×
168
    def on_GET(self, request):
169
        requester = yield self.auth.get_user_by_req(request, rights="delete_pusher")
!
170
        user = requester.user
!
171

172
        app_id = parse_string(request, "app_id", required=True)
!
173
        pushkey = parse_string(request, "pushkey", required=True)
!
174

175
        try:
!
176
            yield self.pusher_pool.remove_pusher(
!
177
                app_id=app_id, pushkey=pushkey, user_id=user.to_string()
178
            )
179
        except StoreError as se:
!
180
            if se.code != 404:
Branches [[0, 182], [0, 184]] missed. !
181
                # This is fine: they're already unsubscribed
182
                raise
!
183

184
        self.notifier.on_new_replication_data()
!
185

186
        request.setResponseCode(200)
!
187
        request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
!
188
        request.setHeader(
!
189
            b"Content-Length", b"%d" % (len(PushersRemoveRestServlet.SUCCESS_HTML),)
190
        )
191
        request.write(PushersRemoveRestServlet.SUCCESS_HTML)
!
192
        finish_request(request)
!
193
        return None
!
194

195
    def on_OPTIONS(self, _):
1×
196
        return 200, {}
!
197

198

199
def register_servlets(hs, http_server):
1×
UNCOV
200
    PushersRestServlet(hs).register(http_server)
!
UNCOV
201
    PushersSetRestServlet(hs).register(http_server)
!
UNCOV
202
    PushersRemoveRestServlet(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