More tests

This commit is contained in:
Jan Kaluza 2012-08-14 12:53:14 +02:00
parent 1972f003a5
commit 594c1eaa2b
4 changed files with 182 additions and 4 deletions

View file

@ -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();
}

View file

@ -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 <Swiften/Swiften.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
@ -81,6 +83,100 @@ class TestingFactory : public Factory {
}
};
class TestingStorageBackend : public StorageBackend {
public:
bool connected;
std::map<std::string, UserInfo> users;
std::map<std::string, bool> 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<std::string, UserInfo>::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<BuddyInfo> &roster) {
return true;
}
/// getOnlineUsers
virtual bool getOnlineUsers(std::vector<std::string> &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<boost::shared_ptr<Swift::Element> > received;
std::string receivedData;
StorageBackend *storage;
UserRegistration *userRegistration;
};

View file

@ -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<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Get, Swift::JID("localhost"), "id", boost::shared_ptr<Swift::Payload>(new Swift::InBandRegistrationPayload()));
iq->setFrom("user@localhost");
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>());
CPPUNIT_ASSERT_EQUAL(false, getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>()->isRegistered());
CPPUNIT_ASSERT_EQUAL(std::string(""), *getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>()->getUsername());
CPPUNIT_ASSERT_EQUAL(std::string(""), *getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>()->getPassword());
}
void getFormRegistered() {
UserInfo user;
user.id = -1;
user.jid = "user@localhost";
user.uin = "legacyname";
user.password = "password";
storage->setUser(user);
boost::shared_ptr<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Get, Swift::JID("localhost"), "id", boost::shared_ptr<Swift::Payload>(new Swift::InBandRegistrationPayload()));
iq->setFrom("user@localhost");
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>());
CPPUNIT_ASSERT_EQUAL(true, getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>()->isRegistered());
CPPUNIT_ASSERT_EQUAL(std::string("legacyname"), *getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>()->getUsername());
CPPUNIT_ASSERT_EQUAL(std::string(""), *getStanza(received[0])->getPayload<Swift::InBandRegistrationPayload>()->getPassword());
}
void registerUser() {
Swift::InBandRegistrationPayload *reg = new Swift::InBandRegistrationPayload();
reg->setUsername("legacyname");
reg->setPassword("password");
boost::shared_ptr<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr<Swift::Payload>(reg));
iq->setFrom("user@localhost");
injectIQ(iq);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received[0])));
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, dynamic_cast<Swift::Presence *>(getStanza(received[0]))->getType());
CPPUNIT_ASSERT(dynamic_cast<Swift::IQ *>(getStanza(received[1])));
CPPUNIT_ASSERT_EQUAL(Swift::IQ::Result, dynamic_cast<Swift::IQ *>(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<Swift::IQ> iq = Swift::IQ::createRequest(Swift::IQ::Set, Swift::JID("localhost"), "id", boost::shared_ptr<Swift::Payload>(reg));
iq->setFrom("user@localhost");
injectIQ(iq);
loop->processEvents();
}
};

View file

@ -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;