File: test_network.py

package info (click to toggle)
bumblebee-status 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: python: 13,430; sh: 68; makefile: 29
file content (108 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from unittest import TestCase, mock
import pytest

import core.config
import core.widget
import modules.contrib.network

import socket

pytest.importorskip("netifaces")


def build_module():
    config = core.config.Config([])
    return modules.contrib.network.Module(config=config, theme=None)


def wireless_default():
    return {"default": {1: ("10.0.1.12", "wlan3")}}


def wired_default():
    return {"default": {18: ("10.0.1.12", "eth3")}}


def exec_side_effect_valid(*args, **kwargs):
    if args[0] == "iwgetid":
        return "ESSID: bumblefoo"
    if "iwconfig" in args[0]:
        return "level=-30"
    return mock.DEFAULT


def exec_side_effect_invalid(*args, **kwargs):
    return "invalid gibberish, can't parse for info"


class TestNetworkUnit(TestCase):
    def test_load_module(self):
        __import__("modules.contrib.network")

    @mock.patch("socket.create_connection")
    def test_no_internet(self, socket_mock):
        socket_mock.side_effect = Exception()
        module = build_module()
        assert module.widgets()[0].full_text() == "No connection"

    @mock.patch("util.cli.execute")
    @mock.patch("netifaces.gateways")
    @mock.patch("socket.create_connection")
    @mock.patch("netifaces.AF_INET", 1)
    @mock.patch("builtins.open", mock.mock_open(read_data="wlan3"))
    def test_valid_wireless_connection(self, socket_mock, gateways_mock, execute_mock):
        socket_mock.return_value = mock.MagicMock()
        fake_ssid = "bumblefoo"
        gateways_mock.return_value = wireless_default()
        execute_mock.side_effect = exec_side_effect_valid

        module = build_module()

        assert fake_ssid in module.widgets()[0].full_text()

    @mock.patch("netifaces.gateways")
    @mock.patch("socket.create_connection")
    @mock.patch("netifaces.AF_INET", 18)
    @mock.patch("builtins.open", mock.mock_open(read_data="wlan3"))
    def test_valid_wired_connection(self, socket_mock, gateways_mock):
        gateways_mock.return_value = wired_default()
        socket_mock.return_value = mock.MagicMock()

        module = build_module()

        assert module.widgets()[0].full_text() == "Ethernet"

    @mock.patch("netifaces.gateways")
    @mock.patch("socket.create_connection")
    def test_invalid_gateways(self, socket_mock, gateways_mock):
        socket_mock.return_value = mock.Mock()
        gateways_mock.return_value = {"xyz": "abc"}

        module = build_module()
        assert module.widgets()[0].full_text() == "No connection"

    @mock.patch("util.cli.execute")
    @mock.patch("socket.create_connection")
    @mock.patch("netifaces.gateways")
    @mock.patch("netifaces.AF_INET", 1)
    @mock.patch("builtins.open", mock.mock_open(read_data="wlan3"))
    def test_invalid_execs(self, gateways_mock, socket_mock, execute_mock):
        execute_mock.side_effect = exec_side_effect_invalid
        socket_mock.return_value = mock.MagicMock()
        gateways_mock.return_value = wireless_default()

        module = build_module()

        assert module.widgets()[0].full_text() == "Unknown ?%"

    @mock.patch("builtins.open", **{"return_value.raiseError.side_effect": Exception()})
    @mock.patch("socket.create_connection")
    @mock.patch("netifaces.gateways")
    @mock.patch("netifaces.AF_INET", 18)
    @mock.patch("builtins.open", mock.mock_open(read_data="wlan3"))
    def test_no_wireless_file(self, gateways_mock, socket_mock, mock_open):
        gateways_mock.return_value = wired_default()
        socket_mock.return_value = mock.MagicMock()
        module = build_module()

        assert module.widgets()[0].full_text() == "Ethernet"