Skip to content

Commit

Permalink
Fixed unit test without network.
Browse files Browse the repository at this point in the history
Signed-off-by: Kouhei Maeda <mkouhei@palmtb.net>
  • Loading branch information
mkouhei committed Jun 17, 2014
1 parent 5e13cf7 commit b1c9d5d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
12 changes: 7 additions & 5 deletions shiori/bookmark/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
else:
from urlparse import urlparse
from netaddr import IPAddress, AddrFormatError
from socket import gaierror, gethostbyname, getaddrinfo, AF_INET6
import socket
from shiori.core.settings import FEED_EXCLUDE_FQDN


Expand Down Expand Up @@ -46,15 +46,17 @@ def getaddr(hostname):
"""
addresses = []
try:
addresses.append(gethostbyname(hostname))
addresses.append(socket.gethostbyname(hostname))
except TypeError as error:
print(error)
except gaierror as error:
except socket.gaierror as error:
print(error)
try:
addresses.append(getaddrinfo(hostname, None, AF_INET6)[0][4][0])
addresses.append(socket.getaddrinfo(hostname,
None,
socket.AF_INET6)[0][4][0])
except TypeError as error:
print(error)
except gaierror as error:
except socket.gaierror as error:
print(error)
return addresses
4 changes: 3 additions & 1 deletion shiori_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import json
from httpretty import HTTPretty, httprettified
from mock import patch
from shiori.api.views import CategoryViewSet
import shiori_tests.tests.vars as v

Expand Down Expand Up @@ -324,7 +325,8 @@ def test_post_bookmark_without_url(self):
self.assertEqual(response.status_code, 400)

@httprettified
def test_post_bookmark_without_title(self):
@patch('shiori.bookmark.validators.validate_url', return_vaule=True)
def test_post_bookmark_without_title(self, _mock):
payload = {'url': v.url,
'title': '',
'category': v.category0,
Expand Down
21 changes: 19 additions & 2 deletions shiori_tests/tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# -*- coding: utf-8 -*-
import unittest
from mock import patch
from django.core.exceptions import ValidationError
from shiori.bookmark import validators


class ValidatorsTests(unittest.TestCase):
def test_validate_url(self):
@patch('socket.gethostbyname', return_value='93.184.216.119')
@patch('socket.getaddrinfo',
return_value=[(10, 1, 6, '',
('2606:2800:220:6d:26bf:1447:1097:aa7', 0, 0, 0)),
(10, 2, 17, '',
('2606:2800:220:6d:26bf:1447:1097:aa7', 0, 0, 0)),
(10, 3, 0, '',
('2606:2800:220:6d:26bf:1447:1097:aa7', 0, 0, 0))])
def test_validate_url(self, _mock0, _mock1):
self.assertTrue(validators.validate_url('http://example.org'))

def test_validate_url_of_localhost(self):
Expand All @@ -23,7 +32,15 @@ def test_validate_url_of_loopback_addr(self):
validators.validate_url,
'http://127.0.0.1')

def test_get_addr(self):
@patch('socket.gethostbyname', return_value='93.184.216.119')
@patch('socket.getaddrinfo',
return_value=[(10, 1, 6, '',
('2606:2800:220:6d:26bf:1447:1097:aa7', 0, 0, 0)),
(10, 2, 17, '',
('2606:2800:220:6d:26bf:1447:1097:aa7', 0, 0, 0)),
(10, 3, 0, '',
('2606:2800:220:6d:26bf:1447:1097:aa7', 0, 0, 0))])
def test_get_addr(self, _mock0, _mock1):
self.assertListEqual(['93.184.216.119',
'2606:2800:220:6d:26bf:1447:1097:aa7'],
validators.getaddr('example.org'))
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ deps=
lxml
defusedxml
requests
pyquery
pyquery
mock
netaddr
bzr+http://bazaar.launchpad.net/~andrewsomething/django-openid-auth/1252445#egg=django-openid-auth
httpretty
Expand Down

0 comments on commit b1c9d5d

Please sign in to comment.