def test_returns_expected_starting_new_story(self, story_code_mock, clue_mock, group_mock): story_code_mock.build_key.return_value.get.return_value = StoryCode(story_uid="STORY") clue_mock.get_by_id.return_value = Clue(text='test') clue = Clue(text='test') group_mock.return_value.current_clue = clue group_mock.gen_uid.return_value = 'abcd' user = User() result = perform_action(START_STORY, Message(text='start blah'), user, None) expected_message_text = [INTRO_INSTRUCTIONS.text, clue.text] self.assertEqual(expected_message_text, [m.text for m in result.messages])
def setUp(self): super(TestScavenger, self).setUp() self.story = Story.from_uid('STORY', default_hint='default hint') self.story.put() self.story_code = StoryCode.from_words('salsa tacos', story_uid=self.story.uid) self.story_code.put() self.start_clue = Clue.from_uid(Clue.build_uid(self.story.uid, 'START'), text='Start the story', hint='clue hint') self.start_clue.put() self.next_clue = Clue.from_uid(Clue.build_uid(self.story.uid, 'NEXT'), text='You made it!', sender="+555") self.next_clue.put() self.answer = Answer.from_uid( Answer.build_uid('STORY', 'START', 'TRANSITION'), pattern=r'my answer is (?P<user_answer>\w+)', next_clue=self.next_clue.uid, ) self.answer.put()
def _pre_delete_hook(cls, key): clue = Clue.get_by_id(get_clue_uid(key.id())) if clue is None: return clue.remove_answer(key.id()) clue.put()
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().set_cache_policy(False) self.story_uid = 'STORY' self.clue_uid = Clue.build_uid(self.story_uid, 'START') self.answer_uid = Answer.build_uid(self.story_uid, 'START', 'TRANSITION') Story.from_uid(self.story_uid, default_hint='default hint').put() Clue.from_uid(self.clue_uid, text='Start the story', hint='clue hint').put() Answer.from_uid( self.answer_uid, pattern=r'my answer is (?P<user_answer>\w+)', next_clue=self.clue_uid, ).put()
def test_formats_group_data(self): data = {'group_name': 'bob', 'group_color': 'red'} user = Mock() user.data = {} message = Clue(text='Hello {group_name}, you like {group_color}?') response = format_message(message, user, Group(data=data)) self.assertEqual('Hello bob, you like red?', response.text)
def test_returns_expected_joined_group(self, group_mock): clue = Clue(text='clue text') group = Mock(uid='code', clue_uid='MYSTORY:MYCLUE', clue=clue) group_mock.get_by_id.return_value = group user = User() result = perform_action(JOIN_GROUP, Message(text='join code'), user, None) self.assertEqual([JOINED_GROUP.text, 'clue text'], [m.text for m in result.messages])
def test_returns_expected_end_of_story(self): clue = Clue(text='blah', answer_uids=[]) group_mock = Mock(clue=clue) group_mock.story.end_message = "END" user = Mock() result = perform_action(ANSWER, Message(text='asdf'), user, group_mock) self.assertEqual([group_mock.story.end_message], [m.text for m in result.messages])
def test_returns_expected_restarted(self): start_message = 'Start the story' clue = Clue(text=start_message) user = Mock() group_mock = Mock(clue_uid='something', clue=clue) result = perform_action(RESTART, Message(text='restart'), user, group_mock) self.assertEqual([RESTARTED.text, start_message], [m.text for m in result.messages])
def test_sends_copies_to_each_recipient_for_CLUE_or_HINT(self): phones = ['+1234', '+555', '+678'] self.group.users = phones messages = [Clue(text='Hey there!')] response = twiml_response(self.user, self.group, CLUE, messages) for p in phones: self.assertIn(p, response) response = twiml_response(self.user, self.group, HINT, messages) for p in phones: self.assertIn(p, response)
def get(self): stories = [s.to_dict() for s in Story.query().fetch()] clues = [c.to_dict() for c in Clue.query().fetch()] answers = [a.to_dict() for a in Answer.query().fetch()] self.request.headers['Content-Type'] = 'application/json' self.response.body = json.dumps({ 'stories': stories, 'clues': clues, 'answers': answers, }, indent=2)
def test_does_not_send_copies_to_each_recipient_for_other_response_types(self): phones = ['+1234', '+555', '+678'] self.group.users = phones other_phones = set(phones) - set(['+1234']) messages = [Clue(text='Hey there!')] response = twiml_response(self.user, self.group, INFO, messages) for p in other_phones: self.assertNotIn(p, response) response = twiml_response(self.user, self.group, JOINED, messages) for p in other_phones: self.assertNotIn(p, response)
def clue(self): if not self.clue_uid: return None return Clue.get_by_id(self.clue_uid)
def test_adding_answer_adds_it_to_clue(self): clue = Clue.get_by_id(self.clue_uid) self.assertIn(self.answer_uid, clue.answer_uids)
def test_deleting_clue_deletes_answer(self): Clue.build_key(uid=self.clue_uid).delete() self.assertIsNone(Answer.get_by_id(self.answer_uid))
def test_delete_answer_removes_from_clue(self): Answer.build_key(self.answer_uid).delete() clue = Clue.get_by_id(self.clue_uid) self.assertNotIn(self.answer_uid, clue.answer_uids)
def test_does_not_include_sender_if_empty(self): messages = [Clue(text='my_clue', sender='')] response = twiml_response(self.user, None, ANSWER, messages) self.assertNotIn('from', response)
def test_sends_from_specific_number_if_specified(self): messages = [Clue(text='my_clue', sender='+1112223333')] response = twiml_response(self.user, None, ANSWER, messages) self.assertIn('from="+1112223333"', response)
def test_extracts_text_from_clues(self): messages = [Clue(text='my_clue'), Clue(text='my_other_clue')] response = twiml_response(self.user, None, START_STORY, messages) self.assertIn('my_clue', response) self.assertIn('my_other_clue', response)
def test_includes_messages_in_response(self): messages = [Clue(text='Hey there!')] response = twiml_response(self.user, None, START_STORY, messages) for m in messages: self.assertIn(m.text, response)
def _pre_put_hook(self): clue = Clue.get_by_id(self.clue_uid) if clue is None: raise ValueError("A clue doesn't exist for this answer") clue.add_answer(self.uid) clue.put()
def test_does_not_send_media_more_than_once(self): messages = [Clue(text='my_clue', media_url='picture.com/my.png'), Clue(text='my_other_clue')] response = twiml_response(self.user, None, START_STORY, messages) count = response.count('picture.com/my.png') self.assertEqual(1, count)
def test_deleting_story_deletes_clue(self): Story.build_key(self.story_uid).delete() self.assertIsNone(Clue.get_by_id(self.clue_uid))
def test_sends_media_urls_from_clues(self): messages = [Clue(text='my_clue', media_url='picture.com/my.png'), Clue(text='my_other_clue', media_url='second.com/my.jpg')] response = twiml_response(self.user, None, START_STORY, messages) self.assertIn('<Media>picture.com/my.png</Media>', response) self.assertIn('<Media>second.com/my.jpg</Media>', response)