From bcb927eda1e94efc2db7cea2c12b10282b31268f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Fischer?= Date: Tue, 1 Oct 2019 09:23:51 +0200 Subject: [PATCH] Unit test for testing RequestManager Class --- test/unitTests/UnitTest.cpp | 66 +++++++++++++++++++++++++++++++++++++ test/unitTests/unittest.h | 26 +++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 test/unitTests/UnitTest.cpp create mode 100644 test/unitTests/unittest.h diff --git a/test/unitTests/UnitTest.cpp b/test/unitTests/UnitTest.cpp new file mode 100644 index 0000000..abf55aa --- /dev/null +++ b/test/unitTests/UnitTest.cpp @@ -0,0 +1,66 @@ +#include "unittest.h" + +void UnitTest::loginSuccessfully() +{ + RequestManager* testAccess = new RequestManager(); + QJsonDocument config = testAccess->getConfig(); + QSignalSpy spy(testAccess, SIGNAL(successLogin())); + testAccess->login(host, email, password); + spy.wait(); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = testAccess->getResponse(); + qDebug() << json; + QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + QVERIFY2(doc["id"].isString(), "No parameter 'id' in response"); +} + +//POST request w/o datas +void UnitTest::getSubjects() +{ + RequestManager* testAccess = new RequestManager(); + testAccess->login(host, email, password); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + testAccess->getRequest("/SubjectManager/getSubjects"); + spy.wait(); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = testAccess->getResponse(); + QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8()); + QVERIFY(doc.isObject()); + QVERIFY2(doc["items"].isArray(), "No parameter 'id' in response"); + qDebug() << doc["items"].toArray()[0].toObject()["id"].toString(); + QVERIFY2(doc["items"].toArray()[0].toObject()["id"].isString(), "ada"); + +} + +void UnitTest::getTokens() +{ + RequestManager* testAccess = new RequestManager(); + testAccess->login(host, email, password); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + testAccess->getRequest("/TokenManager/getTokens"); + QCOMPARE(spy.count(), 0); + spy.wait(); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + qDebug() << testAccess->getResponse(); +} + +void UnitTest::getTokensOpenManager() +{ + RequestManager* testAccess = new RequestManager(); + testAccess->login(host, email, password); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + testAccess->getRequest("/OpenManager/getTokenSubject"); + QCOMPARE(spy.count(), 0); + spy.wait(); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString response = testAccess->getResponse(); + QVERIFY(!response.isEmpty()); + qDebug() << response; +} + + +QTEST_MAIN(UnitTest) diff --git a/test/unitTests/unittest.h b/test/unitTests/unittest.h new file mode 100644 index 0000000..ae27657 --- /dev/null +++ b/test/unitTests/unittest.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + + +class UnitTest : public QObject +{ + Q_OBJECT + +private slots: + + void loginSuccessfully(); + + + void getTokens(); + void getTokensOpenManager(); + void getSubjects(); + +private: + // QString host = "https://localhost:8080"; //colnod server (ssh -L 8080:192.168.254.11:8443 penta-master) + QString host = "https://192.168.1.3:8443"; //local testing server + QString password = "spaceti1"; + QString email = "spaceti"; +};