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.29
/synapse/rest/client/v1/presence.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
""" This module contains REST servlets to do with presence: /presence/<paths>
1×
17
"""
18
import logging
1×
19

20
from six import string_types
1×
21

22
from twisted.internet import defer
1×
23

24
from synapse.api.errors import AuthError, SynapseError
1×
25
from synapse.handlers.presence import format_user_presence_state
1×
26
from synapse.http.servlet import RestServlet, parse_json_object_from_request
1×
27
from synapse.rest.client.v2_alpha._base import client_patterns
1×
28
from synapse.types import UserID
1×
29

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

32

33
class PresenceStatusRestServlet(RestServlet):
1×
34
    PATTERNS = client_patterns("/presence/(?P<user_id>[^/]*)/status", v1=True)
1×
35

36
    def __init__(self, hs):
1×
UNCOV
37
        super(PresenceStatusRestServlet, self).__init__()
!
UNCOV
38
        self.hs = hs
!
UNCOV
39
        self.presence_handler = hs.get_presence_handler()
!
UNCOV
40
        self.clock = hs.get_clock()
!
UNCOV
41
        self.auth = hs.get_auth()
!
42

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

UNCOV
48
        if requester.user != user:
Branches [[0, 49], [0, 56]] missed. !
49
            allowed = yield self.presence_handler.is_visible(
!
50
                observed_user=user, observer_user=requester.user
51
            )
52

53
            if not allowed:
Branches [[0, 54], [0, 56]] missed. !
54
                raise AuthError(403, "You are not allowed to see their presence.")
!
55

UNCOV
56
        state = yield self.presence_handler.get_state(target_user=user)
!
UNCOV
57
        state = format_user_presence_state(state, self.clock.time_msec())
!
58

UNCOV
59
        return 200, state
!
60

61
    @defer.inlineCallbacks
1×
62
    def on_PUT(self, request, user_id):
UNCOV
63
        requester = yield self.auth.get_user_by_req(request)
!
UNCOV
64
        user = UserID.from_string(user_id)
!
65

UNCOV
66
        if requester.user != user:
Branches [[0, 67], [0, 69]] missed. !
67
            raise AuthError(403, "Can only set your own presence state")
!
68

UNCOV
69
        state = {}
!
70

UNCOV
71
        content = parse_json_object_from_request(request)
!
72

UNCOV
73
        try:
!
UNCOV
74
            state["presence"] = content.pop("presence")
!
75

UNCOV
76
            if "status_msg" in content:
Branches [[0, 77], [0, 81]] missed. !
UNCOV
77
                state["status_msg"] = content.pop("status_msg")
!
UNCOV
78
                if not isinstance(state["status_msg"], string_types):
Branches [[0, 79], [0, 81]] missed. !
79
                    raise SynapseError(400, "status_msg must be a string.")
!
80

UNCOV
81
            if content:
Branches [[0, 82], [0, 88]] missed. !
82
                raise KeyError()
!
83
        except SynapseError as e:
Branches [[0, 84], [0, 85]] missed. !
84
            raise e
!
85
        except Exception:
!
86
            raise SynapseError(400, "Unable to parse state")
!
87

UNCOV
88
        if self.hs.config.use_presence:
Branches [[0, 89], [0, 91]] missed. !
UNCOV
89
            yield self.presence_handler.set_state(user, state)
!
90

UNCOV
91
        return 200, {}
!
92

93
    def on_OPTIONS(self, request):
1×
94
        return 200, {}
!
95

96

97
def register_servlets(hs, http_server):
1×
UNCOV
98
    PresenceStatusRestServlet(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