2018-08-17 17:08:45 +02:00
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-04-09 19:44:38 +02:00
|
|
|
from unittest.mock import Mock
|
2018-08-17 17:08:45 +02:00
|
|
|
|
2019-12-05 16:53:10 +01:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-08-17 17:08:45 +02:00
|
|
|
from synapse.rest.client.v1 import presence
|
|
|
|
from synapse.types import UserID
|
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceTestCase(unittest.HomeserverTestCase):
|
|
|
|
""" Tests presence REST API. """
|
|
|
|
|
|
|
|
user_id = "@sid:red"
|
|
|
|
|
|
|
|
user = UserID.from_string(user_id)
|
|
|
|
servlets = [presence.register_servlets]
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
|
2020-11-30 19:28:44 +01:00
|
|
|
presence_handler = Mock()
|
|
|
|
presence_handler.set_state.return_value = defer.succeed(None)
|
|
|
|
|
2018-08-17 17:08:45 +02:00
|
|
|
hs = self.setup_test_homeserver(
|
2020-11-30 19:28:44 +01:00
|
|
|
"red",
|
2020-12-02 17:09:24 +01:00
|
|
|
federation_http_client=None,
|
2020-11-30 19:28:44 +01:00
|
|
|
federation_client=Mock(),
|
|
|
|
presence_handler=presence_handler,
|
2018-08-17 17:08:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return hs
|
|
|
|
|
|
|
|
def test_put_presence(self):
|
|
|
|
"""
|
|
|
|
PUT to the status endpoint with use_presence enabled will call
|
|
|
|
set_state on the presence handler.
|
|
|
|
"""
|
|
|
|
self.hs.config.use_presence = True
|
|
|
|
|
|
|
|
body = {"presence": "here", "status_msg": "beep boop"}
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2018-08-17 17:08:45 +02:00
|
|
|
"PUT", "/presence/%s/status" % (self.user_id,), body
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(channel.code, 200)
|
2020-11-30 19:28:44 +01:00
|
|
|
self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 1)
|
2018-08-17 17:08:45 +02:00
|
|
|
|
|
|
|
def test_put_presence_disabled(self):
|
|
|
|
"""
|
2020-07-09 15:52:58 +02:00
|
|
|
PUT to the status endpoint with use_presence disabled will NOT call
|
2018-08-17 17:08:45 +02:00
|
|
|
set_state on the presence handler.
|
|
|
|
"""
|
|
|
|
self.hs.config.use_presence = False
|
|
|
|
|
|
|
|
body = {"presence": "here", "status_msg": "beep boop"}
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2018-08-17 17:08:45 +02:00
|
|
|
"PUT", "/presence/%s/status" % (self.user_id,), body
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(channel.code, 200)
|
2020-11-30 19:28:44 +01:00
|
|
|
self.assertEqual(self.hs.get_presence_handler().set_state.call_count, 0)
|