create support user (#4141)

Allow for the creation of a support user.

A support user can access the server, join rooms, interact with other users, but does not appear in the user directory nor does it contribute to monthly active user limits.
This commit is contained in:
Neil Johnson 2018-12-14 18:20:59 +00:00 committed by GitHub
parent e93a0ebf50
commit d2f7c4e6b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 371 additions and 47 deletions

View file

@ -50,6 +50,8 @@ class AuthTestCase(unittest.TestCase):
# this is overridden for the appservice tests
self.store.get_app_service_by_token = Mock(return_value=None)
self.store.is_support_user = Mock(return_value=defer.succeed(False))
@defer.inlineCallbacks
def test_get_user_by_req_user_valid_token(self):
user_info = {"name": self.test_user, "token_id": "ditto", "device_id": "device"}

View file

@ -17,7 +17,8 @@ from mock import Mock
from twisted.internet import defer
from synapse.api.errors import ResourceLimitError
from synapse.api.constants import UserTypes
from synapse.api.errors import ResourceLimitError, SynapseError
from synapse.handlers.register import RegistrationHandler
from synapse.types import RoomAlias, UserID, create_requester
@ -64,6 +65,7 @@ class RegistrationTestCase(unittest.TestCase):
requester, frank.localpart, "Frankie"
)
self.assertEquals(result_user_id, user_id)
self.assertTrue(result_token is not None)
self.assertEquals(result_token, 'secret')
@defer.inlineCallbacks
@ -82,7 +84,7 @@ class RegistrationTestCase(unittest.TestCase):
requester, local_part, None
)
self.assertEquals(result_user_id, user_id)
self.assertEquals(result_token, 'secret')
self.assertTrue(result_token is not None)
@defer.inlineCallbacks
def test_mau_limits_when_disabled(self):
@ -169,6 +171,20 @@ class RegistrationTestCase(unittest.TestCase):
rooms = yield self.store.get_rooms_for_user(res[0])
self.assertEqual(len(rooms), 0)
@defer.inlineCallbacks
def test_auto_create_auto_join_rooms_when_support_user_exists(self):
room_alias_str = "#room:test"
self.hs.config.auto_join_rooms = [room_alias_str]
self.store.is_support_user = Mock(return_value=True)
res = yield self.handler.register(localpart='support')
rooms = yield self.store.get_rooms_for_user(res[0])
self.assertEqual(len(rooms), 0)
directory_handler = self.hs.get_handlers().directory_handler
room_alias = RoomAlias.from_string(room_alias_str)
with self.assertRaises(SynapseError):
yield directory_handler.get_association(room_alias)
@defer.inlineCallbacks
def test_auto_create_auto_join_where_no_consent(self):
self.hs.config.user_consent_at_registration = True
@ -179,3 +195,13 @@ class RegistrationTestCase(unittest.TestCase):
yield self.handler.post_consent_actions(res[0])
rooms = yield self.store.get_rooms_for_user(res[0])
self.assertEqual(len(rooms), 0)
@defer.inlineCallbacks
def test_register_support_user(self):
res = yield self.handler.register(localpart='user', user_type=UserTypes.SUPPORT)
self.assertTrue(self.store.is_support_user(res[0]))
@defer.inlineCallbacks
def test_register_not_support_user(self):
res = yield self.handler.register(localpart='user')
self.assertFalse(self.store.is_support_user(res[0]))

View file

@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-
# Copyright 2018 New Vector
#
# 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.
from mock import Mock
from twisted.internet import defer
from synapse.api.constants import UserTypes
from synapse.handlers.user_directory import UserDirectoryHandler
from synapse.storage.roommember import ProfileInfo
from tests import unittest
from tests.utils import setup_test_homeserver
class UserDirectoryHandlers(object):
def __init__(self, hs):
self.user_directory_handler = UserDirectoryHandler(hs)
class UserDirectoryTestCase(unittest.TestCase):
""" Tests the UserDirectoryHandler. """
@defer.inlineCallbacks
def setUp(self):
hs = yield setup_test_homeserver(self.addCleanup)
self.store = hs.get_datastore()
hs.handlers = UserDirectoryHandlers(hs)
self.handler = hs.get_handlers().user_directory_handler
@defer.inlineCallbacks
def test_handle_local_profile_change_with_support_user(self):
support_user_id = "@support:test"
yield self.store.register(
user_id=support_user_id,
token="123",
password_hash=None,
user_type=UserTypes.SUPPORT
)
yield self.handler.handle_local_profile_change(support_user_id, None)
profile = yield self.store.get_user_in_directory(support_user_id)
self.assertTrue(profile is None)
display_name = 'display_name'
profile_info = ProfileInfo(
avatar_url='avatar_url',
display_name=display_name,
)
regular_user_id = '@regular:test'
yield self.handler.handle_local_profile_change(regular_user_id, profile_info)
profile = yield self.store.get_user_in_directory(regular_user_id)
self.assertTrue(profile['display_name'] == display_name)
@defer.inlineCallbacks
def test_handle_user_deactivated_support_user(self):
s_user_id = "@support:test"
self.store.register(
user_id=s_user_id,
token="123",
password_hash=None,
user_type=UserTypes.SUPPORT
)
self.store.remove_from_user_dir = Mock()
self.store.remove_from_user_in_public_room = Mock()
yield self.handler.handle_user_deactivated(s_user_id)
self.store.remove_from_user_dir.not_called()
self.store.remove_from_user_in_public_room.not_called()
@defer.inlineCallbacks
def test_handle_user_deactivated_regular_user(self):
r_user_id = "@regular:test"
self.store.register(user_id=r_user_id, token="123", password_hash=None)
self.store.remove_from_user_dir = Mock()
self.store.remove_from_user_in_public_room = Mock()
yield self.handler.handle_user_deactivated(r_user_id)
self.store.remove_from_user_dir.called_once_with(r_user_id)
self.store.remove_from_user_in_public_room.assert_called_once_with(r_user_id)

View file

@ -19,6 +19,7 @@ import json
from mock import Mock
from synapse.api.constants import UserTypes
from synapse.rest.client.v1.admin import register_servlets
from tests import unittest
@ -147,7 +148,9 @@ class UserRegisterTestCase(unittest.HomeserverTestCase):
nonce = channel.json_body["nonce"]
want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
want_mac.update(nonce.encode('ascii') + b"\x00bob\x00abc123\x00admin")
want_mac.update(
nonce.encode('ascii') + b"\x00bob\x00abc123\x00admin\x00support"
)
want_mac = want_mac.hexdigest()
body = json.dumps(
@ -156,6 +159,7 @@ class UserRegisterTestCase(unittest.HomeserverTestCase):
"username": "bob",
"password": "abc123",
"admin": True,
"user_type": UserTypes.SUPPORT,
"mac": want_mac,
}
)
@ -174,7 +178,9 @@ class UserRegisterTestCase(unittest.HomeserverTestCase):
nonce = channel.json_body["nonce"]
want_mac = hmac.new(key=b"shared", digestmod=hashlib.sha1)
want_mac.update(nonce.encode('ascii') + b"\x00bob\x00abc123\x00admin")
want_mac.update(
nonce.encode('ascii') + b"\x00bob\x00abc123\x00admin"
)
want_mac = want_mac.hexdigest()
body = json.dumps(
@ -202,8 +208,8 @@ class UserRegisterTestCase(unittest.HomeserverTestCase):
def test_missing_parts(self):
"""
Synapse will complain if you don't give nonce, username, password, and
mac. Admin is optional. Additional checks are done for length and
type.
mac. Admin and user_types are optional. Additional checks are done for length
and type.
"""
def nonce():
@ -260,7 +266,7 @@ class UserRegisterTestCase(unittest.HomeserverTestCase):
self.assertEqual('Invalid username', channel.json_body["error"])
#
# Username checks
# Password checks
#
# Must be present
@ -296,3 +302,20 @@ class UserRegisterTestCase(unittest.HomeserverTestCase):
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual('Invalid password', channel.json_body["error"])
#
# user_type check
#
# Invalid user_type
body = json.dumps({
"nonce": nonce(),
"username": "a",
"password": "1234",
"user_type": "invalid"}
)
request, channel = self.make_request("POST", self.url, body.encode('utf8'))
self.render(request)
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual('Invalid user type', channel.json_body["error"])

View file

@ -16,6 +16,8 @@ from mock import Mock
from twisted.internet import defer
from synapse.api.constants import UserTypes
from tests.unittest import HomeserverTestCase
FORTY_DAYS = 40 * 24 * 60 * 60
@ -28,6 +30,7 @@ class MonthlyActiveUsersTestCase(HomeserverTestCase):
self.store = hs.get_datastore()
hs.config.limit_usage_by_mau = True
hs.config.max_mau_value = 50
# Advance the clock a bit
reactor.advance(FORTY_DAYS)
@ -39,14 +42,23 @@ class MonthlyActiveUsersTestCase(HomeserverTestCase):
user1_email = "user1@matrix.org"
user2 = "@user2:server"
user2_email = "user2@matrix.org"
user3 = "@user3:server"
user3_email = "user3@matrix.org"
threepids = [
{'medium': 'email', 'address': user1_email},
{'medium': 'email', 'address': user2_email},
{'medium': 'email', 'address': user3_email},
]
user_num = len(threepids)
# -1 because user3 is a support user and does not count
user_num = len(threepids) - 1
self.store.register(user_id=user1, token="123", password_hash=None)
self.store.register(user_id=user2, token="456", password_hash=None)
self.store.register(
user_id=user3, token="789",
password_hash=None, user_type=UserTypes.SUPPORT
)
self.pump()
now = int(self.hs.get_clock().time_msec())
@ -60,7 +72,7 @@ class MonthlyActiveUsersTestCase(HomeserverTestCase):
active_count = self.store.get_monthly_active_count()
# Test total counts
# Test total counts, ensure user3 (support user) is not counted
self.assertEquals(self.get_success(active_count), user_num)
# Test user is marked as active
@ -221,6 +233,24 @@ class MonthlyActiveUsersTestCase(HomeserverTestCase):
count = self.store.get_registered_reserved_users_count()
self.assertEquals(self.get_success(count), len(threepids))
def test_support_user_not_add_to_mau_limits(self):
support_user_id = "@support:test"
count = self.store.get_monthly_active_count()
self.pump()
self.assertEqual(self.get_success(count), 0)
self.store.register(
user_id=support_user_id,
token="123",
password_hash=None,
user_type=UserTypes.SUPPORT
)
self.store.upsert_monthly_active_user(support_user_id)
count = self.store.get_monthly_active_count()
self.pump()
self.assertEqual(self.get_success(count), 0)
def test_track_monthly_users_without_cap(self):
self.hs.config.limit_usage_by_mau = False
self.hs.config.mau_stats_only = True

View file

@ -16,6 +16,8 @@
from twisted.internet import defer
from synapse.api.constants import UserTypes
from tests import unittest
from tests.utils import setup_test_homeserver
@ -99,6 +101,26 @@ class RegistrationStoreTestCase(unittest.TestCase):
user = yield self.store.get_user_by_access_token(self.tokens[0])
self.assertIsNone(user, "access token was not deleted without device_id")
@defer.inlineCallbacks
def test_is_support_user(self):
TEST_USER = "@test:test"
SUPPORT_USER = "@support:test"
res = yield self.store.is_support_user(None)
self.assertFalse(res)
yield self.store.register(user_id=TEST_USER, token="123", password_hash=None)
res = yield self.store.is_support_user(TEST_USER)
self.assertFalse(res)
yield self.store.register(
user_id=SUPPORT_USER,
token="456",
password_hash=None,
user_type=UserTypes.SUPPORT
)
res = yield self.store.is_support_user(SUPPORT_USER)
self.assertTrue(res)
class TokenGenerator:
def __init__(self):

View file

@ -373,6 +373,7 @@ class HomeserverTestCase(TestCase):
nonce_str += b"\x00admin"
else:
nonce_str += b"\x00notadmin"
want_mac.update(nonce.encode('ascii') + b"\x00" + nonce_str)
want_mac = want_mac.hexdigest()

View file

@ -140,7 +140,6 @@ def default_config(name):
config.rc_messages_per_second = 10000
config.rc_message_burst_count = 10000
config.saml2_enabled = False
config.use_frozen_dicts = False
# we need a sane default_room_version, otherwise attempts to create rooms will