Clarify order of bound manipulations so it is not temporarily exceeded.
[thrasher.git] / perl / tests / test_roster.pl
blob9aa6f752ab881c103c0522882bea8d904fee074a
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 # Test the roster implementation in the Test backend works. Note
7 # that it is the Test in Thrasher::Backend::Test that is referred
8 # to in the name of this script. (That is, it's not just a
9 # redundant statement that this is a test; you may test other
10 # backends' rosters.)
12 use Test::More 'no_plan';
13 use Test::Deep;
14 use Data::Dumper;
16 use Thrasher::Test qw(:all);
18 BEGIN {
19 use_ok 'Thrasher::Test', qw(:all);
20 use_ok 'Thrasher::Backend';
21 use_ok 'Thrasher::Backend::Test';
22 use_ok 'Thrasher::Component';
23 use_ok 'Thrasher::Roster', qw(:all);
24 use_ok 'Thrasher::Protocol::Test';
27 my $backend = new Thrasher::Backend::Test;
29 my $jid = 'romeo@montague.lit';
30 my $legacy = 'juliet';
32 cmp_deeply($backend->get_roster($jid), {},
33 'initially we have an empty roster');
34 $backend->set_roster($jid, {$legacy => $backend->subscribed});
35 cmp_deeply($backend->get_roster($jid), {$legacy => $backend->subscribed},
36 'roster can be set');
37 $backend->set_roster_user_state($jid, $legacy,
38 $backend->unsubscribed);
39 cmp_deeply($backend->get_roster($jid), {},
40 'unsubscribing correctly manifests as a removal');
41 $backend->set_roster_user_state($jid, $legacy,
42 $backend->subscribed);
43 cmp_deeply($backend->get_roster($jid), {$legacy => $backend->subscribed},
44 'correctly remembers that the legacy user is subscribed.');
46 cmp_deeply(roster_diff({}, {}), {},
47 'roster diff compares empties correctly');
48 cmp_deeply(roster_diff({$legacy => subscribed}, {}),
49 {$legacy => [subscribed, unsubscribed]},
50 'roster diff handles people no longer being subscribed');
51 cmp_deeply(roster_diff({}, {$legacy => subscribed}),
52 {$legacy => [unsubscribed, subscribed]},
53 'roster diff handles people now subscribed');
54 cmp_deeply(roster_diff({$legacy => want_subscribe},
55 {$legacy => subscribed}),
56 {$legacy => [want_subscribe, subscribed]},
57 'roster diff handles people with changed status');
59 # This tests the set_current_legacy_roster by forging an old
60 # roster, then forging a new one, to ensure that we get the
61 # behavior we expect.
62 TEST_ROSTER_SYNC: {
63 # Six cases, used in user names for identification:
64 # 1. subscribed -> unsubscribed
65 # 2. subscribed -> want_subscribe
66 # 3. unsubscribed -> subscribed
67 # 4. unsubscribed -> want_subscribe
68 # 5. want_subscribe -> unsubscribed
69 # 6. want_subscribe -> subscribed
71 my $component = logged_in_comp;
73 my $old_roster =
74 { j1 => subscribed,
75 j2 => subscribed,
76 # unsubscribed is "not present"
77 j5 => want_subscribe,
78 j6 => want_subscribe };
80 my $new_roster =
81 { j2 => want_subscribe,
82 j3 => subscribed,
83 j4 => want_subscribe,
84 j6 => subscribed };
86 $component->{protocol}->{backend}->set_roster('romeo@montague.lit',
87 $old_roster);
89 $component->{protocol}->set_current_legacy_roster
90 ($component->session_for('romeo@montague.lit'), $new_roster);
92 my $expected = clean_xml(<<'EXPECTED');
93 <presence from='j1@test.transport'
94 to='romeo@montague.lit'
95 type='unsubscribed'/>
96 <presence from='j1@test.transport'
97 to='romeo@montague.lit'
98 type='unsubscribe'/>
99 <presence from='j2@test.transport'
100 to='romeo@montague.lit'
101 type='unsubscribe'/>
102 <presence from='j2@test.transport'
103 to='romeo@montague.lit'
104 type='unsubscribed'/>
105 <presence from='j2@test.transport'
106 to='romeo@montague.lit'
107 type='subscribe'/>
108 <presence from='j3@test.transport'
109 to='romeo@montague.lit'
110 type='subscribe'/>
111 <presence from='j3@test.transport'
112 to='romeo@montague.lit'
113 type='subscribed'/>
114 <presence from='j4@test.transport'
115 to='romeo@montague.lit'
116 type='subscribe'/>
117 <presence from='j5@test.transport'
118 to='romeo@montague.lit'
119 type='unsubscribe'/>
120 <presence from='j5@test.transport'
121 to='romeo@montague.lit'
122 type='unsubscribed'/>
123 <presence from='j6@test.transport'
124 to='romeo@montague.lit'
125 type='subscribe'/>
126 <presence from='j6@test.transport'
127 to='romeo@montague.lit'
128 type='subscribed'/>
129 EXPECTED
131 is(output, $expected,
132 'set_current_legacy_roster works as expected.');
135 SET_ROSTER_NAME: {
136 my $component = logged_in_comp;
137 $component->set_roster_name('horatio@denmark.lit',
138 'rosencrantz@denmark.lit',
139 'Rosencrantz', 1);
141 my $expected = clean_xml(<<EXPECTED);
142 <iq from='test.transport'
143 id='id3'
144 to='horatio\@denmark.lit'
145 type='set'>
146 <x xmlns='http://jabber.org/protocol/rosterx'>
147 <item action='modify'
148 jid='rosencrantz\@denmark.lit'
149 name='Rosencrantz'/>
150 </x>
151 </iq>
152 EXPECTED
154 is($expected, output, 'set roster name appears to work.');