File indexing completed on 2024-05-19 04:01:09
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "servercontroller.h" 0008 0009 #include <rest/restapi.h> 0010 #include <rest/restclient.h> 0011 0012 #include <core/product.h> 0013 #include <core/survey.h> 0014 0015 #include <QDebug> 0016 #include <QtTest/qtest.h> 0017 #include <QNetworkReply> 0018 #include <QObject> 0019 #include <QSignalSpy> 0020 #include <QStandardPaths> 0021 #include <QUuid> 0022 0023 #include <limits> 0024 0025 using namespace KUserFeedback::Console; 0026 0027 class SurveyApiTest : public QObject 0028 { 0029 Q_OBJECT 0030 private: 0031 ServerController m_server; 0032 0033 ServerInfo testServer() const 0034 { 0035 ServerInfo s; 0036 s.setUrl(m_server.url()); 0037 return s; 0038 } 0039 0040 bool waitForFinished(QNetworkReply *reply) 0041 { 0042 Q_ASSERT(reply); 0043 QSignalSpy spy(reply, &QNetworkReply::finished); 0044 Q_ASSERT(spy.isValid()); 0045 return spy.wait(); 0046 } 0047 0048 private Q_SLOTS: 0049 void initTestCase() 0050 { 0051 QStandardPaths::setTestModeEnabled(true); 0052 QVERIFY(m_server.start()); 0053 } 0054 0055 void init() 0056 { 0057 RESTClient client; 0058 client.setServerInfo(testServer()); 0059 Product p; 0060 p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct")); 0061 auto reply = RESTApi::createProduct(&client, p); 0062 waitForFinished(reply); 0063 } 0064 0065 void cleanup() 0066 { 0067 RESTClient client; 0068 client.setServerInfo(testServer()); 0069 Product p; 0070 p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct")); 0071 auto reply = RESTApi::deleteProduct(&client, p); 0072 waitForFinished(reply); 0073 } 0074 0075 void testSurveyCRUD() 0076 { 0077 RESTClient client; 0078 client.setServerInfo(testServer()); 0079 client.setConnected(true); 0080 QVERIFY(client.isConnected()); 0081 Product p; 0082 p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct")); 0083 0084 // list surveys 0085 auto reply = RESTApi::listSurveys(&client, p); 0086 QVERIFY(waitForFinished(reply)); 0087 QCOMPARE(reply->error(), QNetworkReply::NoError); 0088 QVERIFY(reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith(QLatin1String("text/json"))); 0089 auto surveys = Survey::fromJson(reply->readAll()); 0090 QVERIFY(surveys.isEmpty()); 0091 0092 // add new survey 0093 Survey s; 0094 s.setUuid(QUuid::createUuid()); 0095 s.setName(QStringLiteral("unitTestSurvey")); 0096 s.setUrl(QUrl(QStringLiteral("http://www.kde.org"))); 0097 s.setActive(false); 0098 s.setTarget(QStringLiteral("screen[0].dpi >= 200")); 0099 reply = RESTApi::createSurvey(&client, p, s); 0100 QVERIFY(waitForFinished(reply)); 0101 QCOMPARE(reply->error(), QNetworkReply::NoError); 0102 0103 // verify the new product is there 0104 reply = RESTApi::listSurveys(&client, p); 0105 QVERIFY(waitForFinished(reply)); 0106 QCOMPARE(reply->error(), QNetworkReply::NoError); 0107 surveys = Survey::fromJson(reply->readAll()); 0108 QCOMPARE(surveys.size(), 1); 0109 QCOMPARE(surveys.at(0), s); 0110 0111 // update a survey 0112 s.setActive(true); 0113 reply = RESTApi::updateSurvey(&client, s); 0114 QVERIFY(waitForFinished(reply)); 0115 QCOMPARE(reply->error(), QNetworkReply::NoError); 0116 0117 // verify the update worked 0118 reply = RESTApi::listSurveys(&client, p); 0119 QVERIFY(waitForFinished(reply)); 0120 QCOMPARE(reply->error(), QNetworkReply::NoError); 0121 surveys = Survey::fromJson(reply->readAll()); 0122 QCOMPARE(surveys.size(), 1); 0123 QCOMPARE(s, surveys.at(0)); 0124 0125 // delete a survey 0126 reply = RESTApi::deleteSurvey(&client, s); 0127 QVERIFY(waitForFinished(reply)); 0128 QCOMPARE(reply->error(), QNetworkReply::NoError); 0129 0130 // verify it's gone 0131 reply = RESTApi::listSurveys(&client, p); 0132 QVERIFY(waitForFinished(reply)); 0133 QCOMPARE(reply->error(), QNetworkReply::NoError); 0134 surveys = Survey::fromJson(reply->readAll()); 0135 QVERIFY(surveys.isEmpty()); 0136 } 0137 0138 void testInvalidSurveyOperations() 0139 { 0140 RESTClient client; 0141 client.setServerInfo(testServer()); 0142 client.setConnected(true); 0143 QVERIFY(client.isConnected()); 0144 0145 Product invalidProduct; 0146 invalidProduct.setName(QStringLiteral("org.kde.UserFeedback.Invalid")); 0147 0148 // create survey for non-existing product 0149 Survey s; 0150 s.setName(QStringLiteral("invalidTestSurvey")); 0151 s.setUrl(QUrl(QStringLiteral("http://www.kde.org"))); 0152 auto reply = RESTApi::createSurvey(&client, invalidProduct, s); 0153 QVERIFY(waitForFinished(reply)); 0154 QVERIFY(reply->error() != QNetworkReply::NoError); 0155 0156 // update a non-existing survey 0157 s.setUuid(QUuid::createUuid()); 0158 reply = RESTApi::updateSurvey(&client, s); 0159 QVERIFY(waitForFinished(reply)); 0160 qDebug() << reply->readAll(); 0161 QEXPECT_FAIL("", "check not implemented on the server", Continue); 0162 QVERIFY(reply->error() != QNetworkReply::NoError); 0163 0164 // delete a non-existing survey 0165 reply = RESTApi::deleteSurvey(&client, s); 0166 QVERIFY(waitForFinished(reply)); 0167 QEXPECT_FAIL("", "check not implemented on the server", Continue); 0168 QVERIFY(reply->error() != QNetworkReply::NoError); 0169 } 0170 }; 0171 0172 QTEST_MAIN(SurveyApiTest) 0173 0174 #include "surveyapitest.moc" 0175