Ejemplo n.º 1
0
class TestQuestionTasks(TestCase):

    def setUp(self):
        settings.DEBUG = True
        settings.CELERY_ALWAYS_EAGER = True
        self.email = "*****@*****.**"
        self.pleb = create_user_util_test(self.email)
        self.user = User.objects.get(email=self.email)
        self.question = Question(content="Hey I'm a question",
                                 title=str(uuid1()),
                                 owner_username=self.pleb.username).save()
        self.question.owned_by.connect(self.pleb)

    def tearDown(self):
        settings.DEBUG = False
        settings.CELERY_ALWAYS_EAGER = False

    def test_summary_question_does_not_exist(self):
        query = 'MATCH (a:Solution) OPTIONAL MATCH (a)-[r]-() ' \
                'DELETE a, r'
        db.cypher_query(query)
        bad_uuid = str(uuid1())
        res = create_question_summary_task.apply_async(
            kwargs={"object_uuid": bad_uuid})
        self.assertIsInstance(res.result, Exception)

    def test_summary_question_exists(self):
        content = "My content that needs to be converted into a summary."
        self.question.content = content
        self.question.save()
        res = create_question_summary_task.apply_async(
            kwargs={"object_uuid": self.question.object_uuid})
        self.assertTrue(res.result.summary, content)
Ejemplo n.º 2
0
class TestSpawnCommentNotifications(TestCase):
    def setUp(self):
        settings.CELERY_ALWAYS_EAGER = True
        self.api_endpoint = "http://testserver/v1"
        self.email = "*****@*****.**"
        self.pleb = create_user_util_test(self.email)
        self.user = User.objects.get(email=self.email)
        self.question = Question(title=str(uuid1())).save()
        self.comment = Comment(
            owner_username=self.pleb.username,
            url="%s/questions/%s/" %
            (self.api_endpoint, self.question.object_uuid)).save()
        self.email2 = "*****@*****.**"
        self.pleb2 = create_user_util_test(self.email2)

    def tearDown(self):
        settings.CELERY_ALWAYS_EAGER = False

    def test_spawn_comment_notifications(self):
        data = {
            "object_uuid": self.comment.object_uuid,
            "parent_object_uuid": self.question.object_uuid,
            "from_pleb": self.pleb.username,
            "notification_id": str(uuid1())
        }
        res = spawn_comment_notifications.apply_async(kwargs=data)
        while not res.ready():
            time.sleep(1)
        self.assertTrue(res.result)

    def test_spawn_comment_notification_comment_does_not_exist(self):
        data = {
            "object_uuid": str(uuid1()),
            "parent_object_uuid": self.question.object_uuid,
            "from_pleb": self.pleb.username,
            "notification_id": str(uuid1())
        }
        res = spawn_comment_notifications.apply_async(kwargs=data)
        while not res.ready():
            time.sleep(1)
        self.assertIsInstance(res.result, Exception)

    @requests_mock.mock()
    def test_spawn_comment_notifications_comment_on_comment(self, m):
        m.get("%s/questions/%s/" %
              (self.api_endpoint, self.question.object_uuid),
              json={
                  "url":
                  "http://www.sagebrew.com/v1/questions/%s/" %
                  self.question.object_uuid
              },
              status_code=status.HTTP_200_OK)
        comment2 = Comment(
            owner_username=self.pleb2.username,
            url="%s/questions/%s/" %
            (self.api_endpoint, self.question.object_uuid)).save()
        comment = Comment(
            owner_username=self.pleb.username,
            url="%s/questions/%s/" %
            (self.api_endpoint, self.question.object_uuid)).save()
        self.question.owned_by.connect(self.pleb)
        self.question.owner_username = self.pleb.username
        self.question.save()

        comment.owned_by.connect(self.pleb)
        self.question.comments.connect(comment)

        comment2.owned_by.connect(self.pleb2)
        self.question.comments.connect(comment2)

        notification_id = str(uuid1())
        comment_on_comment_id = str(uuid1())
        data = {
            "object_uuid": comment.object_uuid,
            "parent_object_uuid": self.question.object_uuid,
            "from_pleb": self.pleb.username,
            "notification_id": notification_id,
            "comment_on_comment_id": comment_on_comment_id
        }
        res = spawn_comment_notifications.apply_async(kwargs=data)
        while not res.ready():
            time.sleep(1)
        self.assertTrue(res.result)
        while not res.result['comment_on_comment_task'].ready():
            time.sleep(1)
        notification = Notification.nodes.get(
            object_uuid=comment_on_comment_id)
        self.assertEqual(notification.action_name, "commented on a question "
                         "you commented on")
        self.assertTrue(self.pleb2 in notification.notification_to)