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)
class TestUpdateClosedTask(TestCase):
    def setUp(self):
        settings.CELERY_ALWAYS_EAGER = True
        self.email = "*****@*****.**"
        self.pleb = create_user_util_test(self.email)
        self.user = User.objects.get(email=self.email)
        self.flag = Flag().save()
        self.question = Question(title=str(uuid1())).save()
        self.question.flags.connect(self.flag)
        self.vote_rel = self.question.council_votes.connect(self.pleb)
        self.vote_rel.active = True
        self.vote_rel.vote_type = True
        self.vote_rel.save()

    def tearDown(self):
        settings.CELERY_ALWAYS_EAGER = False

    def test_updated_closed(self):
        task_data = {'object_uuid': self.question.object_uuid}

        res = update_closed_task.apply_async(kwargs=task_data)
        while not res.ready():
            time.sleep(1)
        self.assertFalse(isinstance(res.result, Exception))
        self.question.refresh()
        self.assertTrue(self.question.is_closed)
 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 setUp(self):
     from django.core.cache import cache
     cache.clear()
     self.email = "*****@*****.**"
     create_user_util_test(self.email)
     self.pleb = Pleb.nodes.get(email=self.email)
     self.user = User.objects.get(email=self.email)
     self.question = Question(content='test content',
                              object_uuid=str(uuid1()),
                              owner_username=self.pleb.username,
                              title=str(uuid1())).save()
     self.question.owned_by.connect(self.pleb)
 def setUp(self):
     settings.CELERY_ALWAYS_EAGER = True
     self.email = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.flag = Flag().save()
     self.question = Question(title=str(uuid1())).save()
     self.question.flags.connect(self.flag)
     self.vote_rel = self.question.council_votes.connect(self.pleb)
     self.vote_rel.active = True
     self.vote_rel.vote_type = True
     self.vote_rel.save()
 def test_connect_no_related_element(self):
     cache.clear()
     query = 'MATCH (a:Question) OPTIONAL MATCH (a)-[r]-() DELETE a, r'
     db.cypher_query(query)
     question = Question(title="Hello this is my question",
                         content="This is content",
                         owner_username=self.pleb.username).save()
     location = parse_google_places(wixom_data['address_components'],
                                    wixom_data['place_id'])
     connected = connect_related_element(location, wixom_data['place_id'])
     self.assertIsNone(connected)
     question.delete()
 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 get_public_content(api, username, request):
    then = (datetime.now(pytz.utc) - timedelta(days=120)).strftime("%s")
    query = \
        '// Retrieve all the current users questions\n' \
        'MATCH (a:Pleb {username: "******"})<-[:OWNED_BY]-' \
        '(questions:Question) ' \
        'WHERE questions.to_be_deleted = False AND questions.created > %s' \
        ' AND questions.is_closed = False ' \
        'RETURN questions, NULL AS solutions, ' \
        'questions.created AS created, NULL AS s_question UNION ' \
        '// Retrieve all the current users solutions\n' \
        'MATCH (a:Pleb {username: "******"})<-' \
        '[:OWNED_BY]-(solutions:Solution)<-' \
        '[:POSSIBLE_ANSWER]-(s_question:Question) ' \
        'WHERE s_question.to_be_deleted = False ' \
        'AND solutions.created > %s' \
        ' AND solutions.is_closed = False ' \
        'AND s_question.is_closed = False ' \
        'AND solutions.to_be_deleted = False ' \
        'RETURN solutions, NULL AS questions, ' \
        'solutions.created AS created, s_question AS s_question' \
        % (username, then, username, then)
    news = []
    res, _ = db.cypher_query(query)
    # Profiled with ~50 objects and it was still performing under 1 ms.
    # By the time sorting in python becomes an issue the above mentioned
    # ticket should be resolved.
    res = sorted(res, key=attrgetter('created'), reverse=True)[:5]
    page = api.paginate_queryset(res)
    for row in page:
        news_article = None
        if row.questions is not None:
            row.questions.pull()
            news_article = QuestionSerializerNeo(Question.inflate(
                row.questions),
                                                 context={
                                                     'request': request
                                                 }).data
        elif row.solutions is not None:
            row.s_question.pull()
            row.solutions.pull()
            question_data = QuestionSerializerNeo(
                Question.inflate(row.s_question)).data
            news_article = SolutionSerializerNeo(Solution.inflate(
                row.solutions),
                                                 context={
                                                     'request': request
                                                 }).data
            news_article['question'] = question_data
        news.append(news_article)
    return api.get_paginated_response(news)
 def test_connect_with_existing_element(self):
     question = Question(title="Hello this is my question",
                         content="This is content",
                         external_location_id=self.wixom['place_id'],
                         owner_username=self.pleb.username).save()
     data = {
         'element_id': self.wixom['place_id'],
         'location': self.location
     }
     res = connect_location_to_element.apply_async(kwargs=data)
     while not res.ready():
         time.sleep(1)
     self.assertTrue(res.result)
     self.assertIsInstance(res.result, Question)
     question.delete()
Exemple #10
0
 def setUp(self):
     self.unit_under_test_name = 'pleb'
     self.email = "*****@*****.**"
     self.email = "*****@*****.**"
     self.email2 = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.pleb2 = create_user_util_test(self.email2)
     self.user2 = User.objects.get(email=self.email2)
     self.title = str(uuid1())
     self.question = Question(content="Hey I'm a question",
                              title=self.title,
                              owner_username=self.pleb.username).save()
     self.solution = Solution(content="This is a test solution",
                              owner_username=self.pleb.username,
                              parent_id=self.question.object_uuid).save()
     self.solution.owned_by.connect(self.pleb)
     self.question.owned_by.connect(self.pleb)
     try:
         Tag.nodes.get(name='taxes')
     except DoesNotExist:
         Tag(name='taxes').save()
     try:
         Tag.nodes.get(name='fiscal')
     except DoesNotExist:
         Tag(name='fiscal').save()
     try:
         Tag.nodes.get(name='environment')
     except DoesNotExist:
         Tag(name='environment').save()
Exemple #11
0
 def setUp(self):
     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(owner_username=self.pleb.username,
                              title=str(uuid1())).save()
 def setUp(self):
     self.tags = []
     self.email = "*****@*****.**"
     create_user_util_test(self.email)
     self.pleb = Pleb.nodes.get(email=self.email)
     self.user = User.objects.get(email=self.email)
     self.question = Question(title=str(uuid1())).save()
Exemple #13
0
 def get(self, request, question_uuid=None, slug=None):
     if question_uuid is not None:
         try:
             question = Question.get(question_uuid)
         except (DoesNotExist, Question.DoesNotExist):
             return redirect('404_Error')
         if question.owner_username != request.user.username \
                 and self.template_name == "questions/edit.html":
             return redirect('401_Error')
         return render(
             request, self.template_name, {
                 'sort_by':
                 'uuid',
                 'authors':
                 question.get_conversation_authors(),
                 'solution_placeholder':
                 render_to_string("solutions/placeholder.html"),
                 'question':
                 QuestionSerializerNeo(question,
                                       context={
                                           "request": request
                                       }).data,
             })
     else:
         return render(
             request, self.template_name, {
                 'question_placeholder':
                 render_to_string('questions/placeholder.html')
             })
Exemple #14
0
 def get_focused_on(self, request=None):
     from api.neo_models import SBObject
     from sb_quests.neo_models import Position
     from sb_quests.serializers import PositionSerializer
     from sb_tags.neo_models import Tag
     from sb_tags.serializers import TagSerializer
     from sb_questions.neo_models import Question
     from sb_questions.serializers import QuestionSerializerNeo
     query = 'MATCH (a:Mission {object_uuid: "%s"})-[:FOCUSED_ON]->(b)' \
             'RETURN b' % self.object_uuid
     res, _ = db.cypher_query(query)
     if res.one:
         child_label = SBObject.inflate(res.one).get_child_label()
         if child_label == "Position":
             return PositionSerializer(Position.inflate(res.one),
                                       context={
                                           'request': request
                                       }).data
         elif child_label == "Tag":
             return TagSerializer(Tag.inflate(res.one),
                                  context={
                                      'request': request
                                  }).data
         elif child_label == "Question":
             return QuestionSerializerNeo(Question.inflate(res.one),
                                          context={
                                              'request': request
                                          }).data
     else:
         return None
Exemple #15
0
 def setUp(self):
     self.unit_under_test_name = 'sbcontent'
     self.email = "*****@*****.**"
     create_user_util_test(self.email)
     self.pleb = Pleb.nodes.get(email=self.email)
     self.user = User.objects.get(email=self.email)
     self.url = "http://testserver"
     self.flag = Flag().save()
     self.question = Question(owner_username=self.pleb.username,
                              title=str(uuid1())).save()
     self.question.owned_by.connect(self.pleb)
     self.question.flags.connect(self.flag)
     self.vote_rel = self.question.council_votes.connect(self.pleb)
     self.vote_rel.active = True
     self.vote_rel.vote_type = True
     self.vote_rel.save()
Exemple #16
0
 def test_multiple_votes(self):
     vote = Vote(vote_type=0, reputation_change=-2).save()
     question = Question(object_uuid=str(uuid1()),
                         title=str(uuid1()),
                         owner_username=self.pleb.username).save()
     question.owned_by.connect(self.pleb)
     question.last_votes.connect(vote)
     question.first_votes.connect(vote)
     vote.vote_on.connect(question)
     vote.owned_by.connect(self.pleb)
     task_data = {
         'node_id': str(uuid1()),
         'voter': self.pleb.username,
         'parent_object': question.object_uuid,
         'vote_type': 1
     }
     res = create_vote_node.apply_async(kwargs=task_data)
     while not res.ready():
         time.sleep(1)
     self.assertTrue(res.result)
     test_vote = Vote.nodes.get(object_uuid=task_data['node_id'])
     self.assertEqual(test_vote.reputation_change, 5)
     self.assertTrue(question.first_votes.is_connected(vote))
     self.assertFalse(question.last_votes.is_connected(vote))
     self.assertTrue(question.last_votes.is_connected(test_vote))
Exemple #17
0
    def repopulate_elasticsearch(self):
        # Profiles
        skip = 0
        while True:
            query = 'MATCH (profile:Pleb) RETURN DISTINCT profile ' \
                    'SKIP %s LIMIT 25' % skip
            skip += 24
            res, _ = db.cypher_query(query)
            if not res.one:
                break
            for profile in [Pleb.inflate(row[0]) for row in res]:
                update_search_object.apply_async(kwargs={
                    "object_uuid": profile.object_uuid,
                    "label": "pleb"
                })

        # Questions
        skip = 0
        while True:
            query = 'MATCH (question:Question) RETURN DISTINCT question ' \
                    'SKIP %s LIMIT 25' % skip
            skip += 24
            res, _ = db.cypher_query(query)
            if not res.one:
                break
            for question in [Question.inflate(row[0]) for row in res]:
                update_search_object.apply_async(kwargs={
                    "object_uuid": question.object_uuid,
                    "label": "question"
                })

        # Quests
        skip = 0
        while True:
            query = 'MATCH (quest:Quest) RETURN DISTINCT quest ' \
                    'SKIP %s LIMIT 25' % skip
            skip += 24
            res, _ = db.cypher_query(query)
            if not res.one:
                break
            for quest in [Quest.inflate(row[0]) for row in res]:
                update_search_object.apply_async(kwargs={
                    "object_uuid": quest.object_uuid,
                    "label": "quest"
                })
        # Missions
        skip = 0
        while True:
            query = 'MATCH (mission:Mission) RETURN DISTINCT mission ' \
                    'SKIP %s LIMIT 25' % skip
            skip += 24
            res, _ = db.cypher_query(query)
            if not res.one:
                break
            for mission in [Mission.inflate(row[0]) for row in res]:
                update_search_object.apply_async(kwargs={
                    "object_uuid": mission.object_uuid,
                    "label": "mission"
                })
Exemple #18
0
    def migrate_questions(self):
        query = 'MATCH (a:Question) RETURN a'

        res, _ = db.cypher_query(query)
        for question in [Question.inflate(row[0]) for row in res]:
            spawn_task(task_func=create_question_summary_task,
                       task_param={'object_uuid': question.object_uuid})
        self.stdout.write("completed question migration\n", ending='')
 def test_update_weight_relationship(self):
     sb_object = Question(title="Hello I'm testing weights on my "
                          "question!").save()
     rel = self.pleb.object_weight.connect(sb_object)
     rel.weight = 0
     rel.save()
     res = self.pleb.update_weight_relationship(sb_object, 'seen_search')
     self.assertEqual(res, 5)
 def setUp(self):
     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(object_uuid=str(uuid1()),
                              content="This is some test content "
                                      "that I just created.",
                              title=str(uuid1())).save()
Exemple #21
0
 def setUp(self):
     self.email = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.question = Question(content='test',
                              owner_username=self.pleb.username,
                              wall_owner_username=self.pleb.username,
                              title=str(uuid1())).save()
     self.question.owned_by.connect(self.pleb)
Exemple #22
0
    def test_list_least_recent_ordering(self):
        query = "MATCH (n:SBContent) OPTIONAL MATCH " \
                "(n:SBContent)-[r]-() DELETE n,r"

        res, _ = db.cypher_query(query)
        self.client.force_authenticate(user=self.user)
        question = Question(title='test_title',
                            content='test_content',
                            owner_username=self.pleb2.username).save()
        question.owned_by.connect(self.pleb2)
        # Solution 1
        solution = Solution(content='test_content',
                            owner_username=self.pleb2.username,
                            parent_id=question.object_uuid).save()
        solution.owned_by.connect(self.pleb)
        question.solutions.connect(solution)
        # Solution 2
        solution2 = Solution(content='test_content22',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution2.owned_by.connect(self.pleb)
        question.solutions.connect(solution2)
        # Solution 3
        solution3 = Solution(content='test_content33',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution3.owned_by.connect(self.pleb)
        question.solutions.connect(solution3)
        # Solution 4
        solution4 = Solution(content='test_content44',
                             owner_username=self.pleb2.username,
                             parent_id=question.object_uuid).save()
        solution4.owned_by.connect(self.pleb2)
        question.solutions.connect(solution4)
        # Solution 5
        solution5 = Solution(content='test_content55',
                             owner_username=self.pleb.username,
                             parent_id=question.object_uuid).save()
        solution5.owned_by.connect(self.pleb)
        question.solutions.connect(solution5)
        url = reverse('question-solutions',
                      kwargs={"object_uuid": question.object_uuid}) \
            + "?limit=5&offset=0&expand=true&ordering=created"
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 5)
        self.assertEqual(response.data['results'][0]['id'],
                         solution.object_uuid)
        self.assertEqual(response.data['results'][1]['id'],
                         solution2.object_uuid)
        self.assertEqual(response.data['results'][2]['id'],
                         solution3.object_uuid)
        self.assertEqual(response.data['results'][3]['id'],
                         solution4.object_uuid)
        self.assertEqual(response.data['results'][4]['id'],
                         solution5.object_uuid)
Exemple #23
0
    def test_get_question_list_view_success(self):
        self.client.force_authenticate(user=self.user)
        question = Question(object_uuid=str(uuid1()),
                            content='test',
                            title=str(uuid1()),
                            owner_username=self.pleb.username).save()
        question.owned_by.connect(self.pleb)

        res = self.client.get('/conversations/')
        self.assertTrue(res.status_code, status.HTTP_200_OK)
Exemple #24
0
 def setUp(self):
     cache.clear()
     self.email = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.user = User.objects.get(email=self.email)
     self.pleb.first_name = 'Tyler'
     self.pleb.last_name = 'Wiersing'
     self.pleb.save()
     self.question = Question(title=str(uuid1()),
                              content="some test content").save()
Exemple #25
0
 def setUp(self):
     self.factory = APIRequestFactory()
     self.unit_under_test_name = 'pleb'
     self.email = "*****@*****.**"
     self.pleb = create_user_util_test(self.email)
     self.title = str(uuid1())
     self.question = Question(content=str(uuid1()),
                              title=self.title,
                              owner_username=self.pleb.username).save()
     self.question.owned_by.connect(self.pleb)
Exemple #26
0
 def setUp(self):
     self.unit_under_test_name = 'pleb'
     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.solution = Solution(content="This is a test solution",
                              owner_username=self.pleb.username).save()
Exemple #27
0
 def get_mission(cls, object_uuid, request):
     from sb_solutions.neo_models import Solution
     from sb_questions.neo_models import Question
     comment = Comment.nodes.get(object_uuid=object_uuid)
     if comment.parent_type == "solution":
         return Solution.get_mission(comment.parent_id, request)
     elif comment.parent_type == "question":
         return Question.get_mission(comment.parent_id, request)
     else:
         return None
 def test_get_mission(self):
     quest = Quest(owner_username=self.pleb.username).save()
     quest.owner.connect(self.pleb)
     mission = Mission(owner_username=self.pleb.username).save()
     quest.missions.connect(mission)
     mission.associated_with.connect(self.question)
     res = Question.get_mission(self.question.object_uuid)
     self.assertEqual(res['id'], mission.object_uuid)
     mission.delete()
     quest.delete()
Exemple #29
0
 def migrate_to_new_editor(self):
     skip = 0
     while True:
         query = 'MATCH (m:Mission) RETURN m SKIP %s LIMIT 25' % skip
         skip += 24
         res, _ = db.cypher_query(query)
         if not res.one:
             break
         for mission in [Mission.inflate(row[0]) for row in res]:
             rendered = render_content(
                 markdown.markdown(mission.epic.replace(
                     '&gt;', '>')).replace('<a', '<a target="_blank"'))
             mission.epic = rendered
             mission.temp_epic = rendered
             mission.save()
     skip = 0
     while True:
         query = 'MATCH (m:Question) RETURN m SKIP %s LIMIT 25' % skip
         skip += 24
         res, _ = db.cypher_query(query)
         if not res.one:
             break
         for question in [Question.inflate(row[0]) for row in res]:
             rendered = render_content(
                 markdown.markdown(question.content.replace(
                     '&gt;', '>')).replace('<a', '<a target="_blank"'))
             question.content = rendered
             question.save()
     skip = 0
     while True:
         query = 'MATCH (m:Solution) RETURN m SKIP %s LIMIT 25' % skip
         skip += 24
         res, _ = db.cypher_query(query)
         if not res.one:
             break
         for solution in [Solution.inflate(row[0]) for row in res]:
             rendered = render_content(
                 markdown.markdown(solution.content.replace(
                     '&gt;', '>')).replace('<a', '<a target="_blank"'))
             solution.content = rendered
             solution.save()
     skip = 0
     while True:
         query = 'MATCH (m:Update) RETURN m SKIP %s LIMIT 25' % skip
         skip += 24
         res, _ = db.cypher_query(query)
         if not res.one:
             break
         for update in [Update.inflate(row[0]) for row in res]:
             rendered = render_content(
                 markdown.markdown(update.content.replace(
                     '&gt;', '>')).replace('<a', '<a target="_blank"'))
             update.content = rendered
             update.save()
     cache.set("migrated_to_new_editor", True)
Exemple #30
0
    def test_get_question_edit(self):
        question = Question(object_uuid=str(uuid1()),
                            content='test',
                            title=str(uuid1()),
                            owner_username=self.pleb.username).save()

        question.owned_by.connect(self.pleb)
        res = self.client.get(
            reverse("question-edit",
                    kwargs={"question_uuid": question.object_uuid}))
        self.assertTrue(res.status_code, status.HTTP_200_OK)