Fix handling of connection error to 3rd-party network from Slack and add test for that.
This commit is contained in:
parent
512571a406
commit
deba434ac0
7 changed files with 108 additions and 9 deletions
|
@ -46,7 +46,7 @@ namespace Transport {
|
|||
|
||||
DEFINE_LOGGER(logger, "SlackSession");
|
||||
|
||||
SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo), m_user(NULL) {
|
||||
SlackSession::SlackSession(Component *component, StorageBackend *storageBackend, UserInfo uinfo) : m_uinfo(uinfo), m_user(NULL), m_disconnected(false) {
|
||||
m_component = component;
|
||||
m_storageBackend = storageBackend;
|
||||
|
||||
|
@ -355,6 +355,22 @@ void SlackSession::handleMessageReceived(const std::string &channel, const std::
|
|||
}
|
||||
}
|
||||
|
||||
void SlackSession::handleDisconnected() {
|
||||
m_disconnected = true;
|
||||
}
|
||||
|
||||
void SlackSession::setUser(User *user) {
|
||||
m_user = user;
|
||||
}
|
||||
|
||||
|
||||
void SlackSession::handleConnected() {
|
||||
if (m_disconnected) {
|
||||
m_rtm->getAPI()->imOpen(m_ownerId, boost::bind(&SlackSession::handleImOpen, this, _1, _2, _3, _4));
|
||||
m_disconnected = false;
|
||||
}
|
||||
}
|
||||
|
||||
void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data) {
|
||||
m_ownerChannel = m_rtm->getAPI()->getChannelId(req, ok, resp, data);
|
||||
LOG4CXX_INFO(logger, "Opened channel with team owner: " << m_ownerChannel);
|
||||
|
@ -418,17 +434,16 @@ void SlackSession::handleImOpen(HTTPRequest *req, bool ok, rapidjson::Document &
|
|||
}
|
||||
|
||||
void SlackSession::handleRTMStarted() {
|
||||
std::string ownerId;
|
||||
std::map<std::string, SlackUserInfo> &users = m_rtm->getUsers();
|
||||
for (std::map<std::string, SlackUserInfo>::iterator it = users.begin(); it != users.end(); it++) {
|
||||
SlackUserInfo &info = it->second;
|
||||
if (info.isPrimaryOwner) {
|
||||
ownerId = it->first;
|
||||
m_ownerId = it->first;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_rtm->getAPI()->imOpen(ownerId, boost::bind(&SlackSession::handleImOpen, this, _1, _2, _3, _4));
|
||||
m_rtm->getAPI()->imOpen(m_ownerId, boost::bind(&SlackSession::handleImOpen, this, _1, _2, _3, _4));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -55,9 +55,10 @@ class SlackSession {
|
|||
|
||||
void setPurpose(const std::string &purpose, const std::string &channel = "");
|
||||
|
||||
void setUser(User *user) {
|
||||
m_user = user;
|
||||
}
|
||||
void setUser(User *user);
|
||||
|
||||
void handleDisconnected();
|
||||
void handleConnected();
|
||||
|
||||
private:
|
||||
void handleRTMStarted();
|
||||
|
@ -83,6 +84,8 @@ class SlackSession {
|
|||
User *m_user;
|
||||
Swift::Timer::ref m_onlineBuddiesTimer;
|
||||
std::map<std::string, std::string> m_onlineBuddies;
|
||||
bool m_disconnected;
|
||||
std::string m_ownerId;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -65,6 +65,8 @@ void SlackUser::disconnectUser(const std::string &error, Swift::SpectrumErrorPay
|
|||
else {
|
||||
m_session->sendMessageToAll("Disconnected from 3rd-party network for unknown reason.");
|
||||
}
|
||||
|
||||
m_session->handleDisconnected();
|
||||
m_session->sendMessageToAll("Try using ```.spectrum2 reconnect``` to reconnect.");
|
||||
static_cast<SlackUserManager *>(m_userManager)->moveTempSession(m_jid.toString(), m_session);
|
||||
m_session = NULL;
|
||||
|
|
|
@ -37,6 +37,8 @@ SlackUserManager::SlackUserManager(Component *component, UserRegistry *userRegis
|
|||
m_component = component;
|
||||
m_storageBackend = storageBackend;
|
||||
m_userRegistration = new SlackUserRegistration(component, this, storageBackend);
|
||||
|
||||
onUserCreated.connect(boost::bind(&SlackUserManager::handleUserCreated, this, _1));
|
||||
}
|
||||
|
||||
SlackUserManager::~SlackUserManager() {
|
||||
|
@ -79,6 +81,7 @@ SlackSession *SlackUserManager::moveTempSession(const std::string &user) {
|
|||
|
||||
void SlackUserManager::moveTempSession(const std::string &user, SlackSession *session) {
|
||||
m_tempSessions[user] = session;
|
||||
session->setUser(NULL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -94,5 +97,10 @@ std::string SlackUserManager::getOAuth2URL(const std::vector<std::string> &args)
|
|||
return static_cast<SlackUserRegistration *>(m_userRegistration)->createOAuth2URL(args);
|
||||
}
|
||||
|
||||
void SlackUserManager::handleUserCreated(User *user) {
|
||||
LOG4CXX_INFO(logger, "handleUserCreated");
|
||||
static_cast<SlackUser *>(user)->getSession()->handleConnected();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -62,6 +62,9 @@ class SlackUserManager : public UserManager {
|
|||
SlackSession *moveTempSession(const std::string &user);
|
||||
void moveTempSession(const std::string &user, SlackSession *session);
|
||||
|
||||
private:
|
||||
void handleUserCreated(User *user);
|
||||
|
||||
private:
|
||||
Component *m_component;
|
||||
UserRegistration *m_userRegistration;
|
||||
|
|
66
tests/slack_jabber/bad_password.py
Normal file
66
tests/slack_jabber/bad_password.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
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.add_event_handler("session_start", self.start)
|
||||
self.add_event_handler("message", self.message)
|
||||
|
||||
self.tests = {}
|
||||
self.tests["not_authorized"] = ["'Not Authorized' received", False]
|
||||
self.tests["help_received"] = ["Help received", False]
|
||||
self.tests["register_received"] = ["Password changed", False]
|
||||
self.tests["abc_received"] = ["Test message received", False]
|
||||
|
||||
def message(self, msg):
|
||||
if msg['body'] == "Not Authorized":
|
||||
self.tests["not_authorized"][1] = True
|
||||
elif msg['body'].find("Try using") != -1:
|
||||
self.send_message(mto="spectrum2@spectrum2tests.xmpp.slack.com", mbody=".spectrum2 register client@localhost password #spectrum2_contactlist")
|
||||
self.tests["help_received"][1] = True
|
||||
elif msg['body'] == "You have successfully registered 3rd-party account. Spectrum 2 is now connecting to the 3rd-party network.":
|
||||
self.tests["register_received"][1] = True
|
||||
elif msg['body'] == "abc":
|
||||
self.tests["abc_received"][1] = True
|
||||
self.finished = True
|
||||
|
||||
def start(self, event):
|
||||
self.plugin['xep_0045'].joinMUC(self.room, self.nick, password=self.room_password, wait=False)
|
||||
|
||||
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 = {}
|
||||
|
||||
def message(self, msg):
|
||||
pass
|
||||
#print "client", msg['body']
|
||||
#if msg['body'] == "echo abc" and msg['from'] == self.room + "/responder":
|
||||
#self.tests["echo1_received"][1] = True
|
||||
#self.send_message(mto=self.room + "/responder", mbody="def", mtype='chat')
|
||||
#elif msg['body'] == "echo def" and msg['from'] == self.room + "/responder":
|
||||
#self.tests["echo2_received"][1] = True
|
||||
#self.finished = True
|
||||
|
||||
def start(self, event):
|
||||
self.getRoster()
|
||||
self.sendPresence()
|
||||
self.plugin['xep_0045'].joinMUC(self.room, self.nick, wait=True)
|
||||
self.send_message(mto=self.room, mbody="abc", mtype='groupchat')
|
|
@ -164,8 +164,10 @@ class JabberSlackServerModeConf(BaseTest):
|
|||
|
||||
def skip_test(self, test):
|
||||
os.system("cp ../slack_jabber/slack.sql .")
|
||||
if test in ["muc_whois.py", "muc_change_topic.py", "muc_join_leave.py", "muc_pm.py"]:
|
||||
return True
|
||||
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):
|
||||
|
|
Loading…
Add table
Reference in a new issue