Unit test for SlackRTM

This commit is contained in:
Jan Kaluza 2016-02-01 07:10:28 +01:00
parent 008827e06d
commit 4b7baa6910
7 changed files with 225 additions and 8 deletions

View file

@ -56,12 +56,6 @@ SlackRTM::SlackRTM(Component *component, StorageBackend *storageBackend, SlackId
m_storageBackend->getUserSetting(m_uinfo.id, "bot_token", type, m_token);
m_api = new SlackAPI(component, m_idManager, m_token);
std::string url = "https://slack.com/api/rtm.start?";
url += "token=" + Util::urlencode(m_token);
HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, boost::bind(&SlackRTM::handleRTMStart, this, _1, _2, _3, _4));
req->execute();
}
SlackRTM::~SlackRTM() {
@ -70,6 +64,14 @@ SlackRTM::~SlackRTM() {
m_pingTimer->stop();
}
void SlackRTM::start() {
std::string url = "https://slack.com/api/rtm.start?";
url += "token=" + Util::urlencode(m_token);
HTTPRequest *req = new HTTPRequest(THREAD_POOL(m_component), HTTPRequest::Get, url, boost::bind(&SlackRTM::handleRTMStart, this, _1, _2, _3, _4));
req->execute();
}
#define STORE_STRING(FROM, NAME) rapidjson::Value &NAME##_tmp = FROM[#NAME]; \
if (!NAME##_tmp.IsString()) { \
LOG4CXX_ERROR(logger, "No '" << #NAME << "' string in the reply."); \

View file

@ -64,6 +64,8 @@ class SlackRTM {
virtual ~SlackRTM();
void start();
void sendPing();
void sendMessage(const std::string &channel, const std::string &message);
@ -76,7 +78,9 @@ class SlackRTM {
boost::signal<void (const std::string &channel, const std::string &user, const std::string &text, const std::string &ts)> onMessageReceived;
#ifndef LIBTRANSPORT_TEST
private:
#endif
void handlePayloadReceived(const std::string &payload);
void handleRTMStart(HTTPRequest *req, bool ok, rapidjson::Document &resp, const std::string &data);
void handleWebSocketConnected();

View file

@ -56,6 +56,7 @@ SlackSession::SlackSession(Component *component, StorageBackend *storageBackend,
m_rtm = new SlackRTM(component, storageBackend, m_idManager, uinfo);
m_rtm->onRTMStarted.connect(boost::bind(&SlackSession::handleRTMStarted, this));
m_rtm->onMessageReceived.connect(boost::bind(&SlackSession::handleMessageReceived, this, _1, _2, _3, _4, false));
m_rtm->start();
m_onlineBuddiesTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20000);
m_onlineBuddiesTimer->onTick.connect(boost::bind(&SlackSession::sendOnlineBuddies, this));

View file

@ -0,0 +1,69 @@
#include "BasicSlackTest.h"
#include "XMPPFrontend.h"
#include "XMPPUserRegistration.h"
#include "XMPPUserManager.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 "Swiften/Serializer/GenericPayloadSerializer.h"
#include "storageparser.h"
#include "Swiften/Parser/PayloadParsers/AttentionParser.h"
#include "Swiften/Serializer/PayloadSerializers/AttentionSerializer.h"
#include "Swiften/Parser/PayloadParsers/XHTMLIMParser.h"
#include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h"
#include "Swiften/Parser/PayloadParsers/StatsParser.h"
#include "Swiften/Serializer/PayloadSerializers/StatsSerializer.h"
#include "Swiften/Parser/PayloadParsers/GatewayPayloadParser.h"
#include "Swiften/Serializer/PayloadSerializers/GatewayPayloadSerializer.h"
#include "Swiften/Serializer/PayloadSerializers/SpectrumErrorSerializer.h"
#include "Swiften/Parser/PayloadParsers/MUCPayloadParser.h"
#include "BlockParser.h"
#include "BlockSerializer.h"
#include "Swiften/Parser/PayloadParsers/InvisibleParser.h"
#include "Swiften/Serializer/PayloadSerializers/InvisibleSerializer.h"
using namespace Transport;
void BasicSlackTest::setMeUp (void) {
std::istringstream ifs("service.server_mode = 1\nservice.jid=localhost\nservice.more_resources=1\n");
cfg = new Config();
cfg->load(ifs);
factory = new TestingFactory();
storage = new TestingStorageBackend();
loop = new Swift::DummyEventLoop();
factories = new Swift::DummyNetworkFactories(loop);
userRegistry = new UserRegistry(cfg, factories);
frontend = new SlackFrontend();
component = new Component(frontend, loop, factories, cfg, factory, userRegistry);
component->start();
userManager = frontend->createUserManager(component, userRegistry, storage);
}
void BasicSlackTest::tearMeDown (void) {
delete component;
delete frontend;
delete userRegistry;
delete factories;
delete factory;
delete loop;
delete cfg;
delete storage;
}

View file

@ -0,0 +1,89 @@
/**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#pragma once
#include <vector>
#include "Swiften/Swiften.h"
#include "Swiften/Queries/SetResponder.h"
#include "transport/Conversation.h"
#include "transport/ConversationManager.h"
#include "transport/UserRegistry.h"
#include "transport/Config.h"
#include "transport/StorageBackend.h"
#include "transport/User.h"
#include "transport/Transport.h"
#include "transport/UserManager.h"
#include "transport/UserRegistration.h"
#include "transport/RosterManager.h"
#include "transport/NetworkPluginServer.h"
#include "RosterResponder.h"
#include "discoitemsresponder.h"
#include "transport/LocalBuddy.h"
#include "transport/StorageBackend.h"
#include "transport/Factory.h"
#include "SlackUserRegistration.h"
#include "SlackFrontend.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"
#define HAVE_SWIFTEN_3 (SWIFTEN_VERSION >= 0x030000)
#include "basictest.h"
using namespace Transport;
class BasicSlackTest : public Swift::XMPPParserClient {
public:
void setMeUp (void);
void tearMeDown (void);
void addUser() {
UserInfo user;
user.id = 1;
user.jid = "user@localhost";
user.uin = "legacyname";
user.password = "password";
user.vip = 0;
storage->setUser(user);
}
protected:
UserManager *userManager;
UserRegistry *userRegistry;
Config *cfg;
Swift::Server *server;
Swift::DummyNetworkFactories *factories;
Swift::DummyEventLoop *loop;
TestingFactory *factory;
Component *component;
StorageBackend *storage;
SlackUserRegistration *userRegistration;
Transport::SlackFrontend *frontend;
};

View file

@ -18,12 +18,14 @@ endif()
FILE(GLOB HEADERS ../../include/transport/*.h)
include_directories(../../spectrum/src/frontends/xmpp/)
include_directories(../../spectrum/src/frontends/slack/)
if (CPPUNIT_FOUND)
FILE(GLOB SRC_TEST *.cpp)
FILE(GLOB SRC_TEST_FRONTEND ../../spectrum/src/frontends/xmpp/*.cpp)
FILE(GLOB SRC_TEST_FRONTEND_XMPP ../../spectrum/src/frontends/xmpp/*.cpp)
FILE(GLOB SRC_TEST_FRONTEND_SLACK ../../spectrum/src/frontends/slack/*.cpp)
ADD_EXECUTABLE(libtransport_test ${SRC_TEST} ${SRC_TEST_FRONTEND})
ADD_EXECUTABLE(libtransport_test ${SRC_TEST} ${SRC_TEST_FRONTEND_XMPP} ${SRC_TEST_FRONTEND_SLACK})
set_target_properties(libtransport_test PROPERTIES COMPILE_DEFINITIONS LIBTRANSPORT_TEST=1)

File diff suppressed because one or more lines are too long