Skip to content

Commit

Permalink
fix bug same chat in ChatState for different bots
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmadurga committed Apr 11, 2016
1 parent e2ccf96 commit 2558f2b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
21 changes: 21 additions & 0 deletions microbot/migrations/0002_auto_20160411_0727.py
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.4 on 2016-04-11 12:27
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('microbot', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='chatstate',
name='chat',
field=models.ForeignKey(help_text='Chat in Telegram API format. https://core.telegram.org/bots/api#chat', on_delete=django.db.models.deletion.CASCADE, related_name='chatstates', to='microbot.Chat', verbose_name='Chat'),
),
]
2 changes: 1 addition & 1 deletion microbot/models/bot.py
Expand Up @@ -56,7 +56,7 @@ def handle(self, update):
urlpatterns = []
state_context = {}
try:
chat_state = ChatState.objects.get(chat=update.message.chat)
chat_state = ChatState.objects.get(chat=update.message.chat, state__bot=self)
state_context = chat_state.ctx
for handler in self.handlers.filter(Q(enabled=True), Q(source_states=chat_state.state) | Q(source_states=None)):
urlpatterns.append(handler.urlpattern())
Expand Down
2 changes: 1 addition & 1 deletion microbot/models/handler.py
Expand Up @@ -173,7 +173,7 @@ def process(self, bot, update, state_context, **pattern_context):
context.pop('env', None)
context.pop('state_context', None)
try:
chat_state = ChatState.objects.get(chat=update.message.chat)
chat_state = ChatState.objects.get(chat=update.message.chat, state__bot=bot)
except ChatState.DoesNotExist:
logger.warning("Chat state for update chat %s not exists" %
(update.message.chat.id))
Expand Down
4 changes: 2 additions & 2 deletions microbot/models/state.py
Expand Up @@ -26,8 +26,8 @@ def __str__(self):

@python_2_unicode_compatible
class ChatState(MicrobotModel):
chat = models.OneToOneField(Chat, db_index=True, verbose_name=_('Chat'),
help_text=_("Chat in Telegram API format. https://core.telegram.org/bots/api#chat"))
chat = models.ForeignKey(Chat, db_index=True, verbose_name=_('Chat'), related_name='chatstates',
help_text=_("Chat in Telegram API format. https://core.telegram.org/bots/api#chat"))
state = models.ForeignKey(State, verbose_name=_('State'), related_name='chat',
help_text=_("State related to the chat"))
context = models.TextField(verbose_name=_("Context"),
Expand Down
39 changes: 37 additions & 2 deletions tests/functional/test_handler.py
Expand Up @@ -64,7 +64,7 @@ def test_handler_in_other_state_when_no_chat_state(self):
last_name=self.update.message.chat.last_name)

self._test_message(self.author_get, no_handler=True)

def test_handler_priority(self):
self.handler1 = factories.HandlerFactory(bot=self.bot,
name="handler1",
Expand Down Expand Up @@ -521,7 +521,42 @@ def test_handler_with_state_still_no_chatstate(self):
self.assertEqual(state_context['pattern'], {})
self.assertEqual(state_context['response']['data'][0], {'name': 'author1'})
self.assertEqual(None, state_context.get('state_context', None))


def test_handler_with_state_still_no_chatstate_but_with_state_from_other_bot(self):
self.other_bot = factories.BotFactory(token='190880460:AAELDdTxhhfPbtPRyC59qPaVF5VBX4VGVes')
Author.objects.create(name="author1")
self.request = factories.RequestFactory(url_template=self.live_server_url + '/api/authors/',
method=Request.GET)
self.response = factories.ResponseFactory(text_template='{% for author in response.data %}<b>{{author.name}}</b>{% endfor %}',
keyboard_template='')
self.handler = factories.HandlerFactory(bot=self.bot,
pattern='/authors',
request=self.request,
response=self.response)
self.state = factories.StateFactory(bot=self.bot,
name="state1")
self.state_target = factories.StateFactory(bot=self.bot,
name="state2")
self.handler.target_state = self.state_target
self.handler.save()
self.chat = factories.ChatAPIFactory(id=self.update.message.chat.id,
type=self.update.message.chat.type,
title=self.update.message.chat.title,
username=self.update.message.chat.username,
first_name=self.update.message.chat.first_name,
last_name=self.update.message.chat.last_name)
self.other_bot_same_name = factories.StateFactory(bot=self.other_bot,
name=self.state.name)
self.other_bot_chat_state = factories.ChatStateFactory(chat=self.chat,
state=self.other_bot_same_name)
self._test_message(self.author_get)
self.assertEqual(ChatState.objects.count(), 2)
self.assertEqual(ChatState.objects.get(chat=self.chat, state__bot=self.bot).state, self.state_target)
state_context = ChatState.objects.get(chat=self.chat, state__bot=self.bot).ctx
self.assertEqual(state_context['pattern'], {})
self.assertEqual(state_context['response']['data'][0], {'name': 'author1'})
self.assertEqual(None, state_context.get('state_context', None))

def test_get_request_with_more_priority(self):
Author.objects.create(name="author1")
self.request = factories.RequestFactory(url_template=self.live_server_url + '/api/authors/',
Expand Down

0 comments on commit 2558f2b

Please sign in to comment.