2019-06-14 08:53:25 -04:00
|
|
|
import asyncio
|
2019-04-10 06:14:00 -04:00
|
|
|
import pdb
|
2019-06-14 08:53:25 -04:00
|
|
|
import pprint
|
2019-07-12 09:38:04 -04:00
|
|
|
import pytest
|
2019-04-10 06:14:00 -04:00
|
|
|
|
2019-06-06 05:14:40 -04:00
|
|
|
from nio import RoomMessage
|
|
|
|
|
2019-05-14 15:50:30 -04:00
|
|
|
from conftest import faker
|
2019-07-12 09:38:04 -04:00
|
|
|
from pantalaimon.index import INDEXING_ENABLED
|
2019-06-12 09:39:08 -04:00
|
|
|
from pantalaimon.store import FetchTask
|
2019-06-06 05:14:40 -04:00
|
|
|
|
|
|
|
TEST_ROOM = "!SVkFJHzfwvuaIEawgC:localhost"
|
|
|
|
TEST_ROOM2 = "!testroom:localhost"
|
2019-04-10 06:14:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TestClass(object):
|
2019-06-06 05:14:40 -04:00
|
|
|
@property
|
|
|
|
def test_event(self):
|
|
|
|
return RoomMessage.parse_event(
|
|
|
|
{
|
|
|
|
"content": {"body": "Test message", "msgtype": "m.text"},
|
|
|
|
"event_id": "$15163622445EBvZJ:localhost",
|
|
|
|
"origin_server_ts": 1516362244026,
|
|
|
|
"room_id": "!SVkFJHzfwvuaIEawgC:localhost",
|
|
|
|
"sender": "@example2:localhost",
|
|
|
|
"type": "m.room.message",
|
|
|
|
"unsigned": {"age": 43289803095},
|
|
|
|
"user_id": "@example2:localhost",
|
|
|
|
"age": 43289803095
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def another_event(self):
|
|
|
|
return RoomMessage.parse_event(
|
|
|
|
{
|
|
|
|
"content": {"body": "Another message", "msgtype": "m.text"},
|
2019-06-14 08:53:25 -04:00
|
|
|
"event_id": "$15163622445EBvZK:localhost",
|
|
|
|
"origin_server_ts": 1516362244030,
|
2019-06-06 05:14:40 -04:00
|
|
|
"room_id": "!SVkFJHzfwvuaIEawgC:localhost",
|
|
|
|
"sender": "@example2:localhost",
|
|
|
|
"type": "m.room.message",
|
|
|
|
"unsigned": {"age": 43289803095},
|
|
|
|
"user_id": "@example2:localhost",
|
|
|
|
"age": 43289803095
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-04-10 06:14:00 -04:00
|
|
|
def test_account_loading(self, panstore):
|
2019-04-12 11:59:30 -04:00
|
|
|
accounts = panstore.load_all_users()
|
2019-04-10 06:14:00 -04:00
|
|
|
# pdb.set_trace()
|
|
|
|
assert len(accounts) == 10
|
|
|
|
|
|
|
|
def test_token_saving(self, panstore, access_token):
|
2019-04-12 11:59:30 -04:00
|
|
|
accounts = panstore.load_all_users()
|
2019-04-10 06:14:00 -04:00
|
|
|
user_id = accounts[0][0]
|
|
|
|
device_id = accounts[0][1]
|
|
|
|
|
|
|
|
panstore.save_access_token(user_id, device_id, access_token)
|
|
|
|
|
|
|
|
token = panstore.load_access_token(user_id, device_id)
|
|
|
|
access_token == token
|
2019-04-11 10:59:37 -04:00
|
|
|
|
2019-06-11 10:06:35 -04:00
|
|
|
def test_token_storing(self, panstore_with_users):
|
|
|
|
panstore = panstore_with_users
|
|
|
|
accounts = panstore.load_all_users()
|
|
|
|
user, _ = accounts[0]
|
|
|
|
|
|
|
|
assert not panstore.load_token("example", user)
|
|
|
|
panstore.save_token("example", user, "abc123")
|
|
|
|
|
2019-06-12 09:39:08 -04:00
|
|
|
assert panstore.load_token("example", user) == "abc123"
|
|
|
|
|
|
|
|
def test_fetcher_tasks(self, panstore_with_users):
|
|
|
|
panstore = panstore_with_users
|
|
|
|
accounts = panstore.load_all_users()
|
|
|
|
user, _ = accounts[0]
|
|
|
|
|
|
|
|
task = FetchTask(TEST_ROOM, "abc1234")
|
|
|
|
task2 = FetchTask(TEST_ROOM2, "abc1234")
|
|
|
|
|
|
|
|
assert not panstore.load_fetcher_tasks("example", user)
|
|
|
|
|
|
|
|
panstore.save_fetcher_task("example", user, task)
|
|
|
|
panstore.save_fetcher_task("example", user, task2)
|
|
|
|
|
|
|
|
tasks = panstore.load_fetcher_tasks("example", user)
|
|
|
|
|
|
|
|
assert task in tasks
|
|
|
|
assert task2 in tasks
|
|
|
|
|
|
|
|
panstore.delete_fetcher_task("example", user, task)
|
|
|
|
tasks = panstore.load_fetcher_tasks("example", user)
|
|
|
|
|
|
|
|
assert task not in tasks
|
|
|
|
assert task2 in tasks
|
2019-06-14 08:53:25 -04:00
|
|
|
|
2019-06-19 06:01:11 -04:00
|
|
|
async def test_new_indexstore(self, tempdir):
|
2019-07-12 09:38:04 -04:00
|
|
|
if not INDEXING_ENABLED:
|
|
|
|
pytest.skip("Indexing needs to be enabled to test this")
|
|
|
|
|
|
|
|
from pantalaimon.index import Index, IndexStore
|
2019-06-14 08:53:25 -04:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
store = IndexStore("example", tempdir)
|
|
|
|
|
|
|
|
store.add_event(self.test_event, TEST_ROOM, None, None)
|
|
|
|
store.add_event(self.another_event, TEST_ROOM, None, None)
|
2019-06-19 06:01:11 -04:00
|
|
|
await store.commit_events()
|
2019-06-14 08:53:25 -04:00
|
|
|
|
|
|
|
assert store.event_in_store(self.test_event.event_id, TEST_ROOM)
|
|
|
|
assert not store.event_in_store("FAKE", TEST_ROOM)
|
|
|
|
|
2019-06-19 06:01:11 -04:00
|
|
|
result = await store.search("test", TEST_ROOM, after_limit=10, before_limit=10)
|
2019-06-14 08:53:25 -04:00
|
|
|
pprint.pprint(result)
|
|
|
|
|
|
|
|
assert len(result["results"]) == 1
|
|
|
|
assert result["count"] == 1
|
|
|
|
assert result["results"][0]["result"] == self.test_event.source
|
|
|
|
assert (result["results"][0]["context"]["events_after"][0]
|
|
|
|
== self.another_event.source)
|