Files @ 3aff761db678
Branch filter:

Location: libtransport.git/src/tests/usermanager.cpp - annotation

Jan Kaluza
Do not send subscribe presence just because, but send it as response to probe presence
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
225a246407f7
848f672b07e5
225a246407f7
225a246407f7
225a246407f7
848f672b07e5
225a246407f7
225a246407f7
2f542b507719
a28e64cfe426
cb6af593b6a6
cb6af593b6a6
25642926dd9d
25642926dd9d
225a246407f7
225a246407f7
225a246407f7
225a246407f7
848f672b07e5
225a246407f7
225a246407f7
225a246407f7
848f672b07e5
225a246407f7
225a246407f7
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
a28e64cfe426
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
cb6af593b6a6
25642926dd9d
25642926dd9d
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
25642926dd9d
25642926dd9d
25642926dd9d
25642926dd9d
25642926dd9d
25642926dd9d
25642926dd9d
dc1cc4170e4a
25642926dd9d
9b03ad3f309f
9b03ad3f309f
9b03ad3f309f
9b03ad3f309f
4055180802ab
4055180802ab
4055180802ab
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
4055180802ab
4055180802ab
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
3aff761db678
25642926dd9d
225a246407f7
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
2f542b507719
225a246407f7
225a246407f7
225a246407f7
#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);