Skip to content

Commit

Permalink
Adding 'sudo' args.
Browse files Browse the repository at this point in the history
Add sudo args to terminal calls.
	modified :         tests/test_parsing.py
	modified :         tests/test_scan.py
	modified :         tests/test_schemes.py
	removed :        wifi/dummydata.py
	modified :         wifi/scheme.py

Pull request fix #1.
	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 build issues.
  • Loading branch information
Erf- committed Aug 3, 2015
1 parent a9d0d67 commit 973de9b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 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
2 changes: 2 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def test_absolute_quality(self):
class ScanningTest(TestCase):
def test_scanning(self):
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 @@ -278,3 +279,4 @@ def test_scanning(self):
Encryption key:off
Bit Rates:144 Mb/s
"""

23 changes: 23 additions & 0 deletions tests/test_scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unittest import TestCase

from wifi.scan import Cell
from wifi import subprocess_compat as subprocess
from mock import patch
from test_parsing import IWLIST_SCAN_WEP, IWLIST_SCAN_WPA2


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

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

15 changes: 15 additions & 0 deletions tests/test_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from wifi import Cell
from wifi.scheme import extract_schemes, Scheme
from wifi.exceptions import ConnectionError
from wifi import subprocess_compat as subprocess
from mock import patch


NETWORK_INTERFACES_FILE = """
Expand Down Expand Up @@ -99,6 +101,18 @@ def test_failed_connection(self):
scheme = Scheme('wlan0', 'test')
self.assertRaises(ConnectionError, scheme.parse_ifup_output, FAILED_IFUP_OUTPUT)

def test_activate_is_called_with_good_args(self):
args = ['sudo', '/sbin/ifdown', 'wlan0']
kwargs = {'stderr':subprocess.STDOUT}
scheme = Scheme('wlan0', 'test')
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):
def test_unencrypted(self):
Expand Down Expand Up @@ -218,3 +232,4 @@ def test_wpa(self):
No DHCPOFFERS received.
No working leases in persistent database - sleeping.
"""

8 changes: 6 additions & 2 deletions wifi/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ def __repr__(self):
return 'Cell(ssid={ssid})'.format(**vars(self))

@classmethod
def all(cls, interface):
def all(cls, interface, sudo=False):
"""
Returns a list of all cells extracted from the output of iwlist.
"""
args = ['/sbin/iwlist', interface, 'scan']
if sudo:
args.insert(0, 'sudo')
try:
iwlist_scan = subprocess.check_output(['/sbin/iwlist', interface, 'scan'],
iwlist_scan = subprocess.check_output(args,
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise InterfaceError(e.output.strip())
Expand Down Expand Up @@ -154,3 +157,4 @@ def normalize(cell_block):
cell.encryption_type = 'wep'

return cell

13 changes: 10 additions & 3 deletions wifi/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,20 @@ def as_args(self):

return [self.interface + '=' + self.iface] + args

def activate(self):
def activate(self, sudo=False):
"""
Connects to the network as configured in this scheme.
"""

subprocess.check_output(['/sbin/ifdown', self.interface], stderr=subprocess.STDOUT)
ifup_output = subprocess.check_output(['/sbin/ifup'] + self.as_args(), stderr=subprocess.STDOUT)
args_ifdown = ['/sbin/ifdown', self.interface]
args_ifup = ['/sbin/ifup']
if sudo:
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 973de9b

Please sign in to comment.