2022-08-24 11:54:46 +00:00
|
|
|
from __future__ import absolute_import, division, print_function, annotations
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
import types
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
|
|
|
|
|
|
|
from ansible_collections.famedly.matrix.plugins.module_utils import matrix
|
|
|
|
from ansible_collections.famedly.matrix.plugins.modules import matrix_state
|
2022-08-24 11:54:46 +00:00
|
|
|
from ansible_collections.famedly.matrix.tests.unit.mock_nio.MatrixNioBase import (
|
|
|
|
MatrixNioBase,
|
|
|
|
)
|
|
|
|
from ansible_collections.famedly.matrix.tests.unit.mock_nio.MatrixNioSuccess import (
|
|
|
|
MatrixNioSuccess,
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
from ansible_collections.famedly.matrix.tests.unit.mock_nio.room import failure
|
2022-08-24 11:54:46 +00:00
|
|
|
from ansible_collections.famedly.matrix.tests.unit.mock_nio.utils.RoomSimulator import (
|
|
|
|
RoomSimulator,
|
|
|
|
RoomEvents,
|
|
|
|
)
|
|
|
|
from ansible_collections.famedly.matrix.tests.unit.mock_nio.utils.ClientSimulator import (
|
|
|
|
ClientSimulator,
|
|
|
|
)
|
|
|
|
from ansible_collections.famedly.matrix.tests.unit.utils import (
|
|
|
|
AnsibleExitJson,
|
|
|
|
AnsibleFailJson,
|
|
|
|
set_module_args,
|
|
|
|
exit_json,
|
|
|
|
fail_json,
|
|
|
|
assert_expression,
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestAnsibleModuleMatrixState:
|
|
|
|
@staticmethod
|
2022-08-24 11:54:46 +00:00
|
|
|
def patchAnsibleNioModule(
|
|
|
|
monkeypatch: MonkeyPatch, mock_class: type(MatrixNioBase)
|
|
|
|
):
|
2022-05-12 03:56:52 +00:00
|
|
|
# Mock ansible functions
|
2022-08-24 11:54:46 +00:00
|
|
|
monkeypatch.setattr(matrix.AnsibleModule, "exit_json", exit_json)
|
|
|
|
monkeypatch.setattr(matrix.AnsibleModule, "fail_json", fail_json)
|
2022-05-12 03:56:52 +00:00
|
|
|
# Mock MatrixNio
|
|
|
|
for method in MatrixNioBase.__dict__:
|
2022-08-24 11:54:46 +00:00
|
|
|
if isinstance(
|
|
|
|
getattr(mock_class, method),
|
|
|
|
(types.FunctionType, types.BuiltinFunctionType),
|
|
|
|
):
|
|
|
|
monkeypatch.setattr(
|
|
|
|
matrix.AsyncClient, method, getattr(mock_class, method)
|
|
|
|
)
|
|
|
|
monkeypatch.setattr(matrix.AsyncClient, "logged_in", False)
|
2022-05-12 03:56:52 +00:00
|
|
|
# Setup RoomSimulator
|
|
|
|
room_simulator = RoomSimulator()
|
2022-08-24 11:54:46 +00:00
|
|
|
room_simulator.add_room(
|
|
|
|
"!existingroomid:matrix.example.tld", "#existingroom:matrix.example.tld"
|
|
|
|
)
|
|
|
|
room_simulator.add_room(
|
|
|
|
"!otherroomid:matrix.example.tld", "#otherroom:matrix.example.tld"
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
dummy_event = deepcopy(RoomEvents.M_ROOM_DUMMY)
|
2022-08-24 11:54:46 +00:00
|
|
|
dummy_event["sender"] = "myuser"
|
|
|
|
dummy_event["state_key"] = "mystatekey"
|
|
|
|
room_simulator.send_event(
|
|
|
|
room_id="!existingroomid:matrix.example.tld", event=dummy_event
|
|
|
|
)
|
|
|
|
room_simulator.send_event(
|
|
|
|
room_id="!otherroomid:matrix.example.tld", event=dummy_event
|
|
|
|
)
|
|
|
|
monkeypatch.setenv("ROOM_SIMULATOR", room_simulator.export())
|
2022-05-12 03:56:52 +00:00
|
|
|
# Setup ClientSimulator
|
|
|
|
client_simulator = ClientSimulator()
|
2022-08-24 11:54:46 +00:00
|
|
|
client_simulator.join("!existingroomid:matrix.example.tld")
|
|
|
|
monkeypatch.setenv("CLIENT_SIMULATOR", client_simulator.export())
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
# In room; Event already sent
|
|
|
|
def test_no_changes(self, monkeypatch):
|
|
|
|
self.patchAnsibleNioModule(monkeypatch, MatrixNioSuccess)
|
2022-08-24 11:54:46 +00:00
|
|
|
set_module_args(
|
|
|
|
{
|
|
|
|
"hs_url": "matrix.example.tld",
|
|
|
|
"user_id": "myuser",
|
|
|
|
"password": "supersecretpassword",
|
|
|
|
"room_id": "!existingroomid:matrix.example.tld",
|
|
|
|
"event_type": "m.room.dummy",
|
|
|
|
"state_key": "mystatekey",
|
|
|
|
"content": RoomEvents.M_ROOM_DUMMY["content"],
|
|
|
|
}
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
with pytest.raises(AnsibleExitJson) as result:
|
|
|
|
matrix_state.main()
|
|
|
|
ansible_result = result.value.result
|
2022-08-24 11:54:46 +00:00
|
|
|
assert_expression(ansible_result["changed"] is False)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
# Not in room; Event already sent
|
|
|
|
def test_not_in_room_fail(self, monkeypatch):
|
|
|
|
self.patchAnsibleNioModule(monkeypatch, MatrixNioSuccess)
|
2022-08-24 11:54:46 +00:00
|
|
|
set_module_args(
|
|
|
|
{
|
|
|
|
"hs_url": "matrix.example.tld",
|
|
|
|
"user_id": "myuser",
|
|
|
|
"password": "supersecretpassword",
|
|
|
|
"room_id": "!otherroomid:matrix.example.tld",
|
|
|
|
"event_type": "m.room.dummy",
|
|
|
|
"state_key": "mystatekey",
|
|
|
|
"content": RoomEvents.M_ROOM_DUMMY["content"],
|
|
|
|
}
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
with pytest.raises(AnsibleFailJson) as result:
|
|
|
|
matrix_state.main()
|
|
|
|
ansible_result = result.value.result
|
2022-08-24 11:54:46 +00:00
|
|
|
assert_expression(
|
|
|
|
ansible_result["msg"] == "Not in the room you're trying to set state for."
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
# Not in room; Event already sent
|
|
|
|
def test_joined_rooms_fail(self, monkeypatch):
|
|
|
|
class TestClass(failure.RoomJoinedRooms, MatrixNioSuccess):
|
|
|
|
pass
|
2022-08-24 11:54:46 +00:00
|
|
|
|
2022-05-12 03:56:52 +00:00
|
|
|
self.patchAnsibleNioModule(monkeypatch, TestClass)
|
2022-08-24 11:54:46 +00:00
|
|
|
set_module_args(
|
|
|
|
{
|
|
|
|
"hs_url": "matrix.example.tld",
|
|
|
|
"user_id": "myuser",
|
|
|
|
"password": "supersecretpassword",
|
|
|
|
"room_id": "!otherroomid:matrix.example.tld",
|
|
|
|
"event_type": "m.room.dummy",
|
|
|
|
"state_key": "mystatekey",
|
|
|
|
"content": RoomEvents.M_ROOM_DUMMY["content"],
|
|
|
|
}
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
with pytest.raises(AnsibleFailJson) as result:
|
|
|
|
matrix_state.main()
|
|
|
|
ansible_result = result.value.result
|
2022-08-24 11:54:46 +00:00
|
|
|
assert_expression(ansible_result["msg"] == "Couldn't get joined rooms.")
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
# In room; Event not send yet
|
|
|
|
def test_send_event(self, monkeypatch):
|
|
|
|
self.patchAnsibleNioModule(monkeypatch, MatrixNioSuccess)
|
2022-08-24 11:54:46 +00:00
|
|
|
set_module_args(
|
|
|
|
{
|
|
|
|
"hs_url": "matrix.example.tld",
|
|
|
|
"user_id": "myuser",
|
|
|
|
"password": "supersecretpassword",
|
|
|
|
"room_id": "!existingroomid:matrix.example.tld",
|
|
|
|
"event_type": "m.room.dummy",
|
|
|
|
"state_key": "myotherstatekey",
|
|
|
|
"content": RoomEvents.M_ROOM_DUMMY["content"],
|
|
|
|
}
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
with pytest.raises(AnsibleExitJson) as result:
|
|
|
|
matrix_state.main()
|
|
|
|
ansible_result = result.value.result
|
2022-08-24 11:54:46 +00:00
|
|
|
assert_expression(ansible_result["changed"] is True)
|
2022-05-12 03:56:52 +00:00
|
|
|
|
|
|
|
# In room; send Event
|
|
|
|
def test_send_event_fail(self, monkeypatch):
|
|
|
|
class TestClass(failure.RoomPutState, MatrixNioSuccess):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.patchAnsibleNioModule(monkeypatch, TestClass)
|
2022-08-24 11:54:46 +00:00
|
|
|
set_module_args(
|
|
|
|
{
|
|
|
|
"hs_url": "matrix.example.tld",
|
|
|
|
"user_id": "myuser",
|
|
|
|
"password": "supersecretpassword",
|
|
|
|
"room_id": "!existingroomid:matrix.example.tld",
|
|
|
|
"event_type": "m.room.dummy",
|
|
|
|
"state_key": "myotherstatekey",
|
|
|
|
"content": RoomEvents.M_ROOM_DUMMY["content"],
|
|
|
|
}
|
|
|
|
)
|
2022-05-12 03:56:52 +00:00
|
|
|
with pytest.raises(AnsibleFailJson) as result:
|
|
|
|
matrix_state.main()
|
|
|
|
ansible_result = result.value.result
|
2022-08-24 11:54:46 +00:00
|
|
|
assert_expression("Couldn't set state" in ansible_result["msg"])
|