summaryrefslogtreecommitdiffstats
blob: 5fe41682d7cea03ca9d5a6b6e74359104080a9fe (plain)
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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Elements/SearchPayload.h>
#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h>

using namespace Swift;

class SearchPayloadParserTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(SearchPayloadParserTest);
        CPPUNIT_TEST(testParse_FormRequestResponse);
        CPPUNIT_TEST(testParse_Results);
        CPPUNIT_TEST(testParse_FormRequestResponse_XDATA);
        CPPUNIT_TEST(testParse_Results_XDATA);
        CPPUNIT_TEST_SUITE_END();

    public:
        void testParse_FormRequestResponse() {
            PayloadsParserTester parser;

            CPPUNIT_ASSERT(parser.parse(
                    "<query xmlns=\"jabber:iq:search\">"
                        "<instructions>Foo</instructions>"
                        "<first/>"
                        "<last/>"
                    "</query>"
                ));

            SearchPayload::ref payload = parser.getPayload<SearchPayload>();
            CPPUNIT_ASSERT_EQUAL(std::string("Foo"), *payload->getInstructions());
            CPPUNIT_ASSERT(payload->getFirst());
            CPPUNIT_ASSERT(payload->getLast());
            CPPUNIT_ASSERT(!payload->getNick());
        }

        void testParse_Results() {
            PayloadsParserTester parser;

            CPPUNIT_ASSERT(parser.parse(
                    "<query xmlns=\"jabber:iq:search\">"
                        "<item jid=\"juliet@capulet.com\">"
                            "<first>Juliet</first>"
                            "<last>Capulet</last>"
                            "<nick>JuliC</nick>"
                            "<email>juliet@shakespeare.lit</email>"
                        "</item>"
                        "<item jid=\"tybalt@shakespeare.lit\">"
                            "<first>Tybalt</first>"
                            "<last>Capulet</last>"
                            "<nick>ty</nick>"
                            "<email>tybalt@shakespeare.lit</email>"
                        "</item>"
                    "</query>"
                ));

            SearchPayload::ref payload = parser.getPayload<SearchPayload>();
            CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(payload->getItems().size()));
            CPPUNIT_ASSERT_EQUAL(JID("juliet@capulet.com"), payload->getItems()[0].jid);
            CPPUNIT_ASSERT_EQUAL(std::string("Juliet"), payload->getItems()[0].first);
            CPPUNIT_ASSERT_EQUAL(std::string("Capulet"), payload->getItems()[0].last);
            CPPUNIT_ASSERT_EQUAL(std::string("JuliC"), payload->getItems()[0].nick);
            CPPUNIT_ASSERT_EQUAL(std::string("juliet@shakespeare.lit"), payload->getItems()[0].email);
            CPPUNIT_ASSERT_EQUAL(JID("tybalt@shakespeare.lit"), payload->getItems()[1].jid);
        }

        void testParse_FormRequestResponse_XDATA() {
            PayloadsParserTester parser;

            CPPUNIT_ASSERT(parser.parse(
                            "<query xmlns='jabber:iq:search'>"
                                "<instructions>"
                                    "Use the enclosed form to search. If your Jabber client does not"
                                    " support Data Forms, visit http://shakespeare.lit/"
                                "</instructions>"
                                "<x xmlns='jabber:x:data' type='form'>"
                                    "<title>User Directory Search</title>"
                                    "<instructions>"
                                        "Please provide the following information"
                                        " to search for Shakespearean characters."
                                    "</instructions>"
                                    "<field type='hidden'"
                                        " var='FORM_TYPE'>"
                                        "<value>jabber:iq:search</value>"
                                    "</field>"
                                    "<field type='text-single'"
                                    " label='Given Name'"
                                    " var='first'/>"
                                    "<field type='text-single'"
                                    " label='Family Name'"
                                    " var='last'/>"
                                    "<field type='list-single'"
                                    " label='Gender'"
                                    " var='x-gender'>"
                                        "<option label='Male'><value>male</value></option>"
                                        "<option label='Female'><value>female</value></option>"
                                    "</field>"
                                "</x>"
                            "</query>"
                ));

            SearchPayload::ref payload = parser.getPayload<SearchPayload>();
            CPPUNIT_ASSERT_EQUAL(std::string("Use the enclosed form to search. If your Jabber client does not"
                                             " support Data Forms, visit http://shakespeare.lit/"), *payload->getInstructions());
            CPPUNIT_ASSERT(payload->getForm());
            CPPUNIT_ASSERT_EQUAL(std::string("Please provide the following information"
                                             " to search for Shakespearean characters."), payload->getForm()->getInstructions());
        }

        void testParse_Results_XDATA() {
            PayloadsParserTester parser;

            CPPUNIT_ASSERT(parser.parse("<query xmlns='jabber:iq:search'>"
                                        "    <x xmlns='jabber:x:data' type='result'>"
                                        "        <field type='hidden' var='FORM_TYPE'>"
                                        "            <value>jabber:iq:search</value>"
                                        "        </field>"
                                        "        <reported>"
                                        "            <field var='first' label='Given Name' type='text-single'/>"
                                        "            <field var='last' label='Family Name' type='text-single'/>"
                                        "            <field var='jid' label='Jabber ID' type='jid-single'/>"
                                        "            <field var='x-gender' label='Gender' type='list-single'/>"
                                        "        </reported>"
                                        "        <item>"
                                        "            <field var='first'><value>Benvolio</value></field>"
                                        "            <field var='last'><value>Montague</value></field>"
                                        "            <field var='jid'><value>benvolio@montague.net</value></field>"
                                        "            <field var='x-gender'><value>male</value></field>"
                                        "        </item>"
                                        "        <item>"
                                        "            <field var='first'><value>Romeo</value></field>"
                                        "            <field var='last'><value>Montague</value></field>"
                                        "            <field var='jid'><value>romeo@montague.net</value></field>"
                                        "            <field var='x-gender'><value>male</value></field>"
                                        "        </item>"
                                        "    </x>"
                                        "</query>"));
            SearchPayload::ref payload = parser.getPayload<SearchPayload>();
            CPPUNIT_ASSERT(payload);

            Form::ref dataForm = payload->getForm();
            CPPUNIT_ASSERT(dataForm);

            Form::FormItem reported = dataForm->getReportedFields();
            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), reported.size());

            std::vector<Form::FormItem> items = dataForm->getItems();
            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), items.size());

            Form::FormItem item = items[0];
            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), item.size());

            CPPUNIT_ASSERT_EQUAL(std::string("Benvolio"), item[0]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName());
            CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName());
            CPPUNIT_ASSERT_EQUAL(std::string("benvolio@montague.net"), item[2]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName());
            CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName());

            item = items[1];
            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), item.size());

            CPPUNIT_ASSERT_EQUAL(std::string("Romeo"), item[0]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName());
            CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName());
            CPPUNIT_ASSERT_EQUAL(std::string("romeo@montague.net"), item[2]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName());
            CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getValues()[0]);
            CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName());
        }
};

CPPUNIT_TEST_SUITE_REGISTRATION(SearchPayloadParserTest);