Changeset - 5f0dc6840701
[Not reviewed]
0 3 3
Jan Kaluza - 10 years ago 2015-12-27 16:31:00
jkaluza@redhat.com
Tests for twitter
6 files changed with 145 insertions and 6 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterPlugin.cpp
Show inline comments
 
@@ -30,99 +30,100 @@ static int cmp(std::string a, std::string b)
 
	if(a.size() < b.size()) a = std::string(diff,'0') + a;
 
	else b = std::string(diff,'0') + b;
 
	
 
	if(a == b) return 0;
 
	if(a < b) return -1;
 
	return 1;
 
}
 

	
 

	
 
TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin() 
 
{
 
	this->config = config;
 
	this->storagebackend = storagebackend;
 
	this->m_firstPing = true;
 

	
 
	if (CONFIG_HAS_KEY(config, "twitter.consumer_key") == false) {
 
		consumerKey = "5mFePMiJi0KpeURONkelg";
 
	}
 
	else {
 
		consumerKey = CONFIG_STRING(config, "twitter.consumer_key");
 
	}
 
	if (CONFIG_HAS_KEY(config, "twitter.consumer_secret") == false) {
 
		consumerSecret = "YFZCDJwRhbkccXEnaYr1waCQejTJcOY8F7l5Wim3FA";
 
	}
 
	else {
 
		consumerSecret = CONFIG_STRING(config, "twitter.consumer_secret");
 
	}
 

	
 
	if (consumerSecret.empty() || consumerKey.empty()) {
 
		LOG4CXX_ERROR(logger, "Consumer key and Consumer secret can't be empty.");
 
		exit(1);
 
	}
 

	
 
	adminLegacyName = "twitter.com"; 
 
	adminChatRoom = "#twitter"; 
 
	adminNickName = "twitter"; 
 
	adminAlias = "twitter";
 

	
 
	OAUTH_KEY = "twitter_oauth_token";
 
	OAUTH_SECRET = "twitter_oauth_secret";
 
	MODE = "mode";
 

	
 
	m_factories = new Swift::BoostNetworkFactories(loop);
 
	m_conn = m_factories->getConnectionFactory()->createConnection();
 
	m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
 
	m_conn->connect(Swift::HostAddressPort(Swift::HostAddress(host), port));
 

	
 
	tp = new ThreadPool(loop_, 10);
 
		
 
	tweet_timer = m_factories->getTimerFactory()->createTimer(90000);
 
	message_timer = m_factories->getTimerFactory()->createTimer(90000);
 

	
 
	LOG4CXX_INFO(logger, "Fetch timeout is set to " << CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
 
	tweet_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
 
	message_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
 

	
 
	tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
 
	message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
 

	
 
	tweet_timer->start();
 
	message_timer->start();
 
	
 
	LOG4CXX_INFO(logger, "Starting the plugin.");
 
}
 

	
 
TwitterPlugin::~TwitterPlugin() 
 
{
 
	delete storagebackend;
 
	std::set<std::string>::iterator it;
 
	for(it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions;
 
	delete tp;
 
}
 

	
 
// Send data to NetworkPlugin server
 
void TwitterPlugin::sendData(const std::string &string) 
 
{
 
	m_conn->write(Swift::createSafeByteArray(string));
 
}
 

	
 
// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
 
void TwitterPlugin::_handleDataRead(boost::shared_ptr<Swift::SafeByteArray> data) 
 
{
 
	if (m_firstPing) {
 
		m_firstPing = false;
 
		// Users can join the network without registering if we allow
 
		// one user to connect multiple IRC networks.
 
		NetworkPlugin::PluginConfig cfg;
 
		cfg.setNeedPassword(false);
 
		sendConfig(cfg);
 
	}
 

	
 
	std::string d(data->begin(), data->end());
 
	handleDataRead(d);
 
}
 

	
 
// User trying to login into his twitter account
 
void TwitterPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) 
 
{
 
	if(userdb.count(user) && (userdb[user].connectionState == NEW || 
 
										userdb[user].connectionState == CONNECTED || 
 
										userdb[user].connectionState == WAITING_FOR_PIN)) {
 
		LOG4CXX_INFO(logger, std::string("A session corresponding to ") + user + std::string(" is already active"))
 
		return;
libtransport/Config.cpp
Show inline comments
 
@@ -96,96 +96,97 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
 
		("service.idle_reconnect_time", value<int>()->default_value(0), "Time in seconds after which idle users are reconnected to let their backend die.")
 
		("service.memory_collector_time", value<int>()->default_value(0), "Time in seconds after which backend with most memory is set to die.")
 
		("service.more_resources", value<bool>()->default_value(false), "Allow more resources to be connected in server mode at the same time.")
 
		("service.enable_privacy_lists", value<bool>()->default_value(true), "")
 
		("service.enable_xhtml", value<bool>()->default_value(true), "")
 
		("service.max_room_list_size", value<int>()->default_value(100), "")
 
		("service.login_delay", value<int>()->default_value(0), "")
 
		("service.jid_escaping", value<bool>()->default_value(true), "")
 
		("service.vip_only", value<bool>()->default_value(false), "")
 
		("service.vip_message", value<std::string>()->default_value(""), "")
 
		("service.reconnect_all_users", value<bool>()->default_value(false), "")
 
		("vhosts.vhost", value<std::vector<std::string> >()->multitoken(), "")
 
		("identity.name", value<std::string>()->default_value("Spectrum 2 Transport"), "Name showed in service discovery.")
 
		("identity.category", value<std::string>()->default_value("gateway"), "Disco#info identity category. 'gateway' by default.")
 
		("identity.type", value<std::string>()->default_value(""), "Type of transport ('icq','msn','gg','irc', ...)")
 
		("registration.enable_public_registration", value<bool>()->default_value(true), "True if users should be able to register.")
 
		("registration.language", value<std::string>()->default_value("en"), "Default language for registration form")
 
		("registration.instructions", value<std::string>()->default_value("Enter your legacy network username and password."), "Instructions showed to user in registration form")
 
		("registration.username_label", value<std::string>()->default_value("Legacy network username:"), "Label for username field")
 
		("registration.username_mask", value<std::string>()->default_value(""), "Username mask")
 
		("registration.allowed_usernames", value<std::string>()->default_value(""), "Allowed usernames")
 
		("registration.auto_register", value<bool>()->default_value(false), "Register new user automatically when the presence arrives.")
 
		("registration.encoding", value<std::string>()->default_value("utf8"), "Default encoding in registration form")
 
		("registration.require_local_account", value<bool>()->default_value(false), "True if users have to have a local account to register to this transport from remote servers.")
 
		("registration.notify_jid", value<std::vector<std::string> >()->multitoken(), "Send message to this JID if user registers/unregisters")
 
		("registration.local_username_label", value<std::string>()->default_value("Local username:"), "Label for local usernme field")
 
		("registration.local_account_server", value<std::string>()->default_value("localhost"), "The server on which the local accounts will be checked for validity")
 
		("registration.local_account_server_timeout", value<int>()->default_value(10000), "Timeout when checking local user on local_account_server (msecs)")
 
		("gateway_responder.prompt", value<std::string>()->default_value("Contact ID"), "Value of <prompt> </promt> field")
 
		("gateway_responder.label", value<std::string>()->default_value("Enter legacy network contact ID."), "Label for add contact ID field")
 
		("database.type", value<std::string>()->default_value("none"), "Database type.")
 
		("database.database", value<std::string>()->default_value("/var/lib/spectrum2/$jid/database.sql"), "Database used to store data")
 
		("database.server", value<std::string>()->default_value("localhost"), "Database server.")
 
		("database.user", value<std::string>()->default_value(""), "Database user.")
 
		("database.password", value<std::string>()->default_value(""), "Database Password.")
 
		("database.port", value<int>()->default_value(0), "Database port.")
 
		("database.prefix", value<std::string>()->default_value(""), "Prefix of tables in database")
 
		("database.encryption_key", value<std::string>()->default_value(""), "Encryption key.")
 
		("database.vip_statement", value<std::string>()->default_value(""), "Encryption key.")
 
		("logging.config", value<std::string>()->default_value(""), "Path to log4cxx config file which is used for Spectrum 2 instance")
 
		("logging.backend_config", value<std::string>()->default_value(""), "Path to log4cxx config file which is used for backends")
 
		("backend.default_avatar", value<std::string>()->default_value(""), "Full path to default avatar")
 
		("backend.avatars_directory", value<std::string>()->default_value(""), "Path to directory with avatars")
 
		("backend.no_vcard_fetch", value<bool>()->default_value(false), "True if VCards for buddies should not be fetched. Only avatars will be forwarded.")
 
		("proxy.server", value<std::string>()->default_value("localhost"), "Proxy IP.")
 
		("proxy.user", value<std::string>()->default_value(""), "Proxy user.")
 
		("proxy.password", value<std::string>()->default_value(""), "Proxy Password.")
 
		("proxy.port", value<int>()->default_value(0), "Proxy port.")
 
		("twitter.fetch_timeout", value<int>()->default_value(0), "Proxy port.")
 

	
 
	;
 

	
 
	parsed_options parsed = parse_config_file(ifs, opts, true);
 

	
 
	bool found_working = false;
 
	bool found_pidfile = false;
 
	bool found_portfile = false;
 
	bool found_backend_port = false;
 
	bool found_database = false;
 
	std::string jid = "";
 
	BOOST_FOREACH(option &opt, parsed.options) {
 
		if (opt.string_key == "service.jid") {
 
			if (_jid.empty()) {
 
				jid = opt.value[0];
 
			}
 
			else {
 
				opt.value[0] = _jid;
 
				jid = _jid;
 
			}
 
		}
 
		else if (opt.string_key == "service.backend_port") {
 
			found_backend_port = true;
 
			if (opt.value[0] == "0") {
 
				opt.value[0] = boost::lexical_cast<std::string>(getRandomPort(_jid.empty() ? jid : _jid));
 
			}
 
		}
 
		else if (opt.string_key == "service.working_dir") {
 
			found_working = true;
 
		}
 
		else if (opt.string_key == "service.pidfile") {
 
			found_pidfile = true;
 
		}
 
		else if (opt.string_key == "service.portfile") {
 
			found_portfile = true;
 
		}
 
		else if (opt.string_key == "database.database") {
 
			found_database = true;
 
		}
 
	}
 

	
 
	if (!found_working) {
 
		std::vector<std::string> value;
 
		value.push_back("/var/lib/spectrum2/$jid");
 
		parsed.options.push_back(boost::program_options::basic_option<char>("service.working_dir", value));
 
	}
 
	if (!found_pidfile) {
 
		std::vector<std::string> value;
tests/start.py
Show inline comments
 
@@ -122,90 +122,103 @@ class LibcommuniServerModeConf(BaseTest):
 
		self.directory = "../libcommuni/"
 

	
 
	def pre_test(self):
 
		os.system("ngircd -f ../libcommuni/ngircd.conf &")
 

	
 
	def post_test(self):
 
		os.system("killall ngircd 2>/dev/null")
 
		os.system("killall spectrum2_libcommuni_backend 2>/dev/null")
 

	
 
class JabberServerModeConf(BaseTest):
 
	def __init__(self):
 
		BaseTest.__init__(self, "../libpurple_jabber/jabber_test.cfg", True, "room%conference.localhost@localhostxmpp")
 
		self.directory = "../libpurple_jabber/"
 
		self.client_jid = "client%localhost@localhostxmpp"
 
		self.responder_jid = "responder%localhost@localhostxmpp"
 

	
 
	def skip_test(self, test):
 
		if test in ["muc_whois.py", "muc_change_topic.py"]:
 
			return True
 
		return False
 

	
 
	def pre_test(self):
 
		os.system("prosody --config ../libpurple_jabber/prosody.cfg.lua >prosody.log &")
 
		time.sleep(3)
 
		os.system("../../spectrum_manager/src/spectrum2_manager -c ../libpurple_jabber/manager.conf localhostxmpp register client%localhost@localhostxmpp client@localhost password 2>/dev/null >/dev/null")
 
		os.system("../../spectrum_manager/src/spectrum2_manager -c ../libpurple_jabber/manager.conf localhostxmpp register responder%localhost@localhostxmpp responder@localhost password 2>/dev/null >/dev/null")
 

	
 
	def post_test(self):
 
		os.system("killall lua-5.1 2>/dev/null")
 
		os.system("killall spectrum2_libpurple_backend 2>/dev/null")
 

	
 
class JabberSlackServerModeConf(BaseTest):
 
	def __init__(self):
 
		BaseTest.__init__(self, "../slack_jabber/jabber_slack_test.cfg", True, "room%conference.localhost@localhostxmpp")
 
		self.directory = "../slack_jabber/"
 
		self.client_jid = "client@localhost"
 
		self.client_room = "room@conference.localhost"
 
		self.responder_jid = "owner@spectrum2tests.xmpp.slack.com"
 
		self.responder_password = "spectrum2tests.rkWHkOrjYucxsmBVkA9K"
 
		self.responder_room = "spectrum2_room@conference.spectrum2tests.xmpp.slack.com"
 
		self.responder_nick = "owner"
 
		self.responder_roompassword = "spectrum2tests.rkWHkOrjYucxsmBVkA9K"
 

	
 
	def skip_test(self, test):
 
		os.system("cp ../slack_jabber/slack.sql .")
 
		if test.find("bad_password") != -1:
 
			print "Changing password to 'badpassword'"
 
			os.system("sqlite3 slack.sql \"UPDATE users SET password='badpassword' WHERE id=1\"")
 
			#os.system("sqlite3 slack.sql \"SELECT * FROM users\"")
 
		return False
 

	
 
	def pre_test(self):
 
		os.system("prosody --config ../slack_jabber/prosody.cfg.lua > prosody.log &")
 
		#time.sleep(3)
 
		#os.system("../../../spectrum_manager/src/spectrum2_manager -c manager.conf localhostxmpp set_oauth2_code xoxb-17213576196-VV2K8kEwwrJhJFfs5YWv6La6 use_bot_token 2>/dev/null >/dev/null")
 

	
 
	def post_test(self):
 
		os.system("killall lua-5.1 2>/dev/null")
 
		os.system("killall spectrum2_libpurple_backend 2>/dev/null")
 

	
 
class TwitterServerModeConf(BaseTest):
 
	def __init__(self):
 
		BaseTest.__init__(self, "../twitter/twitter_test.cfg", True, "")
 
		self.directory = "../twitter/"
 
		self.client_password = "testpass123"
 

	
 
	def skip_test(self, test):
 
		os.system("cp ../twitter/twitter.sql .")
 

	
 
	def pre_test(self):
 
		pass
 

	
 
	def post_test(self):
 
		os.system("killall spectrum2_twitter_backend 2>/dev/null")
 

	
 
configurations = []
 
configurations.append(LibcommuniServerModeSingleServerConf())
 
configurations.append(LibcommuniServerModeConf())
 
configurations.append(JabberServerModeConf())
 
configurations.append(JabberSlackServerModeConf())
 
configurations.append(TwitterServerModeConf())
 

	
 
exitcode = 0
 

	
 
for conf in configurations:
 
	for f in os.listdir(conf.directory):
 
		if not f.endswith(".py") or f == "start.py":
 
			continue
 

	
 
		if len(sys.argv) == 2 and sys.argv[1] != f:
 
			continue
 

	
 
		print conf.__class__.__name__ + ": Starting " + f + " test ..."
 
		test = imp.load_source('test', conf.directory + f)
 
		if conf.skip_test(f):
 
			print "Skipped."
 
			continue
 
		#try:
 
		ret = conf.start(test.Client, test.Responder)
 
		#except:
 
			#ret = False
 
		if not ret:
 
			exitcode = -1
 

	
 
sys.exit(exitcode)
 

	
tests/twitter/twitter.sql
Show inline comments
 
new file 100644
 
binary diff not shown
tests/twitter/twitter_test.cfg
Show inline comments
 
new file 100644
 
[service]
 
jid = localhostxmpp
 
password = secret
 
server = 0.0.0.0
 
port = 5223
 
server_mode = 1
 
backend_host=127.0.0.1
 
pidfile=./test.pid
 
# < this option doesn't work yet
 
#backend_port=10001
 
admin_jid=admin
 
admin_password=test
 
#cert=server.pfx #patch to PKCS#12 certificate
 
#cert_password=test #password to that certificate if any
 
users_per_backend=10
 
#backend=../..//backends/swiften/spectrum2_swiften_backend
 
#backend=../../../backends/twitter/spectrum2_twitter_backend
 
backend=../../backends/twitter/spectrum2_twitter_backend
 
protocol=prpl-jabber
 
#protocol=prpl-msn
 
#protocol=any
 
#protocol=prpl-icq
 
working_dir=./
 
portfile=./$jid.port
 
irc_server=localhost
 

	
 
[twitter]
 
fetch_timeout=5000
 

	
 
[backend]
 
#default_avatar=catmelonhead.jpg
 
#no_vcard_fetch=true
 

	
 
[logging]
 
#config=logging.cfg # log4cxx/log4j logging configuration file
 
#backend_config=/home/hanzz/code/libtransport/spectrum/src/backend-logging.cfg # log4cxx/log4j logging configuration file for backends
 

	
 
[database]
 
type=sqlite3
 
database=twitter.sql
 
prefix=
 
#type = mysql # or "none" without database backend.......................................................................................................................
 
#database = test
 
#prefix=
 
#user=root
 
#password=yourrootsqlpassword
 
#encryption_key=hanzzik
tests/twitter/twitter_test.py
Show inline comments
 
new file 100644
 
import optparse
 
import sys
 
import time
 
import subprocess
 
import os
 

	
 
import sleekxmpp
 

	
 

	
 
class Responder(sleekxmpp.ClientXMPP):
 
	def __init__(self, jid, password, room, room_password, nick):
 
		sleekxmpp.ClientXMPP.__init__(self, jid, password)
 
		self.room = room
 
		self.room_password = room_password
 
		self.nick = nick
 
		self.finished = False
 

	
 
		self.tests = {}
 

	
 

	
 
class Client(sleekxmpp.ClientXMPP):
 
	def __init__(self, jid, password, room, nick):
 
		sleekxmpp.ClientXMPP.__init__(self, jid, password)
 
		self.room = room
 
		self.nick = nick
 
		self.add_event_handler("session_start", self.start)
 
		self.add_event_handler("message", self.message)
 
		self.finished = False
 

	
 
		self.tests = {}
 
		self.tests["timeline_poll"] = ["Timeline received automatically", False]
 
		self.tests["help"] = ["#help command", False]
 
		self.tests["status1"] = ["#status command response", False]
 
		self.tests["status2"] = ["#status command pushes status", False]
 
		self.tests["follow"] = ["#follow command", False]
 
		self.tests["friends"] = ["#friends command", False]
 
		self.tests["unfollow"] = ["#unfollow command", False]
 
		self.tests["friends2"] = ["#friends after unfollow command", False]
 

	
 
		self.status = "timeline"
 
		self.timestamp = int(time.time())
 

	
 
	def message(self, msg):
 
		if self.status == "timeline" and msg['body'].find("spectrum2tests") != -1 and msg['body'].find("MsgId") != -1:
 
			self.tests["timeline_poll"][1] = True
 
			self.send_message(mto=msg['from'], mbody="#help")
 
			self.status = "help"
 
		elif self.status == "help" and msg['body'].find("You will receive tweets of people you follow") != -1:
 
			self.tests["help"][1] = True
 
			self.send_message(mto=msg['from'], mbody="#status My testing status " + str(self.timestamp))
 
			self.status = "status"
 
		elif self.status == "status" and msg['body'].find("Status Update successful") != -1:
 
			self.tests["status1"][1] = True
 
		elif self.status == "status" and msg['body'].find("spectrum2tests") != -1 and msg['body'].find("MsgId") != -1:
 
			self.tests["status2"][1] = True
 
			self.status = "follow"
 
			self.send_message(mto=msg['from'], mbody="#follow colinpwheeler")
 
		elif self.status == "follow" and msg['body'] == "You are now following colinpwheeler":
 
			self.status = "friends"
 
			self.tests["follow"][1] = True
 
			self.send_message(mto=msg['from'], mbody="#friends")
 
		elif self.status == "friends" and msg['body'].find("USER LIST") != -1 and msg['body'].find("colinpwheeler") != -1:
 
			self.status = "unfollow"
 
			self.tests["friends"][1] = True
 
			self.send_message(mto=msg['from'], mbody="#unfollow colinpwheeler")
 
		elif self.status == "unfollow" and msg['body'] == "You are not following colinpwheeler anymore":
 
			self.status = "friends2"
 
			self.tests["unfollow"][1] = True
 
			self.send_message(mto=msg['from'], mbody="#friends")
 
		elif self.status == "friends2" and msg['body'].find("USER LIST") != -1 and msg['body'].find("colinpwheeler") == -1:
 
			self.tests["friends2"][1] = True
 
			self.finished = True
 

	
 
	def start(self, event):
 
		self.getRoster()
 
		self.sendPresence()
 

	
0 comments (0 inline, 0 general)