mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 17:00:39 +02:00
Merge branch 'finish-tests' into 'master'
WIP: Finish tests See merge request spaceti-app/integrations/acs/colnod-connector!5
This commit is contained in:
commit
59e0e7ccdb
9
http/curlauth.txt
Normal file
9
http/curlauth.txt
Normal file
@ -0,0 +1,9 @@
|
||||
POST /AaaManager/authSession HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Authorization: Basic c3BhY2V0aTpFN0ZFQTA3REJDODJGQjlCQTM0QjJFNDUzRTJFRkU4OUYwNTgzRTcxODc1NDM1MDdFRjExRTg0NzI2MDcxRkU0Mjg0NDUzNzJGMkU0NkI0NTVFQjBDNzk3NDdDNTFFMzYyRjQwM0NFMzk0NzNBMkE4QzJERkEwQ0U1Qzk4MjZDMA==
|
||||
User-Agent: curl/7.65.3
|
||||
Accept: */*
|
||||
Content-Type: application/json
|
||||
Content-Length: 164
|
||||
|
||||
{"password":"E7FEA07DBC82FB9BA34B2E453E2EFE89F0583E7187543507EF11E84726071FE428445372F2E46B455EB0C79747C51E362F403CE39473A2A8C2DFA0CE5C9826C0","username":"spaceti"}
|
||||
6
http/curlsubject.txt
Normal file
6
http/curlsubject.txt
Normal file
@ -0,0 +1,6 @@
|
||||
POST /SubjectManager/getSubjects HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Authorization: Basic ZmYwMzYwZjYtMzRmZC00ZjAwLTlhNTYtMWZiMzhmNjlhNDIzOg==
|
||||
User-Agent: curl/7.65.3
|
||||
Accept: */*
|
||||
|
||||
14
http/qtauth.txt
Normal file
14
http/qtauth.txt
Normal file
@ -0,0 +1,14 @@
|
||||
POST /AaaManager/authSession HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Authorization: Basic c3BhY2V0aTpFN0ZFQTA3REJDODJGQjlCQTM0QjJFNDUzRTJFRkU4OUYwNTgzRTcxODc1NDM1MDdFRjExRTg0NzI2MDcxRkU0Mjg0NDUzNzJGMkU0NkI0NTVFQjBDNzk3NDdDNTFFMzYyRjQwM0NFMzk0NzNBMkE4QzJERkEwQ0U1Qzk4MjZDMA==
|
||||
Content-Type: application/json; charset=UTF-8
|
||||
Content-Length: 178
|
||||
Connection: Keep-Alive
|
||||
Accept-Encoding: gzip, deflate
|
||||
Accept-Language: en-AI,*
|
||||
User-Agent: Mozilla/5.0
|
||||
|
||||
{
|
||||
"password": "E7FEA07DBC82FB9BA34B2E453E2EFE89F0583E7187543507EF11E84726071FE428445372F2E46B455EB0C79747C51E362F403CE39473A2A8C2DFA0CE5C9826C0",
|
||||
"username": "spaceti"
|
||||
}
|
||||
14
http/qtrequestoutput.txt
Normal file
14
http/qtrequestoutput.txt
Normal file
@ -0,0 +1,14 @@
|
||||
POST /AaaManager/authSession HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Authorization: Basic c3BhY2V0aTpFN0ZFQTA3REJDODJGQjlCQTM0QjJFNDUzRTJFRkU4OUYwNTgzRTcxODc1NDM1MDdFRjExRTg0NzI2MDcxRkU0Mjg0NDUzNzJGMkU0NkI0NTVFQjBDNzk3NDdDNTFFMzYyRjQwM0NFMzk0NzNBMkE4QzJERkEwQ0U1Qzk4MjZDMA==
|
||||
Content-Type: application/json; charset=UTF-8
|
||||
Content-Length: 178
|
||||
Connection: Keep-Alive
|
||||
Accept-Encoding: gzip, deflate
|
||||
Accept-Language: en-AI,*
|
||||
User-Agent: Mozilla/5.0
|
||||
|
||||
{
|
||||
"password": "E7FEA07DBC82FB9BA34B2E453E2EFE89F0583E7187543507EF11E84726071FE428445372F2E46B455EB0C79747C51E362F403CE39473A2A8C2DFA0CE5C9826C0",
|
||||
"username": "spaceti"
|
||||
}
|
||||
8
http/qtrequestoutputsubject.txt
Normal file
8
http/qtrequestoutputsubject.txt
Normal file
@ -0,0 +1,8 @@
|
||||
POST /SubjectManager/getSubjects HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Content-Length: 0
|
||||
Connection: Keep-Alive
|
||||
Accept-Encoding: gzip, deflate
|
||||
Accept-Language: en-AI,*
|
||||
User-Agent: Mozilla/5.0
|
||||
|
||||
8
http/qtsubject.txt
Normal file
8
http/qtsubject.txt
Normal file
@ -0,0 +1,8 @@
|
||||
POST /SubjectManager/getSubjects HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Content-Length: 0
|
||||
Connection: Keep-Alive
|
||||
Accept-Encoding: gzip, deflate
|
||||
Accept-Language: en-AI,*
|
||||
User-Agent: Mozilla/5.0
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
TEMPLATE = app
|
||||
|
||||
QT += core sql network
|
||||
CONFIG += debug console
|
||||
|
||||
SOURCES += main.cpp
|
||||
|
||||
include(../colnod/colnod.pri)
|
||||
TEMPLATE = app
|
||||
|
||||
QT += core sql network
|
||||
CONFIG += debug console
|
||||
|
||||
SOURCES += main.cpp
|
||||
|
||||
include(../colnod/colnod.pri)
|
||||
|
||||
@ -1,10 +1,20 @@
|
||||
#include <QCoreApplication>
|
||||
#include "requestmanager.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
//RequestManager *reqman = new RequestManager();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include "data.h"
|
||||
#include "datamanager.h"
|
||||
#include "colnodapi.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
ColnodAPI *api = new ColnodAPI();
|
||||
|
||||
api->downloadData(); // this will start downloading data
|
||||
QObject::connect(api, &ColnodAPI::dataDownloaded, [=](){
|
||||
qDebug() << "now I have all data";
|
||||
api->syncData();
|
||||
QCoreApplication::quit();
|
||||
});
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
@ -1,19 +1,25 @@
|
||||
INCLUDEPATH += $$PWD
|
||||
#SRC_DIR = $$PWD
|
||||
message($$PWD)
|
||||
SOURCES += \
|
||||
$$PWD/databasemanager.cpp \
|
||||
$$PWD/dataprocessor.cpp \
|
||||
$$PWD/entity.cpp \
|
||||
$$PWD/requestmanager.cpp \
|
||||
$$PWD/subject.cpp \
|
||||
$$PWD/token.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/databasemanager.h \
|
||||
$$PWD/dataprocessor.h \
|
||||
$$PWD/entity.h \
|
||||
$$PWD/requestmanager.h \
|
||||
$$PWD/subject.h \
|
||||
$$PWD/token.h
|
||||
message($$SOURCES)
|
||||
INCLUDEPATH += $$PWD
|
||||
#SRC_DIR = $$PWD
|
||||
message($$PWD)
|
||||
SOURCES += \
|
||||
$$PWD/colnodapi.cpp \
|
||||
$$PWD/data.cpp \
|
||||
$$PWD/databasemanager.cpp \
|
||||
$$PWD/datamanager.cpp \
|
||||
$$PWD/entity.cpp \
|
||||
$$PWD/requestmanager.cpp \
|
||||
$$PWD/response.cpp \
|
||||
$$PWD/subject.cpp \
|
||||
$$PWD/token.cpp
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/colnodapi.h \
|
||||
$$PWD/data.h \
|
||||
$$PWD/databasemanager.h \
|
||||
$$PWD/datamanager.h \
|
||||
$$PWD/entity.h \
|
||||
$$PWD/requestmanager.h \
|
||||
$$PWD/response.h \
|
||||
$$PWD/subject.h \
|
||||
$$PWD/token.h
|
||||
message($$SOURCES)
|
||||
|
||||
54
src/colnod/colnodapi.cpp
Normal file
54
src/colnod/colnodapi.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "colnodapi.h"
|
||||
|
||||
ColnodAPI::ColnodAPI()
|
||||
{
|
||||
reqMan = new RequestManager();
|
||||
//data = new Data();
|
||||
dataMan = new DataManager();
|
||||
connect(this, &ColnodAPI::partReady, this, &ColnodAPI::checkPartOfData);
|
||||
connect(this, &ColnodAPI::partReady, this, &ColnodAPI::areDataReady);
|
||||
}
|
||||
|
||||
|
||||
// PUBLIC FUNCTIONS
|
||||
void ColnodAPI::downloadData()
|
||||
{
|
||||
Response* resp0 = reqMan->getSubjects();
|
||||
connect(resp0, &Response::finished, [=](){
|
||||
dataMan->localSaveSubjects(resp0->reply->readAll());
|
||||
emit partReady();
|
||||
});
|
||||
|
||||
Response* resp1 = reqMan->getTokens();
|
||||
connect(resp1, &Response::finished, [=](){
|
||||
dataMan->localSaveTokens(resp1->reply->readAll());
|
||||
emit partReady();
|
||||
});
|
||||
|
||||
/*
|
||||
QNetworkReply* reply2 = reqMan->getTokens();
|
||||
connect(reply2, &QNetworkReply::finished, [=](){
|
||||
data->saveTokens(reply2->readAll());
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
void ColnodAPI::syncData()
|
||||
{
|
||||
dataMan->pushDataToDB();
|
||||
}
|
||||
|
||||
// PRIVATE FUNCTIONS
|
||||
void ColnodAPI::areDataReady()
|
||||
{
|
||||
if (index >= numOfMembers)
|
||||
{
|
||||
emit dataDownloaded();
|
||||
}
|
||||
}
|
||||
|
||||
void ColnodAPI::checkPartOfData()
|
||||
{
|
||||
index++;
|
||||
}
|
||||
|
||||
39
src/colnod/colnodapi.h
Normal file
39
src/colnod/colnodapi.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef COLNODAPI_H
|
||||
#define COLNODAPI_H
|
||||
|
||||
#include "requestmanager.h"
|
||||
#include "data.h"
|
||||
#include "datamanager.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class ColnodAPI : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
ColnodAPI();
|
||||
void downloadData();
|
||||
void syncData();
|
||||
|
||||
signals:
|
||||
void dataDownloaded();
|
||||
void partReady();
|
||||
|
||||
private slots:
|
||||
void areDataReady();
|
||||
void checkPartOfData();
|
||||
|
||||
|
||||
private:
|
||||
RequestManager* reqMan = nullptr;
|
||||
// Data* data = nullptr;
|
||||
DataManager* dataMan = nullptr;
|
||||
bool dataReady[2] = {false};
|
||||
int index = 0;
|
||||
const int numOfMembers = 2;
|
||||
};
|
||||
|
||||
#endif // COLNODAPI_H
|
||||
7
src/colnod/data.cpp
Normal file
7
src/colnod/data.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "data.h"
|
||||
|
||||
Data::Data()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
24
src/colnod/data.h
Normal file
24
src/colnod/data.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef DATA_H
|
||||
#define DATA_H
|
||||
#include <QObject>
|
||||
|
||||
#include "subject.h"
|
||||
#include "token.h"
|
||||
#include <qjsondocument.h>
|
||||
#include <qjsonarray.h>
|
||||
|
||||
class Data : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Data();
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DATA_H
|
||||
@ -17,14 +17,24 @@ DatabaseManager::DatabaseManager(QString host, QString dbName, QString user, QSt
|
||||
query = QSqlQuery("query", db);
|
||||
}
|
||||
|
||||
void DatabaseManager::saveSubject(Subject subject, QString table)
|
||||
void DatabaseManager::saveSubject(Subject* subject, QString table)
|
||||
{
|
||||
//TODO
|
||||
query.prepare("INSERT INTO subject (id, name) VALUES (:id, :name)");
|
||||
query.bindValue(":table", table);
|
||||
query.bindValue(":id",subject->getId().toString());
|
||||
qDebug() << subject->getId().toString() << subject->getName();
|
||||
query.bindValue(":name", subject->getName());
|
||||
query.exec();
|
||||
}
|
||||
|
||||
void DatabaseManager::saveToken(Subject subject, QString table)
|
||||
void DatabaseManager::saveToken(Token* token, QString table)
|
||||
{
|
||||
//TODO
|
||||
query.prepare("INSERT INTO :table (id, name) VALUES (:id, :data, :subject_id)");
|
||||
query.bindValue(":table", table);
|
||||
query.bindValue(":id",token->getId());
|
||||
query.bindValue(":data", token->getTokenData());
|
||||
query.bindValue(":subject_id", token->getSubjectId());
|
||||
query.exec();
|
||||
}
|
||||
|
||||
QString DatabaseManager::getTestMessage(int id)
|
||||
@ -42,8 +52,13 @@ void DatabaseManager::createTestRecord(QString msg)
|
||||
query.exec("SELECT MAX(id) FROM test");
|
||||
query.next();
|
||||
int id = query.value(0).toInt() + 1;
|
||||
QString statement = QString("INSERT INTO test (id, message) VALUES (%1, '%2')").arg(id).arg(msg);
|
||||
query.exec(statement);
|
||||
//QString statement = QString("INSERT INTO test (id, message) VALUES (%1, '%2')").arg(id).arg(msg);
|
||||
//query.exec(statement);
|
||||
|
||||
query.prepare("INSERT INTO test (id, message) VALUES (:id, :msg)");
|
||||
query.bindValue(":id", id);
|
||||
query.bindValue(":msg", msg);
|
||||
query.exec();
|
||||
query.next();
|
||||
qDebug() << "U inserted new row, id: " << id << "msg: " << getTestMessage(id);
|
||||
|
||||
|
||||
@ -17,8 +17,8 @@ class DatabaseManager : public QObject
|
||||
public:
|
||||
DatabaseManager(QString host, QString dbName, QString user, QString password);
|
||||
|
||||
void saveSubject(Subject subject, QString table);
|
||||
void saveToken(Subject subject, QString table);
|
||||
void saveSubject(Subject* subject, QString table);
|
||||
void saveToken(Token* token, QString table);
|
||||
|
||||
QString getTestMessage(int id);
|
||||
void createTestRecord(QString msg);
|
||||
|
||||
44
src/colnod/datamanager.cpp
Normal file
44
src/colnod/datamanager.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include "datamanager.h"
|
||||
|
||||
DataManager::DataManager()
|
||||
{
|
||||
db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12");
|
||||
}
|
||||
|
||||
void DataManager::localSaveSubjects(QString response)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
QJsonArray items = doc.object()["items"].toArray();
|
||||
for (QJsonValueRef item : items)
|
||||
{
|
||||
auto subject = new Subject();
|
||||
subject->fromJSON(item.toObject());
|
||||
subjects.append(subject);
|
||||
}
|
||||
}
|
||||
|
||||
void DataManager::localSaveTokens(QString response)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
QJsonArray items = doc.object()["items"].toArray();
|
||||
for (QJsonValueRef item : items)
|
||||
{
|
||||
auto token = new Token();
|
||||
token->fromJSON(item.toObject());
|
||||
tokens.append(token);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void DataManager::pushDataToDB()
|
||||
{
|
||||
for (auto subject : subjects)
|
||||
{
|
||||
db->saveSubject(subject, "subject");
|
||||
}
|
||||
|
||||
for (auto token : tokens)
|
||||
{
|
||||
db->saveToken(token, "Subjects");
|
||||
}
|
||||
}
|
||||
35
src/colnod/datamanager.h
Normal file
35
src/colnod/datamanager.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef DATAMANAGER_H
|
||||
#define DATAMANAGER_H
|
||||
|
||||
#include "token.h"
|
||||
#include "subject.h"
|
||||
#include "requestmanager.h"
|
||||
#include "databasemanager.h"
|
||||
#include <QJsonDocument>
|
||||
#include <qjsonarray.h>
|
||||
#include "data.h"
|
||||
|
||||
|
||||
class DataManager : public QObject
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DataManager();
|
||||
|
||||
void localSaveSubjects(QString jsonResponse);
|
||||
void localSaveTokens(QString jsonResponse);
|
||||
void pushDataToDB();
|
||||
|
||||
|
||||
private:
|
||||
DatabaseManager *db = nullptr;
|
||||
QList<QPointer<Subject>> subjects;
|
||||
QList<QPointer<Token>> tokens;
|
||||
//Data *dataToProcess = nullptr;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DATAMANAGER_H
|
||||
@ -1,56 +0,0 @@
|
||||
#include "dataprocessor.h"
|
||||
|
||||
DataProcessor::DataProcessor()
|
||||
{
|
||||
|
||||
db = new DatabaseManager("192.168.1.94", "deloitte", "postgres", "34rjkciea12");
|
||||
reqMan = new RequestManager;
|
||||
}
|
||||
|
||||
void DataProcessor::processSubjects()
|
||||
{
|
||||
reqMan->getSubjects();
|
||||
QEventLoop loop;
|
||||
QObject::connect(reqMan, &RequestManager::responseReady, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
prepareSubjectJson(reqMan->getResponse());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
QUuid DataProcessor::getSubjectId(const QString &email) const
|
||||
{
|
||||
auto subject = this->getSubjectByEmail(email);
|
||||
if (!subject)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return subject->getId();
|
||||
}
|
||||
|
||||
Subject *DataProccesor::getSubjectById(const QUuid &id) const
|
||||
{
|
||||
return this->subjectsById.value(id);
|
||||
}
|
||||
|
||||
Subject DataProcessor::getSubjectByEmail(const QString &email) const
|
||||
{
|
||||
return this->subjectsByEmail.value(email);
|
||||
}
|
||||
|
||||
Token *Client::getTokenById(const QUuid &id) const
|
||||
{
|
||||
return this->tokensById.value(id);
|
||||
}
|
||||
|
||||
Token *Client::getTokenByData(const QString &data) const
|
||||
{
|
||||
return this->tokensByData.value(data);
|
||||
}*/
|
||||
|
||||
void DataProcessor::prepareSubjectJson(QString json)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
subjectArr = doc.object()["items"].toArray();
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
#ifndef DATAPROCESSOR_H
|
||||
#define DATAPROCESSOR_H
|
||||
|
||||
#include "token.h"
|
||||
#include "subject.h"
|
||||
#include "requestmanager.h"
|
||||
#include "databasemanager.h"
|
||||
#include <QJsonDocument>
|
||||
#include <qjsonarray.h>
|
||||
|
||||
|
||||
|
||||
class DataProcessor : public QObject
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
DataProcessor();
|
||||
|
||||
void processSubjects();
|
||||
|
||||
Subject *getSubjectById(const QUuid &id) const;
|
||||
Subject *getSubjectByEmail(const QString &email) const;
|
||||
QUuid getSubjectId(const QString &email) const;
|
||||
|
||||
Token *getTokenById(const QUuid &id) const;
|
||||
Token *getTokenByData(const QString &data) const;
|
||||
|
||||
private slots:
|
||||
void prepareSubjectJson(QString json);
|
||||
|
||||
private:
|
||||
RequestManager *reqMan;
|
||||
DatabaseManager *db;
|
||||
QJsonArray subjectArr;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DATAPROCESSOR_H
|
||||
@ -5,35 +5,34 @@ RequestManager::RequestManager()
|
||||
manager = new QNetworkAccessManager();
|
||||
token = "";
|
||||
loginStatus = false; //should be false, true for testing request header
|
||||
|
||||
|
||||
|
||||
/*
|
||||
QFile configFile(QDir::current().filePath("config.json"));
|
||||
if (!configFile.open(QIODevice::ReadOnly)) {
|
||||
qCritical() << "Couldn't open config file config.json" << QDir::current().filePath("config.json");
|
||||
QCoreApplication::exit(-1);
|
||||
return;
|
||||
}
|
||||
this->config = QJsonDocument::fromJson(configFile.readAll());
|
||||
this->config = QJsonDocument::fromJson(configFile.readAll());*/
|
||||
QObject::connect(manager, &QNetworkAccessManager::sslErrors, this, &RequestManager::onSSLError);
|
||||
}
|
||||
|
||||
void RequestManager::login(QString host, QString username, QString password)
|
||||
{
|
||||
|
||||
QNetworkRequest request;
|
||||
QString hash = encryptedPassword(password);
|
||||
authHeader = getLoginHeader(username, hash);
|
||||
QString hash = encryptedPassword(this->password);
|
||||
authHeader = getLoginHeader(this->user, hash);
|
||||
QJsonObject reqBody = {
|
||||
{"password", hash},
|
||||
{"username", username}
|
||||
{"username", this->user}
|
||||
};
|
||||
this->host = host;
|
||||
//this->host = host;
|
||||
|
||||
request.setUrl(host + "/AaaManager/authSession");
|
||||
request.setUrl(this->host + "/AaaManager/authSession");
|
||||
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8");
|
||||
QJsonDocument jsonDocument;
|
||||
|
||||
QJsonDocument jsonDocument;
|
||||
jsonDocument.setObject(reqBody);
|
||||
QNetworkReply* reply = manager->post(request, jsonDocument.toJson());
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
@ -43,6 +42,8 @@ void RequestManager::login(QString host, QString username, QString password)
|
||||
loginStatus = false;
|
||||
return;
|
||||
}
|
||||
if (loginStatus)
|
||||
return;
|
||||
jsonResponse = reply->readAll();
|
||||
prepareAuthHeader();
|
||||
loginStatus = true;
|
||||
@ -51,51 +52,25 @@ void RequestManager::login(QString host, QString username, QString password)
|
||||
});
|
||||
}
|
||||
|
||||
void RequestManager::getRequest(QString endpoint)
|
||||
QNetworkReply* RequestManager::getRequest(QString endpoint)
|
||||
{
|
||||
if (!loginStatus)
|
||||
{
|
||||
login(config.object()["host"].toString(), config.object()["username"].toString(), config.object()["password"].toString());
|
||||
// QEventLoop loop;
|
||||
// QObject::connect(this, &RequestManager::successLogin, &loop, &QEventLoop::quit);
|
||||
// loop.exec();
|
||||
}
|
||||
|
||||
//
|
||||
//host = config.object()["host"].toString();
|
||||
//
|
||||
|
||||
QNetworkRequest request;
|
||||
request.setUrl(host + endpoint);
|
||||
qDebug() << authHeader;
|
||||
qDebug() << "Auth: " << authHeader;
|
||||
request.setRawHeader("Authorization", authHeader.toLocal8Bit());
|
||||
request.setRawHeader("User-Agent", "curl/7.65.3");
|
||||
request.setRawHeader("Connection", "Keep-Alive");
|
||||
request.setRawHeader("Accept", "*/*");
|
||||
request.setRawHeader("Accept-Encoding", "*");
|
||||
request.setRawHeader("Accept-Language", "*");
|
||||
|
||||
QNetworkReply* reply = manager->post(request, QByteArray());
|
||||
|
||||
//
|
||||
//QEventLoop loop;
|
||||
//QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
// loop.exec();
|
||||
//
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
if (reply->error())
|
||||
{
|
||||
qDebug() << reply->errorString();
|
||||
qDebug() << reply->readAll();
|
||||
loginStatus = false;
|
||||
getRequest(endpoint);
|
||||
//getRequest(endpoint);
|
||||
return;
|
||||
}
|
||||
qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
||||
jsonResponse = reply->readAll();
|
||||
emit responseReady();
|
||||
});
|
||||
return reply;
|
||||
|
||||
}
|
||||
|
||||
QString RequestManager::getFromJson(QString json, const QString &member)
|
||||
@ -124,7 +99,7 @@ void RequestManager::prepareAuthHeader()
|
||||
{
|
||||
token = getFromJson(jsonResponse, "id");
|
||||
QString authValue = token + ":";
|
||||
authHeader = "Basic" + authValue.toLocal8Bit().toBase64();
|
||||
authHeader = "Basic " + authValue.toLocal8Bit().toBase64();
|
||||
}
|
||||
|
||||
void RequestManager::onSSLError(QNetworkReply *reply, const QList<QSslError> &errors)
|
||||
@ -139,7 +114,62 @@ void RequestManager::onSSLError(QNetworkReply *reply, const QList<QSslError> &er
|
||||
|
||||
}
|
||||
|
||||
void RequestManager::getSubjects()
|
||||
Response* RequestManager::getSubjects()
|
||||
{
|
||||
getRequest("/SubjectManager/getSubjects");
|
||||
auto response = new Response;
|
||||
if (!loginStatus){
|
||||
login(host, user, password);
|
||||
connect(this, &RequestManager::successLogin, [=](){
|
||||
auto subjectReply = getRequest("/SubjectManager/getSubjects");
|
||||
response->reply = subjectReply;
|
||||
connect(subjectReply, &QNetworkReply::finished, response, &Response::finished);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
QNetworkReply* reply = getRequest("/SubjectManager/getSubjects");
|
||||
connect(reply, &QNetworkReply::finished, response, &Response::finished);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
Response* RequestManager::getTokens()
|
||||
{
|
||||
auto response = new Response;
|
||||
if (!loginStatus){
|
||||
login(host, user, password);
|
||||
connect(this, &RequestManager::successLogin, [=](){
|
||||
auto subjectReply = getRequest("/TokenManager/getTokens");
|
||||
response->reply = subjectReply;
|
||||
connect(subjectReply, &QNetworkReply::finished, response, &Response::finished);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
QNetworkReply* reply = getRequest("/TokenManager/getTokens");
|
||||
connect(reply, &QNetworkReply::finished, response, &Response::finished);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
Response* RequestManager::getTokenSubject()
|
||||
{
|
||||
auto response = new Response;
|
||||
if (!loginStatus){
|
||||
login(host, user, password);
|
||||
connect(this, &RequestManager::successLogin, [=](){
|
||||
auto subjectReply = getRequest("/OpenManager/getTokenSubject");
|
||||
response->reply = subjectReply;
|
||||
connect(subjectReply, &QNetworkReply::finished, response, &Response::finished);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
QNetworkReply* reply = getRequest("/OpenManager/getTokenSubject");
|
||||
connect(reply, &QNetworkReply::finished, response, &Response::finished);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@ -1,72 +1,71 @@
|
||||
#ifndef REQUESTMANAGER_H
|
||||
#define REQUESTMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <qcoreapplication.h>
|
||||
#include <qnetworkreply.h>
|
||||
#include <QByteArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
#include <QMultiMap>
|
||||
#include <QPointer>
|
||||
#include <QDir>
|
||||
#include <QSslError>
|
||||
|
||||
|
||||
#include "databasemanager.h"
|
||||
#include "token.h"
|
||||
#include "subject.h"
|
||||
|
||||
class RequestManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
RequestManager();
|
||||
void getSubjects();
|
||||
void login(QString endpoint, QString username, QString password);
|
||||
void getRequest(QString endpoint);
|
||||
void loadSubjectsFromColnodToDB();
|
||||
|
||||
|
||||
|
||||
const QJsonDocument &getConfig() const {return config;}
|
||||
const QString &getResponse() const {return jsonResponse;}
|
||||
QString getToken(){return token;}
|
||||
|
||||
private:
|
||||
|
||||
QString getFromJson(QString json, const QString &member);
|
||||
QString encryptedPassword(QString password);
|
||||
QString getLoginHeader(QString username, QString hash);
|
||||
void prepareAuthHeader();
|
||||
|
||||
private slots:
|
||||
void onSSLError(QNetworkReply *reply, const QList<QSslError> &errors);
|
||||
|
||||
signals:
|
||||
void successLogin();
|
||||
void responseReady();
|
||||
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* manager;
|
||||
QJsonDocument config;
|
||||
QString authHeader;
|
||||
QString host;
|
||||
QString token;
|
||||
QString jsonResponse;
|
||||
bool loginStatus;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // REQUESTMANAGER_H
|
||||
#ifndef REQUESTMANAGER_H
|
||||
#define REQUESTMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <qcoreapplication.h>
|
||||
#include <qnetworkreply.h>
|
||||
#include <QByteArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
#include <QMultiMap>
|
||||
#include <QPointer>
|
||||
#include <QDir>
|
||||
#include <QSslError>
|
||||
|
||||
|
||||
#include "response.h"
|
||||
|
||||
class RequestManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
RequestManager();
|
||||
Response* getSubjects();
|
||||
Response* getTokens();
|
||||
Response* getTokenSubject();
|
||||
void login(QString endpoint, QString username, QString password); //I want it private, but unittest???
|
||||
|
||||
const QJsonDocument &getConfig() const {return config;}
|
||||
const QString &getResponse() const {return jsonResponse;}
|
||||
|
||||
private:
|
||||
QNetworkReply* getRequest(QString endpoint);
|
||||
QString getFromJson(QString json, const QString &member);
|
||||
QString encryptedPassword(QString password);
|
||||
QString getLoginHeader(QString username, QString hash);
|
||||
void prepareAuthHeader();
|
||||
|
||||
private slots:
|
||||
void onSSLError(QNetworkReply *reply, const QList<QSslError> &errors);
|
||||
|
||||
|
||||
signals:
|
||||
void successLogin();
|
||||
void responseReady();
|
||||
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* manager = nullptr;
|
||||
QJsonDocument config;
|
||||
QString authHeader;
|
||||
QString host = "https://192.168.1.3:8443";
|
||||
//QString host = "http://localhost:8080";
|
||||
|
||||
QString password = "spaceti1";
|
||||
QString user = "spaceti";
|
||||
QString token;
|
||||
QString jsonResponse;
|
||||
bool loginStatus;
|
||||
|
||||
};
|
||||
|
||||
#endif // REQUESTMANAGER_H
|
||||
|
||||
6
src/colnod/response.cpp
Normal file
6
src/colnod/response.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "response.h"
|
||||
|
||||
Response::Response()
|
||||
{
|
||||
|
||||
}
|
||||
25
src/colnod/response.h
Normal file
25
src/colnod/response.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef RESPONSE_H
|
||||
#define RESPONSE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <qnetworkreply.h>
|
||||
|
||||
class Response : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Response();
|
||||
|
||||
public:
|
||||
QNetworkReply *reply = nullptr;
|
||||
|
||||
signals:
|
||||
void finished();
|
||||
|
||||
public slots:
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif // RESPONSE_H
|
||||
@ -20,12 +20,11 @@ void UnitTest::getSubjects()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/SubjectManager/getSubjects");
|
||||
testAccess->getSubjects();
|
||||
spy.wait();
|
||||
QVERIFY(spy.isValid());
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QString json = testAccess->getResponse();
|
||||
qDebug() << json;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
QVERIFY(doc.isObject());
|
||||
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
||||
@ -38,19 +37,24 @@ void UnitTest::getTokens()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/TokenManager/getTokens");
|
||||
testAccess->getTokens();
|
||||
QCOMPARE(spy.count(), 0);
|
||||
spy.wait();
|
||||
QVERIFY(spy.isValid());
|
||||
QCOMPARE(spy.count(), 1);
|
||||
qDebug() << testAccess->getResponse();
|
||||
QString json = testAccess->getResponse();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
|
||||
QVERIFY(doc.isObject());
|
||||
QVERIFY2(doc.object().value("items").isArray(), "No parameter 'id' in response");
|
||||
qDebug() << doc.object().value("items").toArray()[0].toObject()["id"].toString();
|
||||
QVERIFY2(doc.object().value("items").toArray()[0].toObject()["id"].isString(), "ada");
|
||||
}
|
||||
|
||||
void UnitTest::getTokensOpenManager()
|
||||
void UnitTest::getTokenSubject()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/OpenManager/getTokenSubject");
|
||||
testAccess->getTokenSubject();
|
||||
QCOMPARE(spy.count(), 0);
|
||||
spy.wait();
|
||||
QVERIFY(spy.isValid());
|
||||
@ -75,13 +79,10 @@ void UnitTest::subjectJSONParser()
|
||||
QJsonObject obj = doc.object();
|
||||
QJsonArray arr = obj["items"].toArray();
|
||||
*/
|
||||
QVERIFY(true);
|
||||
return;
|
||||
|
||||
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/SubjectManager/getSubjects");
|
||||
testAccess->getSubjects();
|
||||
spy.wait();
|
||||
QString response = testAccess->getResponse();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
@ -96,19 +97,15 @@ void UnitTest::tokenJSONParser()
|
||||
{
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QSignalSpy spy(testAccess, SIGNAL(responseReady()));
|
||||
testAccess->getRequest("/TokenManager/getTokens");
|
||||
testAccess->getTokens();
|
||||
spy.wait();
|
||||
QString response = testAccess->getResponse();
|
||||
|
||||
//TODO
|
||||
/* QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
|
||||
QJsonArray arr = doc.object()["items"].toArray();
|
||||
auto testSubject = new Subject();
|
||||
testSubject->fromJSON(arr[1].toObject());
|
||||
qDebug() << testSubject->getName();
|
||||
QVERIFY(!testSubject->getName().isEmpty());*/
|
||||
QVERIFY(true);
|
||||
|
||||
auto testToken = new Token();
|
||||
testToken->fromJSON(arr[5].toObject());
|
||||
qDebug() << testToken->getName() << " Size: " << arr.size();
|
||||
QVERIFY(!testToken->getName().isEmpty());
|
||||
}
|
||||
|
||||
void UnitTest::connectToDB()
|
||||
@ -118,7 +115,7 @@ void UnitTest::connectToDB()
|
||||
qDebug() << db->getTestMessage(1);
|
||||
QVERIFY(true);
|
||||
QCOMPARE(db->getTestMessage(1), QStringLiteral("Hello world"));
|
||||
db->createTestRecord("Does it work?");
|
||||
db->createTestRecord("Now it's diffrent");
|
||||
QCOMPARE(db->getTestMessage(2), QStringLiteral("Does it work?"));
|
||||
}
|
||||
|
||||
@ -132,44 +129,43 @@ void UnitTest::debugConnection()
|
||||
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\"}";
|
||||
/*POST /AaaManager/authSession HTTP/1.1
|
||||
Host: localhost:8080
|
||||
Authorization: Basic c3BhY2V0aTpFN0ZFQTA3REJDODJGQjlCQTM0QjJFNDUzRTJFRkU4OUYwNTgzRTcxODc1NDM1MDdFRjExRTg0NzI2MDcxRkU0Mjg0NDUzNzJGMkU0NkI0NTVFQjBDNzk3NDdDNTFFMzYyRjQwM0NFMzk0NzNBMkE4QzJERkEwQ0U1Qzk4MjZDMA==
|
||||
User-Agent: curl/7.65.3
|
||||
Accept:
|
||||
Content-Type: application/json
|
||||
Content-Length: 164
|
||||
|
||||
{"password":"E7FEA07DBC82FB9BA34B2E453E2EFE89F0583E7187543507EF11E84726071FE428445372F2E46B455EB0C79747C51E362F403CE39473A2A8C2DFA0CE5C9826C0","username":"spaceti"}*/
|
||||
qDebug() << tmp.toUtf8();
|
||||
qDebug().noquote() << tmp.toUtf8();
|
||||
ssl.write(tmp.toUtf8());
|
||||
QString data;
|
||||
while (ssl.waitForReadyRead() && data.isEmpty())
|
||||
{
|
||||
data.append(ssl.readAll().data());
|
||||
}
|
||||
qDebug() << data;
|
||||
qDebug().noquote() << data;
|
||||
int index = data.lastIndexOf("id");
|
||||
QString tkn = data.mid( index + 5, 36);
|
||||
tkn = tkn + ":";
|
||||
qDebug() << "parsed token : " <<tkn;
|
||||
/*
|
||||
RequestManager* testAccess = new RequestManager();
|
||||
QJsonDocument config = testAccess->getConfig();
|
||||
QSignalSpy spy(testAccess, SIGNAL(successLogin()));
|
||||
testAccess->login(host, email, password);
|
||||
spy.wait();
|
||||
|
||||
QString token = testAccess->getToken();
|
||||
token = token + ":";*/
|
||||
tmp = "POST /SubjectManager/getSubjects\r\nHTTP/1.1 Host: 192.168.1.3:8443\r\nAuthorization: Basic " + tkn.toUtf8().toBase64() + "\r\nUser-Agent: curl/7.65.3\r\nAccept: */*)";
|
||||
qDebug() << tmp.toUtf8();
|
||||
tmp = "POST /SubjectManager/getSubjects HTTP/1.1\r\nHost: 192.168.1.3:8443\r\nAuthorization: Basic " + tkn.toUtf8().toBase64() + "\r\nUser-Agent: curl/7.65.3\r\nAccept: */*\r\n\r\n";
|
||||
QFile file("output1234.txt");
|
||||
file.open(QIODevice::ReadWrite);
|
||||
QTextStream st(&file);
|
||||
st << tmp;
|
||||
file.close();
|
||||
qDebug().noquote() << tmp.toUtf8();
|
||||
ssl.write(tmp.toUtf8());
|
||||
while (ssl.waitForReadyRead())
|
||||
{
|
||||
qDebug() << ssl.readAll().data();
|
||||
}
|
||||
qDebug() << ssl.errorString();
|
||||
|
||||
QVERIFY(true);
|
||||
}
|
||||
|
||||
void UnitTest::saveDataToDB()
|
||||
{
|
||||
DataManager* dataMan = new DataManager();
|
||||
|
||||
// dataMan->localSaveTokens();
|
||||
QVERIFY(true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
QTEST_MAIN(UnitTest)
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include "subject.h"
|
||||
#include "databasemanager.h"
|
||||
#include <QSslSocket>
|
||||
#include "datamanager.h"
|
||||
|
||||
|
||||
class UnitTest : public QObject
|
||||
@ -18,11 +19,12 @@ private slots:
|
||||
void loginSuccessfully(); // /AaaManager/authSession
|
||||
void getSubjects(); // /SubjectManager/getSubjects
|
||||
void getTokens(); // /TokenManager/getTokens
|
||||
void getTokensOpenManager(); // /OpenManager/getTokenSubject
|
||||
void getTokenSubject(); // /OpenManager/getTokenSubject
|
||||
void subjectJSONParser();
|
||||
void tokenJSONParser();
|
||||
void connectToDB();
|
||||
void debugConnection();
|
||||
void saveDataToDB();
|
||||
|
||||
private:
|
||||
//QString host = "http://localhost:8080"; //colnod server (ssh -L 8080:192.168.254.11:8443 penta-master)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user