Ejemplo n.º 1
0
    def setUp(self):
        user_profile1 = UserProfileFactory()
        user_profile2 = UserProfileFactory()

        # shared answer
        self.user1_answered_question1, _ = (shared_answered_questions_factory(
            user_profile1=user_profile1,
            user_profile2=user_profile2,
        ))

        # another shared answer
        self.user1_answered_question2, _ = (shared_answered_questions_factory(
            user_profile1=user_profile1,
            user_profile2=user_profile2,
        ))

        # a non-shared answer
        shared_answered_questions_factory(
            user_profile1=user_profile1,
            user_profile2=user_profile2,
            different_answers=True,
        )

        self.user1_answered_qs = user_profile1.answered_questions.all()
        self.user2_answered_qs = user_profile2.answered_questions.all()
Ejemplo n.º 2
0
    def test_comparison_returns_answered_questions_for_users_with_shared_qs(
            self):
        user_profile1 = UserProfileFactory()
        user_profile2 = UserProfileFactory()
        user1_answered_question1, user2_answered_question1 = (
            shared_answered_questions_factory(
                user_profile1=user_profile1,
                user_profile2=user_profile2,
            ))
        user1_answered_question2, user2_answered_question2 = (
            shared_answered_questions_factory(
                user_profile1=user_profile1,
                user_profile2=user_profile2,
            ))
        shared_answered_questions_factory(
            user_profile1=user_profile1,
            user_profile2=user_profile2,
            different_answers=True,
        )

        comparison = UserComparison(
            source_user_profile=user_profile1,
            target_user_profile=user_profile2,
        )
        self.assertEqual(list(comparison.sources_shared_answered_questions()),
                         [user1_answered_question1, user1_answered_question2])
        self.assertEqual(list(comparison.targets_shared_answered_questions()),
                         [user2_answered_question1, user2_answered_question2])
Ejemplo n.º 3
0
 def test_will_error_if_not_user_requesting(self, _):
     user_profile = UserProfileFactory()
     user_profile_requesting = UserProfileFactory()
     self.client.authenticate(user_profile_requesting.user)
     result = self.compute_doppelganger({'userProfileId': user_profile.id})
     self.assertEqual([str(e) for e in result.errors],
                      [no_permission_error])
Ejemplo n.º 4
0
    def test_will_return_score_and_user_if_doppelganger_exists(self, _):
        user_profile = UserProfileFactory()
        doppelganger = UserProfileFactory()
        self.client.authenticate(user_profile.user)
        expected_result = {
            'userProfile': {
                'user': {
                    'username': doppelganger.user.username,
                },
            },
            'doppelgangerInfo': {
                'score': 1.0,
            },
        }

        with self.subTest('with explicit variables passed'):
            result = self.compute_doppelganger(
                {'userProfileId': user_profile.id})

            self.assertIsNone(result.errors)
            self.assertEqual(result.data[self.op_name], expected_result)

        with self.subTest('with implicit user through authentication'):
            result = self.compute_doppelganger()

            self.assertIsNone(result.errors)
            self.assertEqual(result.data[self.op_name], expected_result)
Ejemplo n.º 5
0
class UsernameUpdateFormTestCase(TestCase):
    """Tests for the ``UsernameUpdateForm`` form class."""
    longMessage = True

    def setUp(self):
        super(UsernameUpdateFormTestCase, self).setUp()
        self.data = {
            'username': '******',
        }
        self.profile = UserProfileFactory()

    def test_form(self):
        """Should save the username."""
        form = UsernameUpdateForm(instance=self.profile, data=self.data)
        self.assertTrue(form.is_valid(),
                        msg=('Errors: {0}'.format(form.errors.items())))
        instance = form.save()
        self.assertEqual(instance.username, 'foobar')

    def test_unique(self):
        """Checks if username exists before saving."""
        self.profile.username = '******'
        self.profile.save()
        form = UsernameUpdateForm(instance=self.profile, data=self.data)
        self.assertFalse(form.is_valid())
        self.assertTrue('username' in form.errors)
Ejemplo n.º 6
0
 def setUp(self):
     super(CustomCheckinCreateFormTestCase, self).setUp()
     self.profile = UserProfileFactory()
     self.place = PlaceFactory()
     self.data = {
         'user_name': 'Foobar',
         'lat': '1.3568494',
         'lng': '103.9478796',
     }
Ejemplo n.º 7
0
 def test_comparison_returns_answered_questions_for_users_with_no_questions(
         self):
     user_profile1 = UserProfileFactory()
     user_profile2 = UserProfileFactory()
     comparison = UserComparison(
         source_user_profile=user_profile1,
         target_user_profile=user_profile2,
     )
     self.assertEqual(list(comparison.sources_shared_answered_questions()),
                      [])
     self.assertEqual(list(comparison.targets_shared_answered_questions()),
                      [])
Ejemplo n.º 8
0
class UserProfileTestCase(TestCase):
    """Tests for the ``UserProfile`` model."""
    def setUp(self):
        self.profile = UserProfileFactory()

    def test_model(self):
        self.assertTrue(self.profile.pk)

    def test_get_absolute_url(self):
        self.profile.username = '******'
        self.profile.save()
        result = self.profile.get_absolute_url()
        self.assertEqual(result, '/profile/foobar/')
Ejemplo n.º 9
0
class UserProfileTestCase(TestCase):
    """Tests for the ``UserProfile`` model."""
    def setUp(self):
        self.profile = UserProfileFactory()

    def test_model(self):
        self.assertTrue(self.profile.pk)

    def test_get_absolute_url(self):
        self.profile.username = '******'
        self.profile.save()
        result = self.profile.get_absolute_url()
        self.assertEqual(result, '/profile/foobar/')
Ejemplo n.º 10
0
    def test_handler(self):
        user = UserFactory()
        profile = UserProfileFactory(user=user)

        #empty response
        facebook_extra_values(self, user, {}, {})

        # This is how the response from facebook looks like. It contains many
        # more values but we only use these few.
        response = {
            'birthday': '09/08/1982',
            'gender': 'male',
            'username': '******',
            'first_name': 'Foo',
            'last_name': 'Bar',
            'location': {
                'id': '101883206519751',
                'name': 'Singapore, Singapore',
            },
        }
        facebook_extra_values(self, user, response, {})

        profile = UserProfile.objects.get(pk=profile.pk)
        self.assertEqual(profile.birthday,
                         timezone.datetime(1982, 9, 8).date(),
                         msg=('Should set the birthday correctly'))
        self.assertEqual(profile.gender,
                         'male',
                         msg=('Should set the gender correctly'))
        self.assertEqual(profile.location,
                         'Singapore, Singapore',
                         msg=('Should set the location correctly'))
Ejemplo n.º 11
0
    def test_will_return_none_if_no_doppelganger_exists(self, _):
        user_profile = UserProfileFactory(user__username='******')
        self.client.authenticate(user_profile.user)

        result = self.compute_doppelganger({'userProfileId': user_profile.id})

        self.assertIsNone(result.errors)
        self.assertEqual(result.data[self.op_name], None)
Ejemplo n.º 12
0
class PublicProfileViewTestCase(ViewTestMixin, TestCase):
    """Tests for the ``PublicProfileView`` view class."""
    longMessage = True

    def setUp(self):
        self.profile = UserProfileFactory()
        self.profile.username = '******'
        self.profile.save()

    def get_view_name(self):
        return 'user_profile_public_profile'

    def get_view_kwargs(self):
        return {'username': self.profile.username, }

    def test_callable(self):
        """Should be callable."""
        self.should_be_callable_when_anonymous()
Ejemplo n.º 13
0
 def test_will_not_error_if_superuser_requesting(self, _):
     superuser = get_user_model().objects.create_superuser(
         username='******',
         email='*****@*****.**',
         password='******')
     user_profile = UserProfileFactory()
     self.client.authenticate(superuser)
     result = self.compute_doppelganger({'userProfileId': user_profile.id})
     self.assertIsNone(result.errors)
Ejemplo n.º 14
0
 def setUp(self):
     super(CustomCheckinCreateFormTestCase, self).setUp()
     self.profile = UserProfileFactory()
     self.place = PlaceFactory()
     self.data = {
         'user_name': 'Foobar',
         'lat': '1.3568494',
         'lng': '103.9478796',
     }
Ejemplo n.º 15
0
class CustomCheckinCreateFormTestCase(TestCase):
    """Tests for the ``CustomCheckinCreateForm`` form class."""
    def setUp(self):
        super(CustomCheckinCreateFormTestCase, self).setUp()
        self.profile = UserProfileFactory()
        self.place = PlaceFactory()
        self.data = {
            'user_name': 'Foobar',
            'lat': '1.3568494',
            'lng': '103.9478796',
        }

    def test_adds_user_name_field(self):
        """Should add the user_name field if the user has no display name."""
        form = CustomCheckinCreateForm(user=self.profile.user,
                                       place=self.place,
                                       data={})
        self.assertTrue('user_name' in form.fields)

    def test_does_not_add_user_name_field(self):
        """Shouldn't add user_name field if the user has a display name."""
        self.profile.display_name = 'Foobar'
        self.profile.save()
        form = CustomCheckinCreateForm(user=self.profile.user,
                                       place=self.place,
                                       data={})
        self.assertFalse('user_name' in form.fields)

    def test_save(self):
        """
        Should set the user's display name when saved.

        That way the user only has to set it once on the first check-in.

        """
        self.assertEqual(self.profile.display_name, '')
        form = CustomCheckinCreateForm(user=self.profile.user,
                                       place=self.place,
                                       data=self.data)
        self.assertTrue(form.is_valid(),
                        msg=('Errors: {0}'.format(form.errors.items())))
        form.save()
        profile = UserProfile.objects.get(pk=self.profile.pk)
        self.assertEqual(profile.display_name, 'Foobar')
Ejemplo n.º 16
0
 def test_will_error_if_not_user_requesting(self):
     user_profile_requesting = UserProfileFactory()
     self.client.authenticate(user_profile_requesting.user)
     result = self.user_comparison({
         'sourceUserProfileId':
         self.user_profile.id,
         'targetUserProfileId':
         self.target_user_profile.id,
     })
     self.assertEqual([str(e) for e in result.errors],
                      [no_permission_error])
Ejemplo n.º 17
0
class PublicProfileViewTestCase(ViewTestMixin, TestCase):
    """Tests for the ``PublicProfileView`` view class."""
    longMessage = True

    def setUp(self):
        self.profile = UserProfileFactory()
        self.profile.username = '******'
        self.profile.save()

    def get_view_name(self):
        return 'user_profile_public_profile'

    def get_view_kwargs(self):
        return {
            'username': self.profile.username,
        }

    def test_callable(self):
        """Should be callable."""
        self.should_be_callable_when_anonymous()
Ejemplo n.º 18
0
class CustomCheckinCreateFormTestCase(TestCase):
    """Tests for the ``CustomCheckinCreateForm`` form class."""
    def setUp(self):
        super(CustomCheckinCreateFormTestCase, self).setUp()
        self.profile = UserProfileFactory()
        self.place = PlaceFactory()
        self.data = {
            'user_name': 'Foobar',
            'lat': '1.3568494',
            'lng': '103.9478796',
        }

    def test_adds_user_name_field(self):
        """Should add the user_name field if the user has no display name."""
        form = CustomCheckinCreateForm(
            user=self.profile.user, place=self.place, data={})
        self.assertTrue('user_name' in form.fields)

    def test_does_not_add_user_name_field(self):
        """Shouldn't add user_name field if the user has a display name."""
        self.profile.display_name = 'Foobar'
        self.profile.save()
        form = CustomCheckinCreateForm(
            user=self.profile.user, place=self.place, data={})
        self.assertFalse('user_name' in form.fields)

    def test_save(self):
        """
        Should set the user's display name when saved.

        That way the user only has to set it once on the first check-in.

        """
        self.assertEqual(self.profile.display_name, '')
        form = CustomCheckinCreateForm(
            user=self.profile.user, place=self.place, data=self.data)
        self.assertTrue(form.is_valid(), msg=(
            'Errors: {0}'.format(form.errors.items())))
        form.save()
        profile = UserProfile.objects.get(pk=self.profile.pk)
        self.assertEqual(profile.display_name, 'Foobar')
Ejemplo n.º 19
0
class UsernameUpdateViewTestCase(ViewTestMixin, TestCase):
    """Tests for the ``UniqueNameUpdateView`` view class."""
    longMessage = True

    def setUp(self):
        super(UsernameUpdateViewTestCase, self).setUp()
        self.profile = UserProfileFactory()

    def get_view_name(self):
        return 'user_profile_username_update'

    def test_login_required(self):
        """Should redirect to login if anonymous."""
        self.should_redirect_to_login_when_anonymous()

    def test_callable(self):
        """Should be callable if user is authenticated."""
        self.should_be_callable_when_authenticated(self.profile.user)

    def test_not_callable(self):
        """
        Should redirect to profile if user has already taken his username.

        """
        self.profile.username = '******'
        self.profile.save()
        self.login(self.profile.user)
        resp = self.client.get(self.get_url())
        self.assertRedirects(resp, reverse('user_profile_update'))

    def test_save(self):
        """Should save the username on a POST request."""
        self.login(self.profile.user)
        self.client.post(self.get_url(), data={'username': '******'})
        profile = UserProfile.objects.get(pk=self.profile.pk)
        self.assertEqual(profile.username, 'foobar')
Ejemplo n.º 20
0
class UsernameUpdateViewTestCase(ViewTestMixin, TestCase):
    """Tests for the ``UniqueNameUpdateView`` view class."""
    longMessage = True

    def setUp(self):
        super(UsernameUpdateViewTestCase, self).setUp()
        self.profile = UserProfileFactory()

    def get_view_name(self):
        return 'user_profile_username_update'

    def test_login_required(self):
        """Should redirect to login if anonymous."""
        self.should_redirect_to_login_when_anonymous()

    def test_callable(self):
        """Should be callable if user is authenticated."""
        self.should_be_callable_when_authenticated(self.profile.user)

    def test_not_callable(self):
        """
        Should redirect to profile if user has already taken his username.

        """
        self.profile.username = '******'
        self.profile.save()
        self.login(self.profile.user)
        resp = self.client.get(self.get_url())
        self.assertRedirects(resp, reverse('user_profile_update'))

    def test_save(self):
        """Should save the username on a POST request."""
        self.login(self.profile.user)
        self.client.post(self.get_url(), data={'username': '******'})
        profile = UserProfile.objects.get(pk=self.profile.pk)
        self.assertEqual(profile.username, 'foobar')
Ejemplo n.º 21
0
class UsernameUpdateFormTestCase(TestCase):
    """Tests for the ``UsernameUpdateForm`` form class."""
    longMessage = True

    def setUp(self):
        super(UsernameUpdateFormTestCase, self).setUp()
        self.data = {'username': '******', }
        self.profile = UserProfileFactory()

    def test_form(self):
        """Should save the username."""
        form = UsernameUpdateForm(instance=self.profile, data=self.data)
        self.assertTrue(form.is_valid(), msg=(
            'Errors: {0}'.format(form.errors.items())))
        instance = form.save()
        self.assertEqual(instance.username, 'foobar')

    def test_unique(self):
        """Checks if username exists before saving."""
        self.profile.username = '******'
        self.profile.save()
        form = UsernameUpdateForm(instance=self.profile, data=self.data)
        self.assertFalse(form.is_valid())
        self.assertTrue('username' in form.errors)
Ejemplo n.º 22
0
    def test_finds_doppelganger_with_most_similar_answers(self):
        question = QuestionFactory(with_answers=True)
        non_shared_answer = question.answers.first()
        shared_answer = question.answers.last()

        user_profile, doppelganger = [
            AnsweredQuestionFactory(question=question,
                                    answer=shared_answer).user_profile
            for _ in range(2)
        ]

        # Create an answered ques. with a profile that won't be a doppelganger
        AnsweredQuestionFactory(
            question=question,
            answer=non_shared_answer,
        )

        # Create a profile with no answered questions that won't be a
        # doppelganger
        UserProfileFactory()

        self.assertEqual(get_doppelganger_and_score(user_profile),
                         (doppelganger, 1))
Ejemplo n.º 23
0
 def test_if_no_good_candidates_doppelganger_is_none(self):
     user_profile = UserProfileFactory()
     UserProfileFactory()
     self.assertIsNone(get_doppelganger_and_score(user_profile))
Ejemplo n.º 24
0
 def setUp(self):
     super(UserProfileUpdateFormTestCase, self).setUp()
     self.data = {
         'timezone': 'Asia/Singapore',
     }
     self.profile = UserProfileFactory()
Ejemplo n.º 25
0
 def setUp(self):
     super(UsernameUpdateFormTestCase, self).setUp()
     self.data = {
         'username': '******',
     }
     self.profile = UserProfileFactory()
Ejemplo n.º 26
0
 def setUp(self):
     self.profile = UserProfileFactory()
     self.profile.username = '******'
     self.profile.save()
Ejemplo n.º 27
0
 def setUp(self):
     self.profile = UserProfileFactory()
     self.user = self.profile.user
     self.answer = AnswerFactory()
     self.question = self.answer.question
Ejemplo n.º 28
0
    def test_omits_answered_questions_when_arg_passed(self):
        # Non answered question
        question = QuestionFactory()
        answer1 = AnswerFactory(question=question)
        answer2 = AnswerFactory(question=question)

        # answered question
        question2 = QuestionFactory()
        answer3 = AnswerFactory(question=question2)
        AnswerFactory(question=question2)

        # authenticate user with answered question
        user_profile = UserProfileFactory()
        AnsweredQuestionFactory(
            user_profile=user_profile,
            question=question2,
            answer=answer3,
        )
        user = user_profile.user
        self.client.authenticate(user)

        result = self.client.execute(
            '''
            query {
                questions(omitAnsweredQuestions: true) {
                    edges {
                        node {
                            id
                            pk
                            text
                            answers {
                                pk
                                text
                            }
                        }
                    }
                }
            }
            ''', )

        self.assertIsNone(result.errors)
        self.assertEqual(result.data[self.op_name]['edges'], [{
            'node': {
                'id':
                to_global_id(QuestionType._meta.name, question.id),
                'pk':
                question.id,
                'text':
                question.text,
                'answers': [
                    {
                        'pk': answer2.id,
                        'text': answer2.text
                    },
                    {
                        'pk': answer1.id,
                        'text': answer1.text
                    },
                ],
            }
        }])
Ejemplo n.º 29
0
 def setUp(self):
     super(UsernameUpdateFormTestCase, self).setUp()
     self.data = {'username': '******', }
     self.profile = UserProfileFactory()
Ejemplo n.º 30
0
 def setUp(self):
     super(UsernameUpdateViewTestCase, self).setUp()
     self.profile = UserProfileFactory()
Ejemplo n.º 31
0
 def test_will_error_if_not_logged_in(self, _):
     user_profile = UserProfileFactory()
     result = self.compute_doppelganger({'userProfileId': user_profile.id})
     self.assertEqual([str(e) for e in result.errors],
                      [no_permission_error])
Ejemplo n.º 32
0
 def setUp(self):
     self.profile = UserProfileFactory()
     self.profile.username = '******'
     self.profile.save()
Ejemplo n.º 33
0
 def setUp(self):
     super(UsernameUpdateViewTestCase, self).setUp()
     self.profile = UserProfileFactory()
Ejemplo n.º 34
0
 def setUp(self):
     self.profile = UserProfileFactory()
Ejemplo n.º 35
0
 def setUp(self):
     self.profile = UserProfileFactory()
Ejemplo n.º 36
0
 def test_view(self):
     UserProfileFactory()
     UserProfileFactory()
     resp = self.client.get(self.get_url())
     self.assertEqual(resp.content, '"2"')
Ejemplo n.º 37
0
 def setUp(self):
     self.user_profile = UserProfileFactory()
     self.target_user_profile = UserProfileFactory()
Ejemplo n.º 38
0
    def handle(self, *args, **options):
        # question
        create_questions_and_answers(question_answers)

        # user
        user = get_user_model().objects.create_superuser(
            username='******',
            email='*****@*****.**',
            password='******')
        UserProfileFactory(user=user)

        profile_with_doppelganger = UserProfileFactory(
            user__email='*****@*****.**',
            user__password='******',
        )
        AnsweredQuestionFactory(
            user_profile=profile_with_doppelganger,
            question=Question.objects.get(text=BEST_VILLAIN),
            answer=Answer.objects.get(text='Darth Vader'),
        )
        AnsweredQuestionFactory(
            user_profile=profile_with_doppelganger,
            question=Question.objects.get(text=BEST_DJ),
            answer=Answer.objects.get(text='A-Trak'),
        )
        AnsweredQuestionFactory(
            user_profile=profile_with_doppelganger,
            question=Question.objects.get(text=BEST_SEINFELD_CHARACTER),
            answer=Answer.objects.get(text='Jerry'),
        )

        doppelganger_profile = UserProfileFactory(
            user__email='*****@*****.**',
            user__password='******',
        )
        AnsweredQuestionFactory(
            user_profile=doppelganger_profile,
            question=Question.objects.get(text=BEST_VILLAIN),
            answer=Answer.objects.get(text='Darth Vader'),
        )
        AnsweredQuestionFactory(
            user_profile=doppelganger_profile,
            question=Question.objects.get(text=BEST_DJ),
            answer=Answer.objects.get(text='Diplo'),
        )
        AnsweredQuestionFactory(
            user_profile=doppelganger_profile,
            question=Question.objects.get(text=BEST_SEINFELD_CHARACTER),
            answer=Answer.objects.get(text='Jerry'),
        )

        not_doppelganger_profile = UserProfileFactory(
            user__email='*****@*****.**',
            user__password='******',
        )
        AnsweredQuestionFactory(
            user_profile=not_doppelganger_profile,
            question=Question.objects.get(text=BEST_VILLAIN),
            answer=Answer.objects.get(text='Darth Maul'),
        )
        AnsweredQuestionFactory(
            user_profile=not_doppelganger_profile,
            question=Question.objects.get(text=BEST_DJ),
            answer=Answer.objects.get(text='Diplo'),
        )
        AnsweredQuestionFactory(
            user_profile=not_doppelganger_profile,
            question=Question.objects.get(text=BEST_SEINFELD_CHARACTER),
            answer=Answer.objects.get(text='George'),
        )

        UserProfileFactory(
            user__email='*****@*****.**',
            user__password='******',
        )
Ejemplo n.º 39
0
 def setUp(self):
     super(PlaceCreateFormTestCase, self).setUp()
     self.type = PlaceTypeFactory(name='Basketball')
     self.profile = UserProfileFactory()