import unittest from datetime import datetime timedelta from django te

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import unittest
from datetime import datetime, timedelta
from django.test.client import Client
from django.conf import settings
from ffeed.models import Link
from ffeed import views
from ffeed.util import parse_uri
from ffeed.util import is_safelink
class FrontendTestCase(unittest.TestCase):
def testIndex(self):
yalink = 'http://ya.ru'
goolink = 'http://google.com'
killlink = 'http://killerapp.ru'
Link.objects.all().delete()
Link(uri=yalink, entry_id=1, ctime=datetime.now()).save()
c = Client()
resp = c.get('/')
self.assertEqual(resp.status_code, 302)
self.assertEqual(resp['Location'], yalink)
Link(uri=goolink, entry_id=2, ctime=datetime.now() + timedelta(minutes=1)).save()
resp = c.get('/')
self.assertEqual(resp['Location'], goolink)
Link(uri=killlink, entry_id=3, ctime=datetime.now() + timedelta(minutes=2)).save()
resp = c.get('/')
self.assertEqual(resp['Location'], settings.FFEED_SAFE_REDIRECT)
class ParserTestCase(unittest.TestCase):
def testParseUri(self):
yalink = 'http://ya.ru'
sunlink = 'http://ru.wikipedia.org/wiki/%D0%A1%D0%BE%D0%BB%D0%BD%D1%86%D0%B5'
wwwlink = 'www.foobar.com'
dotlink = 'http://ya.ru.'
self.assertEqual(parse_uri(yalink), yalink)
self.assertEqual(parse_uri('foo %s bar' % yalink), yalink)
self.assertEqual(parse_uri('foo %s bar' % yalink), yalink)
self.assertEqual(parse_uri(sunlink), sunlink)
self.assertEqual(parse_uri(wwwlink), 'http://%s' % wwwlink)
self.assertEqual(parse_uri(dotlink), dotlink.rstrip('.'))
def testIsSafelink(self):
self.assertTrue(is_safelink('http://ya.ru/'))
self.assertFalse(is_safelink('http://killerapp.ru'))
self.assertFalse(is_safelink('http://killerapp.ru/foo'))
self.assertFalse(is_safelink('http://www.killerapp.ru/foo'))
self.assertTrue(is_safelink('http://2killerapp.ru/foo'))