File: tc_iq.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 (113 lines) | stat: -rwxr-xr-x 3,614 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
#!/usr/bin/ruby

$:.unshift '../lib'

require 'test/unit'
require 'socket'
require 'xmpp4r/rexmladdons'
require 'xmpp4r/iq'
require 'xmpp4r/errors'
include Jabber

require File::dirname(__FILE__) + '/lib/assert_equal_xml'

class IqTest < Test::Unit::TestCase
  def test_create
    x = Iq.new()
    assert_equal("iq", x.name)
    assert_equal("jabber:client", x.namespace)
    assert_equal_xml("<iq xmlns='jabber:client'/>", x)
  end

  def test_iqauth
    x = Iq.new_authset(JID.new('node@domain/resource'), 'password')
    assert_equal_xml("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:auth'><username>node</username><password>password</password><resource>resource</resource></query></iq>", x)
  end

  def test_iqauth_digest
    x = Iq.new_authset_digest(JID.new('node@domain/resource'), '', 'password')
    assert_equal_xml("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:auth'><username>node</username><digest>#{Digest::SHA1.hexdigest('password')}</digest><resource>resource</resource></query></iq>", x)
  end

  def test_register
    x1 = Iq.new_register
    assert_equal_xml("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:register'/></iq>", x1)
    x2 = Iq.new_register('node')
    assert_equal_xml("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:register'><username>node</username></query></iq>", x2)
    x3 = Iq.new_register('node', 'password')
    assert_equal_xml("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:register'><username>node</username><password>password</password></query></iq>", x3)
  end

  def test_rosterget
    x = Iq.new_rosterget
    assert_equal_xml("<iq type='get' xmlns='jabber:client'><query xmlns='jabber:iq:roster'/></iq>", x)
  end

  def test_rosterset
    x = Iq.new_rosterset
    assert_equal_xml("<iq type='set' xmlns='jabber:client'><query xmlns='jabber:iq:roster'/></iq>", x)
  end

  def test_browseget
    x = Iq.new_browseget
    assert_equal_xml("<iq type='get' xmlns='jabber:client'><query xmlns='jabber:iq:browse'/></iq>", x)
  end

  def test_types
    iq = Iq.new
    assert_equal(nil, iq.type)
    iq.type = :get
    assert_equal(:get, iq.type)
    iq.type = :set
    assert_equal(:set, iq.type)
    iq.type = :result
    assert_equal(:result, iq.type)
    iq.type = :error
    assert_equal(:error, iq.type)
    iq.type = :invalid
    assert_equal(nil, iq.type)
  end

  def test_query
    x = Iq.new(:set)
    assert_equal(nil, x.queryns)
    query = REXML::Element.new('query')
    x.add(query)
    assert_equal('jabber:client', x.queryns)
    query.add_namespace('jabber:iq:auth')
    assert_equal(query.to_s, x.query.to_s)
    assert_equal('jabber:iq:auth', x.queryns)

    query2 = REXML::Element.new('query')
    x.query = query2
    assert_equal('jabber:client', x.queryns)
    query2.add_namespace('jabber:iq:register')
    assert_equal('jabber:iq:register', x.queryns)
  end

  def test_vcard
    x = Iq.new
    assert_equal(nil, x.vcard)
    x.add(vcard = REXML::Element.new('vCard'))
    assert_equal(vcard, x.vcard)
  end

  def test_error
    x = Iq.new(:set)
    e = REXML::Element.new('error')
    x.add(e)
    # test if, after an import, the error element is successfully changed
    # into an ErrorResponse object.
    x2 = Iq.new.import(x)
    assert_equal(ErrorResponse, x2.first_element('error').class)
  end

  def test_new_query
    x = Iq.new_query(:get, JID.new('a@b/c'))
    assert_equal(:get, x.type)
    assert_equal(nil, x.from)
    assert_equal(JID.new('a@b/c'), x.to)
    assert_kind_of(IqQuery, x.query)
    assert_equal('jabber:client', x.queryns)
  end
end