mirror of
git://git.psyced.org/git/pypsyc
synced 2024-08-15 03:20:04 +00:00
added new pypsyc as 'mjacob2'
This commit is contained in:
parent
0abc7a0888
commit
9f7c4147c7
46 changed files with 7243 additions and 0 deletions
680
mjacob2/tests/test_client/test_controller.py
Normal file
680
mjacob2/tests/test_client/test_controller.py
Normal file
|
@ -0,0 +1,680 @@
|
|||
"""
|
||||
:copyright: 2010 by Manuel Jacob
|
||||
:license: MIT
|
||||
"""
|
||||
from mock import Mock, MagicMock
|
||||
from nose.tools import assert_raises
|
||||
from tests.constants import (SERVER1, USER1_UNI, USER2_UNI, USER1_NICK,
|
||||
USER2_NICK, PLACE_UNI, SERVER2_UNI, MESSAGE, PRESENCE_OFFLINE,
|
||||
PRESENCE_ONLINE, ERROR, EXCEPTION)
|
||||
from tests.helpers import mockified, AsyncMethod, PlaceHolder, iter_
|
||||
|
||||
from pypsyc.client.controller import (ListBinding, AccountsController,
|
||||
ConversationController, ConferenceController, TabsController,
|
||||
FriendListController, DumpController, MainController)
|
||||
from pypsyc.client.model import Message
|
||||
from pypsyc.client.observable import ObsList, ObsDict, ObsObj, ObsAttr
|
||||
from pypsyc.util import Event
|
||||
|
||||
|
||||
def _check_obs_list(obs_list):
|
||||
assert obs_list.setitem_evt.observers == []
|
||||
assert obs_list.delitem_evt.observers == []
|
||||
assert obs_list.insert_evt.observers == []
|
||||
assert obs_list.update_evt.observers == []
|
||||
return True
|
||||
|
||||
|
||||
def _check_obs_dict(obs_dict):
|
||||
assert obs_dict.setitem_evt.observers == []
|
||||
assert obs_dict.delitem_evt.observers == []
|
||||
assert obs_dict.update_evt.observers == []
|
||||
return True
|
||||
|
||||
|
||||
def _check_obs_obj(obs_obj):
|
||||
assert obs_obj.update_evt != {}
|
||||
for evt in obs_obj.update_evt.itervalues():
|
||||
assert evt.observers == []
|
||||
return True
|
||||
|
||||
|
||||
class TestListBinding(object):
|
||||
def setup(self):
|
||||
self.model = Mock()
|
||||
self.model_list = ObsList([self.model])
|
||||
self.view_list = []
|
||||
self.make_row = Mock()
|
||||
self.binding = ListBinding(self.model_list, self.view_list,
|
||||
self.make_row)
|
||||
|
||||
def test_bind(self):
|
||||
assert self.make_row.call_args_list == [((self.model,),)]
|
||||
assert self.view_list == [self.make_row.return_value]
|
||||
|
||||
def test_add(self):
|
||||
assert self.make_row.call_args_list == [((self.model,),)]
|
||||
self.make_row.reset_mock()
|
||||
|
||||
model2 = Mock()
|
||||
self.model_list.append(model2)
|
||||
assert self.make_row.call_args_list == [((model2,),)]
|
||||
assert self.view_list == [self.make_row.return_value] * 2
|
||||
|
||||
def test_update(self):
|
||||
assert self.make_row.call_args_list == [((self.model,),)]
|
||||
self.make_row.reset_mock()
|
||||
|
||||
self.model_list.updated_item(self.model)
|
||||
assert self.make_row.call_args_list == [((self.model,),)]
|
||||
assert self.view_list == [self.make_row.return_value]
|
||||
|
||||
def test_delete(self):
|
||||
del self.model_list[0]
|
||||
assert self.view_list == []
|
||||
|
||||
def test_unbind(self):
|
||||
self.binding.unbind()
|
||||
assert _check_obs_list(self.model_list)
|
||||
assert self.view_list == []
|
||||
|
||||
|
||||
class _List(list):
|
||||
def __init__(self, *args, **kwds):
|
||||
list.__init__(self, *args, **kwds)
|
||||
self.updated_item = Mock()
|
||||
|
||||
class TestAccountsController(object):
|
||||
@mockified('pypsyc.client.controller', ['ListBinding'])
|
||||
def setup(self, ListBinding):
|
||||
self.ListBinding = ListBinding
|
||||
self.account = Mock()
|
||||
self.client = Mock()
|
||||
self.client.accounts = _List([self.account])
|
||||
self.view = Mock()
|
||||
self.controller = AccountsController(self.client, self.view)
|
||||
|
||||
def test_binding(self):
|
||||
make_row_ph = PlaceHolder()
|
||||
assert self.ListBinding.call_args_list == [
|
||||
((self.client.accounts, self.view.accounts, make_row_ph),)]
|
||||
|
||||
account = Mock()
|
||||
row = make_row_ph.obj(account)
|
||||
assert row == (account.uni, account.active)
|
||||
|
||||
@mockified('pypsyc.client.controller', ['Account'])
|
||||
def test_add_account(self, Account):
|
||||
account = Account.return_value
|
||||
callback_ph = PlaceHolder()
|
||||
|
||||
self.controller.add_account()
|
||||
assert Account.call_args_list == [
|
||||
((self.client, '', '', '', False, False),)]
|
||||
assert self.view.method_calls == [
|
||||
('show_addedit_dialog', (account.__dict__, True, callback_ph))]
|
||||
|
||||
callback_ph.obj()
|
||||
assert self.client.accounts == [self.account, account]
|
||||
assert self.client.method_calls == [('save_accounts',)]
|
||||
|
||||
def test_edit_account(self):
|
||||
callback_ph = PlaceHolder()
|
||||
|
||||
self.controller.edit_account(0)
|
||||
assert self.view.method_calls == [
|
||||
('show_addedit_dialog', (self.account.__dict__, False,
|
||||
callback_ph))]
|
||||
|
||||
callback_ph.obj()
|
||||
assert self.client.accounts.updated_item.call_args_list == [
|
||||
((self.account,),)]
|
||||
assert self.client.method_calls == [('save_accounts',)]
|
||||
|
||||
def test_remove_account(self):
|
||||
assert_raises(IndexError, self.controller.remove_account, 1)
|
||||
self.controller.remove_account(0)
|
||||
assert self.client.accounts == []
|
||||
assert self.client.method_calls == [('save_accounts',)]
|
||||
|
||||
def test_set_active(self):
|
||||
self.controller.set_active(0, True)
|
||||
assert self.account.active == True
|
||||
assert self.client.accounts.updated_item.call_args_list == [
|
||||
((self.account,),)]
|
||||
assert self.client.method_calls == [('save_accounts',)]
|
||||
|
||||
def test_closed(self):
|
||||
self.controller.closed()
|
||||
assert self.ListBinding.return_value.method_calls == [('unbind',)]
|
||||
|
||||
|
||||
IN = 'i'
|
||||
OUT = 'o'
|
||||
LINE = 'line'
|
||||
|
||||
class _StubAccount(ObsObj):
|
||||
circuit = ObsAttr('circuit')
|
||||
|
||||
class TestDumpController:
|
||||
def setup(self):
|
||||
self.circuit = Mock()
|
||||
self.circuit.dump_evt = Event()
|
||||
self.account = _StubAccount()
|
||||
self.account.uni = USER1_UNI
|
||||
self.client = Mock()
|
||||
self.client.accounts = ObsList()
|
||||
self.view = Mock()
|
||||
|
||||
def test_connected(self):
|
||||
self.account.circuit = self.circuit
|
||||
self.client.accounts.append(self.account)
|
||||
DumpController(self.client, self.view)
|
||||
|
||||
self.circuit.dump_evt(IN, LINE)
|
||||
assert self.view.method_calls == [('show_line', (IN, LINE, USER1_UNI))]
|
||||
|
||||
def test_connect(self):
|
||||
DumpController(self.client, self.view)
|
||||
self.client.accounts.append(self.account)
|
||||
self.account.circuit = self.circuit
|
||||
|
||||
self.circuit.dump_evt(IN, LINE)
|
||||
assert self.view.method_calls == [('show_line', (IN, LINE, USER1_UNI))]
|
||||
|
||||
def test_disconnect(self):
|
||||
self.account.circuit = self.circuit
|
||||
self.client.accounts.append(self.account)
|
||||
DumpController(self.client, self.view)
|
||||
|
||||
del self.account.circuit
|
||||
assert self.circuit.dump_evt.observers == []
|
||||
|
||||
def test_reconnect(self):
|
||||
DumpController(self.client, self.view)
|
||||
self.account.circuit = self.circuit
|
||||
self.client.accounts.append(self.account)
|
||||
|
||||
circuit2 = Mock()
|
||||
circuit2.dump_evt = Event()
|
||||
self.account.circuit = circuit2
|
||||
assert self.circuit.dump_evt.observers == []
|
||||
|
||||
circuit2.dump_evt(OUT, LINE)
|
||||
assert self.view.method_calls == [
|
||||
('show_line', (OUT, LINE, USER1_UNI))]
|
||||
|
||||
def test_remove(self):
|
||||
self.client.accounts.append(self.account)
|
||||
self.account.circuit = self.circuit
|
||||
DumpController(self.client, self.view)
|
||||
|
||||
self.client.accounts.remove(self.account)
|
||||
assert _check_obs_obj(self.account)
|
||||
assert self.circuit.dump_evt.observers == []
|
||||
|
||||
def test_remove_nocircuit(self):
|
||||
self.client.accounts.append(self.account)
|
||||
DumpController(self.client, self.view)
|
||||
|
||||
self.client.accounts.remove(self.account)
|
||||
assert _check_obs_obj(self.account)
|
||||
|
||||
def test_closed(self):
|
||||
self.client.accounts.append(self.account)
|
||||
self.account.circuit = self.circuit
|
||||
dump_controller = DumpController(self.client, self.view)
|
||||
|
||||
dump_controller.closed()
|
||||
assert _check_obs_list(self.client.accounts)
|
||||
assert _check_obs_obj(self.account)
|
||||
assert self.circuit.dump_evt.observers == []
|
||||
|
||||
|
||||
class TestConversationController(object):
|
||||
def setup(self):
|
||||
self.tabs_controller = Mock()
|
||||
self.conversation = Mock()
|
||||
self.conversation.messages = ObsList()
|
||||
self.conversation.unknown_target_evt = Event()
|
||||
self.conversation.delivery_failed_evt = Event()
|
||||
self.view = Mock()
|
||||
self.controller = ConversationController(self.tabs_controller,
|
||||
self.conversation, self.view)
|
||||
|
||||
def test_message(self):
|
||||
MESSAGE_OBJ = Message(USER2_UNI, MESSAGE)
|
||||
MESSAGE_LINE = "(%s) <%s> %s" % (
|
||||
MESSAGE_OBJ.time.strftime("%H:%M:%S"), USER2_UNI, MESSAGE)
|
||||
|
||||
self.conversation.messages.append(MESSAGE_OBJ)
|
||||
assert self.view.method_calls == [('show_message', (MESSAGE_LINE,))]
|
||||
|
||||
def test_unknown_target(self):
|
||||
self.conversation.unknown_target_evt()
|
||||
assert self.view.method_calls == [('show_unknown_target',)]
|
||||
|
||||
def test_delivery_failed(self):
|
||||
self.conversation.delivery_failed_evt(ERROR)
|
||||
assert self.view.method_calls == [('show_delivery_failed', (ERROR,))]
|
||||
|
||||
def test_enter_message(self):
|
||||
self.controller.enter(MESSAGE)
|
||||
assert self.conversation.method_calls == [('send_message', (MESSAGE,))]
|
||||
|
||||
def test_enter_command(self):
|
||||
self.controller.enter("/command")
|
||||
assert self.view.method_calls == [('show_unknown_command',)]
|
||||
assert self.conversation.method_calls == []
|
||||
|
||||
def test_closed(self):
|
||||
self.conversation.messages.append(Message(None, None))
|
||||
self.controller.closed()
|
||||
assert _check_obs_list(self.conversation.messages)
|
||||
assert self.conversation.unknown_target_evt.observers == []
|
||||
assert self.conversation.delivery_failed_evt.observers == []
|
||||
|
||||
|
||||
class TestConferenceController(object):
|
||||
@mockified('pypsyc.client.controller', ['ListBinding'])
|
||||
def setup(self, ListBinding):
|
||||
self.ListBinding = ListBinding
|
||||
self.tabs_controller = Mock()
|
||||
self.conference = Mock()
|
||||
self.conference.members = []
|
||||
self.conference.messages = ObsList()
|
||||
self.conference.unknown_target_evt = Event()
|
||||
self.conference.delivery_failed_evt = Event()
|
||||
self.view = Mock()
|
||||
self.controller = ConferenceController(self.tabs_controller,
|
||||
self.conference, self.view)
|
||||
|
||||
def test_binding(self):
|
||||
makerow_ph = PlaceHolder()
|
||||
assert self.ListBinding.call_args_list == [
|
||||
((self.conference.members, self.view.members, makerow_ph),)]
|
||||
|
||||
member = Mock()
|
||||
row = makerow_ph.obj(member)
|
||||
assert row == (member.uni, member.nick)
|
||||
|
||||
def test_open_conversation(self):
|
||||
member = Mock()
|
||||
self.conference.members.append(member)
|
||||
account = self.conference.conferencing.account
|
||||
conversation = account.get_conversation.return_value
|
||||
|
||||
self.controller.open_conversation(0)
|
||||
assert self.conference.conferencing.account.method_calls == [
|
||||
('get_conversation', (member.uni,))]
|
||||
assert self.tabs_controller.method_calls == [
|
||||
('focus_conversation', (conversation,))]
|
||||
|
||||
def test_closed(self):
|
||||
self.controller.closed()
|
||||
assert self.ListBinding.return_value.method_calls == [('unbind',)]
|
||||
assert _check_obs_list(self.conference.messages)
|
||||
assert self.conference.unknown_target_evt.observers == []
|
||||
assert self.conference.delivery_failed_evt.observers == []
|
||||
|
||||
|
||||
class TestTabsController(object):
|
||||
def setup(self):
|
||||
self.client = Mock()
|
||||
self.client.accounts = ObsList()
|
||||
self.view = Mock()
|
||||
|
||||
self.account = Mock()
|
||||
self.account.conversations = ObsDict()
|
||||
self.account.conferences = ObsDict()
|
||||
self.client.accounts.append(self.account)
|
||||
|
||||
@mockified('pypsyc.client.controller', ['ConversationController',
|
||||
'ConferenceController'])
|
||||
def test_deleted_account(self, ConversationController,
|
||||
ConferenceController):
|
||||
self.account.conversations[USER2_UNI] = Mock()
|
||||
self.account.conferences[PLACE_UNI] = Mock()
|
||||
conv_view = self.view.show_conversation.return_value
|
||||
conv_controller = ConversationController.return_value
|
||||
conf_view = self.view.show_conference.return_value
|
||||
conf_controller = ConferenceController.return_value
|
||||
TabsController(self.client, self.view)
|
||||
self.view.reset_mock()
|
||||
|
||||
del self.client.accounts[0]
|
||||
assert _check_obs_dict(self.account.conversations)
|
||||
assert _check_obs_dict(self.account.conferences)
|
||||
assert self.view.method_calls == [('remove_tab', (conv_view,)),
|
||||
('remove_tab', (conf_view,))]
|
||||
assert conv_controller.method_calls == [('closed',)]
|
||||
assert conf_controller.method_calls == [('closed',)]
|
||||
|
||||
@mockified('pypsyc.client.controller', ['ConversationController'])
|
||||
def test_conversation(self, ConversationController):
|
||||
conversation = Mock()
|
||||
conversation.uni = USER2_UNI
|
||||
conv_view = self.view.show_conversation.return_value
|
||||
conv_controller = ConversationController.return_value
|
||||
controller = TabsController(self.client, self.view)
|
||||
|
||||
self.account.conversations[USER2_UNI] = conversation
|
||||
assert self.view.method_calls == [('show_conversation', (USER2_UNI,))]
|
||||
assert ConversationController.call_args_list == [
|
||||
((controller, conversation, conv_view),)]
|
||||
self.view.reset_mock()
|
||||
|
||||
controller.focus_conversation(conversation)
|
||||
assert self.view.method_calls == [('focus_tab', (conv_view,))]
|
||||
self.view.reset_mock()
|
||||
|
||||
controller.close_tab(conv_view)
|
||||
assert self.account.conversations == {}
|
||||
assert self.view.method_calls == [('remove_tab', (conv_view,))]
|
||||
assert conv_controller.method_calls == [('closed',)]
|
||||
|
||||
@mockified('pypsyc.client.controller', ['ConferenceController'])
|
||||
def test_conference(self, ConferenceController):
|
||||
conference = Mock()
|
||||
conference.uni = PLACE_UNI
|
||||
conf_view = self.view.show_conference.return_value
|
||||
conf_controller = ConferenceController.return_value
|
||||
self.account.conferences[PLACE_UNI] = conference
|
||||
|
||||
controller = TabsController(self.client, self.view)
|
||||
assert self.view.method_calls == [
|
||||
('show_conference', (PLACE_UNI,),)]
|
||||
assert ConferenceController.call_args_list == [
|
||||
((controller, conference, conf_view),)]
|
||||
self.view.reset_mock()
|
||||
|
||||
controller.focus_conversation(conference)
|
||||
assert self.view.method_calls == [('focus_tab', (conf_view,))]
|
||||
self.view.reset_mock()
|
||||
|
||||
controller.close_tab(conf_view)
|
||||
assert self.account.conferences == {}
|
||||
assert self.view.method_calls == [('remove_tab', (conf_view,))]
|
||||
assert conf_controller.method_calls == [('closed',)]
|
||||
|
||||
|
||||
|
||||
class TestFriendListController(object):
|
||||
@mockified('pypsyc.client.controller', ['ListBinding'])
|
||||
def setup(self, ListBinding):
|
||||
self.ListBinding = ListBinding
|
||||
model_list_ph = PlaceHolder()
|
||||
make_row_ph = PlaceHolder()
|
||||
self.client = Mock()
|
||||
self.client.accounts = ObsList()
|
||||
self.view = Mock()
|
||||
self.tabs_controller = Mock()
|
||||
|
||||
self.controller = FriendListController(self.client, self.view,
|
||||
self.tabs_controller)
|
||||
assert self.ListBinding.call_args_list == [
|
||||
((model_list_ph, self.view.friends, make_row_ph),)]
|
||||
self.model_list = model_list_ph.obj
|
||||
self.make_row = make_row_ph.obj
|
||||
|
||||
def test_binding(self):
|
||||
EXTERN_FRIEND_UNI = SERVER2_UNI.chain('~friend')
|
||||
|
||||
friend = Mock()
|
||||
friend.account.server = SERVER1
|
||||
friend.uni = USER1_UNI
|
||||
friend.presence = PRESENCE_OFFLINE
|
||||
row = self.make_row(friend)
|
||||
assert row == (USER1_NICK, False, friend.state)
|
||||
|
||||
friend.uni = EXTERN_FRIEND_UNI
|
||||
friend.presence = PRESENCE_ONLINE
|
||||
row = self.make_row(friend)
|
||||
assert row == (EXTERN_FRIEND_UNI, True, friend.state)
|
||||
|
||||
def test_account(self):
|
||||
update_evt = Mock()
|
||||
friend1 = Mock()
|
||||
friend2 = Mock()
|
||||
account = Mock()
|
||||
account.friends = ObsDict({USER1_UNI: friend1})
|
||||
|
||||
self.model_list.update_evt += update_evt
|
||||
assert self.model_list == []
|
||||
|
||||
self.client.accounts.append(account)
|
||||
assert self.model_list == [friend1]
|
||||
|
||||
account.friends[USER2_UNI] = friend2
|
||||
assert self.model_list == [friend1, friend2]
|
||||
|
||||
account.friends.updated_item(USER2_UNI)
|
||||
assert update_evt.call_args_list == [((1, friend2),)]
|
||||
|
||||
del account.friends[USER1_UNI]
|
||||
assert self.model_list == [friend2]
|
||||
|
||||
del self.client.accounts[0]
|
||||
assert _check_obs_dict(account.friends)
|
||||
assert self.model_list == []
|
||||
|
||||
def test_open_conversation(self):
|
||||
friend = Mock()
|
||||
friend.uni = USER2_UNI
|
||||
self.model_list.append(friend)
|
||||
conversation = friend.account.get_conversation.return_value
|
||||
|
||||
self.controller.open_conversation(0)
|
||||
assert friend.account.method_calls == [
|
||||
('get_conversation', (USER2_UNI,))]
|
||||
assert self.tabs_controller.method_calls == [
|
||||
('focus_conversation', (conversation,))]
|
||||
|
||||
def test_accept_friendship(self):
|
||||
friend = Mock()
|
||||
friend.uni = USER2_UNI
|
||||
friend.account.add_friend.side_effect = AsyncMethod()
|
||||
self.model_list.append(friend)
|
||||
|
||||
self.controller.accept_friendship(0)
|
||||
assert friend.account.method_calls == [('add_friend', (USER2_UNI,))]
|
||||
|
||||
def test_cancel_friendship(self):
|
||||
friend = Mock()
|
||||
friend.uni = USER2_UNI
|
||||
friend.account.remove_friend.side_effect = AsyncMethod()
|
||||
self.model_list.append(friend)
|
||||
|
||||
self.controller.cancel_friendship(0)
|
||||
assert friend.account.method_calls == [('remove_friend', (USER2_UNI,))]
|
||||
|
||||
|
||||
class TestMainController(object):
|
||||
@mockified('pypsyc.client.controller', ['TabsController',
|
||||
'FriendListController'])
|
||||
def setup(self, TabsController, FriendListController):
|
||||
self.client = Mock()
|
||||
self.client.accounts = ObsList()
|
||||
self.view = Mock()
|
||||
self.controller = MainController(self.client, self.view)
|
||||
self.TabsController = TabsController
|
||||
self.FriendListController = FriendListController
|
||||
|
||||
def test_main_controller(self):
|
||||
tabs_controller = self.TabsController.return_value
|
||||
assert self.TabsController.call_args_list == [
|
||||
((self.client, self.view.tabs_view),)]
|
||||
assert self.FriendListController.call_args_list == [
|
||||
((self.client, self.view.friends_view, tabs_controller),)]
|
||||
|
||||
def test_no_password(self):
|
||||
account = self._make_account()
|
||||
self.client.accounts.append(account)
|
||||
callback_ph = PlaceHolder()
|
||||
update_evt = Mock()
|
||||
self.client.accounts.update_evt += update_evt
|
||||
|
||||
account.no_password_evt()
|
||||
assert self.view.method_calls == [
|
||||
('show_password_dialog', (account.uni, account.__dict__,
|
||||
callback_ph))]
|
||||
callback_ph.obj()
|
||||
assert account.active == True
|
||||
assert update_evt.call_args_list == [((0, account),)]
|
||||
assert self.client.method_calls == [('save_accounts',)]
|
||||
|
||||
del self.client.accounts[0]
|
||||
assert account.no_password_evt.observers == []
|
||||
|
||||
def test_connection_error(self):
|
||||
account = self._make_account()
|
||||
self.client.accounts.append(account)
|
||||
|
||||
account.connection_error_evt(EXCEPTION)
|
||||
assert self.view.method_calls == [
|
||||
('show_conn_error', (account.uni, ERROR))]
|
||||
|
||||
del self.client.accounts[0]
|
||||
assert account.connection_error_evt.observers == []
|
||||
|
||||
def test_no_such_user(self):
|
||||
account = self._make_account()
|
||||
self.client.accounts.append(account)
|
||||
|
||||
account.no_such_user_evt(EXCEPTION)
|
||||
assert self.view.method_calls == [('show_no_such_user', (ERROR,))]
|
||||
|
||||
del self.client.accounts[0]
|
||||
assert account.no_such_user_evt.observers == []
|
||||
|
||||
def test_auth_error(self):
|
||||
account = self._make_account()
|
||||
self.client.accounts.append(account)
|
||||
|
||||
account.auth_error_evt(EXCEPTION)
|
||||
assert self.view.method_calls == [
|
||||
('show_auth_error', (account.uni, ERROR))]
|
||||
|
||||
del self.client.accounts[0]
|
||||
assert account.auth_error_evt.observers == []
|
||||
|
||||
def _make_account(self, active=False):
|
||||
account = Mock()
|
||||
account.active = active
|
||||
account.no_password_evt = Event()
|
||||
account.connection_error_evt = Event()
|
||||
account.no_such_user_evt = Event()
|
||||
account.auth_error_evt = Event()
|
||||
return account
|
||||
|
||||
@mockified('pypsyc.client.controller', ['AccountsController'])
|
||||
def test_open_accounts(self, AccountsController):
|
||||
self.controller.open_accounts()
|
||||
assert AccountsController.call_args_list == [
|
||||
((self.client, self.view.show_accounts.return_value),)]
|
||||
|
||||
@mockified('pypsyc.client.controller', ['DumpController'])
|
||||
def test_open_dump(self, DumpController):
|
||||
self.controller.open_dump()
|
||||
assert DumpController.call_args_list == [
|
||||
((self.client, self.view.show_dump_win.return_value),)]
|
||||
|
||||
def test_open_conversation(self):
|
||||
account1 = Mock()
|
||||
account1.active = False
|
||||
account2 = Mock()
|
||||
account2.active = True
|
||||
self.client.accounts = [account1, account2]
|
||||
callback_ph = PlaceHolder()
|
||||
conversation = account2.get_conversation.return_value
|
||||
|
||||
self.controller.open_conversation()
|
||||
assert self.view.method_calls == [
|
||||
('show_open_conv_dialog', (iter_(account2.uni), callback_ph))]
|
||||
|
||||
callback_ph.obj(0, SERVER1, USER2_NICK)
|
||||
assert account2.method_calls == [('get_conversation', (USER2_UNI,))]
|
||||
assert self.TabsController.return_value.method_calls == [
|
||||
('focus_conversation', (conversation,))]
|
||||
|
||||
def test_open_conference(self):
|
||||
account1 = Mock()
|
||||
account1.active = False
|
||||
account2 = Mock()
|
||||
account2.active = True
|
||||
self.client.accounts = [account1, account2]
|
||||
callback_ph = PlaceHolder()
|
||||
conference = account2.get_conference.return_value
|
||||
|
||||
self.controller.open_conference()
|
||||
assert self.view.method_calls == [
|
||||
('show_open_conf_dialog', (iter_(account2.uni), callback_ph))]
|
||||
|
||||
callback_ph.obj(0, SERVER1, 'place')
|
||||
assert account2.method_calls == [
|
||||
('get_conference', (PLACE_UNI,), {'subscribe': True})]
|
||||
assert self.TabsController.return_value.method_calls == [
|
||||
('focus_conversation', (conference,))]
|
||||
|
||||
def test_add_friend(self):
|
||||
account1 = Mock()
|
||||
account1.active = False
|
||||
account2 = Mock()
|
||||
account2.active = True
|
||||
account2.add_friend.side_effect = AsyncMethod()
|
||||
callback_ph = PlaceHolder()
|
||||
self.client.accounts = [account1, account2]
|
||||
|
||||
self.controller.add_friend()
|
||||
assert self.view.method_calls == [
|
||||
('show_add_friend_dialog', (iter_(account2.uni), callback_ph))]
|
||||
|
||||
callback_ph.obj(0, SERVER1, USER2_NICK)
|
||||
assert account2.method_calls == [('add_friend', (USER2_UNI,))]
|
||||
|
||||
def test_show_active_accounts(self):
|
||||
account1 = self._make_account(active=True)
|
||||
account2 = self._make_account(active=True)
|
||||
|
||||
self.client.accounts.append(account1)
|
||||
assert self.view.method_calls == [('show_active_accounts', (True,))]
|
||||
self.view.reset_mock()
|
||||
|
||||
self.client.accounts.append(account2)
|
||||
assert self.view.method_calls == []
|
||||
|
||||
self.client.accounts.remove(account1)
|
||||
assert self.view.method_calls == []
|
||||
|
||||
self.client.accounts.remove(account2)
|
||||
assert self.view.method_calls == [('show_active_accounts', (False,))]
|
||||
|
||||
def test_show_active_accounts_updated(self):
|
||||
account = self._make_account(active=False)
|
||||
|
||||
self.client.accounts.append(account)
|
||||
assert self.view.method_calls == []
|
||||
|
||||
account.active = True
|
||||
self.client.accounts.updated_item(account)
|
||||
assert self.view.method_calls == [('show_active_accounts', (True,))]
|
||||
self.view.reset_mock()
|
||||
|
||||
self.client.accounts.updated_item(account)
|
||||
assert self.view.method_calls == []
|
||||
|
||||
account.active = False
|
||||
self.client.accounts.updated_item(account)
|
||||
assert self.view.method_calls == [('show_active_accounts', (False,))]
|
||||
self.view.reset_mock()
|
||||
|
||||
self.client.accounts.updated_item(account)
|
||||
assert self.view.method_calls == []
|
||||
|
||||
del self.client.accounts[0]
|
||||
assert self.view.method_calls == []
|
||||
|
||||
def test_quit(self):
|
||||
self.controller.quit()
|
||||
assert _check_obs_list(self.client.accounts)
|
||||
assert self.client.method_calls == [('quit',)]
|
563
mjacob2/tests/test_client/test_model.py
Normal file
563
mjacob2/tests/test_client/test_model.py
Normal file
|
@ -0,0 +1,563 @@
|
|||
"""
|
||||
:copyright: 2010 by Manuel Jacob
|
||||
:license: MIT
|
||||
"""
|
||||
from os import remove
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
from mock import Mock, MagicMock, sentinel
|
||||
from nose.tools import assert_raises
|
||||
from tests.constants import (SERVER1, SERVER2, USER1_UNI, USER2_UNI,
|
||||
USER1_NICK, USER2_NICK, RESOURCE, RESOURCE1_UNI, PLACE_UNI, VARIABLES,
|
||||
CONTENT, PASSWORD, MESSAGE, ONLINE, PRESENCE_UNKNOWN, PRESENCE_ONLINE,
|
||||
OFFERED, ESTABLISHED, ERROR, EXCEPTION)
|
||||
from tests.helpers import mockified, PlaceHolder, inited_header, AsyncMethod
|
||||
|
||||
from pypsyc.client.model import (Conversation, Member, Conference, Messaging,
|
||||
Conferencing, Account, FriendList, ClientCircuit, Circuit, Client)
|
||||
from pypsyc.core.psyc import PSYCPacket
|
||||
from pypsyc.protocol import (UnknownTargetError, DeliveryFailedError,
|
||||
AuthenticationError)
|
||||
|
||||
|
||||
class TestConversation(object):
|
||||
def test_send_message(self):
|
||||
message_ph = PlaceHolder()
|
||||
messaging = Mock()
|
||||
conversation = Conversation(messaging, USER2_UNI)
|
||||
|
||||
conversation.send_message(MESSAGE)
|
||||
assert messaging.protocol.method_calls == [
|
||||
('send_private_message', (USER2_UNI, MESSAGE),
|
||||
{'relay': messaging.account.uni})]
|
||||
assert conversation.messages == [message_ph]
|
||||
assert message_ph.obj.source == messaging.account.uni
|
||||
|
||||
|
||||
class TestConference(object):
|
||||
def test_send_message(self):
|
||||
conferencing = Mock()
|
||||
conference = Conference(conferencing, PLACE_UNI)
|
||||
|
||||
conference.send_message(MESSAGE)
|
||||
assert conferencing.protocol.method_calls == [
|
||||
('send_public_message', (PLACE_UNI, MESSAGE))]
|
||||
|
||||
|
||||
class TestMessaging(object):
|
||||
def test_recv_message(self):
|
||||
account = Mock()
|
||||
conversation = account.get_conversation.return_value
|
||||
conversation.messages = []
|
||||
message_ph = PlaceHolder()
|
||||
messaging = Messaging(account)
|
||||
|
||||
messaging.private_message(USER2_UNI, MESSAGE)
|
||||
assert account.get_conversation.call_args_list == [((USER2_UNI,),)]
|
||||
assert conversation.messages == [message_ph]
|
||||
assert message_ph.obj.source == USER2_UNI
|
||||
assert message_ph.obj.message == MESSAGE
|
||||
|
||||
@mockified('pypsyc.client.model', ['MessagingProtocol'])
|
||||
def test_connected(self, MessagingProtocol):
|
||||
circuit = Mock()
|
||||
messaging = Messaging(Mock())
|
||||
|
||||
messaging.connected(circuit)
|
||||
assert circuit.psyc.method_calls == [
|
||||
('add_handler', (MessagingProtocol.return_value,))]
|
||||
|
||||
def test_disconnected(self):
|
||||
Messaging(Mock).disconnected()
|
||||
|
||||
|
||||
class TestConferencing(object):
|
||||
def test_member_entered(self):
|
||||
account = Mock()
|
||||
conference = account.get_conference.return_value
|
||||
conference.members = []
|
||||
conferencing = Conferencing(account)
|
||||
|
||||
conferencing.member_entered(PLACE_UNI, USER2_UNI, USER2_NICK)
|
||||
assert account.get_conference.call_args_list == [((PLACE_UNI,),)]
|
||||
assert conference.members == [Member(USER2_UNI, USER2_NICK)]
|
||||
|
||||
def test_member_left(self):
|
||||
account = Mock()
|
||||
conference = account.get_conference.return_value
|
||||
conference.members = [Member(USER1_UNI, USER1_NICK),
|
||||
Member(USER2_UNI, USER2_NICK)]
|
||||
conferencing = Conferencing(account)
|
||||
|
||||
conferencing.member_left(PLACE_UNI, USER2_UNI)
|
||||
assert conference.members == [Member(USER1_UNI, USER1_NICK)]
|
||||
|
||||
def test_recv_message(self):
|
||||
account = Mock()
|
||||
conference = account.get_conference.return_value
|
||||
conference.messages = []
|
||||
message_ph = PlaceHolder()
|
||||
conferencing = Conferencing(account)
|
||||
|
||||
conferencing.public_message(PLACE_UNI, USER2_UNI, MESSAGE)
|
||||
assert account.get_conference.call_args_list == [((PLACE_UNI,),)]
|
||||
assert conference.messages == [message_ph]
|
||||
assert message_ph.obj.source == USER2_UNI
|
||||
assert message_ph.obj.message == MESSAGE
|
||||
|
||||
@mockified('pypsyc.client.model', ['ConferencingProtocol'])
|
||||
def test_connected(self, ConferencingProtocol):
|
||||
circuit = Mock()
|
||||
conferencing = Conferencing(Mock())
|
||||
|
||||
conferencing.connected(circuit)
|
||||
assert circuit.method_calls == [
|
||||
('psyc.add_handler', (ConferencingProtocol.return_value,)),
|
||||
('add_pvar_handler', (ConferencingProtocol.return_value,))]
|
||||
|
||||
def test_disconnected(self):
|
||||
Conferencing(Mock()).disconnected()
|
||||
|
||||
|
||||
class _Dict(dict):
|
||||
def __init__(self, *args, **kwds):
|
||||
dict.__init__(self, *args, **kwds)
|
||||
self.updated_item = Mock()
|
||||
|
||||
class TestFriendList(object):
|
||||
def test_friendship(self):
|
||||
friend_ph = PlaceHolder()
|
||||
account = Mock()
|
||||
account.friends = _Dict()
|
||||
friend_list = FriendList(account)
|
||||
|
||||
friend_list.friendship(USER1_UNI, OFFERED)
|
||||
assert account.friends == {USER1_UNI: friend_ph}
|
||||
friend = friend_ph.obj
|
||||
assert friend.account == account
|
||||
assert friend.uni == USER1_UNI
|
||||
assert friend.presence == PRESENCE_UNKNOWN
|
||||
assert friend.state == OFFERED
|
||||
|
||||
friend_list.friendship(USER1_UNI, ESTABLISHED)
|
||||
assert friend.state == ESTABLISHED
|
||||
assert account.friends.updated_item.call_args_list == [((USER1_UNI,),)]
|
||||
|
||||
friend_list.friendship_removed(USER1_UNI)
|
||||
assert account.friends == {}
|
||||
|
||||
def test_presence(self):
|
||||
account = Mock()
|
||||
account.friends = _Dict({USER2_UNI: Mock()})
|
||||
friend_list = FriendList(account)
|
||||
|
||||
friend_list.presence(USER2_UNI, ONLINE)
|
||||
assert account.friends[USER2_UNI].presence == PRESENCE_ONLINE
|
||||
assert account.friends.updated_item.call_args_list == [((USER2_UNI,),)]
|
||||
|
||||
@mockified('pypsyc.client.model', ['FriendshipProtocol'])
|
||||
def test_connected(self, FriendshipProtocol):
|
||||
circuit = Mock()
|
||||
friend_list = FriendList(Mock())
|
||||
|
||||
friend_list.connected(circuit)
|
||||
assert circuit.method_calls == [
|
||||
('psyc.add_handler', (FriendshipProtocol.return_value,)),
|
||||
('add_pvar_handler', (FriendshipProtocol.return_value,))]
|
||||
|
||||
def test_disconnected(self):
|
||||
account = Mock()
|
||||
account.friends = _Dict({USER2_UNI: Mock()})
|
||||
friend_list = FriendList(account)
|
||||
|
||||
friend_list.disconnected()
|
||||
assert account.friends == {}
|
||||
|
||||
|
||||
class TestAccount(object):
|
||||
@mockified('pypsyc.client.model', ['ObsDict'])
|
||||
def test_account(self, ObsDict):
|
||||
obs_dict = ObsDict.return_value = MagicMock()
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
assert account.uni == USER1_UNI
|
||||
assert ObsDict.call_args_list == [(), (), ()]
|
||||
assert account.conversations == obs_dict
|
||||
assert account.conferences == obs_dict
|
||||
assert account.friends == obs_dict
|
||||
|
||||
@mockified('pypsyc.client.model', ['Messaging', 'Conversation'])
|
||||
def test_get_conversation(self, Messaging, Conversation):
|
||||
messaging = Messaging.return_value
|
||||
conversation = Conversation.return_value
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
assert Messaging.call_args_list == [((account,),)]
|
||||
|
||||
assert account.get_conversation(USER2_UNI) == conversation
|
||||
assert account.get_conversation(USER2_UNI) == conversation
|
||||
assert Conversation.call_args_list == [((messaging, USER2_UNI),)]
|
||||
assert account.conversations == {USER2_UNI: conversation}
|
||||
|
||||
@mockified('pypsyc.client.model', ['Conferencing', 'Conference'])
|
||||
def test_get_conference(self, Conferencing, Conference):
|
||||
conferencing = Conferencing.return_value
|
||||
conference = Conference.return_value
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
assert Conferencing.call_args_list == [((account,),)]
|
||||
|
||||
assert account.get_conference(PLACE_UNI) == conference
|
||||
assert account.get_conference(PLACE_UNI) == conference
|
||||
assert Conference.call_args_list == [((conferencing, PLACE_UNI),)]
|
||||
assert account.conferences == {PLACE_UNI: conference}
|
||||
|
||||
@mockified('pypsyc.client.model', ['Conferencing', 'Conference'])
|
||||
def test_get_conference_subscribe(self, Conferencing, Conference):
|
||||
conferencing = Conferencing.return_value
|
||||
conference = Conference.return_value
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
client_interface = account.client_interface = Mock()
|
||||
client_interface.subscribe.side_effect = AsyncMethod()
|
||||
|
||||
assert account.get_conference(PLACE_UNI, subscribe=True) == conference
|
||||
assert account.get_conference(PLACE_UNI, subscribe=True) == conference
|
||||
assert Conference.call_args_list == [((conferencing, PLACE_UNI),)]
|
||||
assert account.conferences == {PLACE_UNI: conference}
|
||||
assert client_interface.method_calls == [('subscribe', (PLACE_UNI,))]
|
||||
|
||||
@mockified('pypsyc.client.model', ['Conference'])
|
||||
def test_get_conference_subscribe_unknown_target(self, Conference):
|
||||
conference = Conference.return_value
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
client_interface = account.client_interface = Mock()
|
||||
client_interface.subscribe.side_effect = UnknownTargetError
|
||||
|
||||
account.get_conference(PLACE_UNI, subscribe=True)
|
||||
assert conference.method_calls == [('unknown_target_evt',)]
|
||||
|
||||
@mockified('pypsyc.client.model', ['Conference'])
|
||||
def test_get_conference_subscribe_delivery_failed(self, Conference):
|
||||
conference = Conference.return_value
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
client_interface = account.client_interface = Mock()
|
||||
client_interface.subscribe.side_effect = DeliveryFailedError(ERROR)
|
||||
|
||||
account.get_conference(PLACE_UNI, subscribe=True)
|
||||
assert conference.method_calls == [('delivery_failed_evt', (ERROR,))]
|
||||
|
||||
def test_remove_conference(self):
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
account.conferences[PLACE_UNI] = Mock()
|
||||
client_interface = account.client_interface = Mock()
|
||||
client_interface.unsubscribe.side_effect = AsyncMethod()
|
||||
|
||||
del account.conferences[PLACE_UNI]
|
||||
assert client_interface.method_calls == [('unsubscribe', (PLACE_UNI,))]
|
||||
|
||||
def test_add_friend(self):
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
client_interface = account.client_interface = Mock()
|
||||
|
||||
account.add_friend(USER2_UNI)
|
||||
assert client_interface.method_calls == [('add_friend', (USER2_UNI,))]
|
||||
|
||||
def test_remove_friend(self):
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
client_interface = account.client_interface = Mock()
|
||||
|
||||
account.remove_friend(USER2_UNI)
|
||||
assert client_interface.method_calls == [
|
||||
('remove_friend', (USER2_UNI,))]
|
||||
|
||||
@mockified('pypsyc.client.model', ['FriendList'])
|
||||
def test_friendlist(self, FriendList):
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
assert FriendList.call_args_list == [((account,),)]
|
||||
|
||||
def test_unknown_target_error(self):
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
account.conversations[USER1_UNI] = conversation = Mock()
|
||||
account.conferences[PLACE_UNI] = conference = Mock()
|
||||
|
||||
account.unknown_target_error(USER1_UNI)
|
||||
account.unknown_target_error(USER2_UNI)
|
||||
account.unknown_target_error(PLACE_UNI)
|
||||
assert conversation.method_calls == [('unknown_target_evt',)]
|
||||
assert conference.method_calls == [('unknown_target_evt',)]
|
||||
|
||||
def test_delivery_failed_error(self):
|
||||
account = Account(None, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
account.conversations[USER1_UNI] = conversation = Mock()
|
||||
account.conferences[PLACE_UNI] = conference = Mock()
|
||||
|
||||
account.delivery_failed_error(USER1_UNI, ERROR)
|
||||
account.delivery_failed_error(USER2_UNI, ERROR)
|
||||
account.delivery_failed_error(PLACE_UNI, ERROR)
|
||||
assert conversation.method_calls == [('delivery_failed_evt', (ERROR,))]
|
||||
assert conference.method_calls == [('delivery_failed_evt', (ERROR,))]
|
||||
|
||||
@mockified('pypsyc.client.model', ['resolve_hostname', 'connect',
|
||||
'LinkingProtocol',
|
||||
'RoutingErrorProtocol',
|
||||
'ClientInterfaceProtocol', 'Messaging',
|
||||
'Conferencing', 'FriendList'])
|
||||
def test_active(self, resolve_hostname, connect, LinkingProtocol,
|
||||
RoutingErrorProtocol, ClientInterfaceProtocol, Messaging,
|
||||
Conferencing, FriendList):
|
||||
resolve_hostname.return_value = (sentinel.ip, sentinel.port)
|
||||
circuit = connect.return_value
|
||||
linking_protocol = LinkingProtocol.return_value
|
||||
linked = linking_protocol.link.side_effect = AsyncMethod()
|
||||
messaging = Messaging.return_value
|
||||
conferencing = Conferencing.return_value
|
||||
friend_list = FriendList.return_value
|
||||
client = Mock()
|
||||
account = Account(client, SERVER1, USER1_NICK, PASSWORD, True, False)
|
||||
|
||||
account.active = True
|
||||
account.active = True
|
||||
assert resolve_hostname.call_args_list == [((account.server,),)]
|
||||
assert connect.call_args_list == [
|
||||
((sentinel.ip, sentinel.port, client),)]
|
||||
assert LinkingProtocol.call_args_list == [((circuit,),)]
|
||||
assert linking_protocol.method_calls == [
|
||||
('link', (USER1_UNI, RESOURCE, PASSWORD))]
|
||||
|
||||
linked.callback()
|
||||
assert circuit.psyc.uni == RESOURCE1_UNI
|
||||
assert RoutingErrorProtocol.call_args_list == [((account,),)]
|
||||
assert circuit.psyc.method_calls == [
|
||||
('add_handler', (RoutingErrorProtocol.return_value,))]
|
||||
assert ClientInterfaceProtocol.call_args_list == [((account,),)]
|
||||
assert messaging.method_calls == [('connected', (circuit,))]
|
||||
assert conferencing.method_calls == [('connected', (circuit,))]
|
||||
assert friend_list.method_calls == [('connected', (circuit,))]
|
||||
messaging.reset_mock()
|
||||
conferencing.reset_mock()
|
||||
friend_list.reset_mock()
|
||||
|
||||
account.active = False
|
||||
account.active = False
|
||||
assert circuit.transport.method_calls == [('loseConnection',)]
|
||||
assert not hasattr(account, 'circuit')
|
||||
assert messaging.method_calls == [('disconnected',)]
|
||||
assert conferencing.method_calls == [('disconnected',)]
|
||||
assert friend_list.method_calls == [('disconnected',)]
|
||||
|
||||
@mockified('pypsyc.client.model', ['resolve_hostname'])
|
||||
def test_active_no_password(self, resolve_hostname):
|
||||
no_password_evt = Mock()
|
||||
account = Account(None, SERVER1, USER1_NICK, '', False, False)
|
||||
account.no_password_evt += no_password_evt
|
||||
|
||||
account.active = True
|
||||
assert account.active == False
|
||||
assert no_password_evt.call_args_list == [()]
|
||||
assert resolve_hostname.call_args_list == []
|
||||
|
||||
@mockified('pypsyc.client.model', ['resolve_hostname', 'connect'])
|
||||
def test_active_dns_error(self, resolve_hostname, connect):
|
||||
resolve_hostname.side_effect = EXCEPTION
|
||||
client = Mock()
|
||||
connection_error_evt = Mock()
|
||||
account = Account(client, SERVER1, USER1_NICK, PASSWORD, False, False)
|
||||
account.connection_error_evt += connection_error_evt
|
||||
|
||||
account.active = True
|
||||
assert account.active == False
|
||||
assert client.accounts.updated_item.call_args_list == [((account,),)]
|
||||
assert connection_error_evt.call_args_list == [((EXCEPTION,),)]
|
||||
assert connect.call_args_list == []
|
||||
|
||||
@mockified('pypsyc.client.model', ['resolve_hostname', 'connect',
|
||||
'LinkingProtocol'])
|
||||
def test_active_connect_error(self, resolve_hostname, connect,
|
||||
LinkingProtocol):
|
||||
client = Mock()
|
||||
resolve_hostname.return_value = (sentinel.ip, sentinel.port)
|
||||
connect.side_effect = EXCEPTION
|
||||
connection_error_evt = Mock()
|
||||
account = Account(client, SERVER1, USER1_NICK, PASSWORD, False, False)
|
||||
account.connection_error_evt += connection_error_evt
|
||||
|
||||
account.active = True
|
||||
assert account.active == False
|
||||
assert client.accounts.updated_item.call_args_list == [((account,),)]
|
||||
assert connection_error_evt.call_args_list == [((EXCEPTION,),)]
|
||||
assert LinkingProtocol.call_args_list == []
|
||||
|
||||
@mockified('pypsyc.client.model', ['resolve_hostname', 'connect',
|
||||
'LinkingProtocol'])
|
||||
def test_active_no_such_user(self, resolve_hostname, connect,
|
||||
LinkingProtocol):
|
||||
ERROR = UnknownTargetError()
|
||||
client = Mock()
|
||||
resolve_hostname.return_value = (sentinel.ip, sentinel.port)
|
||||
circuit = connect.return_value
|
||||
LinkingProtocol.return_value.link.side_effect = ERROR
|
||||
no_such_user_evt = Mock()
|
||||
account = Account(client, SERVER1, USER1_NICK, PASSWORD, False, False)
|
||||
account.no_such_user_evt += no_such_user_evt
|
||||
|
||||
account.active = True
|
||||
assert account.active == False
|
||||
assert client.accounts.updated_item.call_args_list == [((account,),)]
|
||||
assert no_such_user_evt.call_args_list == [((ERROR,),)]
|
||||
assert circuit.psyc.uni != account.uni.chain(account.resource)
|
||||
|
||||
@mockified('pypsyc.client.model', ['resolve_hostname', 'connect',
|
||||
'LinkingProtocol'])
|
||||
def test_active_incorrect_password(self, resolve_hostname, connect,
|
||||
LinkingProtocol):
|
||||
ERROR = AuthenticationError()
|
||||
client = Mock()
|
||||
resolve_hostname.return_value = (sentinel.ip, sentinel.port)
|
||||
circuit = connect.return_value
|
||||
LinkingProtocol.return_value.link.side_effect = ERROR
|
||||
auth_error_evt = Mock()
|
||||
account = Account(client, SERVER1, USER1_NICK, 'wrong', False, False)
|
||||
account.auth_error_evt += auth_error_evt
|
||||
|
||||
account.active = True
|
||||
assert account.active == False
|
||||
assert client.accounts.updated_item.call_args_list == [((account,),)]
|
||||
assert auth_error_evt.call_args_list == [((ERROR,),)]
|
||||
assert circuit.psyc.uni != account.uni.chain(account.resource)
|
||||
|
||||
def test_connection_error(self):
|
||||
client = Mock()
|
||||
connection_error_evt = Mock()
|
||||
account = Account(client, SERVER1, USER1_NICK, PASSWORD, False, False)
|
||||
account.connection_error_evt += connection_error_evt
|
||||
|
||||
account.connection_error(EXCEPTION)
|
||||
assert account.active == False
|
||||
assert client.accounts.updated_item.call_args_list == [((account,),)]
|
||||
assert connection_error_evt.call_args_list == [((EXCEPTION,),)]
|
||||
|
||||
|
||||
class TestClientCircuit(object):
|
||||
@mockified(Circuit, ['connectionMade'])
|
||||
def test_dump_out(self, connectionMade):
|
||||
LINE1 = ':_target\t' + USER2_UNI
|
||||
LINE2 = '|'
|
||||
|
||||
cc = ClientCircuit()
|
||||
dump_evt = Mock()
|
||||
cc.dump_evt += dump_evt
|
||||
cc.transport = Mock()
|
||||
orig_write = cc.transport.write
|
||||
|
||||
cc.connectionMade()
|
||||
assert connectionMade.call_args_list == [((cc,),)]
|
||||
|
||||
cc.send({'_target': USER2_UNI}, '')
|
||||
assert dump_evt.call_args_list == [(('o', LINE1),), (('o', LINE2),)]
|
||||
assert orig_write.call_args_list == [((LINE1+'\n'+LINE2+'\n',),)]
|
||||
|
||||
@mockified(Circuit, ['lineReceived'])
|
||||
def test_dump_in(self, lineReceived):
|
||||
lineReceived.return_value = None
|
||||
cc = ClientCircuit()
|
||||
dump_evt = Mock()
|
||||
cc.dump_evt += dump_evt
|
||||
|
||||
cc.dataReceived('a\nb\n')
|
||||
assert dump_evt.call_args_list == [(('i', 'a'),), (('i', 'b'),)]
|
||||
assert lineReceived.call_args_list == [((cc, 'a'),), ((cc, 'b'),)]
|
||||
|
||||
@mockified('pypsyc.client.model', ['PSYCObject'])
|
||||
def test_packet_received(self, PSYCObject):
|
||||
psyc = PSYCObject.return_value
|
||||
psyc.handle_packet.return_value = PSYCPacket()
|
||||
|
||||
cc = ClientCircuit()
|
||||
assert PSYCObject.call_args_list == [((cc.send,),)]
|
||||
|
||||
cc.packet_received({}, CONTENT)
|
||||
assert psyc.method_calls == [('handle_packet', ({}, CONTENT))]
|
||||
|
||||
@mockified('pypsyc.client.model', ['PSYCObject'])
|
||||
def test_relay(self, PSYCObject):
|
||||
psyc = PSYCObject.return_value
|
||||
psyc.uni = RESOURCE1_UNI
|
||||
psyc.handle_packet.return_value = PSYCPacket()
|
||||
cc = ClientCircuit()
|
||||
|
||||
header = inited_header(_source=USER1_UNI, _source_relay=USER2_UNI)
|
||||
cc.packet_received(header, CONTENT)
|
||||
assert header.source == USER2_UNI
|
||||
|
||||
header2 = inited_header(_source=USER2_UNI, _source_relay=USER2_UNI)
|
||||
assert_raises(AssertionError, cc.packet_received, header2, CONTENT)
|
||||
|
||||
@mockified('pypsyc.client.model', ['PSYCObject'])
|
||||
def test_persistent(self, PSYCObject):
|
||||
HEADER = {'_context': USER2_UNI}
|
||||
packet = PSYCPacket({'=': VARIABLES, '+': VARIABLES, '-': VARIABLES})
|
||||
packet.header = inited_header(HEADER)
|
||||
psyc = PSYCObject.return_value
|
||||
psyc.handle_packet.return_value = packet
|
||||
cc = ClientCircuit()
|
||||
|
||||
handler = Mock()
|
||||
state_set_key = handler.state_set_key = Mock()
|
||||
state_add_key = handler.state_add_key = Mock()
|
||||
state_remove_key = handler.state_remove_key = Mock()
|
||||
cc.add_pvar_handler(handler)
|
||||
|
||||
cc.packet_received(HEADER, CONTENT)
|
||||
assert psyc.method_calls == [('handle_packet', (HEADER, CONTENT))]
|
||||
assert state_set_key.call_args_list == [((USER2_UNI, VARIABLES),)]
|
||||
assert state_add_key.call_args_list == [((USER2_UNI, VARIABLES),)]
|
||||
assert state_remove_key.call_args_list == [((USER2_UNI, VARIABLES),)]
|
||||
|
||||
|
||||
class TestClient(object):
|
||||
@mockified('pypsyc.client.model', ['Account'])
|
||||
def test_accounts(self, Account):
|
||||
account = Account.return_value
|
||||
error_ph = PlaceHolder()
|
||||
|
||||
account1 = Mock()
|
||||
account1.server = SERVER1
|
||||
account1.person = USER1_NICK
|
||||
account1.password = PASSWORD
|
||||
account1.save_password = True
|
||||
account1.active = False
|
||||
account2 = Mock()
|
||||
account2.server = SERVER2
|
||||
account2.person = USER2_NICK
|
||||
account2.password = PASSWORD
|
||||
account2.save_password = False
|
||||
account2.active = True
|
||||
|
||||
try:
|
||||
with NamedTemporaryFile(delete=False) as f:
|
||||
f.write('[]')
|
||||
|
||||
client = Client(accounts_file=f.name)
|
||||
assert client.accounts == []
|
||||
client.accounts = [account1, account2]
|
||||
client.save_accounts()
|
||||
|
||||
client = Client(accounts_file=f.name)
|
||||
client.load_accounts()
|
||||
assert client.accounts == [Account.return_value] * 2
|
||||
assert Account.call_args_list == [
|
||||
((client, SERVER1, USER1_NICK, PASSWORD, True, False),),
|
||||
((client, SERVER2, USER2_NICK, '', False, True),)]
|
||||
finally:
|
||||
remove(f.name)
|
||||
|
||||
client.connection_lost(account.circuit, sentinel.error)
|
||||
assert account.method_calls == [('connection_error', (error_ph,))] * 2
|
||||
assert error_ph.obj.args[0] == str(sentinel.error)
|
||||
|
||||
@mockified('pypsyc.client.model', ['reactor'])
|
||||
def test_quit(self, reactor):
|
||||
account = Mock()
|
||||
client = Client()
|
||||
client.accounts = [account]
|
||||
|
||||
client.quit()
|
||||
assert account.active == False
|
||||
assert reactor.method_calls == [('stop',)]
|
173
mjacob2/tests/test_client/test_observable.py
Normal file
173
mjacob2/tests/test_client/test_observable.py
Normal file
|
@ -0,0 +1,173 @@
|
|||
"""
|
||||
:copyright: 2010 by Manuel Jacob
|
||||
:license: MIT
|
||||
"""
|
||||
from mock import Mock
|
||||
from nose.tools import assert_raises
|
||||
|
||||
from pypsyc.client.observable import ObsAttr, ObsObj, ObsList, ObsDict
|
||||
|
||||
|
||||
class StubObsObj(ObsObj):
|
||||
foo = ObsAttr('foo')
|
||||
|
||||
|
||||
def test_obs_obj():
|
||||
obs_obj = StubObsObj()
|
||||
update_evt = Mock()
|
||||
obs_obj.update_evt['foo'] += update_evt
|
||||
|
||||
obs_obj.ignore = 'foo'
|
||||
assert not hasattr(obs_obj, 'foo')
|
||||
obs_obj.foo = 'foo'
|
||||
obs_obj.foo = 'foo'
|
||||
obs_obj.foo = 'bar'
|
||||
assert obs_obj.foo == 'bar'
|
||||
|
||||
del obs_obj.foo
|
||||
assert not hasattr(obs_obj, 'foo')
|
||||
assert_raises(AttributeError, obs_obj.__delattr__, 'foo')
|
||||
|
||||
assert update_evt.call_args_list == [
|
||||
((None, 'foo'),), (('foo', 'bar'),), (('bar', None),)]
|
||||
|
||||
|
||||
class TestObsList(object):
|
||||
def test_basic(self):
|
||||
obs_list = ObsList(xrange(10))
|
||||
setitem_evt = Mock()
|
||||
obs_list.setitem_evt += setitem_evt
|
||||
delitem_evt = Mock()
|
||||
obs_list.delitem_evt += delitem_evt
|
||||
insert_evt = Mock()
|
||||
obs_list.insert_evt += insert_evt
|
||||
|
||||
obs_list[3] = 3
|
||||
obs_list[3] = 333
|
||||
del obs_list[3]
|
||||
obs_list.insert(3, 3)
|
||||
|
||||
assert obs_list == list(xrange(10))
|
||||
assert setitem_evt.call_args_list == [((3, 3, 333),)]
|
||||
assert delitem_evt.call_args_list == [((3, 333),)]
|
||||
assert insert_evt.call_args_list == [((3, 3),)]
|
||||
|
||||
def test_ext_add(self):
|
||||
obs_list = ObsList()
|
||||
insert_evt = Mock()
|
||||
obs_list.insert_evt += insert_evt
|
||||
|
||||
obs_list.append(42)
|
||||
obs_list.extend([42, 1337])
|
||||
obs_list += [42, 1337]
|
||||
|
||||
assert obs_list == [42, 42, 1337, 42, 1337]
|
||||
assert insert_evt.call_args_list == [
|
||||
((0, 42),), ((1, 42),), ((2, 1337),), ((3, 42),), ((4, 1337),)]
|
||||
|
||||
def test_ext_del(self):
|
||||
obs_list = ObsList(xrange(10))
|
||||
delitem_evt = Mock()
|
||||
obs_list.delitem_evt += delitem_evt
|
||||
|
||||
assert obs_list.pop() == 9
|
||||
assert obs_list.pop(8) == 8
|
||||
obs_list.remove(7)
|
||||
|
||||
assert delitem_evt.call_args_list == [((9, 9),), ((8, 8),), ((7, 7),)]
|
||||
assert obs_list == list(xrange(7))
|
||||
|
||||
def test_reverse(self):
|
||||
obs_list = ObsList(xrange(10))
|
||||
obs_list.reverse()
|
||||
assert obs_list == list(reversed(xrange(10)))
|
||||
|
||||
def test_updated_item(self):
|
||||
obs_list = ObsList(['a', 'b'])
|
||||
update_evt = Mock()
|
||||
obs_list.update_evt += update_evt
|
||||
|
||||
obs_list.updated_item('b')
|
||||
assert update_evt.call_args_list == [((1, 'b'),)]
|
||||
|
||||
|
||||
class TestObsDict(object):
|
||||
def test_basic(self):
|
||||
obs_dict = ObsDict(a='A')
|
||||
setitem_evt = Mock()
|
||||
obs_dict.setitem_evt += setitem_evt
|
||||
delitem_evt = Mock()
|
||||
obs_dict.delitem_evt += delitem_evt
|
||||
|
||||
obs_dict['a'] = 'A'
|
||||
obs_dict['a'] = 'X'
|
||||
del obs_dict['a']
|
||||
obs_dict['b'] = 'B'
|
||||
|
||||
assert obs_dict == dict(b='B')
|
||||
assert setitem_evt.call_args_list == [(('a', 'A', 'X'),),
|
||||
(('b', None, 'B'),)]
|
||||
assert delitem_evt.call_args_list == [(('a', 'X'),)]
|
||||
|
||||
def test_pop(self):
|
||||
obs_dict = ObsDict(a='A', b='B')
|
||||
delitem_evt = Mock()
|
||||
obs_dict.delitem_evt += delitem_evt
|
||||
|
||||
assert obs_dict.pop('a') == 'A'
|
||||
assert obs_dict.pop('a', 'X') == 'X'
|
||||
assert obs_dict.pop('b', 'X') == 'B'
|
||||
|
||||
assert obs_dict == {}
|
||||
assert delitem_evt.call_args_list == [(('a', 'A'),), (('b', 'B'),)]
|
||||
|
||||
def test_popitem(self):
|
||||
obs_dict = ObsDict(a='A', b='B')
|
||||
delitem_evt = Mock()
|
||||
obs_dict.delitem_evt += delitem_evt
|
||||
|
||||
k, v = obs_dict.popitem()
|
||||
assert v == k.upper()
|
||||
|
||||
assert len(obs_dict) == 1
|
||||
assert delitem_evt.call_args_list == [((k, v),)]
|
||||
|
||||
def test_clear(self):
|
||||
obs_dict = ObsDict(a='A', b='B')
|
||||
delitem_evt = Mock()
|
||||
obs_dict.delitem_evt += delitem_evt
|
||||
|
||||
obs_dict.clear()
|
||||
|
||||
assert delitem_evt.call_args_list == [(('a', 'A'),), (('b', 'B'),)]
|
||||
|
||||
def test_update(self):
|
||||
obs_dict = ObsDict()
|
||||
setitem_evt = Mock()
|
||||
obs_dict.setitem_evt += setitem_evt
|
||||
|
||||
obs_dict.update({'a': 'A'}, b='B')
|
||||
obs_dict.update((('c', 'C'),))
|
||||
|
||||
assert obs_dict == dict(a='A', b='B', c='C')
|
||||
assert setitem_evt.call_args_list == [
|
||||
(('a', None, 'A'),), (('b', None, 'B'),), (('c', None, 'C'),)]
|
||||
|
||||
def test_setdefault(self):
|
||||
obs_dict = ObsDict(a='A')
|
||||
setitem_evt = Mock()
|
||||
obs_dict.setitem_evt += setitem_evt
|
||||
|
||||
assert obs_dict.setdefault('a') == 'A'
|
||||
assert obs_dict.setdefault('b', 'B') == 'B'
|
||||
|
||||
assert obs_dict == dict(a='A', b='B')
|
||||
assert setitem_evt.call_args_list == [(('b', None, 'B'),)]
|
||||
|
||||
def test_updated_item(self):
|
||||
obs_dict = ObsDict({'a': 'A', 'b': 'B'})
|
||||
update_evt = Mock()
|
||||
obs_dict.update_evt += update_evt
|
||||
|
||||
obs_dict.updated_item('b')
|
||||
assert update_evt.call_args_list == [(('b', 'B'),)]
|
111
mjacob2/tests/test_client/test_view.py
Normal file
111
mjacob2/tests/test_client/test_view.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
"""
|
||||
:copyright: 2010 by Manuel Jacob
|
||||
:license: MIT
|
||||
"""
|
||||
|
||||
|
||||
def _callback(*args, **kwds): # pragma: no cover
|
||||
print ('callback', args, kwds)
|
||||
|
||||
def _test_accounts_view(): # pragma: no cover
|
||||
accounts_view = AccountsView()
|
||||
Controller(controller.AccountsController, accounts_view)
|
||||
accounts_view.window.connect('destroy', lambda w: gtk.main_quit())
|
||||
for i in xrange(10):
|
||||
accounts_view.accounts.append(("psyc://server/~account%i" % i, True))
|
||||
accounts_view.accounts[4] = ("psyc://server/~updatedaccount4", False)
|
||||
#return
|
||||
|
||||
from pypsyc.client.model import Account
|
||||
account = Account(None, "", "", "", False, False)
|
||||
accounts_view.show_addedit_dialog(account.__dict__, True, _callback)
|
||||
|
||||
def _test_tabs_view(): # pragma: no cover
|
||||
status_icon = gtk.StatusIcon()
|
||||
status_icon.set_from_file('pypsyc/client/psyc.ico')
|
||||
status_icon.connect('activate', lambda w: tabs_view.on_status_icon_click())
|
||||
tabs_view = TabsView(status_icon)
|
||||
tabs_view.window.connect('destroy', lambda w: gtk.main_quit())
|
||||
Controller(controller.TabsController, tabs_view)
|
||||
|
||||
conversation_view = tabs_view.show_conversation("Conversation 1")
|
||||
Controller(controller.ConversationController, conversation_view)
|
||||
tabs_view.focus_tab(conversation_view)
|
||||
|
||||
conference_view = tabs_view.show_conference("Conversation 2")
|
||||
Controller(controller.ConferenceController, conference_view)
|
||||
|
||||
def show():
|
||||
for i in xrange(40):
|
||||
conversation_view.show_message("Message %02i" % i)
|
||||
for i in xrange(40):
|
||||
conference_view.members.append(("psyc://server/~person%02i" % i,
|
||||
"person%02i" % i))
|
||||
conference_view.show_message("Message %02i" % i)
|
||||
conference_view.members[20] = ("psyc://server/~longpersonname20",
|
||||
"longpersonname20")
|
||||
idle_add(show)
|
||||
|
||||
def _test_dump_view(): # pragma: no cover
|
||||
dump_view = DumpView()
|
||||
Controller(controller.DumpController, dump_view)
|
||||
dump_view.show_line('o', 'a', 'psyc://server/~account')
|
||||
dump_view.show_line('i', ':_tag_relay\t\xc8\xe6', 'psyc://server/~account')
|
||||
|
||||
def _test_main_view(): # pragma: no cover
|
||||
main_view = MainView()
|
||||
Controller(controller.MainController, main_view)
|
||||
Controller(controller.FriendListController, main_view.friends_view)
|
||||
for i in xrange(40):
|
||||
main_view.friends_view.friends.append(("Friend %02i" % i, False,
|
||||
'pending'))
|
||||
for i in xrange(0, 40, 2):
|
||||
main_view.friends_view.friends[i] = ("Friend %02i updated" % i, False,
|
||||
'offered')
|
||||
for i in xrange(0, 40, 3):
|
||||
main_view.friends_view.friends[i] = ("Friend %02i updated 2" % i, True,
|
||||
'established')
|
||||
#return
|
||||
|
||||
account_dict = {'password': '', 'save_password': False}
|
||||
main_view.show_password_dialog("psyc://server/~account", account_dict,
|
||||
_callback)
|
||||
main_view.show_no_such_user("psyc://server/~account")
|
||||
main_view.show_auth_error("psyc://server/~account", "Error description")
|
||||
main_view.show_open_conv_dialog(["psyc://server/~person1",
|
||||
"psyc://server/~person2"], _callback)
|
||||
main_view.show_open_conf_dialog(["psyc://server/~person1",
|
||||
"psyc://server/~person2"], _callback)
|
||||
main_view.show_add_friend_dialog(["psyc://server/~person1",
|
||||
"psyc://server/~person2"], _callback)
|
||||
|
||||
|
||||
if __name__ == '__main__': # pragma: no cover
|
||||
class Controller(object):
|
||||
def __init__(self, controller, view):
|
||||
self.controller = controller
|
||||
self.c_name = controller.__name__
|
||||
view.controller = self
|
||||
def __getattr__(self, attr):
|
||||
m = getattr(self.controller, attr, None)
|
||||
assert m, "%s has no method %s" % (self.c_name, attr)
|
||||
def f(*args, **kwds):
|
||||
m_argcount = m.__code__.co_argcount - 1 # minus self
|
||||
f_argcount = len(args) + len(kwds)
|
||||
assert m_argcount == f_argcount, \
|
||||
"%s.%s has %s arguments, called with %s arguments" % (
|
||||
self.c_name, attr, m_argcount, f_argcount)
|
||||
print "%s.%s called: %s, %s" % (self.c_name, attr, args, kwds)
|
||||
return f
|
||||
|
||||
from gobject import idle_add
|
||||
import gtk
|
||||
|
||||
from pypsyc.client.view import AccountsView, TabsView, DumpView, MainView
|
||||
from pypsyc.client import controller
|
||||
|
||||
# _test_accounts_view()
|
||||
# _test_tabs_view()
|
||||
# _test_dump_view()
|
||||
# _test_main_view()
|
||||
gtk.main()
|
Loading…
Add table
Add a link
Reference in a new issue