def setUp(self):
     self.g = GoogleGroupsFree(user='******', password='******', group='test')
 def setUp(self):
     self.g = GoogleGroupsFree(
         user='******',
         password='******',
         group='test')
class TestOcupadoPluginGoogleGroupsFree(unittest.TestCase):
    """
    Tests the GoogleGroupsFree plugin.
    """
    def setUp(self):
        self.g = GoogleGroupsFree(user='******', password='******', group='test')

    def test_plugin_google_groups_free_init(self):
        self.assertEquals(self.g._user, 'user')
        self.assertEquals(self.g._password, 'secret')
        self.assertEquals(self.g._group, 'test')

    def test_plugin_google_groups_free_authenticate(self):
        with mock.patch('mechanize.Browser.open') as _open:
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(LOGIN_HTML, RESPONSE_HEADERS,
                                                  LOGIN_URL, 200, '200 OK'),
                False)
            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)
            # authenticate() shouldn't return anything
            self.assertEquals(self.g.authenticate(), None)
            # There should have been two calls
            self.assertEquals(_open.call_count, 2)
            # The first call should have oly taken in the login url
            self.assertEquals(_open.call_args_list[0][0][0], LOGIN_URL)

    def test_plugin_google_groups_free_logout(self):
        with nested(mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.title')) as (_open, _title):

            # We do not inject the fake auth cookies here since open()
            # would be in charge of removing them. That means our test
            # would manually add then manually remove the cookies.

            _title.return_value = 'Google Accounts'
            # logout() should return nothing
            self.assertEquals(self.g.logout(), None)
            _open.assert_called_once_with(LOGOUT_URL)

    def test_plugin_google_groups_free_exists(self):
        with nested(mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.retrieve'),
                    mock.patch('os.unlink')) as (_open, _retrieve, _unlink):
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(LOGIN_HTML, RESPONSE_HEADERS,
                                                  LOGIN_URL, 200, '200 OK'),
                False)
            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)

            self.g.authenticate()

            # Point the retrieve call to test/members.csv
            _retrieve.return_value = ('test/members.csv', [])

            exists, info = self.g.exists('human')
            self.assertTrue(exists)
            self.assertTrue(info['exists'])
            self.assertEquals(info['details']['username'], 'human')

            # Verify the temporary csv file would be gone
            _unlink.assert_called_once_with('test/members.csv')

    def test_plugin_google_groups_free_exists_with_no_results(self):
        with nested(mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.retrieve'),
                    mock.patch('os.unlink')) as (_open, _retrieve, _unlink):
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(LOGIN_HTML, RESPONSE_HEADERS,
                                                  LOGIN_URL, 200, '200 OK'),
                False)

            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)

            self.g.authenticate()

            # Point the retrieve call to test/members.csv
            _retrieve.return_value = ('test/nomembers.csv', [])

            exists, info = self.g.exists('doesnotexist')
            self.assertFalse(exists)
            self.assertFalse(info['exists'])
            self.assertEquals(info['details']['username'], 'doesnotexist')

            # Verify the temporary csv file would be gone
            _unlink.assert_called_once_with('test/nomembers.csv')

    def test_plugin_google_groups_free_get_all_usernames(self):
        with nested(mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.retrieve'),
                    mock.patch('os.unlink')) as (_open, _retrieve, _unlink):
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(LOGIN_HTML, RESPONSE_HEADERS,
                                                  LOGIN_URL, 200, '200 OK'),
                False)

            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)

            self.g.authenticate()

            # Point the retrieve call to test/members.csv
            _retrieve.return_value = ('test/members.csv', [])

            users = self.g.get_all_usernames()
            # Verify the contents
            self.assertIn('human', users)
            self.assertIn('robot', users)
            self.assertNotIn('notthere', users)

            # Verify the temporary csv file would be gone
            _unlink.assert_called_once_with('test/members.csv')
class TestOcupadoPluginGoogleGroupsFree(unittest.TestCase):
    """
    Tests the GoogleGroupsFree plugin.
    """

    def setUp(self):
        self.g = GoogleGroupsFree(
            user='******',
            password='******',
            group='test')

    def test_plugin_google_groups_free_init(self):
        self.assertEquals(self.g._user, 'user')
        self.assertEquals(self.g._password, 'secret')
        self.assertEquals(self.g._group, 'test')

    def test_plugin_google_groups_free_authenticate(self):
        with mock.patch('mechanize.Browser.open') as _open:
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(
                    LOGIN_HTML, RESPONSE_HEADERS,
                    LOGIN_URL, 200, '200 OK'), False)
            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)
            # authenticate() shouldn't return anything
            self.assertEquals(self.g.authenticate(), None)
            # There should have been two calls
            self.assertEquals(_open.call_count, 2)
            # The first call should have oly taken in the login url
            self.assertEquals(_open.call_args_list[0][0][0], LOGIN_URL)

    def test_plugin_google_groups_free_logout(self):
        with nested(
                    mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.title')
                ) as (_open, _title):

            # We do not inject the fake auth cookies here since open()
            # would be in charge of removing them. That means our test
            # would manually add then manually remove the cookies.

            _title.return_value = 'Google Accounts'
            # logout() should return nothing
            self.assertEquals(self.g.logout(), None)
            _open.assert_called_once_with(LOGOUT_URL)

    def test_plugin_google_groups_free_exists(self):
        with nested(
                    mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.retrieve'),
                    mock.patch('os.unlink')
                ) as (_open, _retrieve, _unlink):
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(
                    LOGIN_HTML, RESPONSE_HEADERS,
                    LOGIN_URL, 200, '200 OK'), False)
            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)

            self.g.authenticate()

            # Point the retrieve call to test/members.csv
            _retrieve.return_value = ('test/members.csv', [])

            exists, info = self.g.exists('human')
            self.assertTrue(exists)
            self.assertTrue(info['exists'])
            self.assertEquals(info['details']['username'], 'human')

            # Verify the temporary csv file would be gone
            _unlink.assert_called_once_with('test/members.csv')

    def test_plugin_google_groups_free_exists_with_no_results(self):
        with nested(
                    mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.retrieve'),
                    mock.patch('os.unlink')
                ) as (_open, _retrieve, _unlink):
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(
                    LOGIN_HTML, RESPONSE_HEADERS,
                    LOGIN_URL, 200, '200 OK'), False)

            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)

            self.g.authenticate()

            # Point the retrieve call to test/members.csv
            _retrieve.return_value = ('test/nomembers.csv', [])

            exists, info = self.g.exists('doesnotexist')
            self.assertFalse(exists)
            self.assertFalse(info['exists'])
            self.assertEquals(info['details']['username'], 'doesnotexist')

            # Verify the temporary csv file would be gone
            _unlink.assert_called_once_with('test/nomembers.csv')

    def test_plugin_google_groups_free_get_all_usernames(self):
        with nested(
                    mock.patch('mechanize.Browser.open'),
                    mock.patch('mechanize.Browser.retrieve'),
                    mock.patch('os.unlink')
                ) as (_open, _retrieve, _unlink):
            # Setting return data for the authenticate() state
            self.g._con._set_response(
                mechanize._response.make_response(
                    LOGIN_HTML, RESPONSE_HEADERS,
                    LOGIN_URL, 200, '200 OK'), False)

            # Inject the fake auth cookies for testing
            set_auth_cookies(self.g._cookies)

            self.g.authenticate()

            # Point the retrieve call to test/members.csv
            _retrieve.return_value = ('test/members.csv', [])

            users = self.g.get_all_usernames()
            # Verify the contents
            self.assertIn('human', users)
            self.assertIn('robot', users)
            self.assertNotIn('notthere', users)

            # Verify the temporary csv file would be gone
            _unlink.assert_called_once_with('test/members.csv')