This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
/
channels_test.rb
267 lines (235 loc) · 10.2 KB
/
channels_test.rb
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
require "test_helper"
module Hector
class ChannelsTest < IntegrationTest
test :"channels can be joined" do
authenticated_connections do |c1, c2|
c1.receive_line "JOIN #test"
assert_sent_to c1, ":user1!sam@hector JOIN :#test"
c2.receive_line "JOIN #test"
assert_sent_to c1, ":user2!sam@hector JOIN :#test"
assert_sent_to c2, ":user2!sam@hector JOIN :#test"
end
end
test :"joining a channel twice does nothing" do
authenticated_connection.tap do |c|
c.receive_line "JOIN #test"
assert_nothing_sent_to(c) do
c.receive_line "JOIN #test"
end
end
end
test :"joining an invalid channel name responds with a 403" do
authenticated_connection.tap do |c|
c.receive_line "JOIN #8*(&x"
assert_no_such_channel c, "#8*(&x"
end
end
test :"joining a channel should send session nicknames" do
authenticated_connections(:join => "#test") do |c1, c2, c3|
assert_sent_to c1, ":hector.irc 353 user1 = #test :user1"
assert_sent_to c2, ":hector.irc 353 user2 = #test :user1 user2"
assert_sent_to c3, ":hector.irc 353 user3 = #test :user1 user2 user3"
end
end
test :"channels can be parted" do
authenticated_connections(:join => "#test") do |c1, c2|
c1.receive_line "PART #test :lämnar"
assert_sent_to c1, ":user1!sam@hector PART #test :lämnar"
assert_sent_to c2, ":user1!sam@hector PART #test :lämnar"
end
end
test :"parting a channel should remove the session from the channel" do
authenticated_connections(:join => "#test") do |c1, c2|
c1.receive_line "PART #test"
sent_data = capture_sent_data(c2) { c2.receive_line "NAMES #test" }
assert sent_data !~ /user1/
end
end
test :"quitting should notify all the session's peers" do
authenticated_connections(:join => "#test") do |c1, c2, c3|
c1.receive_line "QUIT :outta here"
assert_not_sent_to c1, ":user1!sam@hector QUIT :Quit: outta here"
assert_sent_to c2, ":user1!sam@hector QUIT :Quit: outta here"
assert_sent_to c3, ":user1!sam@hector QUIT :Quit: outta here"
end
end
test :"quitting should notify peers only once" do
authenticated_connections(:join => ["#test1", "#test2"]) do |c1, c2|
sent_data = capture_sent_data(c2) { c1.receive_line "QUIT :outta here" }
assert_equal ":user1!sam@hector QUIT :Quit: outta here\r\n", sent_data
end
end
test :"quitting should remove the session from its channels" do
authenticated_connections(:join => ["#test1", "#test2"]) do |c1, c2|
c1.receive_line "QUIT :bye"
sent_data = capture_sent_data(c2) do
c2.receive_line "NAMES #test1"
c2.receive_line "NAMES #test2"
end
assert sent_data !~ /user1/
end
end
test :"disconnecting without quitting should notify peers" do
authenticated_connections(:join => "#test") do |c1, c2|
c1.close_connection
assert_sent_to c2, ":user1!sam@hector QUIT :Connection closed"
end
end
test :"disconnecting should remove the session from its channels" do
authenticated_connections(:join => ["#test1", "#test2"]) do |c1, c2|
c1.close_connection
sent_data = capture_sent_data(c2) do
c2.receive_line "NAMES #test1"
c2.receive_line "NAMES #test2"
end
assert sent_data !~ /user1/
end
end
test :"names command should send session nicknames" do
authenticated_connections(:join => "#test") do |c1, c2, c3|
c1.receive_line "NAMES #test"
assert_sent_to c1, ":hector.irc 353 user1 = #test :user1 user2 user3"
assert_sent_to c1, ":hector.irc 366 user1 #test :"
end
end
test :"names command should be split into 512-byte responses" do
authenticated_connections(:join => "#test") do |c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35, c36, c37, c38, c39, c40, c41, c42, c43, c44, c45, c46, c47, c48, c49, c50, c51, c52, c53, c54, c55, c56, c57, c58, c59, c60, c61, c62, c63, c64, c65, c66, c67, c68, c69, c70|
c1.receive_line "NAMES #test"
assert_sent_to c1, ":hector.irc 353 user1 = #test :user1 user2 user3 user4 user5 user6 user7 user8 user9 user10 user11 user12 user13 user14 user15 user16 user17 user18 user19 user20 user21 user22 user23 user24 user25 user26 user27 user28 user29 user30 user31 user32 user33 user34 user35 user36 user37 user38 user39 user40 user41 user42 user43 user44 user45 user46 user47 user48 user49 user50 user51 user52 user53 user54 user55 user56 user57 user58 user59 user60 user61 user62 user63 user64 user65 user66 user67 user68 user69"
assert_sent_to c1, ":hector.irc 353 user1 = #test :user70"
assert_sent_to c1, ":hector.irc 366 user1 #test :"
end
end
test :"topic command with text should set the channel topic" do
authenticated_connections(:join => "#test") do |c1, c2|
c1.receive_line "TOPIC #test :this is my topic"
assert_sent_to c1, ":user1!sam@hector TOPIC #test :this is my topic"
assert_sent_to c2, ":user1!sam@hector TOPIC #test :this is my topic"
end
end
test :"topic command with no arguments should send the channel topic" do
authenticated_connection.tap do |c|
c.receive_line "JOIN #test"
c.receive_line "TOPIC #test :hello world"
assert_sent_to c, ":hector.irc 332 sam #test :hello world" do
c.receive_line "TOPIC #test"
end
end
end
test :"topic command sends timestamp and nickname" do
authenticated_connection.tap do |c|
c.receive_line "JOIN #test"
c.receive_line "TOPIC #test :hello world"
assert_sent_to c, /:hector\.irc 333 sam #test sam \d+/ do
c.receive_line "TOPIC #test"
end
end
end
test :"topic command with no arguments should send 331 when no topic is set" do
authenticated_connection.tap do |c|
c.receive_line "JOIN #test"
assert_sent_to c, ":hector.irc 331 sam #test :" do
c.receive_line "TOPIC #test"
end
end
end
test :"channel topics are erased when the last session parts" do
authenticated_connection("sam").tap do |c|
c.receive_line "JOIN #test"
c.receive_line "TOPIC #test :hello world"
c.receive_line "PART #test"
end
authenticated_connection("clint").tap do |c|
c.receive_line "JOIN #test"
assert_not_sent_to c, ":hector.irc 332 clint #test :hello world"
end
end
test :"channel topics are erased when the last session quits" do
authenticated_connection("sam").tap do |c|
c.receive_line "JOIN #test"
c.receive_line "TOPIC #test :hello world"
c.receive_line "QUIT"
end
authenticated_connection("clint").tap do |c|
c.receive_line "JOIN #test"
assert_not_sent_to c, ":hector.irc 332 clint #test :hello world"
end
end
test :"sending a WHO command to an empty or undefined channel should produce an end of list message" do
authenticated_connection.tap do |c|
c.receive_line "WHO #test"
assert_sent_to c, "315 #test"
assert_not_sent_to c, "352"
end
end
test :"sending a WHO command to a channel you have joined should list each occupant's info" do
authenticated_connections(:join => "#test") do |c1, c2|
c2.receive_line "WHO #test"
assert_sent_to c2, "352 #test sam hector.irc hector.irc user1 H :0 Sam Stephenson"
assert_sent_to c2, "352 #test sam hector.irc hector.irc user2 H :0 Sam Stephenson"
assert_sent_to c2, "315 #test"
end
end
test :"sending a WHO command to an active channel you've not yet joined should still list everyone" do
authenticated_connections do |c1, c2, c3|
c1.receive_line "JOIN #test"
c2.receive_line "JOIN #test"
c3.receive_line "WHO #test"
assert_sent_to c3, "352 #test sam hector.irc hector.irc user1 H :0 Sam Stephenson"
assert_sent_to c3, "352 #test sam hector.irc hector.irc user2 H :0 Sam Stephenson"
assert_sent_to c3, "315 #test"
end
end
test :"sending a WHO command about a real user should list their user data" do
authenticated_connections do |c1, c2|
c1.receive_line "WHO user2"
assert_sent_to c1, "352 * sam hector.irc hector.irc user2 H :0 Sam Stephenson"
assert_sent_to c1, "315 user2"
end
end
test :"sending a WHO command about a non-existent user should produce an end of list message" do
authenticated_connection.tap do |c|
c.receive_line "WHO user2"
assert_sent_to c, "315 user2"
assert_not_sent_to c, "352"
end
end
test :"sending a WHOIS on a non-existent user should reply with a 401 then 318" do
authenticated_connection.tap do |c|
c.receive_line "WHOIS user2"
assert_sent_to c, "401"
assert_sent_to c, "318"
end
end
test :"sending a WHOIS on a user not on any channels should list the following items and 318" do
authenticated_connections do |c1, c2|
c1.receive_line "WHOIS user2"
assert_sent_to c1, "311"
assert_sent_to c1, "312"
assert_sent_to c1, "317"
assert_sent_to c1, "318"
assert_not_sent_to c1, "319" # no channels
end
end
test :"sending a WHOIS on a user on channels should list the following items and 318" do
authenticated_connections(:join => "#test") do |c1, c2|
c1.receive_line "WHOIS user2"
assert_sent_to c1, "311"
assert_sent_to c1, "312"
assert_sent_to c1, "319"
assert_sent_to c1, "317"
assert_sent_to c1, "318"
end
end
test :"changing nicknames should notify peers" do
authenticated_connections(:join => "#test") do |c1, c2, c3, c4|
c4.receive_line "PART #test"
c1.receive_line "NICK sam"
assert_sent_to c1, ":user1 NICK sam"
assert_sent_to c2, ":user1 NICK sam"
assert_sent_to c3, ":user1 NICK sam"
assert_not_sent_to c4, ":user1 NICK sam"
end
end
end
end