diff --git a/src/colnod/colnodapi.cpp b/src/colnod/colnodapi.cpp index 7452c39..9d719f1 100644 --- a/src/colnod/colnodapi.cpp +++ b/src/colnod/colnodapi.cpp @@ -14,15 +14,17 @@ ColnodAPI::ColnodAPI() void ColnodAPI::downloadData() { Response* resp0 = reqMan->getSubjects(); - connect(resp0, &Response::finished, [=](){ + connect(resp0, &Response::ready, [=](){ dataMan->localSaveSubjects(resp0->reply->readAll()); emit partReady(); + resp0->deleteLater(); }); Response* resp1 = reqMan->getTokens(); - connect(resp1, &Response::finished, [=](){ + connect(resp1, &Response::ready, [=](){ dataMan->localSaveTokens(resp1->reply->readAll()); emit partReady(); + resp1->deleteLater(); }); /* diff --git a/src/colnod/databasemanager.cpp b/src/colnod/databasemanager.cpp index 47ddb2a..75d9d8a 100644 --- a/src/colnod/databasemanager.cpp +++ b/src/colnod/databasemanager.cpp @@ -71,7 +71,7 @@ QString DatabaseManager::getTestMessage(int id) return query.value(0).toString(); } -void DatabaseManager::createTestRecord(QString msg) +int DatabaseManager::createTestRecord(QString msg) { query.exec("SELECT MAX(id) FROM test"); query.next(); @@ -82,7 +82,7 @@ void DatabaseManager::createTestRecord(QString msg) query.exec(); query.next(); qDebug() << "U inserted new row, id: " << id << "msg: " << getTestMessage(id); - + return id; } diff --git a/src/colnod/databasemanager.h b/src/colnod/databasemanager.h index e4a0106..8cddc82 100644 --- a/src/colnod/databasemanager.h +++ b/src/colnod/databasemanager.h @@ -23,7 +23,7 @@ public: void saveSubjectAttribute(Subject* subject, QString table); QString getTestMessage(int id); - void createTestRecord(QString msg); + int createTestRecord(QString msg); bool getOpened() {return opened;} diff --git a/src/colnod/requestmanager.cpp b/src/colnod/requestmanager.cpp index 1e4ecd9..dffdeac 100644 --- a/src/colnod/requestmanager.cpp +++ b/src/colnod/requestmanager.cpp @@ -18,7 +18,6 @@ RequestManager::RequestManager() void RequestManager::login(QString host, QString username, QString password) { - QNetworkRequest request; QString hash = encryptedPassword(this->password); authHeader = getLoginHeader(this->user, hash); @@ -28,6 +27,7 @@ void RequestManager::login(QString host, QString username, QString password) }; //this->host = host; + request.setUrl(this->host + "/AaaManager/authSession"); request.setRawHeader("Authorization", authHeader.toLocal8Bit()); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8"); @@ -38,14 +38,16 @@ void RequestManager::login(QString host, QString username, QString password) QObject::connect(reply, &QNetworkReply::finished, [=]() { if (reply->error()) { - qDebug() << reply->errorString(); + qDebug() << "Login failed: "<< reply->errorString(); loginStatus = false; + reply->deleteLater(); return; } jsonResponse = reply->readAll(); prepareAuthHeader(); if (loginStatus) return; + qDebug() << "Logged in successfully."; loginStatus = true; emit successLogin(); reply->deleteLater(); @@ -56,7 +58,6 @@ QNetworkReply* RequestManager::getRequest(QString endpoint) { QNetworkRequest request; request.setUrl(host + endpoint); - qDebug() << "Auth: " << authHeader; request.setRawHeader("Authorization", authHeader.toLocal8Bit()); QNetworkReply* reply = manager->post(request, QByteArray()); QObject::connect(reply, &QNetworkReply::finished, [=]() { @@ -64,14 +65,40 @@ QNetworkReply* RequestManager::getRequest(QString endpoint) { qDebug() << reply->errorString(); loginStatus = false; - //getRequest(endpoint); + reply->deleteLater(); return; } + qDebug() << "Authorized successfully, response ready."; emit responseReady(); + reply->deleteLater(); }); return reply; } +QNetworkReply* RequestManager::postRequest(QString endpoint, QByteArray json) +{ + QNetworkRequest request; + request.setUrl(host + endpoint); + request.setRawHeader("Authorization", authHeader.toLocal8Bit()); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8"); + QNetworkReply* reply = manager->post(request, json); + QObject::connect(reply, &QNetworkReply::finished, [=]() { + if (reply->error()) + { + qDebug() << "Authorization failed: " << reply->errorString(); + loginStatus = false; + reply->deleteLater(); + return; + } + qDebug() << "Authorized successfully, response ready."; + emit responseReady(); + reply->deleteLater(); + }); + return reply; +} + + + QString RequestManager::getFromJson(QString json, const QString &member) { QJsonParseError err; @@ -113,6 +140,14 @@ void RequestManager::onSSLError(QNetworkReply *reply, const QList &er } +QByteArray RequestManager::makeTimeStampJson(uint64_t time) +{ + QJsonObject reqBody = {{"timestamp",QJsonValue(static_cast(time)) }}; + QJsonDocument doc; + doc.setObject(reqBody); + return doc.toJson(); +} + Response* RequestManager::getSubjects() { auto response = new Response; @@ -121,13 +156,13 @@ Response* RequestManager::getSubjects() connect(this, &RequestManager::successLogin, [=](){ auto subjectReply = getRequest("/SubjectManager/getSubjects"); response->reply = subjectReply; - connect(subjectReply, &QNetworkReply::finished, response, &Response::finished); + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); }); } else { QNetworkReply* reply = getRequest("/SubjectManager/getSubjects"); - connect(reply, &QNetworkReply::finished, response, &Response::finished); + connect(reply, &QNetworkReply::finished, response, &Response::ready); } return response; @@ -141,13 +176,13 @@ Response* RequestManager::getTokens() connect(this, &RequestManager::successLogin, [=](){ auto subjectReply = getRequest("/TokenManager/getTokens"); response->reply = subjectReply; - connect(subjectReply, &QNetworkReply::finished, response, &Response::finished); + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); }); } else { QNetworkReply* reply = getRequest("/TokenManager/getTokens"); - connect(reply, &QNetworkReply::finished, response, &Response::finished); + connect(reply, &QNetworkReply::finished, response, &Response::ready); } return response; @@ -161,66 +196,216 @@ Response* RequestManager::getTokenSubject() connect(this, &RequestManager::successLogin, [=](){ auto subjectReply = getRequest("/OpenManager/getTokenSubject"); response->reply = subjectReply; - connect(subjectReply, &QNetworkReply::finished, response, &Response::finished); + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); }); } else { QNetworkReply* reply = getRequest("/OpenManager/getTokenSubject"); - connect(reply, &QNetworkReply::finished, response, &Response::finished); + connect(reply, &QNetworkReply::finished, response, &Response::ready); } return response; } -Response* RequestManager::getSubjectsUpdated(uint64_t) +Response* RequestManager::getSubjectsUpdated(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/SubjectManager/getSubjectsUpdated", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/SubjectManager/getSubjectsUpdated", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } -Response* RequestManager::getSubjectsDeleted(uint64_t) +Response* RequestManager::getSubjectsDeleted(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/SubjectManager/getSubjectsDeleted", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/SubjectManager/getSubjectsDeleted", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } -Response* RequestManager::getTokensUpdated(uint64_t) +Response* RequestManager::getTokensUpdated(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/TokenManager/getTokensUpdated", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/TokenManager/getTokensUpdated", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } -Response* RequestManager::getTokensDeleted(uint64_t) +Response* RequestManager::getTokensDeleted(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/TokenManager/getTokensDeleted", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/TokenManager/getTokensDeleted", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } Response* RequestManager::getAccessGroups() { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = getRequest("/AccessManager/getAccessGroups"); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = getRequest("/AccessManager/getAccessGroups"); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } -Response* RequestManager::getAccessGroupsUpdated(uint64_t) +Response* RequestManager::getAccessGroupsUpdated(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/AccessManager/getAccessGroupsUpdated", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/AccessManager/getAccessGroupsUpdated", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } -Response* RequestManager::getAccessGroupsDeleted(uint64_t) +Response* RequestManager::getAccessGroupsDeleted(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/AccessManager/getAccessGroupsDeleted", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/AccessManager/getAccessGroupsDeleted", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } Response* RequestManager::getAccessGroupMembership() { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = getRequest("/AccessManager/getAccessGroupMembership"); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = getRequest("/AccessManager/getAccessGroupMembership"); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } -Response* RequestManager::getAccessGroupMembershipUpdated(uint64_t) +Response* RequestManager::getAccessGroupMembershipUpdated(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/AccessManager/getAccessGroupMembershipUpdated", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/AccessManager/getAccessGroupMembershipUpdated", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } -Response* RequestManager::getAccessGroupMembershipDeleted(uint64_t) +Response* RequestManager::getAccessGroupMembershipDeleted(uint64_t time) { + auto response = new Response; + if (!loginStatus){ + login(host, user, password); + connect(this, &RequestManager::successLogin, [=](){ + auto subjectReply = postRequest("/AccessManager/getAccessGroupMembershipDeleted", makeTimeStampJson(time)); + response->reply = subjectReply; + connect(subjectReply, &QNetworkReply::finished, response, &Response::ready); + }); + } + else + { + QNetworkReply* reply = postRequest("/AccessManager/getAccessGroupMembershipDeleted", makeTimeStampJson(time)); + connect(reply, &QNetworkReply::finished, response, &Response::ready); + } + return response; } diff --git a/src/colnod/requestmanager.h b/src/colnod/requestmanager.h index 8aba322..4c495d1 100644 --- a/src/colnod/requestmanager.h +++ b/src/colnod/requestmanager.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "response.h" @@ -33,18 +34,18 @@ public: Response* getTokens(); Response* getTokenSubject(); - Response* getSubjectsUpdated(uint64_t); - Response* getSubjectsDeleted(uint64_t); + Response* getSubjectsUpdated(uint64_t time); + Response* getSubjectsDeleted(uint64_t time); - Response* getTokensUpdated(uint64_t); - Response* getTokensDeleted(uint64_t); + Response* getTokensUpdated(uint64_t time); + Response* getTokensDeleted(uint64_t time); Response* getAccessGroups(); - Response* getAccessGroupsUpdated(uint64_t); - Response* getAccessGroupsDeleted(uint64_t); + Response* getAccessGroupsUpdated(uint64_t time); + Response* getAccessGroupsDeleted(uint64_t time); Response* getAccessGroupMembership(); - Response* getAccessGroupMembershipUpdated(uint64_t); - Response* getAccessGroupMembershipDeleted(uint64_t); + Response* getAccessGroupMembershipUpdated(uint64_t time); + Response* getAccessGroupMembershipDeleted(uint64_t time); void login(QString endpoint, QString username, QString password); //I want it private, but unittest??? @@ -53,10 +54,12 @@ public: private: QNetworkReply* getRequest(QString endpoint); + QNetworkReply* postRequest(QString endpoints, QByteArray json); QString getFromJson(QString json, const QString &member); QString encryptedPassword(QString password); QString getLoginHeader(QString username, QString hash); void prepareAuthHeader(); + QByteArray makeTimeStampJson(uint64_t); private slots: void onSSLError(QNetworkReply *reply, const QList &errors); diff --git a/src/colnod/response.h b/src/colnod/response.h index aa5ef96..02dad6e 100644 --- a/src/colnod/response.h +++ b/src/colnod/response.h @@ -14,7 +14,7 @@ public: QNetworkReply *reply = nullptr; signals: - void finished(); + void ready(); public slots: diff --git a/test/unitTests/UnitTest.cpp b/test/unitTests/UnitTest.cpp index 7714ee3..8a68b00 100644 --- a/test/unitTests/UnitTest.cpp +++ b/test/unitTests/UnitTest.cpp @@ -66,6 +66,146 @@ void UnitTest::getTokenSubject() QVERIFY(!json.isEmpty()); } +void UnitTest::getSubjectsUpdated() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getSubjectsUpdated(1568646217767); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getSubjectsDeleted() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getSubjectsDeleted(1568646217767); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getTokensUpdated() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getTokensUpdated(1570109859681); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getTokensDeleted() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getTokensDeleted(1570109859681); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getAccessGroups() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getAccessGroups(); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getAccessGroupsUpdated() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getAccessGroupsUpdated(1568646217767); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getAccessGroupsDeleted() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getAccessGroupsDeleted(1568646217767); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getAccessGroupMembership() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getAccessGroupMembership(); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getAccessGroupMembershipUpdated() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getAccessGroupMembershipUpdated(1568646217767); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + +void UnitTest::getAccessGroupMembershipDeleted() +{ + Response* response = new Response; + RequestManager* testAccess = new RequestManager(); + QSignalSpy spy(testAccess, SIGNAL(responseReady())); + response = testAccess->getAccessGroupMembershipDeleted(1568646217767); + QCOMPARE(spy.count(), 0); + spy.wait(10000); + QVERIFY(spy.isValid()); + QCOMPARE(spy.count(), 1); + QString json = response->reply->readAll(); + QVERIFY(!json.isEmpty()); +} + void UnitTest::subjectJSONParser() { /* @@ -118,48 +258,49 @@ void UnitTest::connectToDB() qDebug() << db->getTestMessage(1); QVERIFY(true); QCOMPARE(db->getTestMessage(1), QStringLiteral("Hello world")); - db->createTestRecord("Now it's diffrent"); - QCOMPARE(db->getTestMessage(2), QStringLiteral("Does it work?")); + QCOMPARE(db->getTestMessage(db->createTestRecord("Now it's diffrent")), QStringLiteral("Does it work?")); } -//void UnitTest::debugConnection() -//{ -// QSslSocket ssl;// = new QSslSocket(); -// ssl.connectToHostEncrypted("192.168.1.3", 8443); -// ssl.ignoreSslErrors(); -// if (!ssl.waitForEncrypted()) -// { -// qDebug() << ssl.errorString(); -// } -// QString tmp = "POST /AaaManager/authSession HTTP/1.1\r\nHost: 192.168.1.3:8443\r\nAuthorization: Basic c3BhY2V0aTpFN0ZFQTA3REJDODJGQjlCQTM0QjJFNDUzRTJFRkU4OUYwNTgzRTcxODc1NDM1MDdFRjExRTg0NzI2MDcxRkU0Mjg0NDUzNzJGMkU0NkI0NTVFQjBDNzk3NDdDNTFFMzYyRjQwM0NFMzk0NzNBMkE4QzJERkEwQ0U1Qzk4MjZDMA==\r\nUser-Agent: curl/7.65.3\r\nAccept: */*\r\nContent-Type: application/json\r\nContent-Length: 164\r\n\r\n{\"password\":\"E7FEA07DBC82FB9BA34B2E453E2EFE89F0583E7187543507EF11E84726071FE428445372F2E46B455EB0C79747C51E362F403CE39473A2A8C2DFA0CE5C9826C0\",\"username\":\"spaceti\"}"; -// qDebug().noquote() << tmp.toUtf8(); -// ssl.write(tmp.toUtf8()); -// QString data; -// while (ssl.waitForReadyRead() && data.isEmpty()) -// { -// data.append(ssl.readAll().data()); -// } -// qDebug().noquote() << data; -// int index = data.lastIndexOf("id"); -// QString tkn = data.mid( index + 5, 36); -// tkn = tkn + ":"; -// qDebug() << "parsed token : " <