def test_whoami(bot, gmi):
    from lowerpines.endpoints.message import Message
    from saucerbot.groupme.handlers import general
    from saucerbot.groupme.models import HistoricalNickname

    fake_user_id = '123456'

    HistoricalNickname.objects.create(group_id=bot.group_id,
                                      groupme_id=fake_user_id,
                                      nickname='abc123',
                                      timestamp=arrow.utcnow().datetime -
                                      timedelta(1))
    HistoricalNickname.objects.create(group_id=bot.group_id,
                                      groupme_id=fake_user_id,
                                      nickname='def456',
                                      timestamp=arrow.utcnow().datetime -
                                      timedelta(2))

    message = Message(gmi)
    message.user_id = fake_user_id

    general.whoami(bot.bot, message)

    assert bot.group.messages.count == 1
    assert bot.group.messages.all(
    )[0].text == 'abc123 a day ago\ndef456 2 days ago\n'
def test_whoami_long(bot, gmi):
    from lowerpines.endpoints.message import Message
    from saucerbot.groupme.handlers import general
    from saucerbot.groupme.models import HistoricalNickname

    long_nickname = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    fake_user_id = '123456'

    for i in range(1, 21):
        HistoricalNickname.objects.create(group_id=bot.group_id,
                                          groupme_id=fake_user_id,
                                          nickname=f'{long_nickname} {i}',
                                          timestamp=arrow.utcnow().datetime -
                                          timedelta(i))

    message = Message(gmi)
    message.user_id = fake_user_id

    general.whoami(bot.bot, message)

    assert bot.group.messages.count == 2

    first_message = bot.group.messages.all()[0]

    assert len(first_message.text) <= 1000

    first_expected_start = f'{long_nickname} 1 a day ago\n' \
                           f'{long_nickname} 2 2 days ago\n'

    first_expected_end = f'{long_nickname} 14 2 weeks ago\n' \
                         f'{long_nickname} 15 2 weeks ago\n'

    assert first_message.text.startswith(first_expected_start)
    assert first_message.text.endswith(first_expected_end)

    second_message = bot.group.messages.all()[1]

    assert len(second_message.text) <= 1000

    second_expected_start = f'{long_nickname} 16 2 weeks ago\n' \
                            f'{long_nickname} 17 2 weeks ago\n'

    second_expected_end = f'{long_nickname} 19 2 weeks ago\n' \
                          f'{long_nickname} 20 2 weeks ago\n'

    assert second_message.text.startswith(second_expected_start)
    assert second_message.text.endswith(second_expected_end)
Beispiel #3
0
    def parse_as_message(self, bot: Bot) -> Message:
        if logger.isEnabledFor(logging.INFO):
            raw_json = json.dumps(self.request.data)
            logger.info("Received raw message: %s", raw_json)

        # Load it as a groupme message
        try:
            return Message.from_json(bot.owner.gmi, self.request.data)
        except Exception:
            raise ParseError('Invalid GroupMe message')
def test_mars_no_message(bot, gmi):
    from lowerpines.endpoints.message import Message
    from saucerbot.groupme.handlers import general

    raw_message = get_sample_message(bot.bot, "", [])

    ret = general.mars(bot.bot, Message.from_json(gmi, raw_message), 1)

    assert not ret
    assert bot.group.messages.count == 0
def test_saucerbot_user(bot, gmi):
    from lowerpines.endpoints.message import Message
    from saucerbot.groupme.handlers import saucer

    raw_message = get_sample_message(bot.bot, "", name='saucerbot')

    ret = saucer.user_named_saucerbot(bot.bot,
                                      Message.from_json(gmi, raw_message))

    assert ret
    assert bot.group.messages.count == 1
def test_mars(bot, gmi):
    from lowerpines.endpoints.message import Message
    from saucerbot.groupme.handlers import general

    raw_message = get_sample_message(bot.bot, "", [{'type': "image"}])

    ret = general.mars(bot.bot, Message.from_json(gmi, raw_message), 1)

    assert ret
    assert bot.group.messages.count == 1

    posted = bot.group.messages.all()[0]

    assert posted.text is not None
    assert len(posted.text) > 0
    assert len(posted.attachments) == 1
    assert posted.attachments[0]['type'] == 'mentions'
    assert posted.attachments[0]['user_ids'] == ['abcdef']
Beispiel #7
0
 def post(self, message):
     text, attachments = smart_split_complex_message(message)
     obj = Message(self.gmi, self.group_id, str(datetime.now()), text,
                   attachments)
     obj.save()
     return obj
Beispiel #8
0
 def get(self, group_id, message_id):
     return Message.get(self.gmi, group_id, message_id)
Beispiel #9
0
 def parse(self, response: JsonType) -> List[Message]:
     messages = []
     for message_json in response["messages"]:
         messages.append(Message.from_json(self.gmi, message_json))
     return messages
 def parse(self, response):
     messages = []
     for message_json in response['messages']:
         messages.append(Message.from_json(self.gmi, message_json))
     return messages
Beispiel #11
0
 def from_json(cls, gmi, json_dict, *args):
     return Message.from_json(gmi, json_dict, *args)