File: muc_test.py

package info (click to toggle)
errbot 6.2.0%2Bds-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,856 kB
  • sloc: python: 11,572; makefile: 163; sh: 97
file content (154 lines) | stat: -rw-r--r-- 4,368 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import logging
import os

import errbot.backends.base
from errbot.backends.test import TestOccupant

log = logging.getLogger(__name__)

extra_plugin_dir = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), "room_plugin"
)


def test_plugin_methods(testbot):
    p = testbot.bot.plugin_manager.get_plugin_obj_by_name("ChatRoom")
    assert p is not None

    assert hasattr(p, "rooms")
    assert hasattr(p, "query_room")


def test_create_join_leave_destroy_lifecycle(testbot):  # noqa
    rooms = testbot.bot.rooms()
    assert len(rooms) == 1

    r1 = rooms[0]
    assert str(r1) == "testroom"
    assert issubclass(r1.__class__, errbot.backends.base.Room)

    r2 = testbot.bot.query_room("testroom2")
    assert not r2.exists

    r2.create()
    assert r2.exists
    rooms = testbot.bot.rooms()
    assert r2 not in rooms
    assert not r2.joined

    r2.destroy()
    rooms = testbot.bot.rooms()
    assert r2 not in rooms

    r2.join()
    assert r2.exists
    assert r2.joined
    rooms = testbot.bot.rooms()
    assert r2 in rooms

    r2 = testbot.bot.query_room("testroom2")
    assert r2.joined

    r2.leave()
    assert not r2.joined
    r2.destroy()
    rooms = testbot.bot.rooms()
    assert r2 not in rooms


def test_occupants(testbot):  # noqa
    room = testbot.bot.rooms()[0]
    assert len(room.occupants) == 1
    assert TestOccupant("err", "testroom") in room.occupants


def test_topic(testbot):  # noqa
    room = testbot.bot.rooms()[0]
    assert room.topic is None

    room.topic = "Errbot rocks!"
    assert room.topic == "Errbot rocks!"
    assert testbot.bot.rooms()[0].topic == "Errbot rocks!"


def test_plugin_callbacks(testbot):  # noqa
    p = testbot.bot.plugin_manager.get_plugin_obj_by_name("RoomTest")
    assert p is not None
    p.purge()

    log.debug("query and join")
    p.query_room("newroom").join()
    assert p.events.get(timeout=5) == "callback_room_joined newroom"

    p.query_room("newroom").topic = "Errbot rocks!"
    assert p.events.get(timeout=5) == "callback_room_topic Errbot rocks!"

    p.query_room("newroom").leave()
    assert p.events.get(timeout=5) == "callback_room_left newroom"


def test_botcommands(testbot):  # noqa
    rooms = testbot.bot.rooms()
    room = testbot.bot.query_room("testroom")
    assert len(rooms) == 1
    assert rooms[0] == room

    assert room.joined
    assert "Left the room testroom" in testbot.exec_command("!room leave testroom")
    room = testbot.bot.query_room("testroom")
    assert not room.joined

    assert "I'm not currently in any rooms." in testbot.exec_command("!room list")

    assert "Destroyed the room testroom" in testbot.exec_command(
        "!room destroy testroom"
    )

    rooms = testbot.bot.rooms()
    room = testbot.bot.query_room("testroom")
    assert not room.exists
    assert room not in rooms

    assert "Created the room testroom" in testbot.exec_command("!room create testroom")
    rooms = testbot.bot.rooms()
    room = testbot.bot.query_room("testroom")
    assert room.exists
    assert room not in rooms
    assert not room.joined

    assert "Joined the room testroom" in testbot.exec_command("!room join testroom")
    rooms = testbot.bot.rooms()
    room = testbot.bot.query_room("testroom")
    assert room.exists
    assert room.joined
    assert room in rooms

    assert "Created the room testroom with spaces" in testbot.exec_command(
        "!room create 'testroom with spaces'"
    )
    rooms = testbot.bot.rooms()
    room = testbot.bot.query_room("testroom with spaces")
    assert room.exists
    assert room not in rooms
    assert not room.joined

    assert "Joined the room testroom with spaces" in testbot.exec_command(
        "!room join 'testroom with spaces'"
    )
    rooms = testbot.bot.rooms()
    room = testbot.bot.query_room("testroom with spaces")
    assert room.exists
    assert room.joined
    assert room in rooms

    assert "testroom" in testbot.exec_command("!room list")
    assert "err" in testbot.exec_command("!room occupants testroom")
    assert "No topic is set for testroom" in testbot.exec_command(
        "!room topic testroom"
    )
    assert "Topic for testroom set." in testbot.exec_command(
        "!room topic testroom 'Errbot rocks!'"
    )
    assert "Topic for testroom: Errbot rocks!" in testbot.exec_command(
        "!room topic testroom"
    )