1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: MIT
*/
#include "servercontroller.h"
#include <rest/restapi.h>
#include <rest/restclient.h>
#include <core/product.h>
#include <core/schemaentryelement.h>
#include <QDebug>
#include <QtTest/qtest.h>
#include <QNetworkReply>
#include <QObject>
#include <QSignalSpy>
#include <QStandardPaths>
using namespace KUserFeedback::Console;
class ProductApiTest : public QObject
{
Q_OBJECT
private:
ServerController m_server;
ServerInfo testServer() const
{
ServerInfo s;
s.setUrl(m_server.url());
return s;
}
bool waitForFinished(QNetworkReply *reply)
{
Q_ASSERT(reply);
QSignalSpy spy(reply, &QNetworkReply::finished);
Q_ASSERT(spy.isValid());
return spy.wait();
}
Product findProduct(const QVector<Product> &products, const QString &name)
{
const auto it = std::find_if(products.cbegin(), products.cend(), [name](const Product &p) {
return p.name() == name;
});
if (it != products.cend())
return *it;
return {};
}
private slots:
void initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
QVERIFY(m_server.start());
}
void testInvalidList()
{
RESTClient client;
auto serverInfo = testServer();
serverInfo.setPassword(QStringLiteral("wrong password"));
client.setServerInfo(serverInfo);
client.setConnected(true);
QVERIFY(client.isConnected());
auto reply = RESTApi::listProducts(&client);
QVERIFY(waitForFinished(reply));
QEXPECT_FAIL("", "local PHP tests don't authenticate", Continue);
QVERIFY(reply->error() != QNetworkReply::NoError);
}
void testProductCRUD()
{
RESTClient client;
client.setServerInfo(testServer());
client.setConnected(true);
QVERIFY(client.isConnected());
Product newProduct;
newProduct.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
// clean up from previous failed runs, if needed
auto reply = RESTApi::deleteProduct(&client, newProduct);
waitForFinished(reply);
// list existing products
reply = RESTApi::listProducts(&client);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
QVERIFY(reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith(QLatin1String("text/json")));
const auto products = Product::fromJson(reply->readAll());
QVERIFY(!findProduct(products, QLatin1String("org.kde.UserFeedback.UnitTestProduct")).isValid());
// add new product
SchemaEntry entry;
entry.setName(QStringLiteral("entry1"));
SchemaEntryElement elem1;
elem1.setName(QStringLiteral("elem11"));
entry.setElements({elem1});
newProduct.setSchema({entry});
QVERIFY(newProduct.isValid());
reply = RESTApi::createProduct(&client, newProduct);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
// verify the new product is there
reply = RESTApi::listProducts(&client);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
auto newProducts = Product::fromJson(reply->readAll());
QCOMPARE(newProducts.size(), products.size() + 1);
const auto createdProduct = findProduct(newProducts, QLatin1String("org.kde.UserFeedback.UnitTestProduct"));
QVERIFY(createdProduct.isValid());
QCOMPARE(createdProduct.schema(), newProduct.schema());
// update a product
reply = RESTApi::updateProduct(&client, newProduct);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
// try to add an already existing product
reply = RESTApi::createProduct(&client, newProduct);
QVERIFY(waitForFinished(reply));
QVERIFY(reply->error() != QNetworkReply::NoError);
// verify it really wasn't added
reply = RESTApi::listProducts(&client);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
newProducts = Product::fromJson(reply->readAll());
QCOMPARE(newProducts.size(), products.size() + 1);
// delete a product
reply = RESTApi::deleteProduct(&client, newProduct);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
// verify it's gone, and nothing else was harmed
reply = RESTApi::listProducts(&client);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
newProducts = Product::fromJson(reply->readAll());
QCOMPARE(newProducts.size(), products.size());
QVERIFY(!findProduct(newProducts, QLatin1String("org.kde.UserFeedback.UnitTestProduct")).isValid());
}
void testInvalidProductOperations()
{
RESTClient client;
client.setServerInfo(testServer());
client.setConnected(true);
QVERIFY(client.isConnected());
Product invalidProduct;
invalidProduct.setName(QStringLiteral("org.kde.UserFeedback.Invalid"));
QVERIFY(invalidProduct.isValid());
// update a non-existing product
auto reply = RESTApi::updateProduct(&client, invalidProduct);
QVERIFY(waitForFinished(reply));
qDebug() << reply->readAll();
QVERIFY(reply->error() != QNetworkReply::NoError);
// delete a non-existing product
reply = RESTApi::deleteProduct(&client, invalidProduct);
QVERIFY(waitForFinished(reply));
QVERIFY(reply->error() != QNetworkReply::NoError);
}
};
QTEST_MAIN(ProductApiTest)
#include "productapitest.moc"
|