Skip to content

Commit

Permalink
creating a private room for the matrix dedicated fixture, leaving and…
Browse files Browse the repository at this point in the history
… forgetting the room at the end of the test
  • Loading branch information
burnout87 committed Sep 29, 2023
1 parent eb8d251 commit a7022d6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
55 changes: 55 additions & 0 deletions cdci_data_analysis/pytest_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import time
import hashlib
import glob
import uuid

from threading import Thread
from collections import OrderedDict
Expand Down Expand Up @@ -322,6 +323,58 @@ def __init__(self,
matrix_sender_access_token=dispatcher_test_conf_with_matrix_options['matrix_options']['matrix_sender_access_token']):
self.matrix_server_url = matrix_server_url
self.matrix_sender_access_token = matrix_sender_access_token
self.room_id = self.create_room()

def create_room(self):
url = os.path.join(self.matrix_server_url, f'_matrix/client/v3/createRoom')
headers = {
'Authorization': ' '.join(['Bearer', self.matrix_sender_access_token]),
'Content-type': 'application/json'
}
room_data = {
'name': 'test room' + '_' + str(uuid.uuid4())[:8],
'visibility': 'private'
}

res = requests.post(url, json=room_data, headers=headers)

if res.status_code == 200:
res_content = res.json()
return res_content['room_id']

return None

def leave_room(self):
url = os.path.join(self.matrix_server_url, f'_matrix/client/v3/rooms/{self.room_id}/leave')
headers = {
'Authorization': ' '.join(['Bearer', self.matrix_sender_access_token]),
'Content-type': 'application/json'
}

res = requests.post(url, headers=headers)

if res.status_code == 200:
res_content = res.json()
if res_content == {}:
return True

return False

def forget_room(self):
url = os.path.join(self.matrix_server_url, f'_matrix/client/v3/rooms/{self.room_id}/forget')
headers = {
'Authorization': ' '.join(['Bearer', self.matrix_sender_access_token]),
'Content-type': 'application/json'
}

res = requests.post(url, headers=headers)

if res.status_code == 200:
res_content = res.json()
if res_content == {}:
return True

return False

def get_matrix_message_record(self, room_id, event_id=None):
logger.info(f"Getting messages from the room id: {room_id}")
Expand Down Expand Up @@ -349,6 +402,8 @@ def get_matrix_message_record(self, room_id, event_id=None):
matrix_sender_access_token=dispatcher_test_conf_with_matrix_options['matrix_options']['matrix_sender_access_token']
)
yield matrix_message_controller
matrix_message_controller.leave_room()
matrix_message_controller.forget_room()


@pytest.fixture
Expand Down
4 changes: 3 additions & 1 deletion tests/test_matrix_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def test_matrix_message_run_analysis_callback(gunicorn_dispatcher_long_living_fi
"mxsub": True,
"mssub": False,
"msdone": False,
"msfail": False
"msfail": False,
"mxroomid": dispatcher_local_matrix_message_server.room_id
}

if token_none:
Expand Down Expand Up @@ -333,6 +334,7 @@ def test_matrix_message_run_analysis_callback(gunicorn_dispatcher_long_living_fi
)



def validate_matrix_message_content(
message_record,
state: str,
Expand Down

0 comments on commit a7022d6

Please sign in to comment.