class CoreWorker(QThread): ready = pyqtSignal(object) status_updated = pyqtSignal(object, str) status_broadcasted = pyqtSignal(object) status_repeated = pyqtSignal(object, str, str, str) status_deleted = pyqtSignal(object, str, str, str) status_pushed_to_queue = pyqtSignal(str) status_poped_from_queue = pyqtSignal(object) status_deleted_from_queue = pyqtSignal() queue_cleared = pyqtSignal() status_posted_from_queue = pyqtSignal(object, str, str) message_deleted = pyqtSignal(object, str, str) message_sent = pyqtSignal(object, str) column_updated = pyqtSignal(object, tuple) account_saved = pyqtSignal() account_loaded = pyqtSignal() account_deleted = pyqtSignal() column_saved = pyqtSignal(str) column_deleted = pyqtSignal(str) status_marked_as_favorite = pyqtSignal(object, str, str, str) status_unmarked_as_favorite = pyqtSignal(object, str, str, str) fetched_user_profile = pyqtSignal(object, str) urls_shorted = pyqtSignal(object) media_uploaded = pyqtSignal(object) friends_list_updated = pyqtSignal() user_muted = pyqtSignal(str) user_unmuted = pyqtSignal(str) user_blocked = pyqtSignal(object) user_reported_as_spam = pyqtSignal(object) user_followed = pyqtSignal(object, str) user_unfollowed = pyqtSignal(object, str) exception_raised = pyqtSignal(object) status_from_conversation = pyqtSignal(object, str, str) fetched_profile_image = pyqtSignal(object) fetched_avatar = pyqtSignal(object, str) fetched_image_preview = pyqtSignal(object) cache_deleted = pyqtSignal() ERROR = -1 LOADING = 0 READY = 1 def __init__(self): QThread.__init__(self) self.queue = Queue.Queue() self.exit_ = False self.status = self.LOADING #self.core = Core() #self.queue_path = os.path.join(self.core.config.basedir, 'queue') #if not os.path.isfile(self.queue_path): # open(self.queue_path, 'w').close() self.core = None self.restart() #def __del__(self): # self.wait() def restart(self): self.register(self.login, (), self.__after_login, None) def add_friend(self, username): # FIXME: On libturpial friends = self.core.config.load_friends() friends.append(username) self.core.config.save_friends(friends) def remove_friend(self, username): # FIXME: On libturpial friends = self.core.config.load_friends() if username in friends: friends.remove(username) self.core.config.save_friends(friends) def __get_from_queue(self, index=0): lines = open(self.queue_path).readlines() if not lines: return None row = lines[index].strip() account_id, message = row.split("\1") del lines[index] open(self.queue_path, 'w').writelines(lines) status = Status() status.account_id = account_id status.text = message return status def __get_column_num_from_id(self, column_id): column_key = None for i in range(1, len(self.get_registered_columns()) + 1): column_num = "column%s" % i stored_id = self.core.config.read('Columns', column_num) if stored_id == column_id: column_key = column_num else: i += 1 return column_key #================================================================ # Core methods #================================================================ def login(self): self.core = Core() self.queue_path = os.path.join(self.core.config.basedir, 'queue') if not os.path.isfile(self.queue_path): open(self.queue_path, 'w').close() def get_default_browser(self): return self.core.get_default_browser() def get_update_interval(self): return self.core.get_update_interval() def get_statuses_per_column(self): return self.core.get_max_statuses_per_column() def get_minimize_on_close(self): return self.core.minimize_on_close() # FIXME: Implement support on libturpial def get_proxy_configuration(self): return self.core.config.read_section('Proxy') # FIXME: Implement support on libturpial def get_socket_timeout(self): return int(self.core.config.read('Advanced', 'socket-timeout')) def get_show_user_avatars(self): show_avatars = self.core.config.read('Advanced', 'show-user-avatars') return True if show_avatars == 'on' else False def get_update_interval_per_column(self, column_id): column_key = self.__get_column_num_from_id(column_id) key = "%s-update-interval" % column_key interval = self.core.config.read('Intervals', key) if not interval: # FIXME: Fix in libturpial config = self.core.config.read_all() if not config.has_key('Intervals'): config['Intervals'] = {key: 5} self.core.config.save(config) else: self.core.config.write('Intervals', key, 5) config = self.core.config.read_all() interval = "5" return int(interval) def set_update_interval_in_column(self, column_id, interval): column_key = self.__get_column_num_from_id(column_id) key = "%s-update-interval" % column_key self.core.config.write('Intervals', key, interval) return interval def get_show_notifications_in_column(self, column_id): column_key = self.__get_column_num_from_id(column_id) key = "%s-notifications" % column_key notifications = self.core.config.read('Notifications', key) if not notifications: # FIXME: Fix in libturpial config = self.core.config.read_all() if not config.has_key('Notifications'): config['Notifications'] = {} self.core.config.save(config) self.core.config.write('Notifications', key, 'on') notifications = 'on' if notifications == 'on': return True return False def set_show_notifications_in_column(self, column_id, value): column_key = self.__get_column_num_from_id(column_id) key = "%s-notifications" % column_key if value: notifications = 'on' else: notifications = 'off' self.core.config.write('Notifications', key, notifications) return value # FIXME: Fix this on libturpial def get_cache_size(self): total_size = 0 for account in self.get_all_accounts(): total_size += account.get_cache_size() return total_size # FIXME: Fix this on libturpial def delete_cache(self): for account in self.get_all_accounts(): account.delete_cache() def get_sound_on_login(self): sound_on_login = self.core.config.read('Sounds', 'login') if sound_on_login is None: self.core.config.write('Sounds', 'login', 'on') return True else: if sound_on_login == 'on': return True return False def get_sound_on_updates(self): sound_on_update = self.core.config.read('Sounds', 'updates') if sound_on_update is None: self.core.config.write('Sounds', 'updates', 'on') return True else: if sound_on_update == 'on': return True return False def get_notify_on_updates(self): try: notify_on_update = self.core.config.cfg.get('Notifications', 'updates', raw=True) if notify_on_update == 'on': return True return False except: config = self.read_config() config['Notifications']['on-updates'] = 'on' self.core.save_all_config(config) return True def get_notify_on_actions(self): try: notify_on_actions = self.core.config.cfg.get('Notifications', 'actions', raw=True) if notify_on_actions == 'on': return True return False except: config = self.read_config() config['Notifications']['actions'] = 'on' self.core.save_all_config(config) return True def get_queue_interval(self): try: queue_interval = self.core.config.cfg.get('General', 'queue-interval', raw=True) return int(queue_interval) except: config = self.read_config() config['General']['queue-interval'] = 30 self.core.save_all_config(config) return config['General']['queue-interval'] def read_config(self): config = {} # FIXME: Implemen this on libturpial for section in self.core.config.cfg.sections(): if not config.has_key(section): config[section] = {} for item in self.core.config.cfg.items(section, raw=True): for value in item: config[section][item[0]] = item[1] return config def update_config(self, new_config): self.core.save_all_config(new_config) def get_shorten_url_service(self): return self.core.get_shorten_url_service() def get_upload_media_service(self): return self.core.get_upload_media_service() def get_available_columns(self): return self.core.available_columns() def get_all_accounts(self): return self.core.registered_accounts() def get_all_columns(self): return self.core.all_columns() def get_registered_accounts(self): return self.core.registered_accounts() def get_available_short_url_services(self): return self.core.available_short_url_services() def get_available_upload_media_services(self): return self.core.available_upload_media_services() def get_registered_columns(self): i = 1 columns = [] while True: column_num = "column%s" % i column_id = self.core.config.read('Columns', column_num) if column_id: account_id = get_account_id_from(column_id) column_slug = get_column_slug_from(column_id) columns.append(Column(account_id, column_slug)) i += 1 else: break return columns def is_muted(self, username): return self.core.is_muted(username) def load_friends_list(self): return self.core.load_all_friends_list() def save_account(self, account): account_id = self.core.register_account(account) self.load_account(account_id, trigger_signal=False) self.__after_save_account() # FIXME: Remove this after implement this in libturpial def load_account(self, account_id, trigger_signal=True): if trigger_signal: self.register(self.core.accman.load, (account_id), self.__after_load_account) else: self.core.accman.load(account_id) self.__after_load_account() def delete_account(self, account_id): # FIXME: Implement try/except for col in self.get_registered_columns(): if col.account_id == account_id: self.delete_column(col.id_) self.core.unregister_account(str(account_id), True) self.__after_delete_account() def save_column(self, column_id): #FIXME: Hack to avoid the libturpial error saving config self.update_config(self.read_config()) reg_column_id = self.core.register_column(column_id) self.__after_save_column(reg_column_id) def delete_column(self, column_id): deleted_column = self.core.unregister_column(column_id) self.__after_delete_column(column_id) def get_column_statuses(self, column, last_id): count = self.core.get_max_statuses_per_column() self.register(self.core.get_column_statuses, (column.account_id, column.slug, count, last_id), self.__after_update_column, (column, count)) def update_status(self, account_id, message, in_reply_to_id=None): self.register(self.core.update_status, (account_id, message, in_reply_to_id), self.__after_update_status, account_id) def broadcast_status(self, accounts, message): self.register(self.core.broadcast_status, (accounts, message), self.__after_broadcast_status) def repeat_status(self, column_id, account_id, status_id): self.register(self.core.repeat_status, (account_id, status_id), self.__after_repeat_status, (column_id, account_id, status_id)) def delete_status(self, column_id, account_id, status_id): self.register(self.core.destroy_status, (account_id, status_id), self.__after_delete_status, (column_id, account_id, status_id)) def delete_direct_message(self, column_id, account_id, status_id): self.register(self.core.destroy_direct_message, (account_id, status_id), self.__after_delete_direct_message, (column_id, account_id)) def send_direct_message(self, account_id, username, message): self.register(self.core.send_direct_message, (account_id, username, message), self.__after_send_direct_message, account_id) def mark_status_as_favorite(self, column_id, account_id, status_id): self.register(self.core.mark_status_as_favorite, (account_id, status_id), self.__after_mark_status_as_favorite, (column_id, account_id, status_id)) def unmark_status_as_favorite(self, column_id, account_id, status_id): self.register(self.core.unmark_status_as_favorite, (account_id, status_id), self.__after_unmark_status_as_favorite, (column_id, account_id, status_id)) def get_user_profile(self, account_id, user_profile=None): self.register(self.core.get_user_profile, (account_id, user_profile), self.__after_get_user_profile, account_id) def short_urls(self, message): self.register(self.core.short_url_in_message, (message), self.__after_short_urls) def upload_media(self, account_id, filepath): self.register(self.core.upload_media, (account_id, filepath), self.__after_upload_media) def get_friends_list(self): self.register(self.core.get_all_friends_list, None, self.__after_get_friends_list) def mute(self, username): self.register(self.core.mute, username, self.__after_mute_user) def unmute(self, username): self.register(self.core.unmute, username, self.__after_unmute_user) def block(self, account_id, username): self.register(self.core.block, (account_id, username), self.__after_block_user) def report_as_spam(self, account_id, username): self.register(self.core.report_as_spam, (account_id, username), self.__after_report_user_as_spam) def follow(self, account_id, username): self.register(self.core.follow, (account_id, username), self.__after_follow_user, account_id) def unfollow(self, account_id, username): self.register(self.core.unfollow, (account_id, username), self.__after_unfollow_user, account_id) def get_status_from_conversation(self, account_id, status_id, column_id, status_root_id): self.register(self.core.get_single_status, (account_id, status_id), self.__after_get_status_from_conversation, (column_id, status_root_id)) def get_profile_image(self, account_id, username): self.register(self.core.get_profile_image, (account_id, username), self.__after_get_profile_image) def get_avatar_from_status(self, status): self.register(self.core.get_status_avatar, (status), self.__after_get_avatar_from_status, status.username) def get_image_preview(self, preview_service, url): self.register(preview_service.do_service, (url), self.__after_get_image_preview) def push_status_to_queue(self, account_id, message): fd = open(self.queue_path, 'a+') row = "%s\1%s\n" % (account_id, message) fd.write(row.encode('utf-8')) fd.close() self.__after_push_status_to_queue(account_id) def pop_status_from_queue(self): status = self.__get_from_queue() self.__after_pop_status_from_queue(status) def delete_status_from_queue(self, index=0): status = self.__get_from_queue(index) self.__after_delete_status_from_queue() def list_statuses_queue(self): statuses = [] lines = [] if os.path.exists(self.queue_path): lines = open(self.queue_path).readlines() for line in lines: account_id, message = line.strip().split("\1") status = Status() status.account_id = account_id status.text = message statuses.append(status) return statuses def clear_statuses_queue(self): open(self.queue_path, 'w').writelines([]) self.__after_clear_queue() def post_status_from_queue(self, account_id, message): self.register(self.core.update_status, (account_id, message), self.__after_post_status_from_queue, (account_id, message)) def delete_cache(self): self.register(self.core.delete_cache, None, self.__after_delete_cache) def list_filters(self): return self.core.list_filters() def save_filters(self, filters): self.core.save_filters(filters) def restore_config(self): self.core.delete_current_config() def get_window_size(self): try: size = self.core.config.cfg.get('Window', 'size', raw=True) window_size = int(size.split(',')[0]), int(size.split(',')[1]) return window_size except: config = self.read_config() config['Window']['size'] = "320,480" self.core.save_all_config(config) return config['Window']['size'] def set_window_size(self, width, height): window_size = "%s,%s" % (width, height) #FIXME: Hack to avoid the libturpial error saving config self.update_config(self.read_config()) self.core.config.write('Window', 'size', window_size) #================================================================ # Callbacks #================================================================ def __after_login(self, response): self.ready.emit(response) def __after_save_account(self): self.account_saved.emit() def __after_load_account(self, response=None): self.account_loaded.emit() def __after_delete_account(self): self.account_deleted.emit() def __after_save_column(self, column_id): self.column_saved.emit(column_id) def __after_delete_column(self, column_id): self.column_deleted.emit(column_id) def __after_update_column(self, response, data): self.column_updated.emit(response, data) def __after_update_status(self, response, account_id): self.status_updated.emit(response, account_id) def __after_broadcast_status(self, response): self.status_broadcasted.emit(response) def __after_repeat_status(self, response, args): column_id = args[0] account_id = args[1] status_id = args[2] self.status_repeated.emit(response, column_id, account_id, status_id) def __after_delete_status(self, response, args): column_id = args[0] account_id = args[1] status_id = args[2] self.status_deleted.emit(response, column_id, account_id, status_id) def __after_delete_direct_message(self, response, args): column_id = args[0] account_id = args[1] self.message_deleted.emit(response, column_id, account_id) def __after_send_direct_message(self, response, account_id): self.message_sent.emit(response, account_id) def __after_mark_status_as_favorite(self, response, args): column_id = args[0] account_id = args[1] status_id = args[2] self.status_marked_as_favorite.emit(response, column_id, account_id, status_id) def __after_unmark_status_as_favorite(self, response, args): column_id = args[0] account_id = args[1] status_id = args[2] self.status_unmarked_as_favorite.emit(response, column_id, account_id, status_id) def __after_get_user_profile(self, response, account_id): self.fetched_user_profile.emit(response, account_id) def __after_short_urls(self, response): self.urls_shorted.emit(response) def __after_upload_media(self, response): self.media_uploaded.emit(response) def __after_get_friends_list(self, response): self.friends_list_updated.emit() def __after_mute_user(self, response): self.user_muted.emit(response) def __after_unmute_user(self, response): self.user_unmuted.emit(response) def __after_block_user(self, response): self.user_blocked.emit(response) def __after_report_user_as_spam(self, response): self.user_reported_as_spam.emit(response) def __after_follow_user(self, response, account_id): self.user_followed.emit(response, account_id) def __after_unfollow_user(self, response, account_id): self.user_unfollowed.emit(response, account_id) def __after_get_status_from_conversation(self, response, args): column_id = args[0] status_root_id = args[1] self.status_from_conversation.emit(response, column_id, status_root_id) def __after_get_profile_image(self, response): self.fetched_profile_image.emit(response) def __after_get_avatar_from_status(self, response, args): username = args self.fetched_avatar.emit(response, username) def __after_get_image_preview(self, response): self.fetched_image_preview.emit(response) def __after_push_status_to_queue(self, account_id): self.status_pushed_to_queue.emit(account_id) def __after_pop_status_from_queue(self, status): self.status_poped_from_queue.emit(status) def __after_delete_status_from_queue(self): self.status_deleted_from_queue.emit() def __after_clear_queue(self): self.queue_cleared.emit() def __after_post_status_from_queue(self, response, args): account_id = args[0] message = args[1] self.status_posted_from_queue.emit(response, account_id, message) def __after_delete_cache(self): self.cache_deleted.emit() #================================================================ # Worker methods #================================================================ def register(self, funct, args, callback, user_data=None): self.queue.put((funct, args, callback, user_data)) def quit(self): self.exit_ = True def run(self): while not self.exit_: try: req = self.queue.get(True, 0.3) except Queue.Empty: continue except: continue (funct, args, callback, user_data) = req try: if type(args) == tuple: rtn = funct(*args) elif args: rtn = funct(args) else: rtn = funct() except Exception, e: #self.exception_raised.emit(e) #continue rtn = e if callback: if user_data: callback(rtn, user_data) else: callback(rtn)
class TestCore: @classmethod @pytest.fixture(autouse=True) def setup_class(self, monkeypatch): self.acc_id = "dummy-twitter" self.core = Core(load_accounts=False) self.account = Account.new("twitter", "dummy") self.account.columns = [Column(self.account.id_, ColumnType.TIMELINE)] self.account2 = Account.new("twitter", "qux") self.account2.columns = [Column(self.account2.id_, ColumnType.TIMELINE)] self.all_accounts = [self.account, self.account2] monkeypatch.setattr(self.core.accman, "get", lambda x: self.account) def test_core_is_a_valid_instance(self): assert isinstance(self.core, Core) def test_local_variables(self): assert self.core.config != None assert isinstance(self.core.config, AppConfig) assert self.core.accman != None assert isinstance(self.core.accman, AccountManager) assert self.core.column_manager != None assert isinstance(self.core.column_manager, ColumnManager) def test_filter_statuses(self, monkeypatch): status = Status() status.id_ = "123" status.username = "******" status.repeated_by = "bar" status.text = "Please, filter me" statuses = [status] monkeypatch.setattr(self.core.config, "load_filters", lambda: []) response = self.core.filter_statuses(statuses) assert response == statuses monkeypatch.setattr(self.core.config, "load_filters", lambda: ['@foo']) response = self.core.filter_statuses(statuses) assert response == [] monkeypatch.setattr(self.core.config, "load_filters", lambda: ['@bar']) response = self.core.filter_statuses(statuses) assert response == [] monkeypatch.setattr(self.core.config, "load_filters", lambda: ['filter']) response = self.core.filter_statuses(statuses) assert response == [] monkeypatch.setattr(self.core.config, "load_filters", lambda: ['dummy']) response = self.core.filter_statuses(statuses) assert response == statuses def test_fetch_image(self, monkeypatch): monkeypatch.setattr(requests, 'get', lambda x: DummyResponse('binarydata')) assert self.core.fetch_image('http://my_image.png') == 'binarydata' def test_list_methods(self): accounts = self.core.list_accounts() assert isinstance(accounts, list) for item in accounts: assert isinstance(item, str) protocols = self.core.list_protocols() assert isinstance(protocols, list) for item in protocols: assert isinstance(item, str) filters = self.core.list_filters() assert isinstance(filters, list) for item in filters: assert isinstance(item, str) def test_registering(self, monkeypatch): dummy = Account('twitter') monkeypatch.setattr(self.core.accman, "register", lambda x: self.acc_id) monkeypatch.setattr(self.core.column_manager, "register", lambda x: "dummy-twitter-column1") result = self.core.register_account(dummy) assert isinstance(result, str) result = self.core.register_column("dummy-twitter-column1") assert isinstance(result, str) def test_unregistering(self, monkeypatch): monkeypatch.setattr(self.core.accman, "unregister", lambda x,y: self.acc_id) monkeypatch.setattr(self.core.column_manager, "unregister", lambda x: "dummy-twitter-column1") result = self.core.unregister_account(self.acc_id) assert isinstance(result, str) result = self.core.unregister_column("dummy-twitter-column1") assert isinstance(result, str) def test_all_columns(self, monkeypatch): monkeypatch.setattr(self.core, 'registered_accounts', lambda: self.all_accounts) columns = self.core.all_columns() assert isinstance(columns, dict) for key, value in columns.iteritems(): assert isinstance(value, list) for col in value: assert (isinstance(col, Column) or isinstance(col, List)) def test_available_columns(self, monkeypatch): monkeypatch.setattr(self.core, 'registered_accounts', lambda: self.all_accounts) columns = self.core.available_columns() assert isinstance(columns, dict) for key, value in columns.iteritems(): assert isinstance(value, list) for col in value: assert (isinstance(col, Column) or isinstance(col, List)) def test_registered_columns(self): columns = self.core.registered_columns() assert isinstance(columns, dict) for key, value in columns.iteritems(): assert isinstance(value, list) for col in value: assert (isinstance(col, Column) or isinstance(col, List)) def test_registered_columns_by_order(self): columns = self.core.registered_columns_by_order() assert isinstance(columns, list) for col in columns: assert (isinstance(col, Column) or isinstance(col, List)) def test_registered_accounts(self): accounts = self.core.registered_accounts() assert isinstance(accounts, list) for acc in accounts: assert isinstance(acc, Account) def test_get_single_column(self, monkeypatch): dummy = Column(self.acc_id, "foo") monkeypatch.setattr(self.core.column_manager, "get", lambda x: dummy) column = self.core.get_single_column("dummy-twitter-column") assert isinstance(column, Column) def test_get_single_account(self): account = self.core.get_single_account(self.acc_id) assert isinstance(account, Account) def test_get_column_statuses(self, monkeypatch): status = Status() result = [status] monkeypatch.setattr(self.account, "get_timeline", lambda x, y: result) response = self.core.get_column_statuses(self.acc_id, "timeline") assert response == result monkeypatch.setattr(self.account, "get_replies", lambda x, y: result) response = self.core.get_column_statuses(self.acc_id, "replies") assert response == result monkeypatch.setattr(self.account, "get_directs", lambda x, y: result) response = self.core.get_column_statuses(self.acc_id, "directs") assert response == result monkeypatch.setattr(self.account, "get_favorites", lambda x: result) response = self.core.get_column_statuses(self.acc_id, "favorites") assert response == result monkeypatch.setattr(self.account, "get_sent", lambda x, y: result) response = self.core.get_column_statuses(self.acc_id, "sent") assert response == result monkeypatch.setattr(self.account, "get_public_timeline", lambda x, y: result) response = self.core.get_column_statuses(self.acc_id, "public") assert response == result list_ = List() list_.id_ = "666" monkeypatch.setattr(self.account, "get_list_id", lambda x: list_) status.id_ = "127" monkeypatch.setattr(self.account, "get_list_statuses", lambda x, y, z: result) response = self.core.get_column_statuses(self.acc_id, "my-list") assert isinstance(response, list) assert response[0].id_, "127" monkeypatch.setattr(self.account, "get_list_id", lambda x: None) with pytest.raises(UserListNotFound): self.core.get_column_statuses(self.acc_id, "unknown-list") monkeypatch.setattr(self.core, 'search', lambda w, x, y, z: result) response = self.core.get_column_statuses(self.acc_id, "search-dummy") assert response == result def test_get_public_timeline(self, monkeypatch): status = Status() status.id_ = "123" result = [status] monkeypatch.setattr(self.account, "get_public_timeline", lambda x, y: result) response = self.core.get_public_timeline(self.acc_id) assert isinstance(response, list) assert response[0].id_, "123" def test_get_followers(self, monkeypatch): profile = Profile() profile.id_ = "dummy" result = [profile] monkeypatch.setattr(self.account, "get_followers", lambda x: result) response = self.core.get_followers(self.acc_id) assert isinstance(response, list) assert response[0].id_, "dummy" def test_get_following(self, monkeypatch): profile = Profile() profile.id_ = "dummy" result = [profile] monkeypatch.setattr(self.account, "get_following", lambda x: result) response = self.core.get_following(self.acc_id) assert isinstance(response, list) assert response[0].id_, "dummy" def test_get_all_friends_list(self, monkeypatch): account = Account.new("twitter") accounts = [account] profile = Profile() profile.username = "******" result = [profile] monkeypatch.setattr(self.core.accman, "accounts", lambda: accounts) monkeypatch.setattr(account, "get_following", lambda: result) friends = self.core.get_all_friends_list() assert isinstance(friends, list) assert friends[0], "dummy" def test_load_all_friends_list(self): result = self.core.load_all_friends_list() assert isinstance(result, list) for friend in result: assert isinstance(friend, str) def test_get_user_profile(self, monkeypatch): profile = Profile() self.account.profile = profile result = [profile] monkeypatch.setattr(self.account, "get_profile", lambda x: profile) monkeypatch.setattr(self.account, "is_friend", lambda x: True) monkeypatch.setattr(self.core, "is_muted", lambda x: False) profile.id_ = "dummy" response = self.core.get_user_profile(self.acc_id) assert isinstance(response, Profile) assert response.id_, "dummy" profile.id_ = "user" response = self.core.get_user_profile(self.acc_id, "user") assert isinstance(response, Profile) assert response.id_, "user" assert response.followed_by, True assert not response.muted, True def test_get_conversation(self, monkeypatch): status = Status() status.id_ = "321" conversation = [status] monkeypatch.setattr(self.account, "get_conversation", lambda x: conversation) response = self.core.get_conversation(self.acc_id, "123") assert isinstance(response, list) assert isinstance(response[0], Status) assert response[0].id_, "321" def test_update_status(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "update_status", lambda x, y, z: status) response = self.core.update_status(self.acc_id, "Dummy message") assert response == status response = self.core.update_status(self.acc_id, "Dummy message", "123456") assert response == status response = self.core.update_status(self.acc_id, "Dummy message", "123456", "/path/to/media") assert response == status def test_broadcast_status(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "update_status", lambda x: status) monkeypatch.setattr(self.core, "registered_accounts", lambda: [self.account]) response = self.core.broadcast_status(None, "Dummy message") assert isinstance(response, dict) assert isinstance(response["dummy-twitter"], Status) assert response["dummy-twitter"] == status response = self.core.broadcast_status(["foo-twitter", "bar-twitter"], "Dummy message") assert isinstance(response, dict) assert isinstance(response["foo-twitter"], Status) assert response["foo-twitter"] == status assert isinstance(response["bar-twitter"], Status) assert response["bar-twitter"] == status monkeypatch.setattr(self.core.accman, "get", lambda x: 'whatever') response = self.core.broadcast_status(["foo-twitter"], "Dummy message") assert isinstance(response["foo-twitter"], Exception) def test_destroy_status(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "destroy_status", lambda x: status) response = self.core.destroy_status("dummy-twitter", "123") assert isinstance(response, Status) assert response == status def test_get_single_status(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "get_status", lambda x: status) response = self.core.get_single_status("dummy-twitter", "123") assert isinstance(response, Status) assert response == status def test_repeat_status(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "repeat_status", lambda x: status) response = self.core.repeat_status("dummy-twitter", "123") assert isinstance(response, Status) assert response == status def test_mark_status_as_favorite(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "mark_as_favorite", lambda x: status) response = self.core.mark_status_as_favorite("dummy-twitter", "123") assert isinstance(response, Status) assert response == status def test_unmark_status_as_favorite(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "unmark_as_favorite", lambda x: status) response = self.core.unmark_status_as_favorite("dummy-twitter", "123") assert isinstance(response, Status) assert response == status def test_send_direct_message(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "send_direct_message", lambda x, y: status) response = self.core.send_direct_message("dummy-twitter", "foo", "Dummy message") assert isinstance(response, Status) assert response == status def test_destroy_direct_message(self, monkeypatch): status = Status() monkeypatch.setattr(self.account, "destroy_direct_message", lambda x: status) response = self.core.destroy_direct_message("dummy-twitter", "123") assert isinstance(response, Status) assert response == status def test_update_profile(self, monkeypatch): profile = Profile() monkeypatch.setattr(self.account, "update_profile", lambda w, x, y, z: profile) # TODO: Test with no params # response = self.core.update_profile(self.acc_id) # raise excetion response = self.core.update_profile(self.acc_id, fullname="Another fullname") assert response == profile response = self.core.update_profile(self.acc_id, bio="Dummy bio") assert response == profile response = self.core.update_profile(self.acc_id, url="http://crazy.url") assert response == profile response = self.core.update_profile(self.acc_id, location="Nowhere") assert response == profile response = self.core.update_profile(self.acc_id, "Another fullname", "http://crazy.url", "Dummy bio", "Nowhere") assert response == profile def test_follow(self, monkeypatch): profile = Profile() profile.username = "******" monkeypatch.setattr(self.account, "follow", lambda x, y: profile) monkeypatch.setattr(self.core, "add_friend", lambda x: None) response = self.core.follow(self.acc_id, "foo") assert response == profile response = self.core.follow(self.acc_id, "123", True) assert response == profile def test_unfollow(self, monkeypatch): profile = Profile() profile.username = "******" monkeypatch.setattr(self.account, "unfollow", lambda x: profile) monkeypatch.setattr(self.core, "remove_friend", lambda x: None) response = self.core.unfollow(self.acc_id, "foo") assert response == profile def test_block(self, monkeypatch): profile = Profile() profile.username = "******" monkeypatch.setattr(self.account, "block", lambda x: profile) monkeypatch.setattr(self.core, "remove_friend", lambda x: None) response = self.core.block(self.acc_id, "foo") assert response == profile def test_unblock(self, monkeypatch): profile = Profile() profile.username = "******" monkeypatch.setattr(self.account, "unblock", lambda x: profile) response = self.core.unblock(self.acc_id, "foo") assert response == profile def test_report_as_spam(self, monkeypatch): profile = Profile() profile.username = "******" monkeypatch.setattr(self.account, "report_as_spam", lambda x: profile) monkeypatch.setattr(self.core, "remove_friend", lambda x: None) response = self.core.report_as_spam(self.acc_id, "foo") assert response == profile def test_mute(self, monkeypatch): monkeypatch.setattr(self.core.config, "append_filter", lambda x: "foo") response = self.core.mute("foo") assert response == "foo" def test_unmute(self, monkeypatch): monkeypatch.setattr(self.core.config, "remove_filter", lambda x: "foo") response = self.core.unmute("foo") assert response == "foo" def test_verify_friendship(self, monkeypatch): monkeypatch.setattr(self.account, "is_friend", lambda x: True) response = self.core.verify_friendship(self.acc_id, "foo") assert response is True monkeypatch.setattr(self.account, "is_friend", lambda x: False) response = self.core.verify_friendship(self.acc_id, "foo") assert response is False def test_search(self, monkeypatch): search = [Status()] monkeypatch.setattr(self.account, "search", lambda w, x, y, z: search) response = self.core.search(self.acc_id, "dummy", since_id="123", count=200, extra="ble") assert isinstance(response, list) assert isinstance(response[0], Status) response = self.core.search(self.acc_id, "dummy") assert isinstance(response, list) assert isinstance(response[0], Status) response = self.core.search(self.acc_id, "dummy", 200, "123", "ble") assert isinstance(response, list) assert isinstance(response[0], Status) def test_get_profile_image(self, monkeypatch): monkeypatch.setattr(os.path, "join", lambda x, y: '/path/to/ble') monkeypatch.setattr(os.path, "isfile", lambda x: True) monkeypatch.setattr(self.core.accman, 'get', lambda x: DummyAccount()) monkeypatch.setattr(__builtin__, 'open', lambda x, y: DummyFileHandler()) response = self.core.get_profile_image(self.acc_id, "foo") assert response == "/path/to/ble" monkeypatch.setattr(self.core, 'fetch_image', lambda x: 'binary') monkeypatch.setattr(os.path, "join", lambda x, y: '/path/to/bla') response = self.core.get_profile_image(self.acc_id, "foo", False) assert response == "/path/to/bla" def test_get_status_avatar(self, monkeypatch): status = Status() status.id_ = "123456789" status.account_id = "foo-twitter" status.username = "******" status.avatar = "http://my.dummy/avatar" monkeypatch.setattr(os.path, "join", lambda x, y: '/path/to/ble') monkeypatch.setattr(os.path, "isfile", lambda x: True) monkeypatch.setattr(self.core.accman, 'get', lambda x: DummyAccount()) response = self.core.get_status_avatar(status) assert response == "/path/to/ble" monkeypatch.setattr(os.path, "join", lambda x, y: '/path/to/bla') monkeypatch.setattr(os.path, "isfile", lambda x: False) monkeypatch.setattr(__builtin__, 'open', lambda x, y: DummyFileHandler()) monkeypatch.setattr(self.core, 'fetch_image', lambda x: 'binary') response = self.core.get_status_avatar(status) assert response == "/path/to/bla" def test_get_available_trend_locations(self, monkeypatch): location = TrendLocation('Global', 1) monkeypatch.setattr(self.account, "available_trend_locations", lambda: [location]) response = self.core.get_available_trend_locations(self.acc_id) assert isinstance(response, list) assert response[0] == location def test_get_trending_topics(self, monkeypatch): trend = Trend('Foo') monkeypatch.setattr(self.account, "trends", lambda x: [trend]) response = self.core.get_trending_topics(self.acc_id, "here") assert isinstance(response, list) assert response[0] == trend def test_update_profile_image(self, monkeypatch): profile = Profile() monkeypatch.setattr(self.account, "update_profile_image", lambda x: profile) response = self.core.update_profile_image(self.acc_id, "/path/to/ble") assert response == profile # Services def test_available_short_url_services(self): response = self.core.available_short_url_services() assert isinstance(response, list) assert isinstance(response[0], str) def test_short_single_url(self, monkeypatch): monkeypatch.setattr(self.core, "get_shorten_url_service", lambda: 'is.gd') with pytest.raises(URLAlreadyShort): self.core.short_single_url('http://is.gd/blable') monkeypatch.setattr(self.core, "_Core__get_short_url_object", lambda x: DummyService('http://is.gd/ble')) response = self.core.short_single_url('http://my.looooon.url') assert response == 'http://is.gd/ble' def test_short_url_in_message(self, monkeypatch): monkeypatch.setattr(self.core, "get_shorten_url_service", lambda: 'is.gd') message = "Hello, this is a message with a dumb url http://dumb.url.com" message_with_short_url = "Hello, this is a message with a short url http://is.gd/dumb" message_with_no_url = "Hello, this is a message with no url" message_expected = "Hello, this is a message with a dumb url http://is.gd/dumb" with pytest.raises(NoURLToShorten): self.core.short_url_in_message(message_with_no_url) monkeypatch.setattr(self.core, "short_single_url", lambda x: 'http://is.gd/dumb') response = self.core.short_url_in_message(message) assert response == message_expected def raise_ex(): raise URLAlreadyShort response = self.core.short_url_in_message(message_with_short_url) assert response == message_with_short_url def test_available_preview_media_services(self): response = self.core.available_preview_media_services() assert isinstance(response, list) assert isinstance(response[0], str) def test_preview_media(self, monkeypatch): media = Media.new_image('foo', 'binary content') with pytest.raises(PreviewServiceNotSupported): self.core.preview_media('http://unsupported.service.com/ble') monkeypatch.setattr('libturpial.lib.services.media.preview.pictwitter.PicTwitterMediaContent._get_content_from_url', lambda x, y: '123') response = self.core.preview_media('http://pic.twitter.com/ble') assert isinstance(response, Media) def test_available_upload_media_services(self): response = self.core.available_upload_media_services() assert isinstance(response, list) assert isinstance(response[0], str) def test_upload_media(self, monkeypatch): monkeypatch.setattr(self.core, "get_upload_media_service", lambda: 'ima.ge') monkeypatch.setattr(self.core, "_Core__get_upload_media_object", lambda x: DummyService('http://ima.ge/bla')) response = self.core.upload_media(self.acc_id, '/path/to/file', "Dummy message") assert response == 'http://ima.ge/bla' # Configuration def test_register_new_config_option(self, monkeypatch): monkeypatch.setattr(self.core.config, 'register_extra_option', lambda x, y, z: None) # TODO: How to test that this works? Return 0 maybe? assert self.core.register_new_config_option('foo', 'bar', 'baz') == None def test_get_shorten_url_service(self): response = self.core.get_shorten_url_service() assert isinstance(response, str) def test_get_upload_media_service(self): response = self.core.get_upload_media_service() assert isinstance(response, str) def test_set_shorten_url_service(self, monkeypatch): monkeypatch.setattr(self.core.config, "write", lambda x, y, z: None) response = self.core.set_shorten_url_service('foo') assert response is None def test_set_upload_media_service(self, monkeypatch): monkeypatch.setattr(self.core.config, "write", lambda x, y, z: None) response = self.core.set_upload_media_service('foo') assert response is None def test_has_stored_passwwd(self, monkeypatch): account = Account.new("twitter") profile = Profile() profile.username = "******" account.profile = profile monkeypatch.setattr(self.core.accman, "get", lambda x: account) profile.password = None response = self.core.has_stored_passwd("foo") assert response == False profile.password = '' response = self.core.has_stored_passwd("foo") assert response == False profile.password = '******' response = self.core.has_stored_passwd("foo") assert response == True def test_is_account_logged_in(self, monkeypatch): account = Account.new("twitter") monkeypatch.setattr(self.core.accman, "get", lambda x: account) account.logged_in = False response = self.core.is_account_logged_in("foo") assert response == False account.logged_in = True response = self.core.is_account_logged_in("foo") assert response == True def test_is_muted(self, monkeypatch): monkeypatch.setattr(self.core.config, "load_filters", lambda: ['@foo', '@bar', 'baz']) response = self.core.is_muted('foo') assert response == True response = self.core.is_muted('baz') assert response == False def test_get_default_browser(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: None) response = self.core.get_default_browser() assert response is None monkeypatch.setattr(self.core.config, "read", lambda x, y: 'chromium') response = self.core.get_default_browser() assert response == 'chromium' def test_show_notifications_in_login(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: 'on') response = self.core.show_notifications_in_login() assert response == True monkeypatch.setattr(self.core.config, "read", lambda x, y: 'off') response = self.core.show_notifications_in_login() assert response == False def test_show_notifications_in_updates(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: 'on') response = self.core.show_notifications_in_updates() assert response == True monkeypatch.setattr(self.core.config, "read", lambda x, y: 'off') response = self.core.show_notifications_in_updates() assert response == False def test_play_sounds_in_login(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: 'on') response = self.core.play_sounds_in_login() assert response == True monkeypatch.setattr(self.core.config, "read", lambda x, y: 'off') response = self.core.play_sounds_in_login() assert response == False def test_play_sounds_in_updates(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: 'on') response = self.core.play_sounds_in_updates() assert response == True monkeypatch.setattr(self.core.config, "read", lambda x, y: 'off') response = self.core.play_sounds_in_updates() assert response == False def test_get_max_statuses_per_column(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: '200') response = self.core.get_max_statuses_per_column() assert isinstance(response, int) def test_get_proxy(self, monkeypatch): proxy = Proxy('1.2.3.4', '666') monkeypatch.setattr(self.core.config, "get_proxy", lambda: proxy) response = self.core.get_proxy() assert isinstance(response, Proxy) def test_get_socket_timeout(self, monkeypatch): monkeypatch.setattr(self.core.config, "get_socket_timeout", lambda: '20') response = self.core.get_socket_timeout() assert isinstance(response, int) def test_get_update_interval(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: '200') response = self.core.get_update_interval() assert isinstance(response, int) def test_minimize_on_close(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: 'on') response = self.core.minimize_on_close() assert response == True monkeypatch.setattr(self.core.config, "read", lambda x, y: 'off') response = self.core.minimize_on_close() assert response == False def test_get_config(self, monkeypatch): monkeypatch.setattr(self.core.config, "read_all", lambda: APP_CFG) response = self.core.get_config() assert isinstance(response, dict) def test_read_config_value(self, monkeypatch): monkeypatch.setattr(self.core.config, "read", lambda x, y: "foo") response = self.core.read_config_value("bar", "baz") assert response == "foo" def test_write_config_value(self, monkeypatch): monkeypatch.setattr(self.core.config, "write", lambda x, y, z: None) response = self.core.write_config_value("foo", "bar", "baz") assert response is None def test_save_all_config(self, monkeypatch): monkeypatch.setattr(self.core.config, "save", lambda x: None) response = self.core.save_all_config({}) assert response is None def test_list_filters(self): response = self.core.list_filters() assert isinstance(response, list) def test_save_filters(self, monkeypatch): monkeypatch.setattr(self.core.config, "save_filters", lambda x: None) response = self.core.save_filters([]) assert response is None def test_delete_current_config(self, monkeypatch): monkeypatch.setattr(self.core.config, "delete", lambda: None) response = self.core.delete_current_config() assert response is None def test_delete_cache(self, monkeypatch): monkeypatch.setattr(self.account, "delete_cache", lambda: None) monkeypatch.setattr(self.core, "registered_accounts", lambda: [self.account]) response = self.core.delete_cache() assert response is None def test_get_cache_size(self, monkeypatch): monkeypatch.setattr(self.account, "get_cache_size", lambda: 10) monkeypatch.setattr(self.core, "registered_accounts", lambda: [self.account]) response = self.core.get_cache_size() assert response == 10 def test_add_friend(self, monkeypatch): monkeypatch.setattr(self.core.config, "save_friends", lambda x: None) response = self.core.add_friend("foo") assert response is None def test_remove_friend(self, monkeypatch): monkeypatch.setattr(self.core.config, "save_friends", lambda x: None) monkeypatch.setattr(self.core.config, "load_friends", lambda: ['foo']) response = self.core.remove_friend("foo") assert response is None