Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Adding sudo args #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ python:
# command to install dependencies
before_install:
- sudo apt-get install wireless-tools
- sudo pip install -U pip wheel setuptools
- sudo pip install -U mock
install:
- sudo python setup.py -q install
# command to run tests
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', sudo=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
"""

22 changes: 22 additions & 0 deletions tests/test_scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do it the other way around, you can set args only once, and then inserting sudo for next test.

kwargs = {'stderr':subprocess.STDOUT}
with patch.object(subprocess, 'check_output',
return_value=expected_output):
Cell.all('interface')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the indent different?

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')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

down comes first, let's keep it logical everywhere, more readable

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