def test_equality(self): status = Status() status.id_ = "456" assert status != self.status status.id_ = "123" assert status == self.status
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_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 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 __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 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_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_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_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_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_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_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 setup_class(self): self.status = Status() self.status.account_id = "dummy-twitter" self.status.username = "******" self.status.id_ = "123" self.status.original_status_id = "123" self.status.created_at = "" self.status.avatar = "my_avatar.png" self.status.text = "Tweet text" self.status.in_reply_to_id = "123" self.status.in_reply_to_user = "******" self.status.is_favorite = False self.status.is_protected = False self.status.is_verified = True self.status.repeated_by = "baz" self.status.datetime = "01-01-1900 00:00" self.status.timestamp = 123456789 self.status.entities = {} self.status.type_ = Status.NORMAL self.status.is_own = True self.status.repeated = False self.status.repeated_count = None self.status.local_timestamp = 123456789 self.status.source = None
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_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_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 json_to_status(self, response, column_id='', _type=StatusType.NORMAL): if isinstance(response, list): statuses = [] for resp in response: if not resp: continue status = self.json_to_status(resp, column_id, _type) if status.reposted_by: # TODO: Handle this #users = self.get_retweet_users(status.id_) #status.reposted_by = users #count = self.get_retweet_count(status.id_) #status.reposted_count = count pass statuses.append(status) return statuses else: reposted_by = None retweeted_id = None if response.has_key('retweeted_status'): reposted_by = response['user']['screen_name'] retweeted_id = response['id'] post = response['retweeted_status'] else: post = response protected = False verified = False if post.has_key('user'): username = post['user']['screen_name'] avatar = post['user']['profile_image_url'] protected = post['user']['protected'] verified = post['user']['verified'] elif post.has_key('sender'): username = post['sender']['screen_name'] avatar = post['sender']['profile_image_url'] protected = post['sender']['protected'] verified = post['sender']['verified'] elif post.has_key('from_user'): username = post['from_user'] avatar = post['profile_image_url'] in_reply_to_id = None in_reply_to_user = None if post.has_key('in_reply_to_status_id') and \ post['in_reply_to_status_id']: in_reply_to_id = post['in_reply_to_status_id'] in_reply_to_user = post['in_reply_to_screen_name'] fav = False if post.has_key('favorited'): fav = post['favorited'] retweeted = False if post.has_key('retweeted'): retweeted = post['retweeted'] source = None if post.has_key('source'): source = post['source'] status = Status() status.id_ = str(post['id']) status.retweeted_id = retweeted_id status.username = username status.avatar = avatar status.source = self.get_source(source) status.text = post['text'] status.in_reply_to_id = in_reply_to_id status.in_reply_to_user = in_reply_to_user status.is_favorite = fav status.is_protected = protected status.is_verified = verified status.reposted_by = reposted_by status.datetime = self.get_str_time(post['created_at']) status.timestamp = self.get_int_time(post['created_at']) status.entities = self.get_entities(post) status._type = _type status.account_id = self.account_id status.is_own = (username.lower() == self.uname.lower()) status.retweeted = retweeted status.set_display_id(column_id) return status
def json_to_status(self, response, column_id='', _type=StatusType.NORMAL): if isinstance(response, list): statuses = [] for resp in response: if not resp: continue status = self.json_to_status(resp, column_id, _type) statuses.append(status) return statuses else: reposted_by = None if response.has_key('retweeted_status'): reposted_by = response['user']['screen_name'] post = response['retweeted_status'] else: post = response protected = False if post.has_key('user'): username = post['user']['screen_name'] avatar = post['user']['profile_image_url'] protected = post['user']['protected'] elif post.has_key('sender'): username = post['sender']['screen_name'] avatar = post['sender']['profile_image_url'] protected = post['sender']['protected'] elif post.has_key('from_user'): username = post['from_user'] avatar = post['profile_image_url'] in_reply_to_id = None in_reply_to_user = None if post.has_key('in_reply_to_status_id') and \ post['in_reply_to_status_id']: in_reply_to_id = post['in_reply_to_status_id'] in_reply_to_user = post['in_reply_to_screen_name'] fav = False if post.has_key('favorited'): fav = post['favorited'] source = None if post.has_key('source'): source = post['source'] status = Status() status.id_ = str(post['id']) status.username = username status.avatar = avatar status.source = self.get_source(source) status.text = post['text'] status.in_reply_to_id = in_reply_to_id status.in_reply_to_user = in_reply_to_user status.is_favorite = fav status.is_protected = protected status.is_verified = False status.reposted_by = reposted_by status.datetime = self.get_str_time(post['created_at']) status.timestamp = self.get_int_time(post['created_at']) status.entities = self.get_entities(post) status._type = _type status.account_id = self.account_id status.is_own = (username.lower() == self.uname.lower()) status.set_display_id(column_id) return status
def json_to_status(self, response, column_id='', type_=Status.NORMAL): if isinstance(response, list): statuses = [] for resp in response: if not resp: continue status = self.json_to_status(resp, column_id, type_) statuses.append(status) return statuses else: repeated_by = None retweeted_id = None if 'retweeted_status' in response: repeated_by = response['user']['screen_name'] retweeted_id = response['id'] post = response['retweeted_status'] else: post = response protected = False verified = False if 'user' in post: username = post['user']['screen_name'] avatar = post['user']['profile_image_url'] protected = post['user']['protected'] verified = post['user']['verified'] elif 'sender' in post: username = post['sender']['screen_name'] avatar = post['sender']['profile_image_url'] protected = post['sender']['protected'] verified = post['sender']['verified'] elif 'from_user' in post: username = post['from_user'] avatar = post['profile_image_url'] in_reply_to_id = None in_reply_to_user = None if 'in_reply_to_status_id' in post and \ post['in_reply_to_status_id']: in_reply_to_id = post['in_reply_to_status_id'] if 'in_reply_to_screen_name' in post: in_reply_to_user = post['in_reply_to_screen_name'] fav = False if 'favorited' in post: fav = post['favorited'] repeated = False if 'retweeted' in post: repeated = post['retweeted'] source = None if 'source' in post: source = post['source'] retweet_count = None if 'retweet_count' in post: retweet_count = int(post['retweet_count']) status = Status() status.id_ = str(post['id']) status.original_status_id = retweeted_id status.created_at = post['created_at'] status.username = username status.avatar = avatar status.text = post['text'] status.in_reply_to_id = in_reply_to_id status.in_reply_to_user = in_reply_to_user status.is_favorite = fav status.is_protected = protected status.is_verified = verified status.repeated_by = repeated_by status.datetime = self.get_str_time(post['created_at']) status.timestamp = self.get_int_time(post['created_at']) status.entities = self.get_entities(post) status.type_ = type_ status.account_id = self.account_id status.is_own = (username.lower() == self.uname.lower()) status.repeated = repeated status.repeated_count = retweet_count status.local_timestamp = timestamp_to_localtime(status.timestamp) status.get_source(source) return status
class TestStatus: @classmethod def setup_class(self): self.status = Status() self.status.account_id = "dummy-twitter" self.status.username = "******" self.status.id_ = "123" self.status.original_status_id = "123" self.status.created_at = "" self.status.avatar = "my_avatar.png" self.status.text = "Tweet text" self.status.in_reply_to_id = "123" self.status.in_reply_to_user = "******" self.status.is_favorite = False self.status.is_protected = False self.status.is_verified = True self.status.repeated_by = "baz" self.status.datetime = "01-01-1900 00:00" self.status.timestamp = 123456789 self.status.entities = {} self.status.type_ = Status.NORMAL self.status.is_own = True self.status.repeated = False self.status.repeated_count = None self.status.local_timestamp = 123456789 self.status.source = None def test_structure(self): assert self.status.account_id == "dummy-twitter" assert self.status.username == "dummy" assert self.status.id_ == "123" assert self.status.original_status_id == "123" assert self.status.created_at == "" assert self.status.avatar == "my_avatar.png" assert self.status.text == "Tweet text" assert self.status.in_reply_to_id == "123" assert self.status.in_reply_to_user == "bar" assert self.status.is_favorite == False assert self.status.is_protected == False assert self.status.is_verified == True assert self.status.repeated_by == "baz" assert self.status.datetime == "01-01-1900 00:00" assert self.status.timestamp == 123456789 assert self.status.entities == {} assert self.status.type_ == Status.NORMAL assert self.status.is_own == True assert self.status.repeated == False assert self.status.repeated_count == None assert self.status.local_timestamp == 123456789 assert self.status.source == None def test_is_direct(self): assert self.status.is_direct() == False self.status.type_ = Status.DIRECT assert self.status.is_direct() == True def test_get_mentions(self): entities = { 'mentions': [ Entity(self.status.account_id, "@dummy", "@dummy", "@dummy"), Entity(self.status.account_id, "@foo", "@foo", "@foo"), Entity(self.status.account_id, "@bar", "@bar", "@bar"), Entity(self.status.account_id, "@baz", "@baz", "@baz"), ], } self.status.entities = entities mentions = self.status.get_mentions() assert isinstance(mentions, list) assert len(mentions) == 4 assert mentions[0] == "dummy" assert mentions[1] == "foo" assert mentions[2] == "bar" assert mentions[3] == "baz" self.status.entities = {} mentions = self.status.get_mentions() assert isinstance(mentions, list) assert len(mentions) == 1 assert mentions[0] == "dummy" def test_get_protocol_id(self): response = self.status.get_protocol_id() assert response == "twitter" def test_get_source(self): self.status.get_source(None) assert self.status.source == None self.status.get_source('web') assert isinstance(self.status.source, Client) assert self.status.source.name == "web" assert self.status.source.url == "http://twitter.com" self.status.get_source('<a href="http://fooclient.com">Foo client</a>') assert isinstance(self.status.source, Client) assert self.status.source.name == "Foo client" assert self.status.source.url == "http://fooclient.com" self.status.get_source('Bar client') assert isinstance(self.status.source, Client) assert self.status.source.name == "Bar client" assert self.status.source.url is None def test_equality(self): status = Status() status.id_ = "456" assert status != self.status status.id_ = "123" assert status == self.status
def json_to_status(self, response, column_id="", _type=StatusType.NORMAL): if isinstance(response, list): statuses = [] for resp in response: if not resp: continue status = self.json_to_status(resp, column_id, _type) statuses.append(status) return statuses else: reposted_by = None if "retweeted_status" in response: reposted_by = response["user"]["screen_name"] post = response["retweeted_status"] else: post = response protected = False if "user" in post: username = post["user"]["screen_name"] avatar = post["user"]["profile_image_url"] protected = post["user"]["protected"] elif "sender" in post: username = post["sender"]["screen_name"] avatar = post["sender"]["profile_image_url"] protected = post["sender"]["protected"] elif "from_user" in post: username = post["from_user"] avatar = post["profile_image_url"] in_reply_to_id = None in_reply_to_user = None if "in_reply_to_status_id" in post and post["in_reply_to_status_id"]: in_reply_to_id = post["in_reply_to_status_id"] in_reply_to_user = post["in_reply_to_screen_name"] fav = False if "favorited" in post: fav = post["favorited"] source = None if "source" in post: source = post["source"] status = Status() status.id_ = str(post["id"]) status.username = username status.avatar = avatar status.source = self.get_source(source) status.text = post["text"] status.in_reply_to_id = in_reply_to_id status.in_reply_to_user = in_reply_to_user status.is_favorite = fav status.is_protected = protected status.is_verified = False status.reposted_by = reposted_by status.datetime = self.get_str_time(post["created_at"]) status.timestamp = self.get_int_time(post["created_at"]) status.entities = self.get_entities(post) status._type = _type status.account_id = self.account_id status.is_own = username.lower() == self.uname.lower() status.set_display_id(column_id) return status