File: tc_muc_mucclient.rb

package info (click to toggle)
ruby-xmpp4r 0.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,384 kB
  • sloc: ruby: 17,382; xml: 74; sh: 12; makefile: 4
file content (834 lines) | stat: -rwxr-xr-x 31,237 bytes parent folder | download
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
#!/usr/bin/ruby


$:.unshift File::dirname(__FILE__) + '/../../lib'

require 'test/unit'
require File::dirname(__FILE__) + '/../lib/clienttester'
require 'xmpp4r/muc'
require 'xmpp4r/semaphore'
include Jabber

class MUCClientTest < Test::Unit::TestCase
  include ClientTester

  def test_new1
    m = MUC::MUCClient.new(@client)
    assert_equal(nil, m.jid)
    assert_equal(nil, m.my_jid)
    assert_equal({}, m.roster)
    assert(!m.active?)
  end

  # JEP-0045: 6.3 Entering a Room
  def test_enter_room
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='400' type='modify'>" +
           "<jid-malformed xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
           "</error></presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
      send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'>" +
          "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='owner' role='moderator'/></x>" +
          "</presence>" +
          "<presence from='darkcave@macbeth.shakespeare.lit/secondwitch' to='hag66@shakespeare.lit/pda'>" +
          "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='admin' role='moderator'/></x>" +
          "</presence>" +
          "<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
          "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
          "</presence>")
    }


    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert(!m.active?)
    assert_nil(m.room)

    assert_raises(ServerError) {
      m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
    }
    wait_state
    assert(!m.active?)
    assert_nil(m.room)

    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
    wait_state
    assert(m.active?)
    assert_equal('darkcave', m.room)
    assert_equal(3, m.roster.size)
    m.roster.each { |resource,pres|
      assert_equal(resource, pres.from.resource)
      assert_equal('darkcave', pres.from.node)
      assert_equal('macbeth.shakespeare.lit', pres.from.domain)
      assert_kind_of(String, resource)
      assert_kind_of(Presence, pres)
      assert(%w(firstwitch secondwitch thirdwitch).include?(resource))
      assert_kind_of(MUC::XMUCUser, pres.x)
      assert_kind_of(Array, pres.x.items)
      assert_equal(1, pres.x.items.size)
    }
    assert_equal(:owner, m.roster['firstwitch'].x.items[0].affiliation)
    assert_equal(:moderator, m.roster['firstwitch'].x.items[0].role)
    assert_equal(:admin, m.roster['secondwitch'].x.items[0].affiliation)
    assert_equal(:moderator, m.roster['secondwitch'].x.items[0].role)
    assert_equal(:member, m.roster['thirdwitch'].x.items[0].affiliation)
    assert_equal(:participant, m.roster['thirdwitch'].x.items[0].role)
    assert_nil(m.roster['thirdwitch'].x.items[0].jid)

    send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='crone1@shakespeare.lit/desktop'>" +
         "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='none' jid='hag66@shakespeare.lit/pda' role='participant'/></x>" +
         "</presence>")
    n = 0
    while m.roster.size != 3 and n < 1000
      Thread::pass
      n += 1
    end
    assert_equal(3, m.roster.size)
    assert_equal(:none, m.roster['thirdwitch'].x.items[0].affiliation)
    assert_equal(:participant, m.roster['thirdwitch'].x.items[0].role)
    assert_equal(JID.new('hag66@shakespeare.lit/pda'), m.roster['thirdwitch'].x.items[0].jid)
  end

  def test_enter_room_password
    state { |pres|
      assert_kind_of(Presence, pres)
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='401' type='auth'><not-authorized xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal('cauldron', pres.x.password)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_raises(ServerError) {
      m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
    }
    wait_state
    assert(!m.active?)

    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch', 'cauldron'))
    wait_state
    assert(m.active?)
  end

  def test_members_only_room
    state { |pres|
      assert_kind_of(Presence, pres)
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='407' type='auth'><registration-required xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_raises(ServerError) {
      m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
    }
    assert(!m.active?)

    wait_state
  end

  def test_banned_users
    state { |pres|
      assert_kind_of(Presence, pres)
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='403' type='auth'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_raises(ServerError) {
      m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
    }
    assert(!m.active?)

    wait_state
  end

  def test_nickname_conflict
    state { |pres|
      assert_kind_of(Presence, pres)
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='409' type='cancel'><conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_raises(ServerError) {
      m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
    }
    assert(!m.active?)

    wait_state
  end

  def test_max_users
    state { |pres|
      assert_kind_of(Presence, pres)
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='503' type='wait'><service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_raises(ServerError) {
      m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
    }
    assert(!m.active?)

    wait_state
  end

  def test_locked_room
    state { |pres|
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='404' type='cancel'><item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_raises(ServerError) {
      m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
    }
    assert(!m.active?)
    wait_state
  end

  def test_exit_room
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_nil(pres.type)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(:unavailable, pres.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_nil(pres.status)
      send("<presence from='darkcave@macbeth.shakespeare.lit/secondwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
           "</presence>")
    }

    ignored_stanzas = 0
    @client.add_stanza_callback { |stanza|
      ignored_stanzas += 1
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_equal(0, ignored_stanzas)
    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
    wait_state
    assert(m.active?)

    assert_equal(0, ignored_stanzas)
    assert_equal(m, m.exit)
    wait_state
    assert(!m.active?)
    assert_equal(1, ignored_stanzas)
  end

  def test_custom_exit_message
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_nil(pres.type)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(:unavailable, pres.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal('gone where the goblins go', pres.status)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
    assert(m.active?)
    wait_state

    assert_equal(m, m.exit('gone where the goblins go'))
    assert(!m.active?)
    wait_state
  end

  def test_joins
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_nil(pres.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(:unavailable, pres.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
      assert_nil(pres.status)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_nil(pres.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/fourthwitch'), pres.to)
      send("<presence from='darkcave@macbeth.shakespeare.lit/fourthwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(:unavailable, pres.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/fourthwitch'), pres.to)
      assert_equal(pres.status, 'Exiting one last time')
      send("<presence from='darkcave@macbeth.shakespeare.lit/fourthwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
           "</presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
    wait_state
    assert(m.active?)

    assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/thirdwitch') }
    assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/fourthwitch') }
    assert(m.active?)

    assert_equal(m, m.exit)
    wait_state
    assert(!m.active?)
    assert_raises(RuntimeError) { m.exit }
    assert(!m.active?)

    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/fourthwitch'))
    wait_state
    assert(m.active?)

    assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/thirdwitch') }
    assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/fourthwitch') }
    assert(m.active?)

    assert_equal(m, m.exit('Exiting one last time'))
    wait_state
    assert(!m.active?)
    assert_raises(RuntimeError) { m.exit }
    assert(!m.active?)
  end

  def test_message_callback
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal('cauldron', pres.x.password)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }

    message_lock = Semaphore.new

    messages_client = 0
    @client.add_message_callback { |msg|
      messages_client += 1
      message_lock.run
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    messages_muc = 0
    m.add_message_callback { |msg|
      messages_muc += 1
      message_lock.run
    }
    messages_muc_private = 0
    m.add_private_message_callback { |msg|
      messages_muc_private += 1
      message_lock.run
    }

    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch', 'cauldron'))
    assert(m.active?)

    assert_equal(0, messages_client)
    assert_equal(0, messages_muc)
    assert_equal(0, messages_muc_private)

    send("<message from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'><body>Hello</body></message>")
    message_lock.wait

    assert_equal(0, messages_client)
    assert_equal(1, messages_muc)
    assert_equal(0, messages_muc_private)

    send("<message from='user@domain/resource' to='hag66@shakespeare.lit/pda'><body>Hello</body></message>")
    message_lock.wait

    assert_equal(1, messages_client)
    assert_equal(1, messages_muc)
    assert_equal(0, messages_muc_private)

    send("<message type='chat' from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'><body>Hello</body></message>")
    message_lock.wait

    assert_equal(1, messages_client)
    assert_equal(1, messages_muc)
    assert_equal(1, messages_muc_private)

    wait_state
  end

  def test_presence_callbacks
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_nil(pres.x.password)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }

    presence_lock = Semaphore.new

    presences_client = 0
    @client.add_presence_callback { |pres|
      presences_client += 1
      presence_lock.run
    }
    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'
    presences_join = 0
    m.add_join_callback { |pres|
      presences_join += 1
      presence_lock.run
    }
    presences_leave = 0
    m.add_leave_callback { |pres|
      presences_leave += 1
      presence_lock.run
    }
    presences_muc = 0
    m.add_presence_callback { |pres|
      presences_muc += 1
      presence_lock.run
    }

    assert_equal(0, presences_client)
    assert_equal(0, presences_join)
    assert_equal(0, presences_leave)
    assert_equal(0, presences_muc)

    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
    assert(m.active?)

    assert_equal(0, presences_client)
    assert_equal(0, presences_join) # Joins before own join won't be called back
    assert_equal(0, presences_leave)
    assert_equal(0, presences_muc)

    send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'>" +
         "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
         "</presence>")
    presence_lock.wait
    assert_equal(0, presences_client)
    assert_equal(1, presences_join)
    assert_equal(0, presences_leave)
    assert_equal(0, presences_muc)

    send("<presence from='user@domain/resource' to='hag66@shakespeare.lit/pda'>" +
         "<show>chat</show>" +
         "</presence>")
    presence_lock.wait
    assert_equal(1, presences_client)
    assert_equal(1, presences_join)
    assert_equal(0, presences_leave)
    assert_equal(0, presences_muc)

    send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'>" +
         "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
         "<show>away</show></presence>")
    presence_lock.wait
    assert_equal(1, presences_client)
    assert_equal(1, presences_join)
    assert_equal(0, presences_leave)
    assert_equal(1, presences_muc)

    send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda' type='unavailable'/>")
    presence_lock.wait
    assert_equal(1, presences_client)
    assert_equal(1, presences_join)
    assert_equal(1, presences_leave)
    assert_equal(1, presences_muc)
    wait_state
  end

  def test_send
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_nil(pres.x.password)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }
    state { |stanza|
      assert_kind_of(Message, stanza)
      assert(:groupchat, stanza.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), stanza.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit'), stanza.to)
      assert_equal('First message', stanza.body)
    }
    state { |stanza|
      assert_kind_of(Message, stanza)
      assert(:chat, stanza.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), stanza.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/secondwitch'), stanza.to)
      assert_equal('Second message', stanza.body)
    }
    state { |stanza|
      assert_kind_of(Message, stanza)
      assert(:chat, stanza.type)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), stanza.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/firstwitch'), stanza.to)
      assert_equal('Third message', stanza.body)
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'

    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
    wait_state
    assert(m.active?)

    m.send(Jabber::Message.new(nil, 'First message'))
    wait_state
    m.send(Jabber::Message.new(nil, 'Second message'), 'secondwitch')
    wait_state
    m.send(Jabber::Message.new('secondwitch', 'Third message'), 'firstwitch')
    wait_state
  end

  def test_nick
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_nil(pres.x.password)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/secondwitch'), pres.to)
      assert_nil(pres.type)
      send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
           "<error code='409' type='cancel'><conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
           "</presence>")
    }
    state { |pres|
      assert_kind_of(Presence, pres)
      assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
      assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/oldhag'), pres.to)
      assert_nil(pres.type)
      send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'>" +
           "<item affiliation='member' jid='hag66@shakespeare.lit/pda' nick='oldhag' role='participant'/>" +
           "<status code='303'/>" +
           "</x></presence>" +
           "<presence from='darkcave@macbeth.shakespeare.lit/oldhag' to='hag66@shakespeare.lit/pda'>" +
           "<x xmlns='http://jabber.org/protocol/muc#user'>" +
           "<item affiliation='member' jid='hag66@shakespeare.lit/pda' role='participant'/>" +
           "</x></presence>")
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = 'hag66@shakespeare.lit/pda'

    assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
    wait_state
    assert(m.active?)
    assert_equal('thirdwitch', m.nick)

    assert_raises(ServerError) {
      m.nick = 'secondwitch'
    }
    wait_state
    assert(m.active?)
    assert_equal('thirdwitch', m.nick)

    m.nick = 'oldhag'
    wait_state
    assert(m.active?)
    assert_equal('oldhag', m.nick)
  end

  # JEP-0045: 10.2 Room Configuration
  def test_configuration
    room = JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch')
    jid = JID.new('hag66@shakespeare.lit/pda')

    state { |pres|
      send(
          "<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
          "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='owner' role='moderator'/></x>" +
          "</presence>"
      )
    }

    state { |iq|
      assert_kind_of(Jabber::Iq,iq)
      assert_equal(jid, iq.from)
      assert_equal(room.strip, iq.to)
      assert_equal(:get, iq.type)

      assert_kind_of(Jabber::MUC::IqQueryMUCOwner, iq.first_element('query'))

      send(muc_config_form.sub("id='config1'","id='#{iq.id}'"))
    }

    state { |room_config|
      assert_kind_of(Jabber::Iq, room_config)
      assert_equal(room.strip, room_config.to)
      assert_equal(:set, room_config.type)

      assert_kind_of(Jabber::MUC::IqQueryMUCOwner, room_config.first_element('query'))

      form = room_config.first_element('query/x')
      assert_kind_of(Dataforms::XData, form)
      assert_equal(:submit, form.type)
      assert_equal(1, form.elements.size)
      assert_equal('muc#roomconfig_roomname', form.first_element('field').var)
      assert_equal(['Dunsinane'], form.first_element('field').values)

      send(muc_config_acknowledgement.sub("id='config1'","id='#{room_config.id}'"))
    }

    m = MUC::MUCClient.new(@client)
    m.my_jid = jid
    m.join(room)
    wait_state
    assert_equal(true, m.owner?)

    assert_equal(%w{muc#roomconfig_roomname muc#roomconfig_roomdesc
      muc#roomconfig_enablelogging muc#roomconfig_changesubject muc#roomconfig_allowinvites
      muc#roomconfig_maxusers muc#roomconfig_presencebroadcast muc#roomconfig_getmemberlist
      muc#roomconfig_publicroom muc#roomconfig_persistentroom muc#roomconfig_moderatedroom
      muc#roomconfig_membersonly muc#roomconfig_passwordprotectedroom muc#roomconfig_roomsecret
      muc#roomconfig_whois muc#roomconfig_roomadmins muc#roomconfig_roomowners}, m.get_room_configuration)
    wait_state

    m.submit_room_configuration( 'muc#roomconfig_roomname' => 'Dunsinane' )
    wait_state
  end

  # example 150 from XEP-0045
  def muc_config_form
      "<iq from='darkcave@macbeth.shakespeare.lit'
          id='config1'
          to='crone1@shakespeare.lit/desktop'
          type='result'>
        <query xmlns='http://jabber.org/protocol/muc#owner'>
          <x xmlns='jabber:x:data' type='form'>
            <title>Configuration for \"darkcave\" Room</title>
            <instructions>
              Complete this form to make changes to
              the configuration of your room.
            </instructions>
            <field
                type='hidden'
                var='FORM_TYPE'>
              <value>http://jabber.org/protocol/muc#roomconfig</value>
            </field>
            <field
                label='Natural-Language Room Name'
                type='text-single'
                var='muc#roomconfig_roomname'>
              <value>A Dark Cave</value>
            </field>
            <field
                label='Short Description of Room'
                type='text-single'
                var='muc#roomconfig_roomdesc'>
              <value>The place for all good witches!</value>
            </field>
            <field
                label='Enable Public Logging?'
                type='boolean'
                var='muc#roomconfig_enablelogging'>
              <value>0</value>
            </field>
            <field
                label='Allow Occupants to Change Subject?'
                type='boolean'
                var='muc#roomconfig_changesubject'>
              <value>0</value>
            </field>
            <field
                label='Allow Occupants to Invite Others?'
                type='boolean'
                var='muc#roomconfig_allowinvites'>
              <value>0</value>
            </field>
            <field
                label='Maximum Number of Occupants'
                type='list-single'
                var='muc#roomconfig_maxusers'>
              <value>10</value>
              <option label='10'><value>10</value></option>
              <option label='20'><value>20</value></option>
              <option label='30'><value>30</value></option>
              <option label='50'><value>50</value></option>
              <option label='100'><value>100</value></option>
              <option label='None'><value>none</value></option>
            </field>
            <field
                label='Roles for which Presence is Broadcast'
                type='list-multi'
                var='muc#roomconfig_presencebroadcast'>
              <value>moderator</value>
              <value>participant</value>
              <value>visitor</value>
              <option label='Moderator'><value>moderator</value></option>
              <option label='Participant'><value>participant</value></option>
              <option label='Visitor'><value>visitor</value></option>
            </field>
            <field
                label='Roles and Affiliations that May Retrieve Member List'
                type='list-multi'
                var='muc#roomconfig_getmemberlist'>
              <value>moderator</value>
              <value>participant</value>
              <value>visitor</value>
              <option label='Moderator'><value>moderator</value></option>
              <option label='Participant'><value>participant</value></option>
              <option label='Visitor'><value>visitor</value></option>
            </field>
            <field
                label='Make Room Publicly Searchable?'
                type='boolean'
                var='muc#roomconfig_publicroom'>
              <value>0</value>
            </field>
            <field
                label='Make Room Persistent?'
                type='boolean'
                var='muc#roomconfig_persistentroom'>
              <value>0</value>
            </field>
            <field
                label='Make Room Moderated?'
                type='boolean'
                var='muc#roomconfig_moderatedroom'>
              <value>0</value>
            </field>
            <field
                label='Make Room Members Only?'
                type='boolean'
                var='muc#roomconfig_membersonly'>
              <value>0</value>
            </field>
            <field
                label='Password Required for Entry?'
                type='boolean'
                var='muc#roomconfig_passwordprotectedroom'>
              <value>1</value>
            </field>
            <field type='fixed'>
              <value>
                If a password is required to enter this room,
                you must specify the password below.
              </value>
            </field>
            <field
                label='Password'
                type='text-private'
                var='muc#roomconfig_roomsecret'>
              <value>cauldronburn</value>
            </field>
            <field
                label='Who May Discover Real JIDs?'
                type='list-single'
                var='muc#roomconfig_whois'>
              <value>moderators</value>
              <option label='Moderators Only'>
                <value>moderators</value>
              </option>
              <option label='Anyone'>
                <value>anyone</value>
              </option>
            </field>
            <field type='fixed'>
              <value>
                You may specify additional people who have
                administrative privileges in the room. Please
                provide one Jabber ID per line.
              </value>
            </field>
            <field
                label='Room Admins'
                type='jid-multi'
                var='muc#roomconfig_roomadmins'>
              <value>wiccarocks@shakespeare.lit</value>
              <value>hecate@shakespeare.lit</value>
            </field>
            <field type='fixed'>
              <value>
                You may specify additional owners for this
                room. Please provide one Jabber ID per line.
              </value>
            </field>
            <field
                label='Room Owners'
                type='jid-multi'
                var='muc#roomconfig_roomowners'/>
          </x>
        </query>
      </iq>"
  end

  def muc_config_acknowledgement
      "<iq from='darkcave@macbeth.shakespeare.lit'
          id='config1'
          to='crone1@shakespeare.lit/desktop'>
        <query xmlns='http://jabber.org/protocol/muc#owner'>
          <x xmlns='jabber:x:data' type='result'>
            <field var='muc#roomconfig_roomname'><value>Dunsinane</value></field>
          </x>
        </query>
      </iq>"
  end
end