Files
@ 3aff761db678
Branch filter:
Location: libtransport.git/src/tests/usermanager.cpp
3aff761db678
5.8 KiB
text/x-c++hdr
Do not send subscribe presence just because, but send it as response to probe presence
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 | #include "transport/userregistry.h"
#include "transport/config.h"
#include "transport/storagebackend.h"
#include "transport/user.h"
#include "transport/transport.h"
#include "transport/conversation.h"
#include "transport/usermanager.h"
#include "transport/localbuddy.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Swiften/Swiften.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Server/Server.h>
#include <Swiften/Network/DummyNetworkFactories.h>
#include <Swiften/Network/DummyConnectionServer.h>
#include "Swiften/Server/ServerStanzaChannel.h"
#include "Swiften/Server/ServerFromClientSession.h"
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
#include "basictest.h"
using namespace Transport;
class UserManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTest {
CPPUNIT_TEST_SUITE(UserManagerTest);
CPPUNIT_TEST(connectUser);
CPPUNIT_TEST(connectTwoResources);
CPPUNIT_TEST(connectUserTransportDisabled);
CPPUNIT_TEST(connectUserRegistrationNeeded);
CPPUNIT_TEST(connectUserRegistrationNeededRegistered);
CPPUNIT_TEST(handleProbePresence);
CPPUNIT_TEST(disconnectUser);
CPPUNIT_TEST_SUITE_END();
public:
void setUp (void) {
setMeUp();
}
void tearDown (void) {
tearMeDown();
}
void connectUserTransportDisabled() {
addUser();
storage->updateUserSetting(1, "enable_transport", "0");
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password"));
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
User *user = userManager->getUser("user@localhost");
CPPUNIT_ASSERT(!user);
}
void connectUserRegistrationNeeded() {
cfg->updateBackendConfig("[registration]\nneedRegistration=1\n");
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password"));
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
CPPUNIT_ASSERT(streamEnded);
}
void connectUserRegistrationNeededRegistered() {
addUser();
cfg->updateBackendConfig("[registration]\nneedRegistration=1\n");
CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount());
userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password"));
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, userManager->getUserCount());
CPPUNIT_ASSERT(!streamEnded);
}
void handleProbePresence() {
UserInfo info;
info.id = 1;
info.jid = "user@localhost";
storage->setUser(info);
Swift::Presence::ref response = Swift::Presence::create();
response->setTo("localhost");
response->setFrom("user@localhost/resource");
response->setType(Swift::Presence::Probe);
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(3, (int) received.size());
CPPUNIT_ASSERT(getStanza(received[0])->getPayload<Swift::DiscoInfo>());
Swift::Presence *presence = dynamic_cast<Swift::Presence *>(getStanza(received[1]));
CPPUNIT_ASSERT(presence);
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Unavailable, presence->getType());
presence = dynamic_cast<Swift::Presence *>(getStanza(received[2]));
CPPUNIT_ASSERT(presence);
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Probe, presence->getType());
received.clear();
response = Swift::Presence::create();
response->setTo("localhost");
response->setFrom("user@localhost");
response->setType(Swift::Presence::Unsubscribed);
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(2, (int) received.size());
presence = dynamic_cast<Swift::Presence *>(getStanza(received[1]));
CPPUNIT_ASSERT(presence);
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, presence->getType());
received.clear();
response = Swift::Presence::create();
response->setTo("localhost");
response->setFrom("user@localhost");
response->setType(Swift::Presence::Error);
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
presence = dynamic_cast<Swift::Presence *>(getStanza(received[0]));
CPPUNIT_ASSERT(presence);
CPPUNIT_ASSERT_EQUAL(Swift::Presence::Subscribe, presence->getType());
storage->removeUser(1);
received.clear();
response = Swift::Presence::create();
response->setTo("localhost");
response->setFrom("user@localhost");
response->setType(Swift::Presence::Unsubscribed);
dynamic_cast<Swift::ServerStanzaChannel *>(component->getStanzaChannel())->onPresenceReceived(response);
loop->processEvents();
CPPUNIT_ASSERT_EQUAL(1, (int) received.size());
}
void connectTwoResources() {
connectUser();
add2Buddies();
connectSecondResource();
// we should get presences
CPPUNIT_ASSERT_EQUAL(4, (int) received2.size());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received2[2])));
CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received2[2]))->getShow());
CPPUNIT_ASSERT_EQUAL(std::string("status1"), dynamic_cast<Swift::Presence *>(getStanza(received2[2]))->getStatus());
CPPUNIT_ASSERT(dynamic_cast<Swift::Presence *>(getStanza(received2[3])));
CPPUNIT_ASSERT_EQUAL(Swift::StatusShow::Away, dynamic_cast<Swift::Presence *>(getStanza(received2[3]))->getShow());
CPPUNIT_ASSERT_EQUAL(std::string("status2"), dynamic_cast<Swift::Presence *>(getStanza(received2[3]))->getStatus());
}
};
CPPUNIT_TEST_SUITE_REGISTRATION (UserManagerTest);
|