diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 5a22749f..f33f9af5 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,86 +1,87 @@ add_definitions( -DRUQOLA_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") macro(add_ruqola_test _source) set( _test ${_source}) get_filename_component( _name ${_source} NAME_WE ) add_executable( ${_name} ${_test} ) add_test(NAME ${_name} COMMAND ${_name} ) ecm_mark_as_test(${_name}) target_link_libraries( ${_name} Qt5::Test libruqolacore) endmacro() add_ruqola_test(rocketchatmessagetest.cpp) add_ruqola_test(roommodeltest.cpp) add_ruqola_test(messagemodeltest.cpp) add_ruqola_test(typingnotificationtest.cpp) add_ruqola_test(utilstest.cpp) add_ruqola_test(usertest.cpp) add_ruqola_test(messageattachmenttest.cpp) add_ruqola_test(restapiutiltest.cpp) add_ruqola_test(rocketchataccountsettingstest.cpp) add_ruqola_test(messagetest.cpp) add_ruqola_test(messageurltest.cpp) add_ruqola_test(roomtest.cpp) add_ruqola_test(ruqolaserverconfigtest.cpp) add_ruqola_test(statusmodeltest.cpp) add_ruqola_test(rocketchatcachetest.cpp) add_ruqola_test(textconvertertest.cpp) add_ruqola_test(loadrecenthistorymanagertest.cpp) add_ruqola_test(notificationtest.cpp) add_ruqola_test(emojitest.cpp) add_ruqola_test(emojimanagertest.cpp) add_ruqola_test(otrtest.cpp) add_ruqola_test(otrmanagertest.cpp) add_ruqola_test(rocketchataccounttest.cpp) add_ruqola_test(usersmodeltest.cpp) add_ruqola_test(usersforroommodeltest.cpp) add_ruqola_test(filetest.cpp) add_ruqola_test(filesforroommodeltest.cpp) add_ruqola_test(filesforroomfilterproxymodeltest.cpp) add_ruqola_test(channeltest.cpp) add_ruqola_test(usersforroomfilterproxymodeltest.cpp) add_ruqola_test(usercompletermodeltest.cpp) add_ruqola_test(roomfilterproxymodeltest.cpp) add_ruqola_test(usercompleterfilterproxymodeltest.cpp) add_ruqola_test(searchchannelmodeltest.cpp) add_ruqola_test(searchchannelfilterproxymodeltest.cpp) add_ruqola_test(inputcompletermodeltest.cpp) add_ruqola_test(loginmethodmodeltest.cpp) add_ruqola_test(inputtextmanagertest.cpp) add_ruqola_test(clipboardproxytest.cpp) add_ruqola_test(authenticationinfotest.cpp) add_ruqola_test(searchmessagemodeltest.cpp) add_ruqola_test(searchmessagefilterproxymodeltest.cpp) add_ruqola_test(searchmessagetest.cpp) add_ruqola_test(accountmanagertest.cpp) add_ruqola_test(rocketchataccountmodeltest.cpp) add_ruqola_test(ruqolatest.cpp) add_ruqola_test(managerdatapathstest.cpp) add_ruqola_test(rocketchataccountfilterproxymodeltest.cpp) add_ruqola_test(roomwrappertest.cpp) add_ruqola_test(notificationoptionstest.cpp) add_ruqola_test(reactionstest.cpp) add_ruqola_test(reactiontest.cpp) #RESTAPI add_ruqola_test(restapimethodtest.cpp) add_ruqola_test(serverinfojobtest.cpp) add_ruqola_test(uploadfilejobtest.cpp) add_ruqola_test(owninfojobtest.cpp) add_ruqola_test(getavatarjobtest.cpp) add_ruqola_test(logoutjobtest.cpp) add_ruqola_test(loginjobtest.cpp) add_ruqola_test(privateinfojobtest.cpp) add_ruqola_test(channellistjobtest.cpp) add_ruqola_test(starmessagejobtest.cpp) add_ruqola_test(downloadfilejobtest.cpp) add_ruqola_test(pinmessagejobtest.cpp) add_ruqola_test(postmessagejobtest.cpp) add_ruqola_test(changechanneltopicjobtest.cpp) add_ruqola_test(changechannelannouncementjobtest.cpp) add_ruqola_test(changegroupstopicjobtest.cpp) add_ruqola_test(changegroupsannouncementjobtest.cpp) add_ruqola_test(changechannelnamejobtest.cpp) add_ruqola_test(changegroupsnamejobtest.cpp) add_ruqola_test(deletemessagejobtest.cpp) add_ruqola_test(createchanneljobtest.cpp) +add_ruqola_test(creategroupsjobtest.cpp) add_subdirectory(qml) diff --git a/autotests/creategroupsjobtest.cpp b/autotests/creategroupsjobtest.cpp new file mode 100644 index 00000000..5b4c3c01 --- /dev/null +++ b/autotests/creategroupsjobtest.cpp @@ -0,0 +1,96 @@ +/* + Copyright (c) 2018 Montel Laurent + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2 of the License or + ( at your option ) version 3 or, at the discretion of KDE e.V. + ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "creategroupsjobtest.h" +#include "restapi/groups/creategroupsjob.h" +#include "ruqola_restapi_helper.h" +#include +#include +QTEST_GUILESS_MAIN(CreateGroupsJobTest) + +CreateGroupsJobTest::CreateGroupsJobTest(QObject *parent) + : QObject(parent) +{ +} + +void CreateGroupsJobTest::shouldHaveDefaultValue() +{ + CreateGroupsJob job; + verifyDefaultValue(&job); + QVERIFY(job.requireHttpAuthentication()); + QVERIFY(!job.readOnly()); + QVERIFY(job.channelName().isEmpty()); + QVERIFY(job.members().isEmpty()); +} + +void CreateGroupsJobTest::shouldGenerateRequest() +{ + CreateGroupsJob job; + QNetworkRequest request = QNetworkRequest(QUrl()); + verifyAuthentication(&job, request); + QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/groups.create"))); + QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json")); +} + +void CreateGroupsJobTest::shouldGenerateJson() +{ + CreateGroupsJob job; + const QString channelname = QStringLiteral("foo1"); + job.setChannelName(channelname); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"name\":\"%1\"}").arg(channelname).toLatin1()); + + bool readOnly = false; + job.setReadOnly(readOnly); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"name\":\"%1\"}").arg(channelname).toLatin1()); + + readOnly = true; + job.setReadOnly(readOnly); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"name\":\"%1\",\"readOnly\":true}").arg(channelname).toLatin1()); + + const QStringList members = {QStringLiteral("foo"), QStringLiteral("bla")}; + job.setMembers(members); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"members\":[\"foo\",\"bla\"],\"name\":\"%1\",\"readOnly\":true}").arg(channelname).toLatin1()); + +} + +void CreateGroupsJobTest::shouldNotStarting() +{ + CreateGroupsJob job; + + RestApiMethod *method = new RestApiMethod; + method->setServerUrl(QStringLiteral("http://www.kde.org")); + job.setRestApiMethod(method); + + QNetworkAccessManager *mNetworkAccessManager = new QNetworkAccessManager; + job.setNetworkAccessManager(mNetworkAccessManager); + QVERIFY(!job.canStart()); + const QString auth = QStringLiteral("foo"); + const QString userId = QStringLiteral("foo"); + job.setAuthToken(auth); + QVERIFY(!job.canStart()); + job.setUserId(userId); + QVERIFY(!job.canStart()); + const QString roomId = QStringLiteral("foo1"); + job.setChannelName(roomId); + QVERIFY(job.canStart()); + + delete method; + delete mNetworkAccessManager; +} diff --git a/autotests/creategroupsjobtest.h b/autotests/creategroupsjobtest.h new file mode 100644 index 00000000..a1635a15 --- /dev/null +++ b/autotests/creategroupsjobtest.h @@ -0,0 +1,39 @@ +/* + Copyright (c) 2018 Montel Laurent + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2 of the License or + ( at your option ) version 3 or, at the discretion of KDE e.V. + ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef CREATEGROUPSJOBTEST_H +#define CREATEGROUPSJOBTEST_H + +#include + +class CreateGroupsJobTest : public QObject +{ + Q_OBJECT +public: + explicit CreateGroupsJobTest(QObject *parent = nullptr); + ~CreateGroupsJobTest() = default; +private Q_SLOTS: + void shouldHaveDefaultValue(); + void shouldGenerateRequest(); + void shouldGenerateJson(); + void shouldNotStarting(); +}; + +#endif // CREATEGROUPSJOBTEST_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6d806c7b..04e90974 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,189 +1,190 @@ set (Ruqola_restapi_SRCS restapi/restapirequest.cpp restapi/restapiutil.cpp restapi/restapimethod.cpp restapi/uploadfilejob.cpp restapi/serverinfojob.cpp restapi/restapiabstractjob.cpp restapi/misc/owninfojob.cpp restapi/getavatarjob.cpp restapi/logoutjob.cpp restapi/loginjob.cpp restapi/privateinfojob.cpp restapi/channellistjob.cpp restapi/downloadfilejob.cpp restapi/chat/starmessagejob.cpp restapi/chat/pinmessagejob.cpp restapi/chat/postmessagejob.cpp restapi/chat/deletemessagejob.cpp restapi/channels/changechanneltopicjob.cpp restapi/channels/changechannelannouncementjob.cpp restapi/channels/changechannelnamejob.cpp restapi/channels/createchanneljob.cpp restapi/groups/changegroupstopicjob.cpp restapi/groups/changegroupsannouncementjob.cpp restapi/groups/changegroupsnamejob.cpp + restapi/groups/creategroupsjob.cpp ) set(Ruqola_ddpapi_SRCS ddpapi/ddpclient.cpp ) set (Ruqola_model_core_srcs model/messagemodel.cpp model/roommodel.cpp model/roomfilterproxymodel.cpp model/usersforroommodel.cpp model/usersforroomfilterproxymodel.cpp model/usersmodel.cpp model/usercompleterfilterproxymodel.cpp model/usercompletermodel.cpp model/statusmodel.cpp model/filesforroommodel.cpp model/filesforroomfilterproxymodel.cpp model/searchchannelmodel.cpp model/searchchannelfilterproxymodel.cpp model/inputcompletermodel.cpp model/loginmethodmodel.cpp model/searchmessagemodel.cpp model/searchmessagefilterproxymodel.cpp model/rocketchataccountmodel.cpp model/rocketchataccountfilterproxymodel.cpp ) set(Ruqola_plugins_srcs plugins/pluginauthenticationinterface.cpp plugins/pluginauthentication.cpp ) set(Ruqola_aboutdata_srcs aboutdata/qmlaboutdata.cpp aboutdata/qmlaboutdataauthormodel.cpp ) set (Ruqola_core_srcs ${Ruqola_model_core_srcs} ${Ruqola_plugins_srcs} ${Ruqola_aboutdata_srcs} ${Ruqola_ddpapi_SRCS} ${Ruqola_restapi_SRCS} authenticationinfo.cpp message.cpp room.cpp roomwrapper.cpp ruqola.cpp ruqolautils.cpp rocketchatbackend.cpp notification.cpp messagequeue.cpp rocketchatmessage.cpp typingnotification.cpp changetemporarystatus.cpp user.cpp utils.cpp emoji.cpp emojimanager.cpp clipboardproxy.cpp otr.cpp otrmanager.cpp abstractwebsocket.cpp ruqolawebsocket.cpp messageattachment.cpp messageurl.cpp rocketchataccount.cpp rocketchataccountsettings.cpp ruqolalogger.cpp ruqolaregisterengine.cpp ruqolaserverconfig.cpp rocketchatcache.cpp texthighlighter.cpp textconverter.cpp loadrecenthistorymanager.cpp unityservicemanager.cpp file.cpp channel.cpp inputtextmanager.cpp authenticationmanager.cpp searchmessage.cpp accountmanager.cpp managerdatapaths.cpp notificationoptions.cpp reactions.cpp reaction.cpp ) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_debug.h IDENTIFIER RUQOLA_LOG CATEGORY_NAME org.kde.ruqola) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_message_debug.h IDENTIFIER RUQOLA_MESSAGE_LOG CATEGORY_NAME org.kde.ruqola.message) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_ddpapi_debug.h IDENTIFIER RUQOLA_DDPAPI_LOG CATEGORY_NAME org.kde.ruqola.ddpapi) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_ddpapi_command_debug.h IDENTIFIER RUQOLA_DDPAPI_COMMAND_LOG CATEGORY_NAME org.kde.ruqola.ddpapi.command) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_restapi_debug.h IDENTIFIER RUQOLA_RESTAPI_LOG CATEGORY_NAME org.kde.ruqola.restapi) add_library(libruqolacore ${Ruqola_core_srcs}) generate_export_header(libruqolacore BASE_NAME libruqolacore) target_link_libraries(libruqolacore Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Qml Qt5::Quick Qt5::WebSockets Qt5::Network Qt5::NetworkAuth Qt5::DBus KF5::CoreAddons KF5::I18n KF5::Crash KF5::Notifications KF5::KIOCore KF5::SyntaxHighlighting ) if (WIN32 OR APPLE) target_link_libraries(libruqolacore KF5::IconThemes) endif() if (Qt5Keychain_FOUND) target_link_libraries(libruqolacore qt5keychain) target_include_directories(libruqolacore PRIVATE ${QTKEYCHAIN_INCLUDE_DIRS}) endif() set_target_properties(libruqolacore PROPERTIES OUTPUT_NAME ruqolacore VERSION ${RUQOLA_LIB_VERSION} SOVERSION ${RUQOLA_LIB_SOVERSION} ) qt5_add_resources(RuqolaResources qml/qml.qrc) add_executable(ruqola main.cpp ${RuqolaResources}) target_link_libraries(ruqola libruqolacore) install(TARGETS ruqola ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install(TARGETS libruqolacore ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} LIBRARY NAMELINK_SKIP) install(PROGRAMS data/org.kde.ruqola.desktop DESTINATION ${KDE_INSTALL_APPDIR}) install(FILES data/org.kde.ruqola.appdata.xml DESTINATION ${KDE_INSTALL_METAINFODIR}) add_subdirectory(plugins) add_subdirectory(icons) diff --git a/src/restapi/groups/creategroupsjob.cpp b/src/restapi/groups/creategroupsjob.cpp new file mode 100644 index 00000000..a5b2ed9f --- /dev/null +++ b/src/restapi/groups/creategroupsjob.cpp @@ -0,0 +1,142 @@ +/* + Copyright (c) 2018 Montel Laurent + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2 of the License or + ( at your option ) version 3 or, at the discretion of KDE e.V. + ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "creategroupsjob.h" + +#include "ruqola_restapi_debug.h" +#include "restapimethod.h" +#include +#include +#include +#include + +CreateGroupsJob::CreateGroupsJob(QObject *parent) + : RestApiAbstractJob(parent) +{ +} + +CreateGroupsJob::~CreateGroupsJob() +{ +} + +bool CreateGroupsJob::start() +{ + if (!canStart()) { + deleteLater(); + return false; + } + const QByteArray baPostData = json().toJson(QJsonDocument::Compact); + addLoggerInfo("CreateGroupsJob::start: " + baPostData); + QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); + connect(reply, &QNetworkReply::finished, this, &CreateGroupsJob::slotCreateChannelFinished); + return true; +} + +void CreateGroupsJob::slotCreateChannelFinished() +{ + QNetworkReply *reply = qobject_cast(sender()); + if (reply) { + const QByteArray data = reply->readAll(); + const QJsonDocument replyJson = QJsonDocument::fromJson(data); + const QJsonObject replyObject = replyJson.object(); + + if (replyObject[QStringLiteral("success")].toBool()) { + qCDebug(RUQOLA_RESTAPI_LOG) << "Change Topic success"; + Q_EMIT createChannelDone(); + } else { + qCWarning(RUQOLA_RESTAPI_LOG) <<" Problem when we tried to change topic" << data; + } + } + deleteLater(); +} + +QStringList CreateGroupsJob::members() const +{ + return mMembers; +} + +void CreateGroupsJob::setMembers(const QStringList &members) +{ + mMembers = members; +} + +QString CreateGroupsJob::channelName() const +{ + return mChannelName; +} + +void CreateGroupsJob::setChannelName(const QString &channelName) +{ + mChannelName = channelName; +} + +bool CreateGroupsJob::readOnly() const +{ + return mReadOnly; +} + +void CreateGroupsJob::setReadOnly(bool readOnly) +{ + mReadOnly = readOnly; +} + +bool CreateGroupsJob::requireHttpAuthentication() const +{ + return true; +} + +bool CreateGroupsJob::canStart() const +{ + if (mChannelName.isEmpty()) { + qCWarning(RUQOLA_RESTAPI_LOG) << "CreateGroupsJob: channelname is empty"; + return false; + } + if (!RestApiAbstractJob::canStart()) { + qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start CreateGroupsJob job"; + return false; + } + return true; +} + +QJsonDocument CreateGroupsJob::json() const +{ + QJsonObject jsonObj; + if (!mMembers.isEmpty()) { + jsonObj[QLatin1String("members")] = QJsonArray::fromStringList(mMembers); + } + jsonObj[QLatin1String("name")] = channelName(); + if (mReadOnly) { + jsonObj[QLatin1String("readOnly")] = true; + } //Default is false + + const QJsonDocument postData = QJsonDocument(jsonObj); + return postData; +} + +QNetworkRequest CreateGroupsJob::request() const +{ + const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::GroupsCreate); + QNetworkRequest request(url); + addAuthRawHeader(request); + request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); + request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); + request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json")); + return request; +} diff --git a/src/restapi/groups/creategroupsjob.h b/src/restapi/groups/creategroupsjob.h new file mode 100644 index 00000000..7721cea1 --- /dev/null +++ b/src/restapi/groups/creategroupsjob.h @@ -0,0 +1,62 @@ +/* + Copyright (c) 2018 Montel Laurent + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2 of the License or + ( at your option ) version 3 or, at the discretion of KDE e.V. + ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef CREATEGROUPSJOB_H +#define CREATEGROUPSJOB_H + +#include "restapiabstractjob.h" +#include "libruqola_private_export.h" + +class LIBRUQOLACORE_TESTS_EXPORT CreateGroupsJob : public RestApiAbstractJob +{ + Q_OBJECT +public: + explicit CreateGroupsJob(QObject *parent = nullptr); + ~CreateGroupsJob() override; + + bool start() override; + bool requireHttpAuthentication() const override; + bool canStart() const override; + + QNetworkRequest request() const override; + + QJsonDocument json() const; + + bool readOnly() const; + void setReadOnly(bool readOnly); + + QString channelName() const; + void setChannelName(const QString &channelName); + + QStringList members() const; + void setMembers(const QStringList &members); + +Q_SIGNALS: + void createChannelDone(); + +private: + Q_DISABLE_COPY(CreateGroupsJob) + void slotCreateChannelFinished(); + QString mChannelName; + QStringList mMembers; + bool mReadOnly = false; +}; + +#endif