2018-08-17 11:08:45 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-08-17 11:08:45 -04:00
|
|
|
#
|
|
|
|
#
|
2022-02-24 13:56:38 -05:00
|
|
|
from http import HTTPStatus
|
2023-08-24 19:38:46 -04:00
|
|
|
from unittest.mock import AsyncMock, Mock
|
2018-08-17 11:08:45 -04:00
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2019-12-05 10:53:10 -05:00
|
|
|
|
2021-04-23 07:21:55 -04:00
|
|
|
from synapse.handlers.presence import PresenceHandler
|
2021-08-17 07:57:58 -04:00
|
|
|
from synapse.rest.client import presence
|
2022-02-24 13:56:38 -05:00
|
|
|
from synapse.server import HomeServer
|
2018-08-17 11:08:45 -04:00
|
|
|
from synapse.types import UserID
|
2022-02-24 13:56:38 -05:00
|
|
|
from synapse.util import Clock
|
2018-08-17 11:08:45 -04:00
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceTestCase(unittest.HomeserverTestCase):
|
2021-06-17 10:20:06 -04:00
|
|
|
"""Tests presence REST API."""
|
2018-08-17 11:08:45 -04:00
|
|
|
|
|
|
|
user_id = "@sid:red"
|
|
|
|
|
|
|
|
user = UserID.from_string(user_id)
|
|
|
|
servlets = [presence.register_servlets]
|
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2023-02-14 14:03:35 -05:00
|
|
|
self.presence_handler = Mock(spec=PresenceHandler)
|
2023-08-24 19:38:46 -04:00
|
|
|
self.presence_handler.set_state = AsyncMock(return_value=None)
|
2020-11-30 13:28:44 -05:00
|
|
|
|
2018-08-17 11:08:45 -04:00
|
|
|
hs = self.setup_test_homeserver(
|
2020-11-30 13:28:44 -05:00
|
|
|
"red",
|
|
|
|
federation_client=Mock(),
|
2023-02-14 14:03:35 -05:00
|
|
|
presence_handler=self.presence_handler,
|
2018-08-17 11:08:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
return hs
|
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
def test_put_presence(self) -> None:
|
2018-08-17 11:08:45 -04:00
|
|
|
"""
|
|
|
|
PUT to the status endpoint with use_presence enabled will call
|
|
|
|
set_state on the presence handler.
|
|
|
|
"""
|
2023-10-26 15:11:24 -04:00
|
|
|
self.hs.config.server.presence_enabled = True
|
2018-08-17 11:08:45 -04:00
|
|
|
|
|
|
|
body = {"presence": "here", "status_msg": "beep boop"}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2018-08-17 11:08:45 -04:00
|
|
|
"PUT", "/presence/%s/status" % (self.user_id,), body
|
|
|
|
)
|
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2023-02-14 14:03:35 -05:00
|
|
|
self.assertEqual(self.presence_handler.set_state.call_count, 1)
|
2018-08-17 11:08:45 -04:00
|
|
|
|
2021-04-23 07:21:55 -04:00
|
|
|
@unittest.override_config({"use_presence": False})
|
2022-02-24 13:56:38 -05:00
|
|
|
def test_put_presence_disabled(self) -> None:
|
2018-08-17 11:08:45 -04:00
|
|
|
"""
|
2023-10-26 15:11:24 -04:00
|
|
|
PUT to the status endpoint with presence disabled will NOT call
|
|
|
|
set_state on the presence handler.
|
|
|
|
"""
|
|
|
|
|
|
|
|
body = {"presence": "here", "status_msg": "beep boop"}
|
|
|
|
channel = self.make_request(
|
|
|
|
"PUT", "/presence/%s/status" % (self.user_id,), body
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
|
|
|
self.assertEqual(self.presence_handler.set_state.call_count, 0)
|
|
|
|
|
|
|
|
@unittest.override_config({"presence": {"enabled": "untracked"}})
|
|
|
|
def test_put_presence_untracked(self) -> None:
|
|
|
|
"""
|
|
|
|
PUT to the status endpoint with presence untracked will NOT call
|
2018-08-17 11:08:45 -04:00
|
|
|
set_state on the presence handler.
|
|
|
|
"""
|
|
|
|
|
|
|
|
body = {"presence": "here", "status_msg": "beep boop"}
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2018-08-17 11:08:45 -04:00
|
|
|
"PUT", "/presence/%s/status" % (self.user_id,), body
|
|
|
|
)
|
|
|
|
|
2022-02-24 13:56:38 -05:00
|
|
|
self.assertEqual(channel.code, HTTPStatus.OK)
|
2023-02-14 14:03:35 -05:00
|
|
|
self.assertEqual(self.presence_handler.set_state.call_count, 0)
|