|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
from mock import AsyncMock |
|
|
|
|
|
|
|
|
|
from synapse.api.errors import Codes, SynapseError |
|
|
|
|
from synapse.api.errors import SynapseError, NextcloudError |
|
|
|
|
from synapse.rest.client.v1 import login, room |
|
|
|
|
from synapse.rest import admin |
|
|
|
|
from tests.unittest import HomeserverTestCase |
|
|
|
@ -18,158 +18,172 @@ class NextcloudHandlerTestCase(HomeserverTestCase): |
|
|
|
|
def prepare(self, reactor, clock, hs): |
|
|
|
|
self.store = hs.get_datastore() |
|
|
|
|
self.nextcloud_handler = hs.get_nextcloud_handler() |
|
|
|
|
|
|
|
|
|
self.keycloak_client = self.nextcloud_handler.keycloak_client |
|
|
|
|
self.nextcloud_client = self.nextcloud_handler.nextcloud_client |
|
|
|
|
|
|
|
|
|
# Create a room with two users : |
|
|
|
|
self.creator = self.register_user("creator", "pass", admin=True) |
|
|
|
|
self.creator_tok = self.login("creator", "pass") |
|
|
|
|
|
|
|
|
|
self.inviter = self.register_user("inviter", "pass") |
|
|
|
|
inviter_tok = self.login("inviter", "pass") |
|
|
|
|
|
|
|
|
|
self.room_id = self.helper.create_room_as(self.creator, tok=self.creator_tok) |
|
|
|
|
self.helper.invite( |
|
|
|
|
self.room_id, src=self.creator, targ=self.inviter, tok=self.creator_tok |
|
|
|
|
) |
|
|
|
|
self.helper.join(self.room_id, self.inviter, tok=inviter_tok) |
|
|
|
|
self.group_name = NEXTCLOUD_GROUP_NAME_PREFIX + self.room_id |
|
|
|
|
|
|
|
|
|
# Mock Nextcloud client functions : |
|
|
|
|
self.nextcloud_client.add_group = AsyncMock() |
|
|
|
|
self.nextcloud_client.delete_group = AsyncMock() |
|
|
|
|
self.nextcloud_client.add_user_to_group = AsyncMock() |
|
|
|
|
self.nextcloud_client.remove_user_from_group = AsyncMock() |
|
|
|
|
self.nextcloud_client.unshare = AsyncMock() |
|
|
|
|
self.nextcloud_client.share = AsyncMock(return_value=1) |
|
|
|
|
self.nextcloud_client.share = AsyncMock(return_value="share_1") |
|
|
|
|
|
|
|
|
|
def test_set_a_new_bind(self): |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.bind(self.creator, self.room_id, "/directory") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
mapped_directory = self.get_success( |
|
|
|
|
self.store.get_path_from_room_id(self.room_id) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
share_id = self.get_success( |
|
|
|
|
self.store.get_nextcloud_share_id_from_room_id(self.room_id) |
|
|
|
|
self.nextcloud_handler.bind(self.creator, self.room_id, "/folder") |
|
|
|
|
) |
|
|
|
|
self.nextcloud_client.add_group.reset_mock() |
|
|
|
|
self.nextcloud_client.add_user_to_group.reset_mock() |
|
|
|
|
self.nextcloud_client.share.reset_mock() |
|
|
|
|
|
|
|
|
|
# Verify that mocked functions are called once |
|
|
|
|
self.nextcloud_client.add_group.assert_called_once() |
|
|
|
|
self.nextcloud_client.share.assert_called_once() |
|
|
|
|
|
|
|
|
|
# Verify that mocked functions are called twice |
|
|
|
|
self.assertEquals(self.nextcloud_client.add_user_to_group.call_count, 2) |
|
|
|
|
|
|
|
|
|
# Verify that mocked functions are not called |
|
|
|
|
self.nextcloud_client.unshare.assert_not_called() |
|
|
|
|
|
|
|
|
|
self.assertEqual(mapped_directory, "/directory") |
|
|
|
|
|
|
|
|
|
def test_update_an_existing_bind(self): |
|
|
|
|
self.get_success(self.store.bind(self.room_id, "/directory", 2)) |
|
|
|
|
def test_unbind(self): |
|
|
|
|
self.get_success(self.nextcloud_handler.unbind(self.room_id)) |
|
|
|
|
share_id = self.get_success(self.store.get_share_id(self.room_id)) |
|
|
|
|
|
|
|
|
|
old_mapped_directory = self.get_success( |
|
|
|
|
self.store.get_path_from_room_id(self.room_id) |
|
|
|
|
) |
|
|
|
|
self.nextcloud_client.delete_group.assert_called() |
|
|
|
|
self.assertIsNone(share_id) |
|
|
|
|
|
|
|
|
|
old_share_id = self.get_success( |
|
|
|
|
self.store.get_nextcloud_share_id_from_room_id(self.room_id) |
|
|
|
|
def test_unbind_with_unexisting_group(self): |
|
|
|
|
self.nextcloud_client.delete_group = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=101, msg="") |
|
|
|
|
) |
|
|
|
|
with self.assertLogs("synapse.handlers.watcha_nextcloud", level="WARN") as cm: |
|
|
|
|
self.get_success(self.nextcloud_handler.unbind(self.room_id)) |
|
|
|
|
|
|
|
|
|
self.assertEqual(old_mapped_directory, "/directory") |
|
|
|
|
self.assertEqual(old_share_id, 2) |
|
|
|
|
|
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.bind(self.creator, self.room_id, "/directory2") |
|
|
|
|
) |
|
|
|
|
share_id = self.get_success(self.store.get_share_id(self.room_id)) |
|
|
|
|
|
|
|
|
|
mapped_directory = self.get_success( |
|
|
|
|
self.store.get_path_from_room_id(self.room_id) |
|
|
|
|
self.assertIn( |
|
|
|
|
f"[watcha] delete nextcloud group {self.group_name} - failed:", |
|
|
|
|
cm.output[0], |
|
|
|
|
) |
|
|
|
|
self.nextcloud_client.delete_group.assert_called() |
|
|
|
|
self.assertIsNone(share_id) |
|
|
|
|
|
|
|
|
|
new_share_id = self.get_success( |
|
|
|
|
self.store.get_nextcloud_share_id_from_room_id(self.room_id) |
|
|
|
|
def test_update_bind(self): |
|
|
|
|
old_share_id = self.get_success(self.store.get_share_id(self.room_id)) |
|
|
|
|
self.nextcloud_client.share = AsyncMock(return_value="share_2") |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.bind(self.creator, self.room_id, "/new_folder") |
|
|
|
|
) |
|
|
|
|
share_id = self.get_success(self.store.get_share_id(self.room_id)) |
|
|
|
|
|
|
|
|
|
# Verify that mocked functions has called : |
|
|
|
|
self.assertEqual(old_share_id, "share_1") |
|
|
|
|
self.nextcloud_client.add_group.assert_called_once() |
|
|
|
|
self.nextcloud_client.share.assert_called_once() |
|
|
|
|
self.assertEquals(self.nextcloud_client.add_user_to_group.call_count, 2) |
|
|
|
|
self.nextcloud_client.unshare.assert_called() |
|
|
|
|
self.assertEquals(share_id, "share_2") |
|
|
|
|
|
|
|
|
|
self.assertEqual(mapped_directory, "/directory2") |
|
|
|
|
self.assertEqual(new_share_id, 1) |
|
|
|
|
def test_update_bind_with_existing_group(self): |
|
|
|
|
self.nextcloud_client.add_group = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=102, msg="") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def test_delete_an_existing_bind(self): |
|
|
|
|
self.get_success(self.store.bind(self.room_id, "/directory", 2)) |
|
|
|
|
self.get_success(self.nextcloud_handler.unbind(self.room_id)) |
|
|
|
|
with self.assertLogs("synapse.handlers.watcha_nextcloud", level="WARN") as cm: |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.bind(self.creator, self.room_id, "/new_folder") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
mapped_directory = self.get_success( |
|
|
|
|
self.store.get_path_from_room_id(self.room_id) |
|
|
|
|
self.assertIn( |
|
|
|
|
f"[watcha] add nextcloud group {self.group_name} - failed: the group already exists", |
|
|
|
|
cm.output[0], |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
share_id = self.get_success( |
|
|
|
|
self.store.get_nextcloud_share_id_from_room_id(self.room_id) |
|
|
|
|
def test_update_bind_with_invalid_input_data(self): |
|
|
|
|
self.nextcloud_client.add_group = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=101, msg="") |
|
|
|
|
) |
|
|
|
|
self.get_failure( |
|
|
|
|
self.nextcloud_handler.bind(self.creator, self.room_id, "/new_folder"), |
|
|
|
|
SynapseError, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.nextcloud_client.delete_group.assert_called() |
|
|
|
|
self.assertIsNone(mapped_directory) |
|
|
|
|
self.assertIsNone(share_id) |
|
|
|
|
|
|
|
|
|
def test_add_user_to_nextcloud_group_without_nextcloud_account(self): |
|
|
|
|
def test_add_user_to_group_without_account(self): |
|
|
|
|
self.nextcloud_client.add_user_to_group = AsyncMock( |
|
|
|
|
side_effect=SynapseError(code=400, msg="") |
|
|
|
|
side_effect=NextcloudError(code=103, msg="") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
with self.assertLogs("synapse.handlers.watcha_nextcloud", level="WARN") as cm: |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.add_room_users_to_nextcloud_group(self.room_id) |
|
|
|
|
self.nextcloud_handler.add_room_members_to_group(self.room_id) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.assertIn( |
|
|
|
|
"Unable to add the user {} to the Nextcloud group {}".format( |
|
|
|
|
self.creator, |
|
|
|
|
NEXTCLOUD_GROUP_NAME_PREFIX + self.room_id, |
|
|
|
|
), |
|
|
|
|
f"[watcha] add user {self.creator} to group {self.group_name} - failed", |
|
|
|
|
cm.output[0], |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.assertIn( |
|
|
|
|
"Unable to add the user {} to the Nextcloud group {}".format( |
|
|
|
|
self.inviter, |
|
|
|
|
NEXTCLOUD_GROUP_NAME_PREFIX + self.room_id, |
|
|
|
|
), |
|
|
|
|
f"[watcha] add user {self.inviter} to group {self.group_name} - failed", |
|
|
|
|
cm.output[1], |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def test_add_user_to_nextcloud_group_with_exception(self): |
|
|
|
|
group_name = NEXTCLOUD_GROUP_NAME_PREFIX + self.room_id |
|
|
|
|
self.nextcloud_client.add_user_to_group = AsyncMock( |
|
|
|
|
side_effect=SynapseError(code=400, msg="") |
|
|
|
|
def test_create_share_with_unexisting_folder(self): |
|
|
|
|
old_share_id = self.get_success(self.store.get_share_id(self.room_id)) |
|
|
|
|
self.nextcloud_client.share = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=404, msg="") |
|
|
|
|
) |
|
|
|
|
self.nextcloud_client.unshare = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=404, msg="") |
|
|
|
|
) |
|
|
|
|
self.nextcloud_handler.unbind = AsyncMock() |
|
|
|
|
|
|
|
|
|
with self.assertLogs("synapse.handlers.watcha_nextcloud", level="WARN") as cm: |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.add_room_users_to_nextcloud_group(self.room_id) |
|
|
|
|
error = self.get_failure( |
|
|
|
|
self.nextcloud_handler.create_share( |
|
|
|
|
self.creator, self.room_id, "/new_folder" |
|
|
|
|
), |
|
|
|
|
SynapseError, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.assertIn( |
|
|
|
|
"Unable to add the user {} to the Nextcloud group {}.".format( |
|
|
|
|
self.creator, group_name |
|
|
|
|
), |
|
|
|
|
cm.output[0], |
|
|
|
|
self.assertEquals(error.value.code, 404) |
|
|
|
|
self.nextcloud_handler.unbind.assert_called_once() |
|
|
|
|
self.assertIn(f"[watcha] unshare {old_share_id} - failed", cm.output[0]) |
|
|
|
|
|
|
|
|
|
def test_create_share_with_other_exceptions(self): |
|
|
|
|
old_share_id = self.get_success(self.store.get_share_id(self.room_id)) |
|
|
|
|
self.nextcloud_client.share = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=400, msg="") |
|
|
|
|
) |
|
|
|
|
self.nextcloud_client.unshare = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=404, msg="") |
|
|
|
|
) |
|
|
|
|
self.nextcloud_handler.unbind = AsyncMock() |
|
|
|
|
|
|
|
|
|
self.assertIn( |
|
|
|
|
"Unable to add the user {} to the Nextcloud group {}.".format( |
|
|
|
|
self.inviter, group_name |
|
|
|
|
), |
|
|
|
|
cm.output[1], |
|
|
|
|
with self.assertLogs("synapse.handlers.watcha_nextcloud", level="WARN") as cm: |
|
|
|
|
error = self.get_failure( |
|
|
|
|
self.nextcloud_handler.create_share( |
|
|
|
|
self.creator, self.room_id, "/new_folder" |
|
|
|
|
), |
|
|
|
|
SynapseError, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.assertEquals(error.value.code, 500) |
|
|
|
|
self.nextcloud_handler.unbind.assert_called_once() |
|
|
|
|
self.assertIn(f"[watcha] unshare {old_share_id} - failed", cm.output[0]) |
|
|
|
|
|
|
|
|
|
def test_add_user_to_unexisting_group(self): |
|
|
|
|
self.nextcloud_client.add_user_to_group = AsyncMock( |
|
|
|
|
side_effect=NextcloudError(code=102, msg="") |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def test_update_existing_nextcloud_share_on_invite_membership(self): |
|
|
|
|
self.get_failure( |
|
|
|
|
self.nextcloud_handler.add_room_members_to_group(self.room_id), |
|
|
|
|
SynapseError, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def test_update_existing_group_on_invite_membership(self): |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.update_share( |
|
|
|
|
self.nextcloud_handler.update_group( |
|
|
|
|
"@second_inviter:test", self.room_id, "invite" |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
@ -177,9 +191,9 @@ class NextcloudHandlerTestCase(HomeserverTestCase): |
|
|
|
|
self.nextcloud_client.add_user_to_group.assert_called_once() |
|
|
|
|
self.nextcloud_client.remove_user_from_group.assert_not_called() |
|
|
|
|
|
|
|
|
|
def test_update_existing_nextcloud_share_on_join_membership(self): |
|
|
|
|
def test_update_existing_group_on_join_membership(self): |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.update_share( |
|
|
|
|
self.nextcloud_handler.update_group( |
|
|
|
|
"@second_inviter:test", self.room_id, "join" |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
@ -187,9 +201,9 @@ class NextcloudHandlerTestCase(HomeserverTestCase): |
|
|
|
|
self.nextcloud_client.add_user_to_group.assert_called_once() |
|
|
|
|
self.nextcloud_client.remove_user_from_group.assert_not_called() |
|
|
|
|
|
|
|
|
|
def test_update_existing_nextcloud_share_on_leave_membership(self): |
|
|
|
|
def test_update_existing_group_on_leave_membership(self): |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.update_share( |
|
|
|
|
self.nextcloud_handler.update_group( |
|
|
|
|
"@second_inviter:test", self.room_id, "leave" |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
@ -197,9 +211,9 @@ class NextcloudHandlerTestCase(HomeserverTestCase): |
|
|
|
|
self.nextcloud_client.remove_user_from_group.assert_called_once() |
|
|
|
|
self.nextcloud_client.add_user_to_group.assert_not_called() |
|
|
|
|
|
|
|
|
|
def test_update_existing_nextcloud_share_on_kick_membership(self): |
|
|
|
|
def test_update_existing_group_on_kick_membership(self): |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.update_share( |
|
|
|
|
self.nextcloud_handler.update_group( |
|
|
|
|
"@second_inviter:test", self.room_id, "kick" |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
@ -207,42 +221,36 @@ class NextcloudHandlerTestCase(HomeserverTestCase): |
|
|
|
|
self.nextcloud_client.remove_user_from_group.assert_called_once() |
|
|
|
|
self.nextcloud_client.add_user_to_group.assert_not_called() |
|
|
|
|
|
|
|
|
|
def test_update_existing_nextcloud_share_on_invite_membership_with_exception(self): |
|
|
|
|
def test_update_existing_group_on_invite_membership_with_exception(self): |
|
|
|
|
self.nextcloud_client.add_user_to_group = AsyncMock( |
|
|
|
|
side_effect=SynapseError(code=400, msg="") |
|
|
|
|
side_effect=NextcloudError(code=103, msg="") |
|
|
|
|
) |
|
|
|
|
second_inviter = "@second_inviter:test" |
|
|
|
|
|
|
|
|
|
with self.assertLogs("synapse.handlers.watcha_nextcloud", level="WARN") as cm: |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.update_share( |
|
|
|
|
self.nextcloud_handler.update_group( |
|
|
|
|
second_inviter, self.room_id, "invite" |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.assertIn( |
|
|
|
|
"Unable to add the user {} to the Nextcloud group {}.".format( |
|
|
|
|
second_inviter, NEXTCLOUD_GROUP_NAME_PREFIX + self.room_id |
|
|
|
|
), |
|
|
|
|
f"[watcha] add user {second_inviter} to group {self.group_name} - failed", |
|
|
|
|
cm.output[0], |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def test_update_existing_nextcloud_share_on_leave_membership_with_exception(self): |
|
|
|
|
def test_update_existing_group_on_leave_membership_with_exception(self): |
|
|
|
|
self.nextcloud_client.remove_user_from_group = AsyncMock( |
|
|
|
|
side_effect=SynapseError(code=400, msg="") |
|
|
|
|
side_effect=NextcloudError(code=103, msg="") |
|
|
|
|
) |
|
|
|
|
second_inviter = "@second_inviter:test" |
|
|
|
|
|
|
|
|
|
with self.assertLogs("synapse.handlers.watcha_nextcloud", level="WARN") as cm: |
|
|
|
|
self.get_success( |
|
|
|
|
self.nextcloud_handler.update_share( |
|
|
|
|
self.nextcloud_handler.update_group( |
|
|
|
|
second_inviter, self.room_id, "leave" |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
self.assertIn( |
|
|
|
|
"Unable to remove the user {} from the Nextcloud group {}.".format( |
|
|
|
|
second_inviter, NEXTCLOUD_GROUP_NAME_PREFIX + self.room_id |
|
|
|
|
), |
|
|
|
|
f"[watcha] remove user {second_inviter} from group {self.group_name} - failed", |
|
|
|
|
cm.output[0], |
|
|
|
|
) |
|
|
|
|