Skip to content

Commit

Permalink
Pull request fix #1.
Browse files Browse the repository at this point in the history
	modifié :         tests/test_scan.py
	modifié :         tests/test_schemes.py

Pull request fix #2.
	modifié:         tests/test_scan.py
	modifié:         tests/test_schemes.py

Pull request fix rockymeza#3.
	modifié:         setup.py
Add 'mock' to the requires.

Pull request fix rockymeza#4.
	modifié:         .travis.yml
Demand for latest update of setuptools when installing.

Pull request fix rockymeza#5.
Improve code readability.

Add Travis to my own repository.

Restore file permissions.

Pull request fixes rebase.

Rebase of the pull request fixes in order to clear the code and to solve some build issues (still least).
  • Loading branch information
Erf- committed Aug 3, 2015
1 parent 3bc5a47 commit b9e3320
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 41 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ python:
# command to install dependencies
before_install:
- sudo apt-get install wireless-tools
- sudo pip install -U pip wheel setuptools
install:
- sudo python setup.py -q install
# command to run tests
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def read(fname):


install_requires = [
'setuptools',
'pbkdf2',
'mock',
]
try:
import argparse
Expand Down
15 changes: 2 additions & 13 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from wifi.scan import Cell
from wifi.exceptions import InterfaceError
import string


class IWListParserTest(TestCase):
Expand Down Expand Up @@ -68,7 +67,8 @@ def test_absolute_quality(self):

class ScanningTest(TestCase):
def test_scanning(self):
self.assertRaises(InterfaceError, Cell.all, 'fake-interface')#test fails
self.assertRaises(InterfaceError, Cell.all, 'fake-interface')
self.assertRaises(InterfaceError, Cell.all, 'fake-interface', True)


IWLIST_SCAN_NO_ENCRYPTION = """Cell 02 - Address: 38:83:45:CC:58:74
Expand Down Expand Up @@ -280,14 +280,3 @@ def test_scanning(self):
Bit Rates:144 Mb/s
"""

output = string.join([IWLIST_SCAN_NO_ENCRYPTION,
IWLIST_SCAN_WEP,
IWLIST_SCAN_WPA2,
IWLIST_SCAN_WPA1,
ALTERNATIVE_OUTPUT,
ALTERNATIVE_OUTPUT2,
NONAME_WIRELESS_NETWORK,
NO_CHANNEL_OUTPUT,
LIST_INDEX_ERROR,
FREQUENCY_NO_CHANNEL_OUTPUT,
ABSOLUTE_QUALITY], '\n')
19 changes: 11 additions & 8 deletions tests/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

from wifi.scan import Cell
from wifi import subprocess_compat as subprocess
from wifi.subprocess_compat import check_output
from mock import MagicMock
from test_parsing import output
from test_parsing import IWLIST_SCAN_WEP, IWLIST_SCAN_WPA2
import string


expected_output = string.join([IWLIST_SCAN_WEP, IWLIST_SCAN_WPA2,], '\n')

class ScanTest(TestCase):
def test_all_calls_check_output_with_good_args(self):
args = ['sudo', '/sbin/iwlist', 'interface', 'scan']
kwargs = {'stderr':subprocess.STDOUT}
subprocess.check_output = MagicMock(return_value=output)
Cell.all('interface', True)
subprocess.check_output.assert_called_once_with(args, **kwargs)
args = ['/sbin/iwlist', 'interface', 'scan']
subprocess.check_output = MagicMock(return_value=output)
kwargs = {'stderr':subprocess.STDOUT}
subprocess.check_output = MagicMock(return_value=expected_output)
Cell.all('interface')
subprocess.check_output.assert_called_once_with(args, **kwargs)
args.insert(0, 'sudo')
subprocess.check_output = MagicMock(return_value=expected_output)
Cell.all('interface', sudo=True)
subprocess.check_output.assert_called_once_with(args, **kwargs)

20 changes: 9 additions & 11 deletions tests/test_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from wifi.scheme import extract_schemes, Scheme
from wifi.exceptions import ConnectionError
from wifi import subprocess_compat as subprocess
from wifi.subprocess_compat import check_output
from mock import MagicMock
from mock import patch


NETWORK_INTERFACES_FILE = """
Expand Down Expand Up @@ -103,17 +102,16 @@ def test_failed_connection(self):
self.assertRaises(ConnectionError, scheme.parse_ifup_output, FAILED_IFUP_OUTPUT)

def test_activate_is_called_with_good_args(self):
args = ['sudo', '/sbin/ifdown', 'wlan0', 'wlan0=wlan0-test']
args = ['sudo', '/sbin/ifdown', 'wlan0']
kwargs = {'stderr':subprocess.STDOUT}
scheme = Scheme('wlan0', 'test')
subprocess.check_output = MagicMock(return_value=SUCCESSFUL_IFUP_OUTPUT)
scheme.activate(True)
subprocess.check_output.assert_called_with(args,
**kwargs)
args = ['/sbin/ifdown', 'wlan0', 'wlan0=wlan0-test']
scheme.activate()
subprocess.check_output.assert_called_with(args,
**kwargs)
with patch.object(subprocess, 'check_output',
return_value=SUCCESSFUL_IFUP_OUTPUT):
scheme.activate(sudo=True)
subprocess.check_output.assert_any_call(args, **kwargs)
args = ['/sbin/ifdown', 'wlan0']
scheme.activate()
subprocess.check_output.assert_any_call(args, **kwargs)


class TestForCell(TestCase):
Expand Down
5 changes: 1 addition & 4 deletions wifi/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def all(cls, interface, sudo=False):
raise InterfaceError(e.output.strip())
else:
iwlist_scan = iwlist_scan.decode('utf-8')
cells = map(Cell.from_string, cells_re.split(iwlist_scan)[1:])
cells = map(Cell.from_string, cells_re.split(iwlist_scan)[1:])

return cells

Expand Down Expand Up @@ -158,6 +158,3 @@ def normalize(cell_block):

return cell

# all with sudo arg does not work
# Everything is ok when launched in admin mode
# Seems to depend on distribution, worked on xubuntu...
12 changes: 8 additions & 4 deletions wifi/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,15 @@ def activate(self, sudo=False):
Connects to the network as configured in this scheme.
"""

args = ['/sbin/ifdown', self.interface]
args_ifdown = ['/sbin/ifdown', self.interface]
args_ifup = ['/sbin/ifup']
if sudo:
args.insert(0, 'sudo')
subprocess.check_output(args, stderr=subprocess.STDOUT)#role?
ifup_output = subprocess.check_output(args + self.as_args(), stderr=subprocess.STDOUT)
args_ifdown.insert(0, 'sudo')
args_ifup.insert(0, 'sudo')
subprocess.check_output(args_ifdown, stderr=subprocess.STDOUT)
ifup_output = subprocess.check_output(args_ifup +
self.as_args(),
stderr=subprocess.STDOUT)
ifup_output = ifup_output.decode('utf-8')

return self.parse_ifup_output(ifup_output)
Expand Down

0 comments on commit b9e3320

Please sign in to comment.