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_new_with_params(self, monkeypatch): monkeypatch.setattr('libturpial.api.models.account.Account', DummyAccount) account = Account.new_from_params('twitter', 'foo', '123', '456', '789') assert isinstance(account, DummyAccount)
def test_load(self, monkeypatch): monkeypatch.setattr('libturpial.config.AccountConfig', DummyConfig) with pytest.raises(ErrorLoadingAccount): Account.load('foo-twitter') # Monkeypatching Account.exists monkeypatch.setattr(os.path, 'isfile', lambda x: True) # Monkeypatching AppConfig create monkeypatch.setattr(os, 'makedirs', lambda x: None) monkeypatch.setattr(__builtin__, 'open', lambda x, y: DummyFileHandler()) monkeypatch.setattr(AccountConfig, 'create', lambda: None) monkeypatch.setattr(AccountConfig, 'load_oauth_credentials', lambda x: ('123', '456')) monkeypatch.setattr('libturpial.api.models.account.Account', DummyAccount) Account.load('foo-twitter')
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_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 load(self, account_id): """ Load and existing account identified by *account_id*. If the load fails an :class:`libturpial.exceptions.ErrorLoadingAccount` exception will raise. Return the id of the account loaded on success """ # TODO: Set the timeout #timeout = int(self.config.read('Advanced', 'socket-timeout')) #self.protocol.timeout = timeout self.__accounts[account_id] = Account.load(account_id) return account_id
def register_account(username): account = build_account_id(username) if account in accounts: print 'Your account is already registered. Nothing to do' else: new_access = Account.new('twitter') url = new_access.request_oauth_access() print "Please go to the following URL, log-in and allow access for libturpial. Then write the PIN in here." print url cod = raw_input('PIN:') new_access.authorize_oauth_access(cod) core.register_account(new_access) print "Account {access_id} registered successfully".format(access_id=new_access.id_)
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 register(self, username, protocol_id, passwd, auth): if username == '' or protocol_id == '': return None account_id = "%s-%s" % (username, protocol_id) if self.__accounts.has_key(account_id): self.log.debug('Account %s is already registered' % account_id) self.__accounts[account_id].update(passwd) else: account = Account(username, account_id, protocol_id, passwd, auth) timeout = int(self.config.read('Advanced', 'socket-timeout')) account.protocol.timeout = timeout self.log.debug('Using %i sec for socket timeout in account %s' % (account.protocol.timeout, account_id)) self.__accounts[account_id] = account self.log.debug('Account %s registered successfully' % account_id) return account_id
def load(self, account_id): cfg = AccountConfig(account_id) auth = cfg.read_section('OAuth') username = cfg.read('Login', 'username') protocol = cfg.read('Login', 'protocol') p = cfg.revert(cfg.read('Login', 'password'), username) if self.__accounts.has_key(account_id): self.log.debug('Account %s is already registered' % account_id) else: account = Account(username, account_id, protocol, p, auth, cfg) timeout = int(self.config.read('Advanced', 'socket-timeout')) account.protocol.timeout = timeout self.log.debug('Using %i sec for socket timeout in account %s' % (account.protocol.timeout, account_id)) self.__accounts[account_id] = account self.log.debug('Account %s loaded successfully' % account_id) return account_id
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 setup_class(self, monkeypatch): config = AppConfig() self.account = Account.new("twitter", "foo") self.accman = AccountManager(config, load=False)
def test_new(self): account = Account.new('twitter', 'foo') assert isinstance(account, Account)
def test_accounts(self, monkeypatch): account2 = Account.new("twitter", "bar") accounts = {self.account.id_: self.account, 'bar-twitter': account2} monkeypatch.setattr(self.accman, '_AccountManager__accounts', accounts) assert account2 in self.accman.accounts()
class TestAccount: @classmethod @pytest.fixture(autouse=True) def setup_class(self, monkeypatch): self.account = Account('twitter', 'foo') def test_init(self): account = Account('twitter') assert isinstance(account.protocol, MainTwitter) account = Account('identica') assert isinstance(account.protocol, MainIdentica) def test_repr(self): assert str(self.account) == "libturpial.api.models.Account foo-twitter" def test_new(self): account = Account.new('twitter', 'foo') assert isinstance(account, Account) def test_new_with_params(self, monkeypatch): monkeypatch.setattr('libturpial.api.models.account.Account', DummyAccount) account = Account.new_from_params('twitter', 'foo', '123', '456', '789') assert isinstance(account, DummyAccount) def test_load(self, monkeypatch): monkeypatch.setattr('libturpial.config.AccountConfig', DummyConfig) with pytest.raises(ErrorLoadingAccount): Account.load('foo-twitter') # Monkeypatching Account.exists monkeypatch.setattr(os.path, 'isfile', lambda x: True) # Monkeypatching AppConfig create monkeypatch.setattr(os, 'makedirs', lambda x: None) monkeypatch.setattr(__builtin__, 'open', lambda x, y: DummyFileHandler()) monkeypatch.setattr(AccountConfig, 'create', lambda: None) monkeypatch.setattr(AccountConfig, 'load_oauth_credentials', lambda x: ('123', '456')) monkeypatch.setattr('libturpial.api.models.account.Account', DummyAccount) Account.load('foo-twitter') def test_request_oauth_access(self, monkeypatch): monkeypatch.setattr(self.account, 'protocol', DummyProtocol()) assert self.account.request_oauth_access() == 'token' def test_authorize_oauth_access(self, monkeypatch): protocol = DummyProtocol() protocol.profile.username = '******' monkeypatch.setattr(self.account, 'protocol', protocol) self.account.authorize_oauth_access('123') assert self.account.profile.username == 'foo' def test_save(self, monkeypatch): monkeypatch.setattr(self.account, 'is_authenticated', lambda: False) with pytest.raises(AccountNotAuthenticated): self.account.save() # Monkeypatching AppConfig create monkeypatch.setattr(os, 'makedirs', lambda x: None) monkeypatch.setattr(AccountConfig, 'create', lambda x: None) monkeypatch.setattr(AccountConfig, 'load', lambda x: None) monkeypatch.setattr(AccountConfig, 'load_failsafe', lambda x: None) monkeypatch.setattr(AccountConfig, 'exists', lambda x, y: True) monkeypatch.setattr(AccountConfig, 'save_oauth_credentials', lambda x, y, z: None) monkeypatch.setattr(self.account, 'is_authenticated', lambda: True) monkeypatch.setattr(self.account, 'get_oauth_token', lambda: DummyToken()) # TODO: How to test that this works? assert self.account.save() == None def test_fetch(self, monkeypatch): monkeypatch.setattr(self.account, 'protocol', DummyProtocol()) self.account.fetch() assert len(self.account.columns) == 5 for col in self.account.columns: assert isinstance(col, Column) def test_fetch_friends(self, monkeypatch): protocol = DummyProtocol() protocol.profile.username = '******' protocol.friends = ['bar', 'baz'] monkeypatch.setattr(self.account, 'protocol', protocol) friends = self.account.fetch_friends() assert friends == ['bar', 'baz'] def test_get_column(self, monkeypatch): monkeypatch.setattr(self.account, 'columns', []) assert self.account.get_columns() == [] def test_get_list_id(self, monkeypatch): monkeypatch.setattr(self.account, 'lists', None) assert self.account.get_list_id('bla') == None l1 = List() l1.id_ = '123' l1.slug = 'foolist' lists = [l1] monkeypatch.setattr(self.account, 'lists', lists) assert self.account.get_list_id('foolist') == '123' assert self.account.get_list_id('ble') == None def test_purge_config(self, monkeypatch): monkeypatch.setattr(self.account, 'config', DummyConfig('bla')) # TODO: How to test that this works? assert self.account.purge_config() == None def test_delete_cache(self, monkeypatch): monkeypatch.setattr(self.account, 'config', DummyConfig('bla')) # TODO: How to test that this works? assert self.account.delete_cache() == None def test_get_cache_size(self, monkeypatch): config = DummyConfig('bla') config.cache_size = 1000 monkeypatch.setattr(self.account, 'config', config) assert self.account.get_cache_size() == 1000 def test_is_authenticated(self, monkeypatch): monkeypatch.setattr(self.account, 'profile', None) monkeypatch.setattr(self.account, 'id_', None) assert self.account.is_authenticated() == False monkeypatch.setattr(self.account, 'id_', '123') assert self.account.is_authenticated() == False profile = Profile() monkeypatch.setattr(self.account, 'profile', profile) monkeypatch.setattr(self.account, 'id_', None) assert self.account.is_authenticated() == False monkeypatch.setattr(self.account, 'id_', '123') assert self.account.is_authenticated() == True def test_update_profile(self, monkeypatch): monkeypatch.setattr(self.account, 'protocol', DummyProtocol()) profile = self.account.update_profile(fullname='foo') assert profile.fullname == 'foo' assert profile.url == None assert profile.bio == None assert profile.location == None profile = self.account.update_profile(fullname='bar', url='http://example.com') assert profile.fullname == 'bar' assert profile.url == 'http://example.com' assert profile.bio == None assert profile.location == None profile = self.account.update_profile(fullname='baz', url='http://hello.com', bio='Lorem Ipsum') assert profile.fullname == 'baz' assert profile.url == 'http://hello.com' assert profile.bio == 'Lorem Ipsum' assert profile.location == None profile = self.account.update_profile(fullname='qux', url='http://bye.com', bio='Lorem', location='here') assert profile.fullname == 'qux' assert profile.url == 'http://bye.com' assert profile.bio == 'Lorem' assert profile.location == 'here' def test_get_attr(self, monkeypatch): monkeypatch.setattr(self.account, 'profile', DummyProfile()) monkeypatch.setattr(self.account, 'protocol', DummyProtocol()) assert self.account.is_me() == True assert self.account.request_token() == 'token' with pytest.raises(AttributeError): self.account.foobar()
def test_list(self, monkeypatch): account2 = Account.new("twitter", "bar") accounts = {self.account.id_: self.account, 'bar-twitter': account2} monkeypatch.setattr(self.accman, '_AccountManager__accounts', accounts) assert self.accman.list() == ['bar-twitter', 'foo-twitter']
choices=core.list_protocols(), help="Protocol", required=True) parser.add_argument("-m", "--message", dest="message", help="Message", required=True) args = parser.parse_args() username = args.username protocol = args.protocol account_pattern = "-".join([username, protocol]) message = args.message print "Trying to send message '%s'\nas user: %s, protocol: %s" % (message, username, protocol) if account_pattern in core.list_accounts(): core.update_status(account_pattern, message) else: # If account is not already registered in libturpial, # access must be granted: account = Account.new(protocol) url = account.request_oauth_access() instructions = "Please go to the following URL, log-in and allow access" \ " for libturpial. Then write the PIN in here." print "%s\n%s" % (instructions, url) cod = raw_input("PIN: ") account.authorize_oauth_access(cod) account_id = core.register_account(account) core.update_status(account_id, message)
def setup_class(self, monkeypatch): self.account = Account('twitter', 'foo')
def test_init(self): account = Account('twitter') assert isinstance(account.protocol, MainTwitter) account = Account('identica') assert isinstance(account.protocol, MainIdentica)
#!/usr/bin/python # -*- coding: utf-8 -*- # Libturpial usage example # # Author: Carlos Guerrero <*****@*****.**> # 24 June 2013 from libturpial.api.models.account import Account from libturpial.api.core import Core account = "username-twitter" #Replace <username> with the user you want to send tweet with message = "Tweet sent using Libturpial" c = Core() accounts = c.list_accounts() if account in accounts: c.update_status(account,message) else: #If account is not already registered in libturpial, acces must be granted: a = Account.new('twitter') #you can also create identi.ca accounts url = a.request_oauth_access() print "Please go to the following URL, log-in and allow access for Libturpial. Then write the PIN in here." print url cod = raw_input("PIN:") a.authorize_oauth_access(cod) c.register_account(a)