def test_cache_set(self, _urandom_mock): """shorten populates cache with ShortenedUrl slug""" app = FBApp.objects.create(appid=1, name='Share!', secret='sekret') client = app.clients.create(codename='testerson') campaign = client.campaigns.create(campaign_id=1) shorts = models.ShortenedUrl.objects.values('campaign_id', 'event_type', 'slug', 'url') self.assertEqual(shorts.count(), 0) cache = DictCache() self.assertEqual(len(cache), 0) with patch('django.core.cache.cache', cache): out = utils.shorten('http://www.reddit.com/r/food/', 'food', campaign) self.assertEqual(out, "/r/food-jSgnUTqp/") self.assertEqual(shorts.count(), 1) self.assertEqual(shorts.get(), { 'slug': 'food-jSgnUTqp', 'campaign_id': 1, 'event_type': 'initial_redirect', 'url': 'http://www.reddit.com/r/food/', }) self.assertEqual(len(cache), 1) slug = cache['shorturl|initial_redirect|food|1|cc06be0890c05085b3fbeec2bea1ad9d'] self.assertEqual(slug, 'food-jSgnUTqp')
def test_read_db_ignore_prefix(self, _mock): # This one is older: recently = models.ShortenedUrl.objects.create( event_type='initial_redirect', slug='testIslug', url=URL, ) recently.created = datetime.now() - timedelta(hours=12) recently.save() # This one is newer but has a prefix: models.ShortenedUrl.objects.create(event_type='initial_redirect', slug='test-slug', url=URL) shorts = models.ShortenedUrl.objects.all() self.assertEqual(shorts.count(), 2) cache = DictCache() self.assertEqual(len(cache), 0) with patch('django.core.cache.cache', cache): out = utils.shorten(URL) self.assertEqual(out, "/r/testIslug/") self.assertEqual(shorts.count(), 2) self.assertEqual(len(cache), 1) slug = cache['shorturl|initial_redirect|||cc06be0890c05085b3fbeec2bea1ad9d'] self.assertEqual(slug, 'testIslug')
def test_read_db_campaign(self, _mock): app = FBApp.objects.create(appid=1, name='Share!', secret='sekret') client = app.clients.create(codename='testerson') campaign = client.campaigns.create(campaign_id=1) campaign.shortenedurls.create( event_type='initial_redirect', slug='testIslug', url=URL, ) shorts = models.ShortenedUrl.objects.all() self.assertEqual(shorts.count(), 1) cache = DictCache() self.assertEqual(len(cache), 0) with patch('django.core.cache.cache', cache): out = utils.shorten(URL, campaign=1) self.assertEqual(out, "/r/testIslug/") self.assertEqual(shorts.count(), 1) self.assertEqual(len(cache), 1) slug = cache['shorturl|initial_redirect||1|cc06be0890c05085b3fbeec2bea1ad9d'] self.assertEqual(slug, 'testIslug')
def test_cache_campaign_id(self, _urandom_mock): """shorten checks cache for existing ShortenedUrl slug and respects campaign ID""" shorts = models.ShortenedUrl.objects.all() self.assertEqual(shorts.count(), 0) out = utils.shorten('http://www.reddit.com/r/food/', campaign=1) self.assertEqual(out, "/r/jSgnUTqptPrV/") self.assertEqual(shorts.count(), 0)
def test_cache_prefix(self, _urandom_mock): """shorten checks cache for existing ShortenedUrl slug and respects prefix""" shorts = models.ShortenedUrl.objects.all() self.assertEqual(shorts.count(), 0) out = utils.shorten('http://www.reddit.com/r/food/', prefix='food') self.assertEqual(out, "/r/food-jSgnUTqp/") self.assertEqual(shorts.count(), 0)
def test_prefix(self, _mock): shorts = models.ShortenedUrl.objects.values('campaign_id', 'event_type', 'slug', 'url') self.assertEqual(shorts.count(), 0) out = utils.shorten('http://www.english.com/alphabet/a/b/c/d/efgh/', prefix='en') self.assertEqual(out, "/r/en-jSgnUTqpt/") self.assertEqual(shorts.get(), { 'campaign_id': None, 'event_type': 'initial_redirect', 'slug': 'en-jSgnUTqpt', 'url': 'http://www.english.com/alphabet/a/b/c/d/efgh/', })
def test_cache_campaign(self, _urandom_mock): """shorten checks cache for existing ShortenedUrl slug and respects Campaign""" app = FBApp.objects.create(appid=1, name='Share!', secret='sekret') client = app.clients.create(codename='testerson') campaign = client.campaigns.create(campaign_id=1) shorts = models.ShortenedUrl.objects.all() self.assertEqual(shorts.count(), 0) out = utils.shorten('http://www.reddit.com/r/food/', campaign=campaign) self.assertEqual(out, "/r/jSgnUTqptPrV/") self.assertEqual(shorts.count(), 0)
def shorten(context, *args, **kws): """Return an absolute shortened URL for the given absolute long URL. {% shorten 'http://www.english.com/alphabet/a/b/c/defghi/' %} Optionally specify the shortened slug `prefix`, the `campaign` to associate with the mapping, and the `event_type` to associate with the mapping (defaulting to 'initial_redirect'). The location of the routing service, `server`, may also be specified; (see `short_url` and `CHAPO_SERVER`). {% shorten 'http://www.english.com/alphabet/a/b/c/defghi/' prefix='en' campaign=campaign server='https://edgefl.ip' %} """ server = server_fallback(context, kws.pop("server", None)) return utils.shorten(*args, server=server, **kws)
def test_retry(self, log_mock, _urandom_mock): models.ShortenedUrl.objects.create(url=URL) # pre-existing shorts = models.ShortenedUrl.objects.values('campaign_id', 'event_type', 'slug', 'url') self.assertEqual(shorts.count(), 1) out = utils.shorten('http://www.english.com/alphabet/a/b/c/d/efgh/') self.assertEqual(out, "/r/WSTUhHUshgrt/") self.assertEqual(shorts.count(), 2) self.assertEqual(shorts.exclude(slug='jSgnUTqptPrV').get(), { 'campaign_id': None, 'event_type': 'initial_redirect', 'slug': 'WSTUhHUshgrt', 'url': 'http://www.english.com/alphabet/a/b/c/d/efgh/', }) log_mock.warning.assert_called_once_with("shorten required %s attempts", 2)
def test_read_db_event_type(self, _mock): models.ShortenedUrl.objects.create(event_type='initial_redirect', slug='testIslug', url=URL) shorts = models.ShortenedUrl.objects.all() self.assertEqual(shorts.count(), 1) cache = DictCache() self.assertEqual(len(cache), 0) with patch('django.core.cache.cache', cache): out = utils.shorten(URL) self.assertEqual(out, "/r/testIslug/") self.assertEqual(shorts.count(), 1) self.assertEqual(len(cache), 1) slug = cache['shorturl|initial_redirect|||cc06be0890c05085b3fbeec2bea1ad9d'] self.assertEqual(slug, 'testIslug')
def test_read_db_prefix(self, _mock): # This one won't be the latest: yesterday = models.ShortenedUrl.objects.create( event_type='initial_redirect', slug='test-BLUG', url=URL, ) yesterday.created = datetime.now() - timedelta(1) yesterday.save() # This one is just right: recently = models.ShortenedUrl.objects.create( event_type='initial_redirect', slug='test-slug', url=URL, ) recently.created = datetime.now() - timedelta(hours=12) recently.save() # This one's prefix doesn't match: models.ShortenedUrl.objects.create( event_type='initial_redirect', slug='BEST-SLUG', url=URL, ) shorts = models.ShortenedUrl.objects.all() self.assertEqual(shorts.count(), 3) cache = DictCache() self.assertEqual(len(cache), 0) with patch('django.core.cache.cache', cache): out = utils.shorten(URL, prefix='TEST') self.assertEqual(out, "/r/test-slug/") self.assertEqual(shorts.count(), 3) self.assertEqual(len(cache), 1) slug = cache['shorturl|initial_redirect|test||cc06be0890c05085b3fbeec2bea1ad9d'] self.assertEqual(slug, 'test-slug')