Example #1
0
class TestEnsureUsersFromTokens(EdgeFlipTestCase):

    fixtures = ['test_client_data']
    frozen_time = '2014-03-17'

    def setUp(self):
        super(TestEnsureUsersFromTokens, self).setUp()
        self.command = ensure_users_from_tokens.Command()
        self.synced_fbid = 123
        self.visited_fbid = 456
        self.appid = 10101
        self.client_id = 1
        self.the_past = timezone.now() - datetime.timedelta(days=5)
        self.the_future = timezone.now() + datetime.timedelta(days=5)
        self.synced_token = Token(fbid=self.synced_fbid, appid=self.appid, token='1', expires=self.the_future)
        self.synced_token.save()
        self.visited_token = Token(fbid=self.visited_fbid, appid=self.appid, token='1', expires=self.the_past)
        self.visited_token.save()

    def test_ensure_user_client(self):
        new_expires_ts = time.time()
        new_expires_obj = timezone.make_aware(datetime.datetime.utcfromtimestamp(new_expires_ts), timezone.utc)
        with patch(
            'urllib2.urlopen',
            **{'return_value.read.return_value': json.dumps({
                'data': {
                    'expires_at': new_expires_ts,
                    'app_id': self.appid,
                    'user_id': self.synced_fbid,
                    'application': 'This organization',
                },
            })}
        ):
            queryset = UserClient.objects.filter(fbid__in=[self.visited_fbid, self.synced_fbid], client_id=1)
            self.assertEqual(queryset.count(), 0)

            self.command.execute()
            self.assertEqual(queryset.count(), 2)
            self.assertEqual(
                Token.items.get_item(fbid=self.synced_fbid, appid=self.appid).expires.timetuple(),
                new_expires_obj.timetuple()
            )
            self.assertEqual(Token.items.get_item(fbid=self.visited_fbid, appid=self.appid).expires, self.the_past)

            # don't put in duplicates
            self.command.execute()
            self.assertEqual(queryset.count(), 2)

    @patch('django.core.management.base.OutputWrapper')
    def test_ensure_user_client_exception(self, output_wrapper):
        with patch(
            'targetshare.integration.facebook.client.debug_token',
            side_effect=IOError
        ):
            self.command.execute()
            self.assertEqual(Token.items.get_item(fbid=self.visited_fbid, appid=self.appid).expires, self.the_past)
            self.assertEqual(Token.items.get_item(fbid=self.synced_fbid, appid=self.appid).expires, self.the_future)

        self.assertTrue(self.command.stderr.write.called)
Example #2
0
 def setUp(self):
     super(TestEnsureUsersFromTokens, self).setUp()
     self.command = ensure_users_from_tokens.Command()
     self.synced_fbid = 123
     self.visited_fbid = 456
     self.appid = 10101
     self.client_id = 1
     self.the_past = timezone.now() - datetime.timedelta(days=5)
     self.the_future = timezone.now() + datetime.timedelta(days=5)
     self.synced_token = Token(fbid=self.synced_fbid, appid=self.appid, token='1', expires=self.the_future)
     self.synced_token.save()
     self.visited_token = Token(fbid=self.visited_fbid, appid=self.appid, token='1', expires=self.the_past)
     self.visited_token.save()