Merge lp:~diegosarmentero/ubuntu-sso-client/network-detect into lp:ubuntu-sso-client

Proposed by Diego Sarmentero on 2011-09-26
Status: Merged
Approved by: Alejandro J. Cura on 2012-01-04
Approved revision: 824
Merged at revision: 827
Proposed branch: lp:~diegosarmentero/ubuntu-sso-client/network-detect
Merge into: lp:ubuntu-sso-client
Diff against target: 407 lines (+253/-11)
7 files modified
ubuntu_sso/networkstate/__init__.py (+8/-0)
ubuntu_sso/networkstate/linux.py (+24/-0)
ubuntu_sso/networkstate/tests/test_linux.py (+137/-2)
ubuntu_sso/networkstate/tests/test_windows.py (+64/-0)
ubuntu_sso/networkstate/windows.py (+13/-5)
ubuntu_sso/qt/controllers.py (+2/-2)
ubuntu_sso/qt/tests/test_controllers.py (+5/-2)
To merge this branch: bzr merge lp:~diegosarmentero/ubuntu-sso-client/network-detect
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) Approve on 2012-01-04
Natalia Bidart 2011-09-26 Approve on 2011-11-07
Review via email: mp+77048@code.launchpad.net

Commit Message

- Fixed: Missing page: No network detected (LP: #804610).

Description of the Change

Fixed: Missing page: No network detected (LP: #804610).

To post a comment you must log in.
796. By Diego Sarmentero on 2011-09-26

lint issues fixed.

Natalia Bidart (nataliabidart) wrote :

The method is_machine_connected should return something, no? Either a boolean if the result is synchronous, or a deferred.

review: Needs Information
797. By Diego Sarmentero on 2011-09-29

Improving networkstate to support is_machine_connected for both: linux and windows.

Natalia Bidart (nataliabidart) wrote :

* Can you please change the docstring to be:

"""Return a deferred that when fired, will return True if the machine is online."""

* Do not add this check:

if not d.called:

if the deferred was already called, then we have a bug we need to solve.

* I think this is not correct:

result = (state != OFFLINE)

online will it be:

result = int(state) in NM_STATE_CONNECTED_LIST

* I think you need to refactor the test, or maybe you forgot a commit/push? The tests should patch NetworkManagerState and assert over the returned deferred.

review: Needs Fixing
Natalia Bidart (nataliabidart) wrote :

Also, the windows tets for is_already_connected needs to be modified so they assert a deferred is returned and when fired, return the proper result.

798. By Diego Sarmentero on 2011-10-03

Improved tests.

Natalia Bidart (nataliabidart) wrote :

I still get lint issues:

ubuntu_sso/networkstate/tests/test_windows.py:
    126: [C0111, FakeWininet] Missing docstring
    130: [C0103, FakeWininet.InternetGetConnectedState] Invalid name "InternetGetConnectedState" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))

review: Needs Fixing
799. By Diego Sarmentero on 2011-10-03

Fixed lint issues.

Natalia Bidart (nataliabidart) wrote :

This docstring is not pep-257 compliant:

    """Return a deferred that when fired, will return True if the

    machine is online."""

must be something like:

    """Return a deferred that when fired, returns if the machine is online."""

* Why you patch dbus on ubuntu_sso/networkstate/tests/test_linux.py? I see you use it in the first test, but that test is not testing real functionality.

You need to patch the NetworkManagerState at setUp time, and change the fake implementation so you can customize the returned state. That way, you can safely ensure that your code is returning True when the result is in the NM_STATE_CONNECTED_LIST (so you need to have a test for each element inside NM_STATE_CONNECTED_LIST).

So, summing up:

 - Remove the 'fake_network_manager' legend from test names
 - Build a fake NetworkManagerState that behaves more like the real one (ie make it so it can returns any code from the constant list)
 - You should have a test per each constant, asserting either True or False accordingly.

* Since is_machine_connected returns a deferred, the test should use that deferred as such. So, as soon as is_machine_conencted finishes, you can't assume the returned deferred is fired, so you need to yield on it (and thus make the test an inlinecallback generator). Same applies to the windows tests.

* In both implementation, the deferred you're returning should errback if there is any error. So you'd need to add try-except clauses to the calculation of the net state and errback the deferred on error.

review: Needs Fixing
800. By Diego Sarmentero on 2011-10-06

Improved use of Deferred.

801. By Diego Sarmentero on 2011-10-07

Improved Sign In Page flow, to use check_connection.

Alejandro J. Cura (alecu) wrote :

The branch looks mostly fine, but here are a few comments:
--
Instead of "Exception(NETWORK_FAIL)", we should use a new custom exception (like NetworkFailException), and use it from both platform modules, and to compare on tests (instead of comparing with the error string).
--
It's usually error prone to set class attributes (or any other global state) from tests, so instead of:

   FakeNetworkManagerState.connection_state = NM_STATE_CONNECTED_OLD

please use something like:

   self.patch(FakeNetworkManagerState, "connection_state", NM_STATE_CONNECTED_OLD)
--
thanks!

review: Needs Fixing
802. By Diego Sarmentero on 2011-10-07

merge

803. By Diego Sarmentero on 2011-10-07

Improved Network state.

804. By Diego Sarmentero on 2011-10-11

merge

Natalia Bidart (nataliabidart) wrote :

Lots of lint issues! :-)

== Python Lint Notices ==

ubuntu_sso/networkstate/__init__.py:
    30: [C0111, NetworkFailException] Missing docstring

ubuntu_sso/networkstate/linux.py:
    146: [W0702, is_machine_connected] No exception type(s) specified

ubuntu_sso/networkstate/tests/test_linux.py:
    80: [C0103, TestConnection.test_is_machine_connected_NM_STATE_CONNECTED_OLD] Invalid name "test_is_machine_connected_NM_STATE_CONNECTED_OLD" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    88: [C0103, TestConnection.test_is_machine_connected_NM_STATE_CONNECTED_GLOBAL] Invalid name "test_is_machine_connected_NM_STATE_CONNECTED_GLOBAL" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    96: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_NM_STATE_UNKNOWN] Invalid name "test_is_machine_disconnected_NM_STATE_NM_STATE_UNKNOWN" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    104: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_ASLEEP_OLD] Invalid name "test_is_machine_disconnected_NM_STATE_ASLEEP_OLD" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    112: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_ASLEEP] Invalid name "test_is_machine_disconnected_NM_STATE_ASLEEP" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    120: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_CONNECTING_OLD] Invalid name "test_is_machine_disconnected_NM_STATE_CONNECTING_OLD" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    128: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_CONNECTING] Invalid name "test_is_machine_disconnected_NM_STATE_CONNECTING" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    136: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_CONNECTED_LOCAL] Invalid name "test_is_machine_disconnected_NM_STATE_CONNECTED_LOCAL" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    144: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_CONNECTED_SITE] Invalid name "test_is_machine_disconnected_NM_STATE_CONNECTED_SITE" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    152: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_DISCONNECTED_OLD] Invalid name "test_is_machine_disconnected_NM_STATE_DISCONNECTED_OLD" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    160: [C0103, TestConnection.test_is_machine_disconnected_NM_STATE_DISCONNECTED] Invalid name "test_is_machine_disconnected_NM_STATE_DISCONNECTED" (should match ([a-z_][a-z0-9_]{2,79}$|setUp|tearDown))
    174: [W0703, TestConnection.test_is_machine_defer_error] Catch "Exception"

ubuntu_sso/networkstate/tests/test_windows.py:
    184: [W0703, TestConnection.test_is_machine_connected_error] Catch "Exception"

ubuntu_sso/networkstate/windows.py:
    183: [W0702, is_machine_connected] No exception type(s) specified

review: Needs Fixing
805. By Diego Sarmentero on 2011-10-12

Fixed lint issues.

Natalia Bidart (nataliabidart) wrote :

Lint issues still appears:

== Python Lint Notices ==

ubuntu_sso/networkstate/tests/test_linux.py:
    174: [W0703, TestConnection.test_is_machine_defer_error] Catch "Exception"

ubuntu_sso/networkstate/tests/test_windows.py:
    184: [W0703, TestConnection.test_is_machine_connected_error] Catch "Exception"

ubuntu_sso/networkstate/windows.py:
    183: [W0702, is_machine_connected] No exception type(s) specified

review: Needs Fixing
Natalia Bidart (nataliabidart) wrote :

I keep having the same lint issues.

review: Needs Fixing
806. By Diego Sarmentero on 2011-10-17

Fixed lint issues

807. By Diego Sarmentero on 2011-10-18

merge

808. By Diego Sarmentero on 2011-10-18

Fixed lint issue.

Natalia Bidart (nataliabidart) wrote :

I'm now having these test errors when running ./run-tests -qt under Linux:

===============================================================================
[ERROR]
Traceback (most recent call last):
  File "/home/nessita/canonical/ussoc/review_network-detect/ubuntu_sso/qt/tests/test_controllers.py", line 755, in test_set_next_existing
    self.controller._set_next_existing()
  File "/home/nessita/canonical/ussoc/review_network-detect/ubuntu_sso/qt/controllers.py", line 168, in _set_next_existing
    self.view.check_connection()
exceptions.AttributeError: 'FakeChooseSignInView' object has no attribute 'check_connection'

ubuntu_sso.qt.tests.test_controllers.ChooseSignInControllerTestCase.test_set_next_existing
===============================================================================
[ERROR]
Traceback (most recent call last):
  File "/home/nessita/canonical/ussoc/review_network-detect/ubuntu_sso/qt/tests/test_controllers.py", line 762, in test_set_next_new
    self.controller._set_next_new()
  File "/home/nessita/canonical/ussoc/review_network-detect/ubuntu_sso/qt/controllers.py", line 174, in _set_next_new
    self.view.check_connection()
exceptions.AttributeError: 'FakeChooseSignInView' object has no attribute 'check_connection'

ubuntu_sso.qt.tests.test_controllers.ChooseSignInControllerTestCase.test_set_next_new
-------------------------------------------------------------------------------

review: Needs Fixing
Natalia Bidart (nataliabidart) wrote :

I get the same errors today.

review: Needs Fixing
809. By Diego Sarmentero on 2011-10-20

Fixed tests.

Diego Sarmentero (diegosarmentero) wrote :

> I get the same errors today.

Fixed

810. By Diego Sarmentero on 2011-10-20

Reverted commented code

Natalia Bidart (nataliabidart) wrote :

Code looks good, but I would recommend these improvements:

* Do not hardcode any message for NetworkFailException, so we can build the exception with whatever message we want/need.

* on is_machine_connected, within the except clause, I would grab the exception and use that to build the NetworkFailException. Also, always, always log the errors (the same 2 changes apply to the windows version). A mock would be:

def is_machine_connected():
    """Return a deferred that when fired, returns if the machine is online."""
    d = defer.Deferred()

    def got_state(state):
        """The state was retrieved from the Network Manager."""
        result = int(state) in NM_STATE_CONNECTED_LIST
        d.callback(result)

    try:
        network = NetworkManagerState(got_state)
        network.find_online_state()
    except Exception, e: # pylint: disable=W0703
        logger.exception('is_machine_connected failed with:')
        d.errback(NetworkFailException(e))

    return d

* This test code (the same applies to the windows version):

        # pylint: disable=W0703
        try:
            yield is_machine_connected()
        except Exception, reason:
            self.assertEqual(type(reason), NetworkFailException)
        # pylint: enable=W0703

should be written like this:

        yield self.assertFailure(is_machine_connected(), NetworkFailException)

otherwise, in your version of the code, there is no test failure if the except clause was not exercised.

* Can you also call patch in the windows tests instead of "FakeWininet.connection_state = 1"?

* No need to change this, but using "d" as a variable, in the twisted context, usually means that the 'd' is a deferred. In the TestConnection tests, you're using as the result of the deferred, so is usually a better choice to use "result = yield <the_deferred>".

review: Needs Fixing
811. By Diego Sarmentero on 2011-10-24

reverting changes

812. By Diego Sarmentero on 2011-10-24

Reverted changes from success page upload by mistake.

Natalia Bidart (nataliabidart) wrote :

* Lint error:

ubuntu_sso/networkstate/linux.py:
    145: [W0703, is_machine_connected] Catch "Exception"

* Also, there is no need to define and call super on __init__ in the NetworkFailException class.

* This docstring is not pep-257 correct:

    """Test the state of the connection.

    This TestCase tests over all the connection states possible."""

It should be:

    """Test the state of the connection.

    This TestCase tests over all the connection states possible.

    """

* Remember to fix the test test_is_machine_defer_error to use self.assertFailure (instead of a try-except block), in windows is changed but not in the linux code.

review: Needs Fixing
813. By Diego Sarmentero on 2011-10-28

Fixed test and lint issues.

Natalia Bidart (nataliabidart) wrote :

I guess this is a typo:

    """Return a deferred that when fired, will return True if the

    machine is online."""

since that is not a pep-257 compliant docstring. You could use the same docstring as in the linux implementation.

review: Needs Fixing
Natalia Bidart (nataliabidart) wrote :

Also, running the suite gives me errors, but trunk works OK:

===============================================================================
[FAIL]
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/ubuntuone-dev-tools/ubuntuone/devtools/testing/txcheck.py", line 355, in run
    raise problem
ubuntuone.devtools.testing.txcheck.MissingReturnValue: MissingReturnValue for ubuntu_sso.networkstate.tests.test_linux.TestConnection.setUp

ubuntuone.devtools.testing.txcheck.TXCheckTest.runTest
===============================================================================
[FAIL]
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/ubuntuone-dev-tools/ubuntuone/devtools/testing/txcheck.py", line 355, in run
    raise problem
ubuntuone.devtools.testing.txcheck.SuperResultDiscarded: SuperResultDiscarded for ubuntu_sso.networkstate.tests.test_linux.TestConnection.setUp

ubuntuone.devtools.testing.txcheck.TXCheckTest.runTest

814. By Diego Sarmentero on 2011-11-07

merge

815. By Diego Sarmentero on 2011-11-07

merge

816. By Diego Sarmentero on 2011-11-07

Fixed tests and lint issue.

817. By Diego Sarmentero on 2011-11-07

Tests fixed.

Natalia Bidart (nataliabidart) wrote :

Looks good!

Please fix the docstring

(01:59:04 PM) nessita: def is_machine_connected():
(01:59:04 PM) nessita: """Return a deferred that when fired, will return True if the
(01:59:04 PM) nessita: machine is online."""

before landing.

review: Approve
818. By Diego Sarmentero on 2011-11-07

Fixed docstring.

819. By Diego Sarmentero on 2011-11-11

merge

820. By Diego Sarmentero on 2011-11-14

success ui updated

821. By Diego Sarmentero on 2011-11-14

merge

822. By Diego Sarmentero on 2011-11-14

success integration.

Alejandro J. Cura (alecu) wrote :

The branch looks great!
----
Just a small request:

Since is_machine_connected() is not an async function on windows, it could be shortened a fair amount by using defer.succeed and defer.fail, like this:

def is_machine_connected():
    """Return a deferred that when fired, returns if the machine is online."""
    try:
        wininet = windll.wininet
        flags = DWORD()
        connected = wininet.InternetGetConnectedState(byref(flags), None)
        return defer.succeed(connected == 1)
    except Exception, e:
        logger.exception('is_machine_connected failed with:')
        return defer.fail(NetworkFailException(e))

review: Needs Fixing
823. By Diego Sarmentero on 2012-01-04

merge

824. By Diego Sarmentero on 2012-01-04

is_machine_connect improved.

Alejandro J. Cura (alecu) wrote :

Tests pass, code looks good: approved!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/networkstate/__init__.py'
2--- ubuntu_sso/networkstate/__init__.py 2011-11-08 15:06:00 +0000
3+++ ubuntu_sso/networkstate/__init__.py 2012-01-04 16:53:28 +0000
4@@ -26,15 +26,23 @@
5 OFFLINE = None
6 UNKNOWN = None
7
8+
9+class NetworkFailException(Exception):
10+
11+ """Exception for when the network detect process fails."""
12+
13+
14 if sys.platform == 'win32':
15 from ubuntu_sso.networkstate import windows
16 NetworkManagerState = windows.NetworkManagerState
17 ONLINE = windows.ONLINE
18 OFFLINE = windows.OFFLINE
19 UNKNOWN = windows.UNKNOWN
20+ is_machine_connected = windows.is_machine_connected
21 else:
22 from ubuntu_sso.networkstate import linux
23 NetworkManagerState = linux.NetworkManagerState
24 ONLINE = linux.ONLINE
25 OFFLINE = linux.OFFLINE
26 UNKNOWN = linux.UNKNOWN
27+ is_machine_connected = linux.is_machine_connected
28
29=== modified file 'ubuntu_sso/networkstate/linux.py'
30--- ubuntu_sso/networkstate/linux.py 2011-11-08 15:06:00 +0000
31+++ ubuntu_sso/networkstate/linux.py 2012-01-04 16:53:28 +0000
32@@ -21,6 +21,9 @@
33
34 import dbus
35
36+from twisted.internet import defer
37+
38+from ubuntu_sso.networkstate import NetworkFailException
39 from ubuntu_sso.logger import setup_logging
40 logger = setup_logging("ubuntu_sso.networkstate")
41
42@@ -125,3 +128,24 @@
43 error_handler=self.got_error)
44 except Exception, e: # pylint: disable=W0703
45 self.got_error(e)
46+
47+
48+def is_machine_connected():
49+ """Return a deferred that when fired, returns if the machine is online."""
50+ d = defer.Deferred()
51+
52+ def got_state(state):
53+ """The state was retrieved from the Network Manager."""
54+ result = int(state) in NM_STATE_CONNECTED_LIST
55+ d.callback(result)
56+
57+ # pylint: disable=W0702, W0703
58+ try:
59+ network = NetworkManagerState(got_state)
60+ network.find_online_state()
61+ except Exception, e:
62+ logger.exception('is_machine_connected failed with:')
63+ d.errback(NetworkFailException(e))
64+ # pylint: enable=W0702, W0703
65+
66+ return d
67
68=== modified file 'ubuntu_sso/networkstate/tests/test_linux.py'
69--- ubuntu_sso/networkstate/tests/test_linux.py 2011-11-08 15:06:00 +0000
70+++ ubuntu_sso/networkstate/tests/test_linux.py 2012-01-04 16:53:28 +0000
71@@ -19,9 +19,18 @@
72 # with this program. If not, see <http://www.gnu.org/licenses/>.
73 """Tests for the network state detection code."""
74
75+from twisted.internet.defer import inlineCallbacks
76+
77+from ubuntu_sso.tests import TestCase
78 from ubuntu_sso.networkstate import (NetworkManagerState,
79 ONLINE, OFFLINE, UNKNOWN)
80-from ubuntu_sso.networkstate.linux import (DBUS_UNKNOWN_SERVICE,
81+
82+from ubuntu_sso.networkstate import NetworkFailException
83+from ubuntu_sso.networkstate import linux
84+from ubuntu_sso.networkstate.linux import (is_machine_connected,
85+ DBUS_UNKNOWN_SERVICE,
86+ NM_STATE_ASLEEP,
87+ NM_STATE_ASLEEP_OLD,
88 NM_STATE_CONNECTING,
89 NM_STATE_CONNECTING_OLD,
90 NM_STATE_CONNECTED_OLD,
91@@ -29,7 +38,8 @@
92 NM_STATE_CONNECTED_SITE,
93 NM_STATE_CONNECTED_GLOBAL,
94 NM_STATE_DISCONNECTED,
95- NM_STATE_DISCONNECTED_OLD)
96+ NM_STATE_DISCONNECTED_OLD,
97+ NM_STATE_UNKNOWN)
98
99 from mocker import ARGS, KWARGS, ANY, MockerTestCase
100
101@@ -41,6 +51,131 @@
102 return "Test Exception Message"
103
104
105+class FakeNetworkManagerState(object):
106+
107+ """Fake Network Manager State."""
108+
109+ connection_state = None
110+
111+ def __init__(self, function):
112+ """Initialize Fake class."""
113+ self.call_function = function
114+
115+ def find_online_state(self):
116+ """Fake find_online_state for linux module."""
117+ self.call_function(self.connection_state)
118+
119+
120+class TestConnection(TestCase):
121+
122+ """Test the state of the connection.
123+
124+ This TestCase tests over all the connection states possible.
125+
126+ """
127+
128+ @inlineCallbacks
129+ def setUp(self):
130+ """Setup the mocker dbus object tree."""
131+ yield super(TestConnection, self).setUp()
132+ self.patch(linux, "NetworkManagerState", FakeNetworkManagerState)
133+
134+ @inlineCallbacks
135+ def test_is_machine_connected_nm_state_connected_old(self):
136+ """Fake the NetworkManagerState with connected return."""
137+ self.patch(FakeNetworkManagerState, "connection_state",
138+ NM_STATE_CONNECTED_OLD)
139+ d = yield is_machine_connected()
140+ self.assertTrue(d)
141+
142+ @inlineCallbacks
143+ def test_is_machine_connected_nm_state_connected_global(self):
144+ """Fake the NetworkManagerState with connected return."""
145+ self.patch(FakeNetworkManagerState, "connection_state",
146+ NM_STATE_CONNECTED_GLOBAL)
147+ d = yield is_machine_connected()
148+ self.assertTrue(d)
149+
150+ @inlineCallbacks
151+ def test_is_machine_disconnected_nm_state_nm_state_unknown(self):
152+ """Fake the NetworkManagerState with disconnected return."""
153+ self.patch(FakeNetworkManagerState, "connection_state",
154+ NM_STATE_UNKNOWN)
155+ d = yield is_machine_connected()
156+ self.assertFalse(d)
157+
158+ @inlineCallbacks
159+ def test_is_machine_disconnected_nm_state_asleep_old(self):
160+ """Fake the NetworkManagerState with disconnected return."""
161+ self.patch(FakeNetworkManagerState, "connection_state",
162+ NM_STATE_ASLEEP_OLD)
163+ d = yield is_machine_connected()
164+ self.assertFalse(d)
165+
166+ @inlineCallbacks
167+ def test_is_machine_disconnected_nm_state_asleep(self):
168+ """Fake the NetworkManagerState with disconnected return."""
169+ self.patch(FakeNetworkManagerState, "connection_state",
170+ NM_STATE_ASLEEP)
171+ d = yield is_machine_connected()
172+ self.assertFalse(d)
173+
174+ @inlineCallbacks
175+ def test_is_machine_disconnected_nm_state_connecting_old(self):
176+ """Fake the NetworkManagerState with disconnected return."""
177+ self.patch(FakeNetworkManagerState, "connection_state",
178+ NM_STATE_CONNECTING_OLD)
179+ d = yield is_machine_connected()
180+ self.assertFalse(d)
181+
182+ @inlineCallbacks
183+ def test_is_machine_disconnected_nm_state_connecting(self):
184+ """Fake the NetworkManagerState with disconnected return."""
185+ self.patch(FakeNetworkManagerState, "connection_state",
186+ NM_STATE_CONNECTING)
187+ d = yield is_machine_connected()
188+ self.assertFalse(d)
189+
190+ @inlineCallbacks
191+ def test_is_machine_disconnected_nm_state_connected_local(self):
192+ """Fake the NetworkManagerState with disconnected return."""
193+ self.patch(FakeNetworkManagerState, "connection_state",
194+ NM_STATE_CONNECTED_LOCAL)
195+ d = yield is_machine_connected()
196+ self.assertFalse(d)
197+
198+ @inlineCallbacks
199+ def test_is_machine_disconnected_nm_state_connected_site(self):
200+ """Fake the NetworkManagerState with disconnected return."""
201+ self.patch(FakeNetworkManagerState, "connection_state",
202+ NM_STATE_CONNECTED_SITE)
203+ d = yield is_machine_connected()
204+ self.assertFalse(d)
205+
206+ @inlineCallbacks
207+ def test_is_machine_disconnected_nm_state_disconnected_old(self):
208+ """Fake the NetworkManagerState with disconnected return."""
209+ self.patch(FakeNetworkManagerState, "connection_state",
210+ NM_STATE_DISCONNECTED_OLD)
211+ d = yield is_machine_connected()
212+ self.assertFalse(d)
213+
214+ @inlineCallbacks
215+ def test_is_machine_disconnected_nm_state_disconnected(self):
216+ """Fake the NetworkManagerState with disconnected return."""
217+ self.patch(FakeNetworkManagerState, "connection_state",
218+ NM_STATE_DISCONNECTED)
219+ d = yield is_machine_connected()
220+ self.assertFalse(d)
221+
222+ @inlineCallbacks
223+ def test_is_machine_defer_error(self):
224+ """Fake the NetworkManagerState with disconnected return."""
225+ self.patch(FakeNetworkManagerState, "connection_state",
226+ 'no-int')
227+ yield self.assertFailure(is_machine_connected(), NetworkFailException)
228+
229+
230 class TestNmNotAvailableException(Exception):
231
232 """An exception to test unavailability conditions."""
233
234=== modified file 'ubuntu_sso/networkstate/tests/test_windows.py'
235--- ubuntu_sso/networkstate/tests/test_windows.py 2011-11-08 15:06:00 +0000
236+++ ubuntu_sso/networkstate/tests/test_windows.py 2012-01-04 16:53:28 +0000
237@@ -16,8 +16,15 @@
238 # You should have received a copy of the GNU General Public License along
239 # with this program. If not, see <http://www.gnu.org/licenses/>.
240 """Tests for the network manager."""
241+
242+from ctypes import windll
243 from mocker import MockerTestCase
244+from twisted.internet.defer import inlineCallbacks
245+
246+from ubuntu_sso.tests import TestCase
247+from ubuntu_sso.networkstate import NetworkFailException
248 from ubuntu_sso.networkstate.windows import (
249+ is_machine_connected,
250 NetworkManager,
251 NetworkManagerState,
252 ONLINE,
253@@ -117,3 +124,60 @@
254 self.mocker.replay()
255 self.state.find_online_state(listener=self.network_manager,
256 listener_thread=self.thread)
257+
258+
259+class FakeWininet(object):
260+
261+ """Fake wininet for windll."""
262+
263+ connection_state = -1
264+
265+ # pylint: disable=C0103
266+ def InternetGetConnectedState(self, *args, **kwargs):
267+ """Fake InternetGetConnectedState function from wininet."""
268+ return self.connection_state
269+ # pylint: enable=C0103
270+
271+
272+class FakeWininetException(object):
273+
274+ """Fake wininet for windll."""
275+
276+ connection_state = -1
277+
278+ # pylint: disable=C0103
279+ def InternetGetConnectedState(self, *args, **kwargs):
280+ """Fake InternetGetConnectedState function from wininet."""
281+ raise Exception()
282+ # pylint: enable=C0103
283+
284+
285+class TestConnection(TestCase):
286+
287+ """Test the state of the connection."""
288+
289+ @inlineCallbacks
290+ def setUp(self):
291+ """Setup the mocker dbus object tree."""
292+ yield super(TestConnection, self).setUp()
293+ self.patch(windll, "wininet", FakeWininet())
294+
295+ @inlineCallbacks
296+ def test_is_machine_connected_connected(self):
297+ """Fake the NetworkManagerState."""
298+ self.patch(FakeWininet, "connection_state", 1)
299+ result = yield is_machine_connected()
300+ self.assertTrue(result)
301+
302+ @inlineCallbacks
303+ def test_is_machine_connected_disconnected(self):
304+ """Fake the NetworkManagerState."""
305+ self.patch(FakeWininet, "connection_state", 0)
306+ result = yield is_machine_connected()
307+ self.assertFalse(result)
308+
309+ @inlineCallbacks
310+ def test_is_machine_connected_error(self):
311+ """Fake the NetworkManagerState."""
312+ self.patch(windll, "wininet", FakeWininetException())
313+ yield self.assertFailure(is_machine_connected(), NetworkFailException)
314
315=== modified file 'ubuntu_sso/networkstate/windows.py'
316--- ubuntu_sso/networkstate/windows.py 2011-11-08 15:06:00 +0000
317+++ ubuntu_sso/networkstate/windows.py 2012-01-04 16:53:28 +0000
318@@ -26,11 +26,13 @@
319 from ctypes.wintypes import DWORD
320 from threading import Thread
321
322+from twisted.internet import defer
323 # pylint: disable=F0401
324 from win32com.server.policy import DesignatedWrapPolicy
325 from win32com.client import Dispatch
326 # pylint: enable=F0401
327
328+from ubuntu_sso.networkstate import NetworkFailException
329 from ubuntu_sso.logger import setup_logging
330
331 logger = setup_logging("ubuntu_sso.networkstate")
332@@ -163,11 +165,17 @@
333
334
335 def is_machine_connected():
336- """The state was retrieved from the Network Manager."""
337- wininet = windll.wininet
338- flags = DWORD()
339- connected = wininet.InternetGetConnectedState(byref(flags), None)
340- return connected == 1
341+ """Return a deferred that when fired, returns if the machine is online."""
342+ # pylint: disable=W0703, W0702
343+ try:
344+ wininet = windll.wininet
345+ flags = DWORD()
346+ connected = wininet.InternetGetConnectedState(byref(flags), None)
347+ return defer.succeed(connected == 1)
348+ except Exception, e:
349+ logger.exception('is_machine_connected failed with:')
350+ return defer.fail(NetworkFailException(e))
351+ # pylint: enable=W0703, W0702
352
353
354 class NetworkManagerState(object):
355
356=== modified file 'ubuntu_sso/qt/controllers.py'
357--- ubuntu_sso/qt/controllers.py 2011-12-12 17:42:20 +0000
358+++ ubuntu_sso/qt/controllers.py 2012-01-04 16:53:28 +0000
359@@ -164,13 +164,13 @@
360 """Set the next id and fire signal."""
361 logger.debug('ChooseSignInController._set_next_existing')
362 self.view.next = self.view.wizard().current_user_page_id
363- self.view.wizard().next()
364+ self.view.check_connection()
365
366 def _set_next_new(self):
367 """Set the next id and fire signal."""
368 logger.debug('ChooseSignInController._set_next_new')
369 self.view.next = self.view.wizard().setup_account_page_id
370- self.view.wizard().next()
371+ self.view.check_connection()
372
373
374 class CurrentUserController(BackendController):
375
376=== modified file 'ubuntu_sso/qt/tests/test_controllers.py'
377--- ubuntu_sso/qt/tests/test_controllers.py 2011-12-01 12:13:42 +0000
378+++ ubuntu_sso/qt/tests/test_controllers.py 2012-01-04 16:53:28 +0000
379@@ -201,6 +201,10 @@
380 self.existing_account_button = FakeButton()
381 self.setup_account_button = FakeButton()
382
383+ def check_connection(self):
384+ """Fake Check Connection."""
385+ self.properties['check_connection'] = True
386+
387
388 class FakeCurrentUserView(FakeGenericView):
389
390@@ -759,16 +763,15 @@
391 def test_set_next_existing(self):
392 """Test the execution of the callback."""
393 self.controller._set_next_existing()
394- self.assertTrue(self.view.properties['wizard_next'])
395 self.assertEqual(self.view.next,
396 self.view.fake_wizard.current_user_page_id)
397
398 def test_set_next_new(self):
399 """Test the execution of the callback."""
400 self.controller._set_next_new()
401- self.assertTrue(self.view.properties['wizard_next'])
402 self.assertEqual(self.view.next,
403 self.view.fake_wizard.setup_account_page_id)
404+ self.assertTrue(self.view.properties.get('check_connection', False))
405
406
407 class CurrentUserControllerTestCase(BaseTestCase):

Subscribers

People subscribed via source and target branches