예제 #1
0
 def setUp(self):
     super(_TestSafeStateMatchBase, self).setUp()
     candidate = CANDIDATE_CLINTON
     self.state_safe = StateFactory.create(
         safe_for=candidate,
         safe_rank=1
     )
     self.user = UserFactory.create(
         profile__state=self.state_safe.name,
         profile__preferred_candidate=candidate)
     # Create friends in safe states
     for i in range(10):
         state = StateFactory.create(
             safe_for=self.user.profile.preferred_candidate,
             safe_rank=i+2)  # +2 because state_safe is rank 1
         friend = UserFactory.create(profile__state=state.name)
         self.user.profile.friends.add(friend.profile)
     # Create friends in swing states
     self.swing_state_1 = StateFactory.create(tipping_point_rank=1)
     swing_user_1 = UserFactory.create(
         profile__state=self.swing_state_1.name,
         profile__preferred_candidate=CANDIDATE_JOHNSON)
     self.user.profile.friends.add(swing_user_1.profile)
     self.swing_state_2 = StateFactory.create(tipping_point_rank=2)
     swing_user_2 = UserFactory.create(
         profile__state=self.swing_state_2.name,
         profile__preferred_candidate=CANDIDATE_STEIN)
     self.user.profile.friends.add(swing_user_2.profile)
     self.swing_state_3 = StateFactory.create(tipping_point_rank=3)
     swing_user_3 = UserFactory.create(
         profile__state=self.swing_state_3.name,
         profile__preferred_candidate=candidate)
     self.user.profile.friends.add(swing_user_3.profile)
     # The ordering of expected_matches matters, it's ordered by state rank
     self.expected_matches = [swing_user_1.profile, swing_user_2.profile]
예제 #2
0
 def test_latest(self):
     yesterday = timezone.now() - datetime.timedelta(days=1)
     StateFactory.create(name='California', updated=yesterday)
     state2 = StateFactory.create(name='California')
     self.assertEqual(len(State.objects.all()), 1)
     self.assertEqual(len(State.all_objects.all()), 2)
     self.assertEqual(list(State.objects.all()), [state2])
예제 #3
0
 def setUp(self):
     candidate = CANDIDATE_CLINTON
     self.state_safe = StateFactory.create(
         safe_for=candidate,
         safe_rank=1
     )
     self.user = UserFactory.create(
         profile__state=self.state_safe.name,
         profile__preferred_candidate=candidate)
     self.state_swing = StateFactory.create(
         tipping_point_rank=1)
     self.friend = UserFactory.create(
         profile__state=self.state_swing.name,
         profile__preferred_candidate=CANDIDATE_STEIN)
     self.user.profile.friends.add(self.friend.profile)
     self.foaf = UserFactory.create(
         profile__state=self.state_swing.name,
         profile__preferred_candidate=CANDIDATE_JOHNSON)
     self.friend.profile.friends.add(self.foaf.profile)
     self.friend_proposal = PairProposal.objects.create(
         from_profile=self.user.profile,
         to_profile=self.friend.profile)
     self.foaf_proposal = PairProposal.objects.create(
         from_profile=self.foaf.profile,
         to_profile=self.user.profile)
예제 #4
0
 def test_latest_states(self):
     sorted_states = sorted([state[0] for state in STATES])
     for i in range(5):
         for i in range(127):
             StateFactory.create()
         self.assertEqual(
             sorted(list(State.objects.values_list('name', flat=True))),
             sorted_states)
예제 #5
0
 def setUp(self):
     super(_TestSafeStateFriendsOfFriendsMatchBase, self).setUp()
     candidate = CANDIDATE_CLINTON
     self.state_safe = StateFactory.create(
         safe_for=candidate,
         safe_rank=1
     )
     self.user = UserFactory.create(
         profile__state=self.state_safe.name,
         profile__preferred_candidate=candidate)
     tipping_point_rank = 1
     self.foaf_expected_matches = []
     # Create friends that haven't specified a vote choice just for links
     for i in range(2):
         friend_profile = ProfileFactory.create(state='')
         self.user.profile.friends.add(friend_profile)
         # And create friends of these friends in swing states
         for i in range(2):
             state = StateFactory.create(
                 tipping_point_rank=tipping_point_rank)
             foaf = UserFactory.create(
                 profile__state=state.name,
                 profile__preferred_candidate=CANDIDATE_STEIN)
             tipping_point_rank += 1
             friend_profile.friends.add(foaf.profile)
             self.foaf_expected_matches.append(foaf.profile)
     # And make another foaf that's friends with both of my friends
     state = StateFactory.create(
         tipping_point_rank=tipping_point_rank)
     self.foafoaf = UserFactory.create(
         profile__state=state.name,
         profile__preferred_candidate=CANDIDATE_JOHNSON)
     for friend in self.user.profile.friends.all():
         friend.friends.add(self.foafoaf.profile)
     self.foaf_expected_matches.append(self.foafoaf.profile)
     tipping_point_rank += 1
     self.direct_expected_matches = []
     # Create friends in swing states
     for i in range(2):
         state = StateFactory.create(
             tipping_point_rank=tipping_point_rank)
         friend = UserFactory.create(
             profile__state=state.name,
             profile__preferred_candidate=CANDIDATE_JOHNSON)
         tipping_point_rank += 1
         self.user.profile.friends.add(friend.profile)
         self.direct_expected_matches.append(friend.profile)
     # Direct friends are always preferred, so prepend them to expected
     self.expected_matches = (
         self.direct_expected_matches + self.foaf_expected_matches)
예제 #6
0
 def setUp(self):
     super(TestProfileContextStates, self).setUp()
     self.safe_clinton = StateFactory.create(
         safe_rank=1,
         safe_for=CANDIDATE_CLINTON)
     self.lean_clinton = StateFactory.create(
         lean_rank=1,
         leans=CANDIDATE_CLINTON)
     self.lean_trump = StateFactory.create(
         lean_rank=1,
         leans=CANDIDATE_TRUMP)
     self.safe_trump = StateFactory.create(
         safe_rank=1,
         safe_for=CANDIDATE_TRUMP)
예제 #7
0
 def setUp(self):
     super(TestEmailBase, self).setUp()
     self.from_state = StateFactory.create(tipping_point_rank=1)
     self.from_user = UserFactory.create(
         profile__state=self.from_state.name,
         profile__preferred_candidate=CANDIDATE_JOHNSON)
     self.to_state = StateFactory.create(safe_rank=1)
     self.to_user = UserFactory.create(
         profile__state=self.to_state.name,
         profile__preferred_candidate=CANDIDATE_CLINTON)
     self.proposal = PairProposal.objects.create(
         from_profile=self.from_user.profile,
         to_profile=self.to_user.profile,
         date_rejected=timezone.now())
예제 #8
0
 def setUp(self):
     super(TestConfirmSwapView, self).setUp()
     self.request = RequestFactory()
     from_state = StateFactory.create(tipping_point_rank=1)
     self.from_profile = UserFactory.create(
         profile__state=from_state.name).profile
     to_state = StateFactory.create(safe_rank=1)
     self.to_profile = UserFactory.create(
         profile__state=to_state.name).profile
     self.other_profile = ProfileFactory.create()
     self.from_profile.friends.add(self.to_profile)
     self.from_profile.friends.add(self.other_profile)
     self.proposal = PairProposal.objects.create(
         from_profile=self.from_profile, to_profile=self.to_profile)
예제 #9
0
 def setUp(self):
     super(_TestSwingStateMatchBase, self).setUp()
     candidate = CANDIDATE_JOHNSON
     self.state_safe = StateFactory.create(
         tipping_point_rank=1,
     )
     self.user = UserFactory.create(
         profile__state=self.state_safe.name,
         profile__preferred_candidate=candidate,
     )
     # Make two friends for each candidate in a safe state for each
     # The ordering of expected matches is by state safe_rank
     self.expected_matches = []
     safe_rank = 1
     self.safe_states = []
     for (preferred_candidate, safe_for) in [
             (CANDIDATE_CLINTON, CANDIDATE_CLINTON),
             (CANDIDATE_TRUMP, CANDIDATE_CLINTON),
             (CANDIDATE_CLINTON, CANDIDATE_TRUMP),
             (CANDIDATE_TRUMP, CANDIDATE_TRUMP)]:
         for i in range(2):
             state = StateFactory.create(
                 safe_for=safe_for,
                 safe_rank=safe_rank)
             self.safe_states.append(state)
             friend = UserFactory.create(
                 profile__state=state.name,
                 profile__preferred_candidate=preferred_candidate)
             self.user.profile.friends.add(friend.profile)
             if preferred_candidate == CANDIDATE_CLINTON:
                 self.expected_matches.append(friend.profile)
             safe_rank += 1
     # Create friends in swing states
     swing_state_1 = StateFactory.create(tipping_point_rank=2)
     swing_user_1 = UserFactory.create(
         profile__state=swing_state_1.name,
         profile__preferred_candidate=CANDIDATE_JOHNSON)
     self.user.profile.friends.add(swing_user_1.profile)
     swing_state_2 = StateFactory.create(tipping_point_rank=3)
     swing_user_2 = UserFactory.create(
         profile__state=swing_state_2.name,
         profile__preferred_candidate=CANDIDATE_STEIN)
     self.user.profile.friends.add(swing_user_2.profile)
     swing_state_3 = StateFactory.create(tipping_point_rank=4)
     swing_user_3 = UserFactory.create(
         profile__state=swing_state_3.name,
         profile__preferred_candidate=candidate)
     self.user.profile.friends.add(swing_user_3.profile)
예제 #10
0
 def setUp(self):
     super(TestProfileView, self).setUp()
     self.safe_state = StateFactory.create(
         safe_rank=1, safe_for=CANDIDATE_CLINTON)
     self.request = RequestFactory()
     self.user = UserFactory.create(
         profile__preferred_candidate=CANDIDATE_CLINTON,
         profile__state=self.safe_state.name)
예제 #11
0
 def test_paired_call_to_action(self):
     friend_state = StateFactory(safe_rank=1)
     friend = UserFactory.create(profile__state=friend_state.name)
     self.user.profile.friends.add(friend.profile)
     self.user.profile.paired_with = friend.profile
     request = self.request.get(reverse('users:profile'))
     request.user = self.user
     response = profile(request)
     self.assertContains(response, 'You pledged to swap your vote')
예제 #12
0
 def test_no_match_major_in_swing_state(self):
     state_safe = StateFactory.create(
         tipping_point_rank=1,
     )
     user = UserFactory.create(
         profile__state=state_safe.name,
         profile__preferred_candidate=CANDIDATE_CLINTON,
     )
     self.assertTrue(
         isinstance(get_friend_matches(user.profile), NoMatchNecessary))
예제 #13
0
 def setUp(self):
     self.user = UserFactory(profile=None)
     self.request = RequestFactory()
     self.state = StateFactory()
     self.session = {
         'landing_page_form': {
             'state': self.state.name,
             'preferred_candidate': CANDIDATE_CLINTON,
             'reason': u'just because',
         }
     }
예제 #14
0
 def test_some_matches_call_to_action(self):
     friend_state = StateFactory(tipping_point_rank=1)
     friend = UserFactory.create(profile__state=friend_state.name)
     friend.profile.preferred_candidate = CANDIDATE_STEIN
     friend.profile.save()
     self.user.profile.friends.add(friend.profile)
     request = self.request.get(reverse('users:profile'))
     request.user = self.user
     response = profile(request)
     self.assertContains(response, 'Expected to see more matches?')
     self.assertContains(response, 'There aren\'t any more friends')
예제 #15
0
 def test_no_match_minor_in_safe_state(self):
     state_safe = StateFactory.create(
         safe_for=CANDIDATE_TRUMP,
         safe_rank=1,
     )
     user = UserFactory.create(
         profile__state=state_safe.name,
         profile__preferred_candidate=CANDIDATE_JOHNSON,
     )
     self.assertTrue(
         isinstance(get_friend_matches(user.profile), NoMatchNecessary))
예제 #16
0
 def test_success(self):
     user_state = StateFactory.create(tipping_point_rank=1)
     user = UserFactory.create(profile__state=user_state.name)
     match_state = StateFactory.create(safe_rank=1)
     match = UserFactory.create(profile__state=match_state.name)
     user.profile.friends.add(match.profile)
     request = self.request.post(reverse('users:propose_swap'),
                                 {'to_profile': match.profile.id})
     request.user = user
     self.assertFalse(PairProposal.objects.all())
     self.assertEqual(len(testmail.outbox), 0)
     response = propose_swap(request)
     self.assertEqual(len(testmail.outbox), 1)
     self.assertEqual(response.status_code, HTTP_OK)
     self.assertEqual(response.content,
                      json.dumps({
                          'status': 'ok',
                          'errors': {}
                      }))
     proposal = PairProposal.objects.get(from_profile=user.profile)
     self.assertTrue(proposal)
     self.assertEqual(proposal.to_profile, match.profile)
예제 #17
0
 def test_reject_one_good_potential_match(self):
     # Add a new friend that's a good potential match
     extra_state = StateFactory.create(safe_rank=2)
     self.from_user.profile.friends.add(
         UserFactory.create(
             profile__state=extra_state.name,
             profile__preferred_candidate=CANDIDATE_CLINTON).profile)
     _send_reject_swap_email(self.to_user, self.proposal)
     [message] = testmail.outbox
     bodies = [message.body, message.alternatives[0][0]]
     for contents in bodies:
         contents = contents.replace('\n', ' ')
         self.assertIn("You have 1 good potential match", contents)
         self.assertIn("https://voteswap.us/swap", contents)
예제 #18
0
 def test_email_required(self):
     new_state = StateFactory.create()
     new_reason = "Why do you need my email, anyway?"
     data = {
         'preferred_candidate': self.user.profile.preferred_candidate,
         'state': new_state.name,
         'reason': new_reason,
         'email': ''
     }
     request = self.request.post(reverse('users:update_profile'), data=data)
     request.user = self.user
     response = update_profile(request)
     self.assertEqual(response.status_code, HTTP_OK)
     self.assertContains(response, "error")
     self.assertContains(response, "This field is required")
예제 #19
0
 def test_latest_multiple_states(self):
     yesterday = timezone.now() - datetime.timedelta(days=1)
     StateFactory.create(name='California', updated=yesterday)
     ca = StateFactory.create(name='California')
     StateFactory.create(name='Florida', updated=yesterday)
     fl = StateFactory.create(name='Florida')
     states = State.objects.all()
     self.assertEqual(len(states), 2)
     self.assertEqual(len(State.all_objects.all()), 4)
     self.assertEqual(set(State.objects.all()), set([ca, fl]))
예제 #20
0
 def test_post(self):
     state = StateFactory()
     data = {'preferred_candidate': CANDIDATE_CLINTON,
             'state': state.name}
     request = self.request.post(reverse(landing_page), data=data)
     request.user = AnonymousUser()
     request.session = {}
     response = landing_page(request)
     self.assertTrue('landing_page_form' in request.session)
     form = LandingPageForm(data=data)
     self.assertTrue(form.is_valid())
     full_data = form.cleaned_data
     self.assertEqual(request.session['landing_page_form'], full_data)
     self.assertEqual(response.status_code, HTTP_REDIRECT)
     self.assertTrue(response.has_header('Location'))
     redirect_to = "{base}?next={next}".format(
         base=reverse('social:begin', args=['facebook']),
         next=reverse('confirm_signup'))
     self.assertEqual(response.get('Location'), redirect_to)
예제 #21
0
 def test_update(self):
     new_state = StateFactory.create()
     new_reason = "I want to get off of Mr. Trump's wild ride"
     new_email = '*****@*****.**'
     data = {
         'preferred_candidate': self.user.profile.preferred_candidate,
         'state': new_state.name,
         'reason': new_reason,
         'email': new_email
     }
     request = self.request.post(reverse('users:update_profile'), data=data)
     request.user = self.user
     response = update_profile(request)
     self.assertEqual(response.status_code, HTTP_REDIRECT)
     self.assertTrue(response.has_header('Location'))
     self.assertEqual(response.get('Location'), reverse('users:profile'))
     profile = Profile.objects.get(id=self.user.profile.id)
     self.assertEqual(profile.state, new_state.name)
     self.assertEqual(profile.reason_decoded, new_reason)
     user = User.objects.get(id=self.user.id)
     self.assertEqual(user.email, new_email)
예제 #22
0
 def setUp(self):
     super(TestProfileView, self).setUp()
     self.request = RequestFactory()
     self.state = StateFactory.create()
     self.user = UserFactory.create(profile__state=self.state.name,
                                    profile__reason="#NeverTrump")
예제 #23
0
 def setUp(self):
     self.swing_state = StateFactory.create(tipping_point_rank=1)
     self.safe_state = StateFactory.create(safe_rank=1)
예제 #24
0
 def setUp(self):
     super(TestLandingPageForm, self).setUp()
     self.state = StateFactory.create(name='California')
예제 #25
0
 def setUp(self):
     self.user = UserFactory(profile=None)
     self.request = RequestFactory()
     # Need some states to load the landing page
     StateFactory.create(tipping_point_rank=1)
     StateFactory.create(safe_rank=1)