productapitest.cpp 6.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
    Copyright (C) 2016 Volker Krause <vkrause@kde.org>

    This program 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) 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 Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

18
19
#include "servercontroller.h"

20
21
22
23
#include <rest/restapi.h>
#include <rest/restclient.h>

#include <core/product.h>
24
#include <core/schemaentryelement.h>
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <QDebug>
#include <QtTest/qtest.h>
#include <QNetworkReply>
#include <QObject>
#include <QSignalSpy>
#include <QStandardPaths>

using namespace UserFeedback::Analyzer;

class ProductApiTest : public QObject
{
    Q_OBJECT
private:
39
40
    ServerController m_server;

41
42
43
    ServerInfo testServer() const
    {
        ServerInfo s;
44
        s.setUrl(m_server.url());
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
        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);
70
        QVERIFY(m_server.start());
71
72
73
74
75
76
77
78
79
80
    }

    void testInvalidList()
    {
        RESTClient client;
        auto serverInfo = testServer();
        serverInfo.setPassword(QStringLiteral("wrong password"));
        client.connectToServer(serverInfo);
        QVERIFY(client.isConnected());

81
        auto reply = RESTApi::listProducts(&client);
82
        QVERIFY(waitForFinished(reply));
83
        QEXPECT_FAIL("", "local PHP tests don't authenticate", Continue);
84
85
86
87
88
89
90
91
        QVERIFY(reply->error() != QNetworkReply::NoError);
    }

    void testProductCRUD()
    {
        RESTClient client;
        client.connectToServer(testServer());
        QVERIFY(client.isConnected());
92
93
94
95
96
97
        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);
98
99

        // list existing products
100
        reply = RESTApi::listProducts(&client);
101
102
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);
103
        QVERIFY(reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith(QLatin1String("text/json")));
104
105
106
107
        const auto products = Product::fromJson(reply->readAll());
        QVERIFY(!findProduct(products, QLatin1String("org.kde.UserFeedback.UnitTestProduct")).isValid());

        // add new product
108
109
110
111
        SchemaEntry entry;
        entry.setName(QStringLiteral("entry1"));
        entry.setType(SchemaEntry::StringType);
        entry.setAggregationType(SchemaEntry::Category);
112
113
114
115
        SchemaEntryElement elem1;
        elem1.setName(QStringLiteral("elem11"));
        elem1.setType(SchemaEntryElement::Integer);
        entry.setElements({elem1});
116
        newProduct.setSchema({entry});
117
118
119
120
121
122
        QVERIFY(newProduct.isValid());
        reply = RESTApi::createProduct(&client, newProduct);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);

        // verify the new product is there
123
        reply = RESTApi::listProducts(&client);
124
125
126
127
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);
        auto newProducts = Product::fromJson(reply->readAll());
        QCOMPARE(newProducts.size(), products.size() + 1);
128
129
130
        const auto createdProduct = findProduct(newProducts, QLatin1String("org.kde.UserFeedback.UnitTestProduct"));
        QVERIFY(createdProduct.isValid());
        QCOMPARE(createdProduct.schema(), newProduct.schema());
131
132
133
134
135
136
137
138
139
140
141
142

        // 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
143
        reply = RESTApi::listProducts(&client);
144
145
146
147
148
149
150
151
152
153
154
        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
155
        reply = RESTApi::listProducts(&client);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        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.connectToServer(testServer());
        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"