Skip to content

Commit

Permalink
shuffle alphabet and ensure min length of 3 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
miltontony committed Feb 28, 2014
1 parent fe16284 commit 06f3874
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
12 changes: 10 additions & 2 deletions shortener/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- test-case-name: shortener.tests.test_api -*-
import random
import string
from urlparse import urljoin, urlparse

Expand All @@ -11,6 +12,8 @@
from shortener.models import ShortenerTables, NoShortenerTables

DEFAULT_ALPHABET = string.digits + string.ascii_letters
SHORT_URL_OFFSET = 4000

DEFAULT_USER_TOKEN = 'generic-user-token'


Expand Down Expand Up @@ -100,12 +103,17 @@ def update_short_url(self, row_id, short_url):
finally:
yield conn.close()

def shuffle_string(self, str):
shuffle = lambda seq: random.Random(1234).sample(seq, len(seq))
return ''.join(shuffle(list(str)))

This comment has been minimized.

Copy link
@jerith

jerith Feb 28, 2014

Member

You don't need the list() or lambda here:
return ''.join(random.Random(1234).sample(seq, len(seq)))
Also, str is a builtin and probably shouldn't be used as a variable.


def generate_token(self, counter, alphabet=DEFAULT_ALPHABET):
if not isinstance(counter, int):
raise TypeError('an integer is required')

alphabet = self.shuffle_string(alphabet)
base = len(alphabet)
if counter == 0:
return alphabet[0]
counter += SHORT_URL_OFFSET

digits = []
while counter > 0:
Expand Down
18 changes: 9 additions & 9 deletions shortener/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_create_url_simple(self):
pool=self.pool)

result = yield treq.json_content(resp)
self.assertEqual(result['short_url'], 'http://wtxt.io/1')
self.assertEqual(result['short_url'], 'http://wtxt.io/q0r')

@inlineCallbacks
def test_create_url_no_user_token(self):
Expand All @@ -75,15 +75,15 @@ def test_create_url_no_user_token(self):
pool=self.pool)

result = yield treq.json_content(resp)
self.assertEqual(result['short_url'], 'http://wtxt.io/1')
self.assertEqual(result['short_url'], 'http://wtxt.io/q0r')

@inlineCallbacks
def test_resolve_url_simple(self):
url = 'http://en.wikipedia.org/wiki/Cthulhu'
yield self.service.shorten_url(url)

resp = yield treq.get(
self.make_url('/1'),
self.make_url('/q0r'),
allow_redirects=False,
pool=self.pool)

Expand All @@ -107,7 +107,7 @@ def test_resolve_url_404(self):
def test_url_shortening(self):
long_url = 'http://en.wikipedia.org/wiki/Cthulhu'
short_url = yield self.service.shorten_url(long_url)
self.assertEqual(short_url, 'http://wtxt.io/1')
self.assertEqual(short_url, 'http://wtxt.io/q0r')

@inlineCallbacks
def test_short_url_generation(self):
Expand Down Expand Up @@ -137,7 +137,7 @@ def test_resolve_url(self):
yield self.service.shorten_url(url + '3')
yield self.service.shorten_url(url + '4')

result = yield self.service.get_row_by_short_url('4')
result = yield self.service.get_row_by_short_url('q0H')
self.assertEqual(result['long_url'], url + '4')

@inlineCallbacks
Expand All @@ -147,8 +147,8 @@ def test_short_url_sequencing(self):
for u in urls:
yield self.service.shorten_url(u)

result = yield self.service.get_row_by_short_url('1p')
self.assertEqual(result['long_url'], url + '87')
result = yield self.service.get_row_by_short_url('qR1')
self.assertEqual(result['long_url'], url + '40')

result = yield self.service.get_row_by_short_url('1b')
self.assertEqual(result['long_url'], url + '73')
result = yield self.service.get_row_by_short_url('qR3')
self.assertEqual(result['long_url'], url + '55')

0 comments on commit 06f3874

Please sign in to comment.