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

Chk fra #13

Open
wants to merge 2 commits 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
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@

## Wifi Connectivity Manager
# Wifi Connectivity Manager

The app exposes a REST API to control wifi connectivity. Using the right network interface, it is possible to connect and disconnect from wifi networks, but also store and manage network configurations in /etc/network/interfaces and in a Sqlite3 database.

The app runs with [Python 2.7][1].

#### Option 1: system install
## INSTALLATION

### Option 1: system install

Make sure [pip][2] is installed on your system, then set up the environment:

pip install -r requirements.txt

Launch the app by running the bash script: `wifi_manager/interpreter/python_wifi.sh`.

#### Option 2: venv install
### Option 2: venv install

Create a virtual environment and install the requirements:

virtualenv -p /usr/bin/python .venv
. .venv/bin/activate
pip install -r requirements.txt
# At the end of installation
deactivate

## RUNNING

Launch the app by running the bash script: `wifi_manager/interpreter/python_venv.sh`.
In other shell tab, launch this command: . .venv/bin/activate


## REST API specification

#### REST API specification
The app listens by default on port 5000. Every request to the REST API must include the following header:

X-Api-Key: <API KEY HERE>
Expand Down
10 changes: 8 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Flask
git+https://github.com/martel-innovate/wifi.git@rpi-fix
python-wifi

#In case of error, comment this line
Copy link

Choose a reason for hiding this comment

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

Why not leaving just the more "fixed" version git+https://github.com/martel-innovate/wifi.git@rpi-fix ?
there should be only one line per each module in the requirements.txt

wifi

#In case of error, uncomment this line
#git+https://github.com/martel-innovate/wifi.git@rpi-fix

python-wifi
Empty file modified tests/interpreter/python_test_core.sh
100644 → 100755
Empty file.
Empty file modified tests/interpreter/python_test_rest.sh
100644 → 100755
Empty file.
21 changes: 21 additions & 0 deletions tests/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Run test eWine-Connectivity-Manager

## RUNNING
* open 2 shell tabs
In the 1st shell :
$ /home/pi/eWine-connectivity-manager/wifi_manager/interpreter/python_venv.sh

* In the 2nd shell:
$ cd /home/pi/eWine-connectivity-manager/
$ . .venv/bin/activate

$ sudo ifup wlan0 #to activate the wireless card, if it is down
$ wifi list #once time command to check if your wifi connection is stored
$ wifi add yournamewifi SSID #USE this command ONLY if your wifi connection is not in the list --- more information ../wifi/docs/wifi_command.rst
$ cd tests
$ sudo python test_core.py #launch test

if the test fails, try to launch these commands:
sudo ifup wlan0
sudo wifi connect SSID #even if your wireless card is already connected
sudo python test_core.py #launch test
73 changes: 63 additions & 10 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from context import os, core
import time
import pdb
import unittest
import sqlite3
import tempfile
Expand All @@ -24,48 +26,60 @@ def setUp(self):
self.gps_inf = -1000.0

def tearDown(self):
time.sleep(20)
os.close(self.db_fd)
os.unlink(self.db_name)

def test_0a_disable(self):
time.sleep(20)
Copy link

Choose a reason for hiding this comment

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

When you have to do the same at the beginning of all your tests... who do you call?

code = core.disable(self.iface)
self.assertEqual(code, 0)

def test_0b_interfaces(self):
time.sleep(20)
ifaces = core.interfaces()
self.assertIsInstance(ifaces, list)
self.assertNotIn(self.iface, ifaces)

def test_0c_status(self):
time.sleep(20)
status = core.status(self.iface)
self.assertEqual(status, '')

def test_0d_scan(self):
time.sleep(20)
self.assertRaises(core.WifiException, core.cell_all, self.iface)

def test_0e_available(self):
time.sleep(20)
self.assertRaises(core.WifiException, core.available, self.iface)

def test_0f_save(self):
time.sleep(20)
self.assertRaises(core.WifiException, core.save, self.iface, 'foo', 'foo', self.db)

def test_0g_connect(self):
time.sleep(20)
self.assertRaises(core.WifiException, core.connect, self.iface, 'foo', 'foo', self.db)

def test_1a_enable(self):
time.sleep(20)
code = core.enable(self.iface)
self.assertEqual(code, 0)

def test_1b_interfaces(self):
time.sleep(20)
ifaces = core.interfaces()
self.assertIsInstance(ifaces, list)
self.assertNotIn(self.iface, ifaces)
self.assertIn(self.iface, ifaces)
Copy link

Choose a reason for hiding this comment

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

Do we have an explanation for this maybe? a comment or something would help. Same for the change in test below.


def test_1c_status(self):
time.sleep(20)
status = core.status(self.iface)
self.assertEqual(status, '')

self.assertNotEqual(status, '')
def test_1d_scan(self):
time.sleep(20)
cells = core.cell_all(self.iface)
self.assertIsInstance(cells, list)

Expand All @@ -84,31 +98,37 @@ def test_1d_scan(self):
self.assertIsInstance(c["encryption_type"], str)

def available_test(self):
time.sleep(20)
# the network configuration must be stored before running the test
avail = core.available(self.iface)
self.assertIsInstance(avail, str)
self.assertTrue(len(avail) > 0)
return avail

def test_1e_save(self):
time.sleep(20)
self.assertRaises(core.WifiException, core.save, self.iface, 'foo', 'foo', self.db)

def test_2a_connect(self):
time.sleep(20)
avail = self.available_test()
core.connect(self.iface, avail, None, self.db)

def test_2b_interfaces(self):
time.sleep(20)
ifaces = core.interfaces()
self.assertIsInstance(ifaces, list)
self.assertIn(self.iface, ifaces)

def test_2c_status(self):
time.sleep(20)
status = core.status(self.iface)
avail = self.available_test()
self.assertIsInstance(status, str)
self.assertEqual(status, avail)

def test_2d_location(self):
time.sleep(20)
avail = self.available_test()
lat, lng = core.get_last_location(avail, self.db)
self.assertIsInstance(lat, float)
Expand All @@ -117,10 +137,12 @@ def test_2d_location(self):
self.assertEquals(lng, self.gps_inf)

def test_3a_delete_all(self):
time.sleep(20)
total, deleted = core.delete_all(self.db, db_only=True)
self.assertEquals(total, deleted)

def test_3b_location(self):
time.sleep(20)
avail = self.available_test()
lat, lng = core.get_last_location(avail, self.db)
self.assertIsInstance(lat, float)
Expand All @@ -129,10 +151,12 @@ def test_3b_location(self):
self.assertEquals(lng, self.gps_inf)

def test_3c_disable(self):
time.sleep(20)
code = core.disable(self.iface)
self.assertEqual(code, 0)

def test_scheme_all(self):
time.sleep(20)
schemes = core.scheme_all()
for obj in schemes:
iface, name = obj['interface'], obj['name']
Expand All @@ -143,13 +167,42 @@ def test_scheme_all(self):

options = obj['options']
self.assertIsInstance(options, dict)
channel, psk, ssid = options['wireless-channel'], options['wpa-psk'], options['wpa-ssid']
self.assertIsInstance(channel, str)
self.assertIsInstance(psk, str)
self.assertIsInstance(ssid, str)
self.assertTrue(len(channel) > 0)
self.assertTrue(len(psk) > 0)
self.assertTrue(len(ssid) > 0)
channel, psk, ssid = options.get('wireless-channel'), options.get('wpa-psk'), options.get('wpa-ssid')
if channel is not None:
Copy link

Choose a reason for hiding this comment

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

Some things to comment here:

  • What is it exactly that you want to test? E.g, if channel is not defined, should the test fail or not?
  • Tests should not be printing much stuff to output, but rather passing quietly or failing with error messages.
  • Avoid re-doing the same questions.

self.assertIsInstance(channel, str)
print("channel is defined")
else:
print("channel is not defined")

if psk is not None:
self.assertIsInstance(psk, str)
print("psk is defined")
else:
print("psk is not defined")

if ssid is not None:
self.assertIsInstance(ssid, str)
print("ssid is defined")
else:
print("ssid is not defined")

if channel is not None:
self.assertTrue(len(channel) > 0)
print("channel is true")
else:
print("channel is false")

if psk is not None:
self.assertTrue(len(psk) > 0)
print("psk is true")
else:
print("psk is false")

if ssid is not None:
self.assertTrue(len(ssid) > 0)
print("ssid is true")
else:
print("ssid is false")


if __name__ == '__main__':
Expand Down
Loading