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
|
/*
* Copyright (c) 2018 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <memory>
#include <string>
#include <gtest/gtest.h>
#include <Swiften/Client/DummyStanzaChannel.h>
#include <Swiften/Elements/DiscoItems.h>
#include <Swiften/Elements/Form.h>
#include <Swiften/Elements/FormField.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/Payload.h>
#include <Swiften/Elements/PubSub.h>
#include <Swiften/Elements/PubSubItems.h>
#include <Swiften/Elements/PubSubPublish.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swift/Controllers/FdpFormSubmitController.h>
#include <Swift/Controllers/UIEvents/FdpFormSubmitWindowOpenUIEvent.h>
#include <Swift/Controllers/UIEvents/UIEventStream.h>
#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindowFactory.h>
#include <Swift/Controllers/UnitTest/MockFdpFormSubmitWindow.h>
namespace Swift {
class FdpFormSubmitControllerTest : public ::testing::Test {
protected:
void SetUp() {
clientJid_ = JID(clientJIDString_);
stanzaChannel_ = std::make_unique<DummyStanzaChannel>();
iqRouter_ = std::make_unique<IQRouter>(stanzaChannel_.get());
uiEventStream_ = std::make_unique<UIEventStream>();
fdpWindowFactory_ = std::make_unique<MockFdpFormSubmitWindowFactory>();
fdpController_ = std::make_unique<FdpFormSubmitController>(clientJid_, iqRouter_.get(), uiEventStream_.get(), fdpWindowFactory_.get());
auto fdpWindowOpenUIEvent = std::make_shared<FdpFormSubmitWindowOpenUIEvent>();
uiEventStream_->send(fdpWindowOpenUIEvent);
fdpWindow_ = fdpWindowFactory_->getMockFdpFormSubmitWindow();
}
void TearDown() {
}
std::shared_ptr<DiscoItems> createDiscoItemsResult();
std::shared_ptr<PubSub> createTemplatePubSubResult(const std::shared_ptr<Form>& form);
std::shared_ptr<PubSub> createSubmittedPubSubResult();
std::string clientJIDString_ = "testjid@test.im/swift";
JID clientJid_;
std::unique_ptr<DummyStanzaChannel> stanzaChannel_;
std::unique_ptr<IQRouter> iqRouter_;
std::unique_ptr<UIEventStream> uiEventStream_;
std::unique_ptr<MockFdpFormSubmitWindowFactory> fdpWindowFactory_;
std::unique_ptr<FdpFormSubmitController> fdpController_;
MockFdpFormSubmitWindow* fdpWindow_;
};
std::shared_ptr<DiscoItems> FdpFormSubmitControllerTest::createDiscoItemsResult() {
auto discoItems = std::make_shared<DiscoItems>();
discoItems->addItem(DiscoItems::Item("node0", JID(), "fdp/template/testNode0"));
discoItems->addItem(DiscoItems::Item("node1", JID(), "fdp/template/testNode1"));
discoItems->addItem(DiscoItems::Item("node2", JID(), "fdp/template/testNode2"));
return discoItems;
}
std::shared_ptr<PubSub> FdpFormSubmitControllerTest::createTemplatePubSubResult(const std::shared_ptr<Form>& form) {
auto pubSubItem = std::make_shared<PubSubItem>();
pubSubItem->addData(form);
auto pubSubItems = std::make_shared<PubSubItems>();
pubSubItems->addItem(pubSubItem);
auto pubSub = std::make_shared<PubSub>();
pubSub->setPayload(pubSubItems);
return pubSub;
}
std::shared_ptr<PubSub> FdpFormSubmitControllerTest::createSubmittedPubSubResult() {
auto pubSubItem = std::make_shared<PubSubItem>();
pubSubItem->setID("testID");
auto pubSubPublish = std::make_shared<PubSubPublish>();
pubSubPublish->addItem(pubSubItem);
pubSubPublish->setNode("fdp/submitted/test");
auto pubSub = std::make_shared<PubSub>();
pubSub->setPayload(pubSubPublish);
return pubSub;
}
TEST_F(FdpFormSubmitControllerTest, testRequestPubSubNodeData) {
std::string domainName = "fdp.example.test";
fdpWindow_->onRequestPubSubNodeData(domainName);
ASSERT_EQ(1, stanzaChannel_->sentStanzas.size());
auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
ASSERT_EQ(domainName, requestIq->getTo());
auto discoItemsPayloads = requestIq->getPayloads<DiscoItems>();
ASSERT_EQ(1, discoItemsPayloads.size());
auto discoItemsResult = createDiscoItemsResult();
auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), discoItemsResult);
stanzaChannel_->onIQReceived(resultIq);
auto discoItemsList = discoItemsResult->getItems();
ASSERT_EQ(discoItemsList.size(), fdpWindow_->nodeData.size());
for (unsigned int i = 0; i < fdpWindow_->nodeData.size(); i++) {
auto nodeItem = fdpWindow_->nodeData[i];
auto discoItem = discoItemsList[i];
ASSERT_EQ(discoItem.getNode(), nodeItem.first);
ASSERT_EQ(discoItem.getName(), nodeItem.second);
}
ASSERT_EQ(FdpFormSubmitWindow::NodeError::NoError, fdpWindow_->nodeError_);
}
TEST_F(FdpFormSubmitControllerTest, testRequestPubSubNodeDataError) {
std::string domainName = "fdp.example.test";
fdpWindow_->onRequestPubSubNodeData(domainName);
ASSERT_EQ(1, stanzaChannel_->sentStanzas.size());
auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
ASSERT_EQ(domainName, requestIq->getTo());
auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID());
stanzaChannel_->onIQReceived(resultIq);
ASSERT_EQ(true, fdpWindow_->nodeData.empty());
ASSERT_EQ(FdpFormSubmitWindow::NodeError::DomainNotFound, fdpWindow_->nodeError_);
}
TEST_F(FdpFormSubmitControllerTest, testRequestTemplateForm) {
std::string domainName = "fdp.example.test";
fdpWindow_->onRequestPubSubNodeData(domainName);
auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
auto discoItemsResult = createDiscoItemsResult();
auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
stanzaChannel_->onIQReceived(discoItemsResultIq);
std::string templateNodeName = "fdp/template/test";
fdpWindow_->onRequestTemplateForm(templateNodeName);
ASSERT_EQ(2, stanzaChannel_->sentStanzas.size());
auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
ASSERT_EQ(domainName, requestIq->getTo());
auto pubSubPayloads = requestIq->getPayloads<PubSub>();
ASSERT_EQ(1, pubSubPayloads.size());
std::string value0("value0");
std::string value1("value1@example.test");
auto field0 = std::make_shared<FormField>(FormField::TextSingleType, value0);
auto field1 = std::make_shared<FormField>(FormField::JIDSingleType, value1);
auto form = std::make_shared<Form>();
form->addField(field0);
form->addField(field1);
auto pubSubResult = createTemplatePubSubResult(form);
auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), pubSubResult);
stanzaChannel_->onIQReceived(resultIq);
ASSERT_EQ(form, fdpWindow_->templateForm_);
auto fields = fdpWindow_->templateForm_->getFields();
ASSERT_EQ(2, fields.size());
ASSERT_EQ(field0, fields[0]);
ASSERT_EQ(field1, fields[1]);
ASSERT_EQ(value0, fields[0]->getTextSingleValue());
ASSERT_EQ(value1, fields[1]->getJIDSingleValue());
ASSERT_EQ(FdpFormSubmitWindow::TemplateError::NoError, fdpWindow_->templateError_);
}
TEST_F(FdpFormSubmitControllerTest, testRequestTemplateFormError) {
std::string domainName = "fdp.example.test";
fdpWindow_->onRequestPubSubNodeData(domainName);
auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
auto discoItemsResult = createDiscoItemsResult();
auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
stanzaChannel_->onIQReceived(discoItemsResultIq);
std::string templateNodeName = "fdp/template/test";
fdpWindow_->onRequestTemplateForm(templateNodeName);
ASSERT_EQ(2, stanzaChannel_->sentStanzas.size());
auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
ASSERT_EQ(domainName, requestIq->getTo());
auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID());
stanzaChannel_->onIQReceived(resultIq);
ASSERT_EQ(nullptr, fdpWindow_->templateForm_);
ASSERT_EQ(FdpFormSubmitWindow::TemplateError::RequestFailed, fdpWindow_->templateError_);
}
TEST_F(FdpFormSubmitControllerTest, testSubmitForm) {
std::string domainName = "fdp.example.test";
std::string templateNodeName = "fdp/template/test";
fdpWindow_->onRequestPubSubNodeData(domainName);
auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
auto discoItemsResult = createDiscoItemsResult();
auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
stanzaChannel_->onIQReceived(discoItemsResultIq);
fdpWindow_->onRequestTemplateForm(templateNodeName);
auto templateFormRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
auto templateForm = std::make_shared<Form>();
auto templatePubSubResult = createTemplatePubSubResult(templateForm);
auto templatePubSubResultIq = IQ::createResult(clientJid_, domainName, templateFormRequestIq->getID(), templatePubSubResult);
stanzaChannel_->onIQReceived(templatePubSubResultIq);
std::string value0("value0");
std::string value1("value1@example.test");
auto field0 = std::make_shared<FormField>(FormField::TextSingleType, value0);
auto field1 = std::make_shared<FormField>(FormField::JIDSingleType, value1);
auto form = std::make_shared<Form>();
form->addField(field0);
form->addField(field1);
fdpWindow_->onSubmitForm(form);
ASSERT_EQ(3, stanzaChannel_->sentStanzas.size());
auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[2]);
ASSERT_EQ(domainName, requestIq->getTo());
auto pubSubPayloads = requestIq->getPayloads<PubSub>();
ASSERT_EQ(1, pubSubPayloads.size());
auto pubSubPayload = pubSubPayloads[0];
auto pubSubPublishPayload = std::dynamic_pointer_cast<PubSubPublish>(pubSubPayload->getPayload());
ASSERT_TRUE(pubSubPublishPayload);
auto pubSubItems = pubSubPublishPayload->getItems();
ASSERT_EQ(1, pubSubItems.size());
auto dataList = pubSubItems[0]->getData();
ASSERT_EQ(1, dataList.size());
auto submittedForm = std::dynamic_pointer_cast<Form>(dataList[0]);
ASSERT_TRUE(submittedForm);
ASSERT_EQ(form, submittedForm);
auto fields = submittedForm->getFields();
ASSERT_EQ(2, fields.size());
ASSERT_EQ(field0, fields[0]);
ASSERT_EQ(field1, fields[1]);
ASSERT_EQ(value0, fields[0]->getTextSingleValue());
ASSERT_EQ(value1, fields[1]->getJIDSingleValue());
auto pubSubResult = createSubmittedPubSubResult();
auto resultIq = IQ::createResult(clientJid_, domainName, requestIq->getID(), pubSubResult);
stanzaChannel_->onIQReceived(resultIq);
ASSERT_EQ(true, fdpWindow_->submissionSuccess_);
}
TEST_F(FdpFormSubmitControllerTest, testSubmitFormError) {
std::string domainName = "fdp.example.test";
std::string templateNodeName = "fdp/template/test";
fdpWindow_->onRequestPubSubNodeData(domainName);
auto nodeDataRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[0]);
auto discoItemsResult = createDiscoItemsResult();
auto discoItemsResultIq = IQ::createResult(clientJid_, domainName, nodeDataRequestIq->getID(), discoItemsResult);
stanzaChannel_->onIQReceived(discoItemsResultIq);
fdpWindow_->onRequestTemplateForm(templateNodeName);
auto templateFormRequestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[1]);
auto templateForm = std::make_shared<Form>();
auto templatePubSubResult = createTemplatePubSubResult(templateForm);
auto templatePubSubResultIq = IQ::createResult(clientJid_, domainName, templateFormRequestIq->getID(), templatePubSubResult);
stanzaChannel_->onIQReceived(templatePubSubResultIq);
auto form = std::make_shared<Form>();
fdpWindow_->onSubmitForm(form);
ASSERT_EQ(3, stanzaChannel_->sentStanzas.size());
auto requestIq = std::dynamic_pointer_cast<IQ>(stanzaChannel_->sentStanzas[2]);
ASSERT_EQ(domainName, requestIq->getTo());
auto resultIq = IQ::createError(clientJid_, domainName, requestIq->getID());
stanzaChannel_->onIQReceived(resultIq);
ASSERT_EQ(false, fdpWindow_->submissionSuccess_);
}
}
|