async def test_publish_no_timestamp(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=None) a.event_id = a.calc_event_id() with pytest.raises(BadDataException) as excinfo: await controller.act(a, data=deepcopy(correct_data)) assert excinfo.value.args[0] == 'remote actions should always have a timestamp' assert len(controller.ds.data) == 0
async def test_publish_invalid_args(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() with pytest.raises(BadDataException) as excinfo: await controller.act(a, foo='bar') assert excinfo.value.args[0] == "Conversations.add_remote: missing a required argument: 'data'" assert len(controller.ds.data) == 0
async def test_publish_conversation_two_messages_wrong_id(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() ds = controller.ds assert len(ds.data) == 0 data = deepcopy(correct_data) data['messages'][0]['id'] = 'id1' data['messages'].extend([ { 'author': '*****@*****.**', 'body': 'the second body', 'id': 'id2', 'parent': 'id1', 'timestamp': datetime.datetime(2016, 1, 2), }, { 'author': '*****@*****.**', 'body': 'the third body', 'id': 'id3', 'parent': 'foobar', 'timestamp': datetime.datetime(2016, 1, 3), } ]) with pytest.raises(ComponentNotFound) as excinfo: await controller.act(a, data=data) assert excinfo.value.args[0] == 'message foobar not found'
async def test_publish_conversation_two_messages(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() ds = controller.ds assert len(ds.data) == 0 data = deepcopy(correct_data) data['messages'].append( { 'author': '*****@*****.**', 'body': 'the second body', 'id': 'different_id', 'parent': 'd21625d617b1e8eb8989aa3d57a5aae691f9ed2a', 'timestamp': datetime.datetime(2016, 1, 2), } ) await controller.act(a, data=data) assert len(ds.data) == 1 assert ds.data[0]['conv_id'] == conv_id assert ds.data[0]['subject'] == 'the subject' assert len(ds.data[0]['messages']) == 2 messages = list(ds.data[0]['messages'].values()) assert messages[0]['body'] == 'the body' assert messages[1]['body'] == 'the second body' assert ds.data[0]['timestamp'] == datetime.datetime(2016, 1, 2) assert len(ds.data[0]['participants']) == 2
async def test_publish_misshaped_data(controller, data, exc): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() with pytest.raises(BadDataException) as excinfo: await controller.act(a, data=data) assert exc == excinfo.value.args[0] assert len(controller.ds.data) == 0
async def test_wrong_hash(controller): w_conv_id = conv_id + '+wrong' a = Action('*****@*****.**', w_conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() with pytest.raises(BadHash) as excinfo: await controller.act(a, data=deepcopy(correct_data)) assert excinfo.value.args[0] == 'provided hash {} does not match computed hash {}'.format(w_conv_id, conv_id) assert len(controller.ds.data) == 0
async def test_publish_conversation_wrong_message_id(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() ds = controller.ds assert len(ds.data) == 0 data = deepcopy(correct_data) data['messages'][0]['parent'] = 'not None' with pytest.raises(MisshapedDataException) as excinfo: await controller.act(a, data=data) assert excinfo.value.args[0] == 'first message parent should be None'
async def test_publish_simple_conversation(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, Components.CONVERSATIONS, timestamp=timestamp) a.event_id = a.calc_event_id() ds = controller.ds assert len(ds.data) == 0 await controller.act(a, data=deepcopy(correct_data)) assert len(ds.data) == 1 assert ds.data[0]['conv_id'] == conv_id assert ds.data[0]['subject'] == 'the subject' assert len(ds.data[0]['messages']) == 1 assert list(ds.data[0]['messages'].values())[0]['body'] == 'the body' assert ds.data[0]['timestamp'] == datetime.datetime(2016, 1, 2) assert len(ds.data[0]['participants']) == 2
async def test_publish_reply_bad_ts(two_controllers): ctrl1, ctrl2, conv_id = await two_controllers() assert (len(ctrl1.ds.data), len(ctrl2.ds.data)) == (1, 0) a = Action('*****@*****.**', conv_id, Verbs.PUBLISH, Components.CONVERSATIONS) await ctrl1.act(a) assert (len(ctrl1.ds.data), len(ctrl2.ds.data)) == (1, 1) conv_id = ctrl1.ds.data[0]['conv_id'] msg1_id = list(ctrl2.ds.data[0]['messages'])[0] a = Action('*****@*****.**', conv_id, Verbs.ADD, Components.MESSAGES, timestamp=datetime_tz(year=2000)) a.event_id = a.calc_event_id() with pytest.raises(BadDataException) as excinfo: await ctrl1.act(a, parent_id=msg1_id, body='this is a reply') assert excinfo.value.args[0] == 'timestamp not after parent timestamp: 2000-01-01 00:00:00+00:00'
async def test_publish_conversation_two_messages_repeat_id(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() ds = controller.ds assert len(ds.data) == 0 data = deepcopy(correct_data) data['messages'].append( { 'author': '*****@*****.**', 'body': 'the second body', 'id': 'd21625d617b1e8eb8989aa3d57a5aae691f9ed2a', 'parent': 'd21625d617b1e8eb8989aa3d57a5aae691f9ed2a', 'timestamp': datetime.datetime(2016, 1, 2), } ) with pytest.raises(BadDataException) as excinfo: await controller.act(a, data=data) assert excinfo.value.args[0] == 'message id d21625d617b1e8eb8989aa3d57a5aae691f9ed2a already exists'
async def test_publish_conversation_two_messages_wrong_timestamp(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = a.calc_event_id() ds = controller.ds assert len(ds.data) == 0 data = deepcopy(correct_data) data['messages'].extend([ { 'author': '*****@*****.**', 'body': 'the second body', 'id': 'id2', 'parent': 'd21625d617b1e8eb8989aa3d57a5aae691f9ed2a', 'timestamp': datetime.datetime(2015, 1, 1), }, ]) with pytest.raises(BadDataException) as excinfo: await controller.act(a, data=data) assert excinfo.value.args[0] == 'timestamp 2015-01-01 00:00:00 not after parent'
async def test_publish_wrong_event_id(controller): a = Action('*****@*****.**', conv_id, Verbs.ADD, timestamp=timestamp) a.event_id = 'foobar' with pytest.raises(BadHash) as excinfo: await controller.act(a, data=deepcopy(correct_data)) assert excinfo.value.args[0] == 'event_id "foobar" incorrect'