File: submittest.cpp

package info (click to toggle)
kuserfeedback 1.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,496 kB
  • sloc: cpp: 13,251; php: 2,192; xml: 224; yacc: 90; lex: 82; sh: 17; makefile: 8
file content (213 lines) | stat: -rw-r--r-- 7,050 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include "servercontroller.h"

#include <rest/restapi.h>
#include <rest/restclient.h>

#include <provider/core/auditloguicontroller.h>
#include <provider/core/provider.h>
#include <provider/core/platforminfosource.h>
#include <provider/core/screeninfosource.h>

#include <core/product.h>
#include <core/schemaentryelement.h>
#include <core/schemaentrytemplates.h>

#include <QAbstractItemModel>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QtTest/qtest.h>
#include <QNetworkReply>
#include <QObject>
#include <QSettings>
#include <QSignalSpy>
#include <QStandardPaths>

using namespace KUserFeedback;
using namespace KUserFeedback::Console;

class SubmitTest : public QObject
{
    Q_OBJECT
private:
    ServerController m_server;

    bool waitForFinished(QNetworkReply *reply)
    {
        Q_ASSERT(reply);
        QSignalSpy spy(reply, &QNetworkReply::finished);
        Q_ASSERT(spy.isValid());
        return spy.wait();
    }

private slots:
    void initTestCase()
    {
        Q_INIT_RESOURCE(schematemplates);
        QStandardPaths::setTestModeEnabled(true);
        QVERIFY(m_server.start());
    }

    void init()
    {
        // clear past audit logs
        AuditLogUiController alc;
        alc.clear();
        QVERIFY(alc.logEntryModel());
        QCOMPARE(alc.logEntryModel()->rowCount(), 0);
    }

    void testProvider_data()
    {
        QTest::addColumn<QString>("path");
        QTest::newRow("direct") << QString();
        QTest::newRow("absolute redirect") << QStringLiteral("/absRedirect");
        QTest::newRow("relative redirect") << QStringLiteral("/relRedirect");
    }

    void testProvider()
    {
        QFETCH(QString, path);
        ServerInfo serverInfo;
        auto serverUrl = m_server.url();
        serverInfo.setUrl(serverUrl);
        serverUrl.setPath(path);

        // delete previous leftovers
        RESTClient client;
        client.setServerInfo(serverInfo);
        client.setConnected(true);
        QVERIFY(client.isConnected());
        Product p;
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        auto reply = RESTApi::deleteProduct(&client, p);
        waitForFinished(reply);

        // create test product
        for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
            p.addTemplate(tpl);
        QVERIFY(p.isValid());
        reply = RESTApi::createProduct(&client, p);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);

        // submit data
        Provider provider;
        provider.setProductIdentifier(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        provider.setTelemetryMode(Provider::DetailedUsageStatistics);
        provider.setFeedbackServer(serverUrl);
        provider.addDataSource(new ScreenInfoSource);
        provider.addDataSource(new PlatformInfoSource);
        provider.submit();
        QTest::qWait(100); // HACK submit is async

        // retrieve data
        reply = RESTApi::listSamples(&client, p);
        QVERIFY(waitForFinished(reply));
        QVERIFY(reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith(QLatin1String("text/json")));
        auto doc = QJsonDocument::fromJson(reply->readAll());
        QVERIFY(doc.isArray());
        auto a = doc.array();
        QCOMPARE(a.size(), 1);

        auto obj = a.at(0).toObject();
        QVERIFY(!obj.isEmpty());
        QVERIFY(obj.contains(QLatin1String("id")));
        QVERIFY(obj.contains(QLatin1String("timestamp")));

        QVERIFY(obj.contains(QLatin1String("platform")));
        auto sub = obj.value(QLatin1String("platform")).toObject();
        QVERIFY(sub.contains(QLatin1String("os")));
        QVERIFY(sub.contains(QLatin1String("version")));

        QVERIFY(obj.contains(QLatin1String("screens")));
        a = obj.value(QLatin1String("screens")).toArray();
        QVERIFY(a.size() > 0);
        sub = a.at(0).toObject();
        QVERIFY(sub.contains(QLatin1String("height")));
        QVERIFY(sub.contains(QLatin1String("width")));

        // check we wrote an audit log
        AuditLogUiController alc;
        QVERIFY(alc.logEntryModel());
        QCOMPARE(alc.logEntryModel()->rowCount(), 1);
    }

    void testRedirectLoop()
    {
        auto serverUrl = m_server.url();
        serverUrl.setPath(QLatin1String("/circleRedirect"));

        // this must pass without ending in an infinite loop
        Provider provider;
        provider.setProductIdentifier(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        provider.setTelemetryMode(Provider::DetailedUsageStatistics);
        provider.setFeedbackServer(serverUrl);
        provider.addDataSource(new ScreenInfoSource);
        provider.addDataSource(new PlatformInfoSource);
        provider.submit();
        QTest::qWait(500); // HACK submit is async, and we have no way of knowning the redirect loop ended...
    }

    void testSurveyWithoutTelemetry()
    {
        ServerInfo serverInfo;
        auto serverUrl = m_server.url();
        serverInfo.setUrl(serverUrl);

        // delete previous leftovers
        RESTClient client;
        client.setServerInfo(serverInfo);
        client.setConnected(true);
        QVERIFY(client.isConnected());
        Product p;
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        auto reply = RESTApi::deleteProduct(&client, p);
        waitForFinished(reply);

        // create test product
        for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
            p.addTemplate(tpl);
        QVERIFY(p.isValid());
        reply = RESTApi::createProduct(&client, p);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);

        // submit data
        Provider provider;
        provider.setProductIdentifier(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        provider.setTelemetryMode(Provider::NoTelemetry);
        provider.setSubmissionInterval(7);
        provider.setSurveyInterval(30);
        provider.setFeedbackServer(serverUrl);
        provider.addDataSource(new ScreenInfoSource);
        provider.addDataSource(new PlatformInfoSource);
        provider.submit();
        QTest::qWait(100); // HACK submit is async

        // retrieve data
        reply = RESTApi::listSamples(&client, p);
        QVERIFY(waitForFinished(reply));
        QVERIFY(reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith(QLatin1String("text/json")));
        auto doc = QJsonDocument::fromJson(reply->readAll());
        QVERIFY(doc.isArray());
        auto a = doc.array();
        QCOMPARE(a.size(), 0);

        // check we wrote an audit log
        AuditLogUiController alc;
        QVERIFY(alc.logEntryModel());
        QCOMPARE(alc.logEntryModel()->rowCount(), 1);
    }
};

QTEST_MAIN(SubmitTest)

#include "submittest.moc"