2018-08-17 18:08:45 +03: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.
|
2022-02-24 21:56:38 +03:00
|
|
|
from http import HTTPStatus
|
2023-08-25 02:38:46 +03:00
|
|
|
from unittest.mock import AsyncMock, Mock
|
2018-08-17 18:08:45 +03:00
|
|
|
|
2022-02-24 21:56:38 +03:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2019-12-05 18:53:10 +03:00
|
|
|
|
2021-04-23 14:21:55 +03:00
|
|
|
from synapse.handlers.presence import PresenceHandler
|
2021-08-17 14:57:58 +03:00
|
|
|
from synapse.rest.client import presence
|
2022-02-24 21:56:38 +03:00
|
|
|
from synapse.server import HomeServer
|
2018-08-17 18:08:45 +03:00
|
|
|
from synapse.types import UserID
|
2022-02-24 21:56:38 +03:00
|
|
|
from synapse.util import Clock
|
2018-08-17 18:08:45 +03:00
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceTestCase(unittest.HomeserverTestCase):
|
2021-06-17 17:20:06 +03:00
|
|
|
"""Tests presence REST API."""
|
2018-08-17 18:08:45 +03:00
|
|
|
|
|
|
|
user_id = "@sid:red"
|
|
|
|
|
|
|
|
user = UserID.from_string(user_id)
|
|
|
|
servlets = [presence.register_servlets]
|
|
|
|
|
2022-02-24 21:56:38 +03:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2023-02-14 22:03:35 +03:00
|
|
|
self.presence_handler = Mock(spec=PresenceHandler)
|
2023-08-25 02:38:46 +03:00
|
|
|
self.presence_handler.set_state = AsyncMock(return_value=None)
|
2020-11-30 21:28:44 +03:00
|
|
|
|
2018-08-17 18:08:45 +03:00
|
|
|
hs = self.setup_test_homeserver(
|
2020-11-30 21:28:44 +03:00
|
|
|
"red",
|
|
|
|
federation_client=Mock(),
|
2023-02-14 22:03:35 +03:00
|
|
|
presence_handler=self.presence_handler,
|
2018-08-17 18:08:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return hs
|
|
|
|
|
2022-02-24 21:56:38 +03:00
|
|
|
def test_put_presence(self) -> None:
|
2018-08-17 18:08:45 +03:00
|
|
|
"""
|
|
|
|
PUT to the status endpoint with use_presence enabled will call
|
|
|
|
set_state on the presence handler.
|
|
|
|
"""
|
2021-09-29 13:44:15 +03:00
|
|
|
self.hs.config.server.use_presence = True
|
2018-08-17 18:08:45 +03:00
|
|
|
|
|
|
|
body = {"presence": "here", "status_msg": "beep boop"}
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2018-08-17 18:08:45 +03:00
|
|
|
"PUT", "/presence/%s/status" % (self.user_id,), body
|
|
|
|
)
|
|
|
|
|
2022-02-24 21:56:38 +03:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2023-02-14 22:03:35 +03:00
|
|
|
self.assertEqual(self.presence_handler.set_state.call_count, 1)
|
2018-08-17 18:08:45 +03:00
|
|
|
|
2021-04-23 14:21:55 +03:00
|
|
|
@unittest.override_config({"use_presence": False})
|
2022-02-24 21:56:38 +03:00
|
|
|
def test_put_presence_disabled(self) -> None:
|
2018-08-17 18:08:45 +03:00
|
|
|
"""
|
2020-07-09 16:52:58 +03:00
|
|
|
PUT to the status endpoint with use_presence disabled will NOT call
|
2018-08-17 18:08:45 +03:00
|
|
|
set_state on the presence handler.
|
|
|
|
"""
|
|
|
|
|
|
|
|
body = {"presence": "here", "status_msg": "beep boop"}
|
2020-12-15 17:44:04 +03:00
|
|
|
channel = self.make_request(
|
2018-08-17 18:08:45 +03:00
|
|
|
"PUT", "/presence/%s/status" % (self.user_id,), body
|
|
|
|
)
|
|
|
|
|
2022-02-24 21:56:38 +03:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2023-02-14 22:03:35 +03:00
|
|
|
self.assertEqual(self.presence_handler.set_state.call_count, 0)
|