def test_replace_user_data(): from plugins.core.chan_track import UsersDict, replace_user_data, Channel from plugins.core.server_info import handle_prefixes conn = MockConn() serv_info = conn.memory['server_info'] handle_prefixes('(YohvV)!@%+-', serv_info) users = UsersDict(conn) conn.memory['users'] = users chan = Channel('#test', conn) chan.data['new_users'] = [ '@+foo!bar@baz', '@ExampleUser!bar@baz', 'ExampleUser2!bar@baz', '!@%+-foo1!bar@baz', ] replace_user_data(conn, chan) assert chan.users['foo'].user.mask == Prefix('foo', 'bar', 'baz') assert chan.users['foo1'].user.mask == Prefix('foo1', 'bar', 'baz') assert chan.users['exampleuser'].user.mask == Prefix( 'ExampleUser', 'bar', 'baz' ) assert chan.users['exampleuser2'].user.mask == Prefix( 'ExampleUser2', 'bar', 'baz' ) assert chan.users['foo'].status == conn.get_statuses('@+') assert chan.users['exampleuser'].status == conn.get_statuses('@') assert chan.users['Foo1'].status == conn.get_statuses('!@%+-') assert not chan.users['exampleuser2'].status
def test_replace_user_data(): conn = MockConn() serv_info = conn.memory["server_info"] server_info.handle_prefixes("(YohvV)!@%+-", serv_info) users = chan_track.UsersDict(conn) conn.memory["users"] = users chan = chan_track.Channel("#test", conn) chan.data["new_users"] = [ "@+foo!bar@baz", "@ExampleUser!bar@baz", "ExampleUser2!bar@baz", "!@%+-foo1!bar@baz", ] chan_track.replace_user_data(conn, chan) assert chan.users["foo"].user.mask == Prefix("foo", "bar", "baz") assert chan.users["foo1"].user.mask == Prefix("foo1", "bar", "baz") assert chan.users["exampleuser"].user.mask == Prefix( "ExampleUser", "bar", "baz" ) assert chan.users["exampleuser2"].user.mask == Prefix( "ExampleUser2", "bar", "baz" ) assert chan.users["foo"].status == conn.get_statuses("@+") assert chan.users["exampleuser"].status == conn.get_statuses("@") assert chan.users["Foo1"].status == conn.get_statuses("!@%+-") assert not chan.users["exampleuser2"].status
def parse_names_item(item, statuses, has_multi_prefix, has_userhost): """ Parse an entry from /NAMES :param item: The entry to parse :param statuses: Status prefixes on this network :param has_multi_prefix: Whether multi-prefix CAP is enabled :param has_userhost: Whether userhost-in-names CAP is enabled :return: The parsed data """ user_status = [] while item[:1] in statuses: status, item = item[:1], item[1:] user_status.append(statuses[status]) if not has_multi_prefix: # Only remove one status prefix # if we don't have multi prefix enabled break user_status.sort(key=attrgetter("level"), reverse=True) if has_userhost: prefix = Prefix.parse(item) else: prefix = Prefix(item) return prefix.nick, prefix.user, prefix.host, user_status
def __init__(self, name, conn): """ :type name: str :type conn: cloudbot.client.Client """ self.mask = Prefix(name) self.conn = weakref.proxy(conn) self.realname = None self._account = None self.server = None self.is_away = False self.away_message = None self.is_oper = False self.channels = KeyFoldWeakValueDict() super().__init__()
def test_eq(self, nick, user, host): """Test equals""" assert Prefix(nick, user, host) == Prefix(nick, user, host)
def test_tellcmd(mock_db): from cloudbot.util import database from plugins import tell database = importlib.reload(database) importlib.reload(tell) metadata = database.metadata assert 'tells' in metadata.tables assert 'tell_ignores' in metadata.tables assert 'tell_user_ignores' in metadata.tables db_engine = mock_db.engine metadata.create_all(db_engine) session = mock_db.session() tell.load_cache(session) tell.load_disabled(session) tell.load_ignores(session) mock_event = MagicMock() mock_event.is_nick_valid.return_value = True mock_conn = MagicMock() mock_conn.nick = "BotNick" mock_conn.name = "MockConn" sender = Prefix("TestUser", "user", "example.com") def _test(text, output): tell.tell_cmd( text, sender.nick, session, mock_conn, sender.mask, mock_event, ) mock_event.notice.assert_called_with(output) mock_event.reset_mock() tell.tell_cmd( "OtherUser", sender.nick, session, mock_conn, sender.mask, mock_event, ) mock_event.notice_doc.assert_called_once_with() mock_event.reset_mock() _test( "OtherUser some message", "Your message has been saved, and OtherUser will be notified once they are active." ) assert tell.count_unread(session, mock_conn.name, "OtherUser") == 1 for i in range(9): _test( "OtherUser some message", "Your message has been saved, and OtherUser will be notified once they are active." ) assert tell.count_unread(session, mock_conn.name, "OtherUser") == 2 + i assert tell.count_unread(session, mock_conn.name, "OtherUser") == 10 _test("OtherUser some message", "Sorry, OtherUser has too many messages queued already.") assert tell.count_unread(session, mock_conn.name, "OtherUser") == 10 mock_event.is_nick_valid.return_value = False _test("OtherUser some message", "Invalid nick 'OtherUser'.") assert tell.count_unread(session, mock_conn.name, "OtherUser") == 10 mock_event.is_nick_valid.return_value = True _test(sender.nick + " some message", "Have you looked in a mirror lately?") assert tell.count_unread(session, mock_conn.name, "OtherUser") == 10 with patch('plugins.tell.can_send_to_user') as mocked: mocked.return_value = False _test("OtherUser some message", "You may not send a tell to that user.") assert tell.count_unread(session, mock_conn.name, "OtherUser") == 10
def test_ne(self, nick, user, host): assert not (Prefix(nick, user, host) != Prefix(nick, user, host))
def test_bool_false(self, nick, user, host): """Test cases where bool(Prefix) == False""" assert not Prefix(nick, user, host)
class TestMessage: """Test parsing an entire IRC message""" def test_parse_bytes(self): """Test parsing bytes""" line = Message.parse(b"COMMAND some params :and stuff") assert line.command == "COMMAND" assert line.parameters == ["some", "params", "and stuff"] @pytest.mark.parametrize( "obj,text", [ (Message(None, None, None), ""), (Message(None, None, None, None), ""), (Message(None, None, None, []), ""), (Message(None, None, "COMMAND"), "COMMAND"), (Message(["a=b"], None, "COMMAND"), "@a=b COMMAND"), (Message([MessageTag("a", "b")], None, "COMMAND"), "@a=b COMMAND"), (Message({"a": "b"}, None, "COMMAND"), "@a=b COMMAND"), (Message({"a": "b"}, "nick", "COMMAND"), "@a=b :nick COMMAND"), (Message(None, ("nick",), "COMMAND"), ":nick COMMAND"), (Message(None, ("nick", "user"), "COMMAND"), ":nick!user COMMAND"), ( Message(None, ("nick", "user", "host"), "COMMAND"), ":nick!user@host COMMAND", ), ( Message({"a": "b"}, "nick", "COMMAND", "a", "b"), "@a=b :nick COMMAND a b", ), ], ) def test_str(self, obj, text): """Test string conversion""" assert str(obj) == text @pytest.mark.parametrize( "tags,prefix,command,params", [ (None, None, None, None), ("some tag", None, "COMMAND", ["param", ""]), ], ) def test_eq(self, tags, prefix, command, params): """Test equals""" assert Message(tags, prefix, command, params) == Message( tags, prefix, command, params ) @pytest.mark.parametrize( "tags,prefix,command,params", [ (None, None, None, None), ("some tag", None, "COMMAND", ["param", ""]), ], ) def test_ne(self, tags, prefix, command, params): """Test not-equals""" b = Message(tags, prefix, command, params) != Message( tags, prefix, command, params ) assert not b @pytest.mark.parametrize( "obj,other", [ (Message(None, None, None), 0), (Message(None, None, None), None), (Message(None, None, None), ()), ], ) def test_no_cmp(self, obj, other): """Test Message.__ne__""" assert obj != other assert other != obj assert not obj == other assert not other == obj @pytest.mark.parametrize( "obj", [ Message(None, None, "COMMAND"), ], ) def test_bool(self, obj): """Test the cases where bool(Message) should return True""" assert obj @pytest.mark.parametrize( "obj", [ Message(None, None, None), Message(None, None, ""), Message(None, "", ""), Message("", "", ""), Message([], [], "", []), Message({}, [], "", []), Message(TagList(), Prefix(), "", ParamList()), ], ) def test_bool_false(self, obj): """Test all the cases where bool(Message) should return False""" assert not obj
def test_ne_str(self, text, nick, user, host): """Test not-equals string comparisons""" b = Prefix(nick, user, host) != text assert not b b1 = text != Prefix(nick, user, host) assert not b1
def test_bool(self, nick, user, host): """Test cases where bool(Prefix) == True""" assert Prefix(nick, user, host)
def host(self, value): self.mask = Prefix(self.nick, self.ident, value)
def test_eq(self, nick, user, host): assert Prefix(nick, user, host) == Prefix(nick, user, host)
class TestPrefix: """Test parsing a prefix""" @pytest.mark.parametrize( "text,nick,user,host", [ ("", "", "", ""), (":", "", "", ""), ("somenick", "somenick", "", ""), ("somenick!user", "somenick", "user", ""), ("somenick@host", "somenick", "", "host"), ("somenick!user@host", "somenick", "user", "host"), (":somenick", "somenick", "", ""), (":somenick!user", "somenick", "user", ""), (":somenick@host", "somenick", "", "host"), (":somenick!user@host", "somenick", "user", "host"), (":nick!@", "nick", "", ""), (":nick!user@", "nick", "user", ""), (":nick!@host", "nick", "", "host"), (":!@", "", "", ""), (":!user@", "", "user", ""), (":!@host", "", "", "host"), (":!user@host", "", "user", "host"), ], ) def test_parse(self, text, nick, user, host): """Test parsing a string""" p = Prefix.parse(text) assert p.nick == nick assert p.user == user assert p.ident == user assert p.host == host @pytest.mark.parametrize( "nick,user,host", [ ("nick", None, None), ("nick", "user", "host"), ("nick", None, "host"), ("nick", "user", None), ], ) def test_eq(self, nick, user, host): """Test equals""" assert Prefix(nick, user, host) == Prefix(nick, user, host) @pytest.mark.parametrize( "nick,user,host", [ ("nick", None, None), ("nick", "user", "host"), ("nick", None, "host"), ("nick", "user", None), ], ) def test_ne(self, nick, user, host): """Test not-equals""" b = Prefix(nick, user, host) != Prefix(nick, user, host) assert not b @pytest.mark.parametrize( "text,nick,user,host", [ ("nick", "nick", None, None), ("nick!user@host", "nick", "user", "host"), ("nick@host", "nick", None, "host"), ("nick!user", "nick", "user", None), ], ) def test_eq_str(self, text, nick, user, host): """Test equals string""" assert Prefix(nick, user, host) == text assert text == Prefix(nick, user, host) @pytest.mark.parametrize( "text,nick,user,host", [ ("nick", "nick", None, None), ("nick!user@host", "nick", "user", "host"), ("nick@host", "nick", None, "host"), ("nick!user", "nick", "user", None), ], ) def test_ne_str(self, text, nick, user, host): """Test not-equals string comparisons""" b = Prefix(nick, user, host) != text assert not b b1 = text != Prefix(nick, user, host) assert not b1 @pytest.mark.parametrize( "obj,other", [ (Prefix(""), 0), (Prefix(""), None), (Prefix(""), ()), ], ) def test_no_cmp(self, obj, other): """Test not-equals""" assert obj != other assert other != obj assert not obj == other assert not other == obj @pytest.mark.parametrize( "obj,nick,user,host", [ (Prefix("nick"), "nick", "", ""), (Prefix("nick", "user", "host"), "nick", "user", "host"), (Prefix("nick", host="host"), "nick", "", "host"), (Prefix("nick", "user"), "nick", "user", ""), ], ) def test_unpack(self, obj, nick, user, host): """Test unpacking Prefix""" n, u, h = obj assert (n, u, h) == (nick, user, host) @pytest.mark.parametrize( "nick,user,host", [ ("nick", "", ""), ("nick", None, None), ("nick", "user", "host"), ("nick", "", "host"), ("nick", "user", ""), ("nick", None, "host"), ("nick", "user", None), ("", "user", ""), ("", "user", "host"), ("", "", "host"), (None, "user", None), (None, "user", "host"), (None, None, "host"), ], ) def test_bool(self, nick, user, host): """Test cases where bool(Prefix) == True""" assert Prefix(nick, user, host) @pytest.mark.parametrize( "nick,user,host", [ ("", "", ""), (None, None, None), ("", None, None), (None, "", None), (None, None, ""), ("", "", None), ("", None, ""), (None, "", ""), ], ) def test_bool_false(self, nick, user, host): """Test cases where bool(Prefix) == False""" assert not Prefix(nick, user, host)
def nick(self, value): self.mask = Prefix(value, self.ident, self.host)
def test_eq_str(self, text, nick, user, host): assert Prefix(nick, user, host) == text assert text == Prefix(nick, user, host)
class TestMessage: def test_parse_bytes(self): line = Message.parse(b"COMMAND some params :and stuff") assert line.command == 'COMMAND' assert line.parameters == ['some', 'params', 'and stuff'] @pytest.mark.parametrize('obj,text', [ (Message(None, None, None), ''), (Message(None, None, None, None), ''), (Message(None, None, None, []), ''), (Message(None, None, 'COMMAND'), 'COMMAND'), (Message(['a=b'], None, 'COMMAND'), '@a=b COMMAND'), (Message([MessageTag('a', 'b')], None, 'COMMAND'), '@a=b COMMAND'), (Message({'a': 'b'}, None, 'COMMAND'), '@a=b COMMAND'), (Message({'a': 'b'}, 'nick', 'COMMAND'), '@a=b :nick COMMAND'), (Message(None, ('nick', ), 'COMMAND'), ':nick COMMAND'), (Message(None, ('nick', 'user'), 'COMMAND'), ':nick!user COMMAND'), (Message(None, ('nick', 'user', 'host'), 'COMMAND'), ':nick!user@host COMMAND'), (Message({'a': 'b'}, 'nick', 'COMMAND', 'a', 'b'), '@a=b :nick COMMAND a b'), ]) def test_str(self, obj, text): assert str(obj) == text @pytest.mark.parametrize('tags,prefix,command,params', [ (None, None, None, None), ('some tag', None, 'COMMAND', ['param', '']), ]) def test_eq(self, tags, prefix, command, params): assert Message(tags, prefix, command, params) == Message(tags, prefix, command, params) @pytest.mark.parametrize('tags,prefix,command,params', [ (None, None, None, None), ('some tag', None, 'COMMAND', ['param', '']), ]) def test_ne(self, tags, prefix, command, params): assert not (Message(tags, prefix, command, params) != Message( tags, prefix, command, params)) @pytest.mark.parametrize('obj,other', [ (Message(None, None, None), 0), (Message(None, None, None), None), (Message(None, None, None), ()), ]) def test_no_cmp(self, obj, other): assert obj != other assert other != obj assert not (obj == other) assert not (other == obj) @pytest.mark.parametrize('obj', [ Message(None, None, 'COMMAND'), ]) def test_bool(self, obj): assert obj @pytest.mark.parametrize('obj', [ Message(None, None, None), Message(None, None, ''), Message(None, '', ''), Message('', '', ''), Message([], [], '', []), Message({}, [], '', []), Message(TagList(), Prefix(), '', ParamList()), ]) def test_bool_false(self, obj): assert not obj
def test_bool_false(self, nick, user, host): assert not Prefix(nick, user, host)
def test_bool(self, nick, user, host): assert Prefix(nick, user, host)
def test_ne_str(self, text, nick, user, host): assert not (Prefix(nick, user, host) != text) assert not (text != Prefix(nick, user, host))
def ident(self, value): self.mask = Prefix(self.nick, value, self.host)
def test_ne(self, nick, user, host): """Test not-equals""" b = Prefix(nick, user, host) != Prefix(nick, user, host) assert not b
def test_eq_str(self, text, nick, user, host): """Test equals string""" assert Prefix(nick, user, host) == text assert text == Prefix(nick, user, host)
class TestPrefix: @pytest.mark.parametrize('text,nick,user,host', [ ('', '', '', ''), (':', '', '', ''), ('somenick', 'somenick', '', ''), ('somenick!user', 'somenick', 'user', ''), ('somenick@host', 'somenick', '', 'host'), ('somenick!user@host', 'somenick', 'user', 'host'), (':somenick', 'somenick', '', ''), (':somenick!user', 'somenick', 'user', ''), (':somenick@host', 'somenick', '', 'host'), (':somenick!user@host', 'somenick', 'user', 'host'), (":nick!@", 'nick', '', ''), (":nick!user@", 'nick', 'user', ''), (":nick!@host", 'nick', '', 'host'), (":!@", '', '', ''), (":!user@", '', 'user', ''), (":!@host", '', '', 'host'), (":!user@host", '', 'user', 'host'), ]) def test_parse(self, text, nick, user, host): p = Prefix.parse(text) assert p.nick == nick assert p.user == user assert p.ident == user assert p.host == host @pytest.mark.parametrize('nick,user,host', [ ('nick', None, None), ('nick', 'user', 'host'), ('nick', None, 'host'), ('nick', 'user', None), ]) def test_eq(self, nick, user, host): assert Prefix(nick, user, host) == Prefix(nick, user, host) @pytest.mark.parametrize('nick,user,host', [ ('nick', None, None), ('nick', 'user', 'host'), ('nick', None, 'host'), ('nick', 'user', None), ]) def test_ne(self, nick, user, host): assert not (Prefix(nick, user, host) != Prefix(nick, user, host)) @pytest.mark.parametrize('text,nick,user,host', [ ('nick', 'nick', None, None), ('nick!user@host', 'nick', 'user', 'host'), ('nick@host', 'nick', None, 'host'), ('nick!user', 'nick', 'user', None), ]) def test_eq_str(self, text, nick, user, host): assert Prefix(nick, user, host) == text assert text == Prefix(nick, user, host) @pytest.mark.parametrize('text,nick,user,host', [ ('nick', 'nick', None, None), ('nick!user@host', 'nick', 'user', 'host'), ('nick@host', 'nick', None, 'host'), ('nick!user', 'nick', 'user', None), ]) def test_ne_str(self, text, nick, user, host): assert not (Prefix(nick, user, host) != text) assert not (text != Prefix(nick, user, host)) @pytest.mark.parametrize('obj,other', [ (Prefix(''), 0), (Prefix(''), None), (Prefix(''), ()), ]) def test_no_cmp(self, obj, other): assert obj != other assert other != obj assert not (obj == other) assert not (other == obj) @pytest.mark.parametrize('obj,nick,user,host', [ (Prefix('nick'), 'nick', '', ''), (Prefix('nick', 'user', 'host'), 'nick', 'user', 'host'), (Prefix('nick', host='host'), 'nick', '', 'host'), (Prefix('nick', 'user'), 'nick', 'user', ''), ]) def test_unpack(self, obj, nick, user, host): n, u, h = obj assert (n, u, h) == (nick, user, host) @pytest.mark.parametrize('nick,user,host', [ ('nick', '', ''), ('nick', None, None), ('nick', 'user', 'host'), ('nick', '', 'host'), ('nick', 'user', ''), ('nick', None, 'host'), ('nick', 'user', None), ('', 'user', ''), ('', 'user', 'host'), ('', '', 'host'), (None, 'user', None), (None, 'user', 'host'), (None, None, 'host'), ]) def test_bool(self, nick, user, host): assert Prefix(nick, user, host) @pytest.mark.parametrize('nick,user,host', [ ('', '', ''), (None, None, None), ('', None, None), (None, '', None), (None, None, ''), ('', '', None), ('', None, ''), (None, '', ''), ]) def test_bool_false(self, nick, user, host): assert not Prefix(nick, user, host)