From 594c1eaa2b317e47a17c66647df2dd3a730b06dc Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Tue, 14 Aug 2012 12:53:14 +0200 Subject: [PATCH] More tests --- src/tests/basictest.cpp | 10 +++- src/tests/basictest.h | 98 ++++++++++++++++++++++++++++++++++ src/tests/userregistration.cpp | 76 +++++++++++++++++++++++++- src/userregistration.cpp | 2 - 4 files changed, 182 insertions(+), 4 deletions(-) diff --git a/src/tests/basictest.cpp b/src/tests/basictest.cpp index ea14155f..34530f0d 100644 --- a/src/tests/basictest.cpp +++ b/src/tests/basictest.cpp @@ -2,6 +2,7 @@ #include "transport/userregistry.h" #include "transport/config.h" #include "transport/storagebackend.h" +#include "transport/userregistration.h" #include "transport/user.h" #include "transport/transport.h" #include "transport/conversation.h" @@ -46,6 +47,8 @@ void BasicTest::setMeUp (void) { factory = new TestingFactory(); + storage = new TestingStorageBackend(); + loop = new Swift::DummyEventLoop(); factories = new Swift::DummyNetworkFactories(loop); @@ -54,7 +57,10 @@ void BasicTest::setMeUp (void) { component = new Component(loop, factories, cfg, factory, userRegistry); component->start(); - userManager = new UserManager(component, userRegistry); + userManager = new UserManager(component, userRegistry, storage); + + userRegistration = new UserRegistration(component, userManager, storage); + userRegistration->start(); payloadSerializers = new Swift::FullPayloadSerializerCollection(); payloadParserFactories = new Swift::FullPayloadParserFactoryCollection(); @@ -100,6 +106,8 @@ void BasicTest::tearMeDown (void) { delete loop; delete cfg; delete parser; + delete storage; + delete userRegistration; received.clear(); receivedData.clear(); } diff --git a/src/tests/basictest.h b/src/tests/basictest.h index edc86a28..d9a36fd6 100644 --- a/src/tests/basictest.h +++ b/src/tests/basictest.h @@ -32,7 +32,9 @@ #include "transport/transport.h" #include "transport/conversation.h" #include "transport/usermanager.h" +#include "transport/userregistration.h" #include "transport/localbuddy.h" +#include "transport/storagebackend.h" #include #include @@ -81,6 +83,100 @@ class TestingFactory : public Factory { } }; +class TestingStorageBackend : public StorageBackend { + public: + bool connected; + std::map users; + std::map online_users; + long buddyid; + + TestingStorageBackend() { + buddyid = 0; + connected = false; + } + + /// connect + virtual bool connect() { + connected = true; + return true; + } + + /// createDatabase + virtual bool createDatabase() {return true;} + + /// setUser + virtual void setUser(const UserInfo &user) { + users[user.jid] = user; + } + + /// getuser + virtual bool getUser(const std::string &barejid, UserInfo &user) { + if (users.find(barejid) == users.end()) { + return false; + } + user = users[barejid]; + return true; + } + + std::string findUserByID(long id) { + for (std::map::const_iterator it = users.begin(); it != users.end(); it++) { + if (it->second.id == id) { + return it->first; + } + } + return ""; + } + + /// setUserOnline + virtual void setUserOnline(long id, bool online) { + std::string user = findUserByID(id); + if (user.empty()) { + return; + } + online_users[user] = online; + } + + /// removeUser + virtual bool removeUser(long id) { + std::string user = findUserByID(id); + if (user.empty()) { + return false; + } + users.erase(user); + return true; + } + + /// getBuddies + virtual bool getBuddies(long id, std::list &roster) { + return true; + } + + /// getOnlineUsers + virtual bool getOnlineUsers(std::vector &users) { + return true; + } + + virtual long addBuddy(long userId, const BuddyInfo &buddyInfo) { + return buddyid++; + } + virtual void updateBuddy(long userId, const BuddyInfo &buddyInfo) { + + } + + virtual void removeBuddy(long id) { + + } + + virtual void getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value) {} + virtual void updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value) {} + + virtual void getUserSetting(long userId, const std::string &variable, int &type, std::string &value) {} + virtual void updateUserSetting(long userId, const std::string &variable, const std::string &value) {} + + virtual void beginTransaction() {} + virtual void commitTransaction() {} +}; + class BasicTest : public Swift::XMPPParserClient { public: @@ -119,5 +215,7 @@ class BasicTest : public Swift::XMPPParserClient { Component *component; std::vector > received; std::string receivedData; + StorageBackend *storage; + UserRegistration *userRegistration; }; diff --git a/src/tests/userregistration.cpp b/src/tests/userregistration.cpp index d714abe2..6f8596e3 100644 --- a/src/tests/userregistration.cpp +++ b/src/tests/userregistration.cpp @@ -24,6 +24,9 @@ using namespace Transport; class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest { CPPUNIT_TEST_SUITE(UserRegistrationTest); CPPUNIT_TEST(getForm); + CPPUNIT_TEST(getFormRegistered); + CPPUNIT_TEST(registerUser); + CPPUNIT_TEST(unregisterUser); CPPUNIT_TEST_SUITE_END(); public: @@ -38,7 +41,78 @@ class UserRegistrationTest : public CPPUNIT_NS :: TestFixture, public BasicTest } void getForm() { - + boost::shared_ptr iq = Swift::IQ::createRequest(Swift::IQ::Get, Swift::JID("localhost"), "id", boost::shared_ptr(new Swift::InBandRegistrationPayload())); + iq->setFrom("user@localhost"); + injectIQ(iq); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[0]))); + CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast(getStanza(received[0]))->getType()); + + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + CPPUNIT_ASSERT_EQUAL(false, getStanza(received[0])->getPayload()->isRegistered()); + CPPUNIT_ASSERT_EQUAL(std::string(""), *getStanza(received[0])->getPayload()->getUsername()); + CPPUNIT_ASSERT_EQUAL(std::string(""), *getStanza(received[0])->getPayload()->getPassword()); + } + + void getFormRegistered() { + UserInfo user; + user.id = -1; + user.jid = "user@localhost"; + user.uin = "legacyname"; + user.password = "password"; + storage->setUser(user); + + boost::shared_ptr iq = Swift::IQ::createRequest(Swift::IQ::Get, Swift::JID("localhost"), "id", boost::shared_ptr(new Swift::InBandRegistrationPayload())); + iq->setFrom("user@localhost"); + injectIQ(iq); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(1, (int) received.size()); + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[0]))); + CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast(getStanza(received[0]))->getType()); + + CPPUNIT_ASSERT(getStanza(received[0])->getPayload()); + CPPUNIT_ASSERT_EQUAL(true, getStanza(received[0])->getPayload()->isRegistered()); + CPPUNIT_ASSERT_EQUAL(std::string("legacyname"), *getStanza(received[0])->getPayload()->getUsername()); + CPPUNIT_ASSERT_EQUAL(std::string(""), *getStanza(received[0])->getPayload()->getPassword()); + } + + void registerUser() { + Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload(); + reg->setUsername("legacyname"); + reg->setPassword("password"); + boost::shared_ptr iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr(reg)); + iq->setFrom("user@localhost"); + injectIQ(iq); + loop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(2, (int) received.size()); + + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[0]))); + CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast(getStanza(received[0]))->getType()); + + CPPUNIT_ASSERT(dynamic_cast(getStanza(received[1]))); + CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast(getStanza(received[1]))->getType()); + + UserInfo user; + CPPUNIT_ASSERT_EQUAL(true, storage->getUser("user@localhost", user)); + + CPPUNIT_ASSERT_EQUAL(std::string("legacyname"), user.uin); + CPPUNIT_ASSERT_EQUAL(std::string("password"), user.password); + } + + void unregisterUser() { + registerUser(); + received.clear(); + + Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload(); + reg->setRemove(true); + boost::shared_ptr iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr(reg)); + iq->setFrom("user@localhost"); + injectIQ(iq); + loop->processEvents(); } }; diff --git a/src/userregistration.cpp b/src/userregistration.cpp index e32507df..8691791a 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -397,8 +397,6 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID } } - printf("here\n"); - if (!payload->getUsername() || !payload->getPassword()) { sendError(from, id, ErrorPayload::NotAcceptable, ErrorPayload::Modify); return true;