| 1 |
import logging |
| 2 |
import os |
| 3 |
|
| 4 |
import errbot.backends.base |
| 5 |
from errbot.backends.test import TestOccupant |
| 6 |
|
| 7 |
log = logging.getLogger(__name__) |
| 8 |
|
| 9 |
extra_plugin_dir = os.path.join( |
| 10 |
os.path.dirname(os.path.realpath(__file__)), "room_plugin" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
def test_plugin_methods(testbot): |
| 15 |
p = testbot.bot.plugin_manager.get_plugin_obj_by_name("ChatRoom") |
| 16 |
assert p is not None |
| 17 |
|
| 18 |
assert hasattr(p, "rooms") |
| 19 |
assert hasattr(p, "query_room") |
| 20 |
|
| 21 |
|
| 22 |
def test_create_join_leave_destroy_lifecycle(testbot): # noqa |
| 23 |
rooms = testbot.bot.rooms() |
| 24 |
assert len(rooms) == 1 |
| 25 |
|
| 26 |
r1 = rooms[0] |
| 27 |
assert str(r1) == "testroom" |
| 28 |
assert issubclass(r1.__class__, errbot.backends.base.Room) |
| 29 |
|
| 30 |
r2 = testbot.bot.query_room("testroom2") |
| 31 |
assert not r2.exists |
| 32 |
|
| 33 |
r2.create() |
| 34 |
assert r2.exists |
| 35 |
rooms = testbot.bot.rooms() |
| 36 |
assert r2 not in rooms |
| 37 |
assert not r2.joined |
| 38 |
|
| 39 |
r2.destroy() |
| 40 |
rooms = testbot.bot.rooms() |
| 41 |
assert r2 not in rooms |
| 42 |
|
| 43 |
r2.join() |
| 44 |
assert r2.exists |
| 45 |
assert r2.joined |
| 46 |
rooms = testbot.bot.rooms() |
| 47 |
assert r2 in rooms |
| 48 |
|
| 49 |
r2 = testbot.bot.query_room("testroom2") |
| 50 |
assert r2.joined |
| 51 |
|
| 52 |
r2.leave() |
| 53 |
assert not r2.joined |
| 54 |
r2.destroy() |
| 55 |
rooms = testbot.bot.rooms() |
| 56 |
assert r2 not in rooms |
| 57 |
|
| 58 |
|
| 59 |
def test_occupants(testbot): # noqa |
| 60 |
room = testbot.bot.rooms()[0] |
| 61 |
assert len(room.occupants) == 1 |
| 62 |
assert TestOccupant("err", "testroom") in room.occupants |
| 63 |
|
| 64 |
|
| 65 |
def test_topic(testbot): # noqa |
| 66 |
room = testbot.bot.rooms()[0] |
| 67 |
assert room.topic is None |
| 68 |
|
| 69 |
room.topic = "Errbot rocks!" |
| 70 |
assert room.topic == "Errbot rocks!" |
| 71 |
assert testbot.bot.rooms()[0].topic == "Errbot rocks!" |
| 72 |
|
| 73 |
|
| 74 |
def test_plugin_callbacks(testbot): # noqa |
| 75 |
p = testbot.bot.plugin_manager.get_plugin_obj_by_name("RoomTest") |
| 76 |
assert p is not None |
| 77 |
p.purge() |
| 78 |
|
| 79 |
log.debug("query and join") |
| 80 |
p.query_room("newroom").join() |
| 81 |
assert p.events.get(timeout=5) == "callback_room_joined newroom" |
| 82 |
|
| 83 |
p.query_room("newroom").topic = "Errbot rocks!" |
| 84 |
assert p.events.get(timeout=5) == "callback_room_topic Errbot rocks!" |
| 85 |
|
| 86 |
p.query_room("newroom").leave() |
| 87 |
assert p.events.get(timeout=5) == "callback_room_left newroom" |
| 88 |
|
| 89 |
|
| 90 |
def test_botcommands(testbot): # noqa |
| 91 |
rooms = testbot.bot.rooms() |
| 92 |
room = testbot.bot.query_room("testroom") |
| 93 |
assert len(rooms) == 1 |
| 94 |
assert rooms[0] == room |
| 95 |
|
| 96 |
assert room.joined |
| 97 |
assert "Left the room testroom" in testbot.exec_command("!room leave testroom") |
| 98 |
room = testbot.bot.query_room("testroom") |
| 99 |
assert not room.joined |
| 100 |
|
| 101 |
assert "I'm not currently in any rooms." in testbot.exec_command("!room list") |
| 102 |
|
| 103 |
assert "Destroyed the room testroom" in testbot.exec_command( |
| 104 |
"!room destroy testroom" |
| 105 |
) |
| 106 |
|
| 107 |
rooms = testbot.bot.rooms() |
| 108 |
room = testbot.bot.query_room("testroom") |
| 109 |
assert not room.exists |
| 110 |
assert room not in rooms |
| 111 |
|
| 112 |
assert "Created the room testroom" in testbot.exec_command("!room create testroom") |
| 113 |
rooms = testbot.bot.rooms() |
| 114 |
room = testbot.bot.query_room("testroom") |
| 115 |
assert room.exists |
| 116 |
assert room not in rooms |
| 117 |
assert not room.joined |
| 118 |
|
| 119 |
assert "Joined the room testroom" in testbot.exec_command("!room join testroom") |
| 120 |
rooms = testbot.bot.rooms() |
| 121 |
room = testbot.bot.query_room("testroom") |
| 122 |
assert room.exists |
| 123 |
assert room.joined |
| 124 |
assert room in rooms |
| 125 |
|
| 126 |
assert "Created the room testroom with spaces" in testbot.exec_command( |
| 127 |
"!room create 'testroom with spaces'" |
| 128 |
) |
| 129 |
rooms = testbot.bot.rooms() |
| 130 |
room = testbot.bot.query_room("testroom with spaces") |
| 131 |
assert room.exists |
| 132 |
assert room not in rooms |
| 133 |
assert not room.joined |
| 134 |
|
| 135 |
assert "Joined the room testroom with spaces" in testbot.exec_command( |
| 136 |
"!room join 'testroom with spaces'" |
| 137 |
) |
| 138 |
rooms = testbot.bot.rooms() |
| 139 |
room = testbot.bot.query_room("testroom with spaces") |
| 140 |
assert room.exists |
| 141 |
assert room.joined |
| 142 |
assert room in rooms |
| 143 |
|
| 144 |
assert "testroom" in testbot.exec_command("!room list") |
| 145 |
assert "err" in testbot.exec_command("!room occupants testroom") |
| 146 |
assert "No topic is set for testroom" in testbot.exec_command( |
| 147 |
"!room topic testroom" |
| 148 |
) |
| 149 |
assert "Topic for testroom set." in testbot.exec_command( |
| 150 |
"!room topic testroom 'Errbot rocks!'" |
| 151 |
) |
| 152 |
assert "Topic for testroom: Errbot rocks!" in testbot.exec_command( |
| 153 |
"!room topic testroom" |
| 154 |
) |