diff options
author | Andras Becsi <andras.becsi@digia.com> | 2013-06-19 10:25:16 +0200 |
---|---|---|
committer | Andras Becsi <andras.becsi@digia.com> | 2013-06-19 13:18:07 +0200 |
commit | 8850b68bc80eb7ce2855ad32ed3f8516094871b6 (patch) | |
tree | 68c622c17c6e83fcf71768a56c5e23fba75bac11 /tests/auto | |
parent | 990c8d6e669da4e63560ee8207242ea6c9b10820 (diff) | |
download | qtenginio-8850b68bc80eb7ce2855ad32ed3f8516094871b6.tar.gz |
Refactor EnginioBackendManager and add createObjectType function.
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/common/common.cpp | 150 | ||||
-rw-r--r-- | tests/auto/common/common.h | 13 | ||||
-rw-r--r-- | tests/auto/enginioclient/tst_enginioclient.cpp | 15 |
3 files changed, 111 insertions, 67 deletions
diff --git a/tests/auto/common/common.cpp b/tests/auto/common/common.cpp index ea2d488..e9355c1 100644 --- a/tests/auto/common/common.cpp +++ b/tests/auto/common/common.cpp @@ -44,6 +44,10 @@ namespace EnginioTests { +static const QByteArray GET = QByteArrayLiteral("GET"); +static const QByteArray POST = QByteArrayLiteral("POST"); +static const QByteArray DELETE = QByteArrayLiteral("DELETE"); + EnginioBackendManager::EnginioBackendManager(QObject *parent) : QObject(parent) , _email(qgetenv("ENGINIO_EMAIL_ADDRESS")) @@ -54,8 +58,14 @@ EnginioBackendManager::EnginioBackendManager(QObject *parent) qDebug("Needed environment variables ENGINIO_EMAIL_ADDRESS, ENGINIO_LOGIN_PASSWORD are not set. Backend setup failed!"); return; } + + _headers["Accept"] = QStringLiteral("application/json"); + QObject::connect(&_client, SIGNAL(error(EnginioReply *)), this, SLOT(error(EnginioReply *))); QObject::connect(&_client, SIGNAL(finished(EnginioReply *)), this, SLOT(finished(EnginioReply *))); + + if (!authenticate()) + qDebug("ERROR: Session authentication failed!"); } EnginioBackendManager::~EnginioBackendManager() @@ -80,109 +90,121 @@ bool EnginioBackendManager::synchronousRequest(const QByteArray &httpOperation, { QSignalSpy finishedSpy(&_client, SIGNAL(finished(EnginioReply *))); QSignalSpy errorSpy(&_client, SIGNAL(error(EnginioReply *))); + _responseData = QJsonObject(); _client.customRequest(_url, httpOperation, data); return finishedSpy.wait(10000) && !errorSpy.count(); } -QString EnginioBackendManager::authenticate() +bool EnginioBackendManager::authenticate() { - qDebug("\tAuthenticating with Enginio server."); - QJsonObject obj; QJsonObject credentials; credentials["email"] = _email; credentials["password"] = _password; - QJsonObject headers; - headers["Accept"] = QStringLiteral("application/json"); + QJsonObject obj; obj["payload"] = credentials; - obj["headers"] = headers; + obj["headers"] = _headers; _url.setPath(QStringLiteral("/v1/account/auth/identity")); // Authenticate developer - if (!synchronousRequest(QByteArrayLiteral("POST"), obj)) - return QString(); + synchronousRequest(POST, obj); + QString sessionToken = _responseData["sessionToken"].toString(); + _headers["Enginio-Backend-Session"] = sessionToken; - return _responseData["sessionToken"].toString(); + return !sessionToken.isEmpty(); } -bool EnginioBackendManager::createBackend(const QString& backendName) +QString EnginioBackendManager::getBackenId(const QString &backendName) { - qDebug("\t## Creating backend: %s", backendName.toUtf8().data()); - QString sessionToken = authenticate(); - if (sessionToken.isEmpty()) { - qDebug("ERROR: Session authentication failed!"); - return false; + QJsonArray results = getAllBackends(); + QString backendId; + foreach (const QJsonValue& value, results) { + QJsonObject data = value.toObject(); + if (data["name"].toString() == backendName) { + backendId = data["id"].toString(); + break; + } } + return backendId; +} - // Get all backends +QJsonArray EnginioBackendManager::getAllBackends() +{ QJsonObject obj; - QJsonObject headers; - headers["Accept"] = QStringLiteral("application/json"); - headers["Enginio-Backend-Session"] = sessionToken; + obj["headers"] = _headers; _url.setPath("/v1/account/apps"); - obj["headers"] = headers; - if (!synchronousRequest(QByteArrayLiteral("GET"), obj)) + synchronousRequest(GET, obj); + return _responseData["results"].toArray(); +} + +bool EnginioBackendManager::removeBackendWithId(const QString &backendId) +{ + if (backendId.isEmpty()) return false; - QJsonArray results = _responseData["results"].toArray(); - qDebug() << "\t" << results.count() << "backends found."; - foreach (const QJsonValue& value, results) { - if (value.toObject()["name"].toString() == backendName) { - removeBackend(backendName); - break; - } - } + QJsonObject obj; + obj["headers"] = _headers; + QString appsPath = QStringLiteral("/v1/account/apps/"); + appsPath.append(backendId); + _url.setPath(appsPath); + return synchronousRequest(DELETE, obj); +} + +bool EnginioBackendManager::createBackend(const QString &backendName) +{ + qDebug("## Creating backend: %s", backendName.toUtf8().data()); + QJsonObject obj; + obj["headers"] = _headers; QJsonObject backend; backend["name"] = backendName; obj["payload"] = backend; + _url.setPath("/v1/account/apps"); - if (!synchronousRequest(QByteArrayLiteral("POST"), obj)) - return false; - - return true; + return synchronousRequest(POST, obj); } bool EnginioBackendManager::removeBackend(const QString &backendName) { - qDebug("\t## Deleting backend: %s", backendName.toUtf8().data()); - QString sessionToken = authenticate(); - if (sessionToken.isEmpty()) { - qDebug("ERROR: Session authentication failed!"); - return false; - } - - // Get all backends - QJsonObject obj; - QJsonObject headers; - QString appsPath = QStringLiteral("/v1/account/apps"); - headers["Accept"] = QStringLiteral("application/json"); - headers["Enginio-Backend-Session"] = sessionToken; - _url.setPath(appsPath); - obj["headers"] = headers; + qDebug("## Deleting backend: %s", backendName.toUtf8().data()); + return removeBackendWithId(getBackenId(backendName)); +} - if (!synchronousRequest(QByteArrayLiteral("GET"), obj)) - return false; +bool EnginioBackendManager::createObjectType(const QString &backendName, const QString &environment, const QJsonObject &schema) +{ + qDebug("## Create new object type on backend: %s", backendName.toUtf8().data()); + + QJsonArray environments = _responseData["environments"].toArray(); + if (environments.isEmpty()) { + // FIXME: This should not be needed if we know how to query the environments. + removeBackend(backendName); + createBackend(backendName); + environments = _responseData["environments"].toArray(); + } QString backendId; - QJsonArray results = _responseData["results"].toArray(); - foreach (const QJsonValue& value, results) { - QJsonObject data = value.toObject(); - if (data["name"].toString() == backendName) { - backendId = data["id"].toString(); - break; + QString backendMasterKey; + + foreach (const QJsonValue &value, environments) { + QJsonObject env = value.toObject(); + qDebug() << env; + if (env["name"].toString() == environment) { + backendId = env["id"].toString(); + QJsonArray masterKeys = env["masterKeys"].toArray(); + QJsonObject masterKey = masterKeys.first().toObject(); + backendMasterKey = masterKey["key"].toString(); } } - if (backendId.isEmpty()) - return false; + _headers["Enginio-Backend-Id"] = backendId; + _headers["Enginio-Backend-MasterKey"] = backendMasterKey; - appsPath.append("/").append(backendId); - _url.setPath(appsPath); - if (!synchronousRequest(QByteArrayLiteral("DELETE"), obj)) - return false; + QJsonObject obj; + obj["headers"] = _headers; + obj["payload"] = schema; - return true; + _url.setPath("/v1/object_types"); + synchronousRequest(POST, obj); + return !_responseData["properties"].toArray().isEmpty(); } - } diff --git a/tests/auto/common/common.h b/tests/auto/common/common.h index cf460b8..04f1607 100644 --- a/tests/auto/common/common.h +++ b/tests/auto/common/common.h @@ -2,6 +2,8 @@ #define ENGINIOTESTSCOMMON_H #include <Enginio/enginioclient.h> +#include <QtCore/qjsonobject.h> +#include <QtCore/qjsonarray.h> #include <QtCore/qurl.h> class EnginioReply; @@ -17,13 +19,17 @@ class EnginioBackendManager: public QObject Q_OBJECT EnginioClient _client; + QJsonObject _headers; QJsonObject _responseData; QString _email; QString _password; QUrl _url; bool synchronousRequest(const QByteArray &httpOperation, const QJsonObject &data = QJsonObject()); - QString authenticate(); + bool removeBackendWithId(const QString &backendId); + bool authenticate(); + QString getBackenId(const QString &backendName); + QJsonArray getAllBackends(); public slots: void error(EnginioReply *reply); @@ -32,8 +38,9 @@ public slots: public: explicit EnginioBackendManager(QObject *parent = 0); virtual ~EnginioBackendManager(); - bool createBackend(const QString& backendName); - bool removeBackend(const QString& backendName); + bool createBackend(const QString &backendName); + bool removeBackend(const QString &backendName); + bool createObjectType(const QString &backendName, const QString &environment, const QJsonObject &schema); }; } diff --git a/tests/auto/enginioclient/tst_enginioclient.cpp b/tests/auto/enginioclient/tst_enginioclient.cpp index 99c364c..0d58801 100644 --- a/tests/auto/enginioclient/tst_enginioclient.cpp +++ b/tests/auto/enginioclient/tst_enginioclient.cpp @@ -288,6 +288,21 @@ void tst_EnginioClient::customRequest() EnginioTests::EnginioBackendManager backendManager(this); QVERIFY(backendManager.createBackend(backendName)); + QJsonObject schema; + schema["name"] = QStringLiteral("places"); + QJsonArray array; + QJsonObject title; + title["name"] = QStringLiteral("title"); + title["type"] = QStringLiteral("string"); + title["indexed"] = false; + QJsonObject photo; + photo["name"] = QStringLiteral("photo"); + photo["type"] = QStringLiteral("ref"); + photo["objectType"] = QStringLiteral("files"); + array.append(title); + array.append(photo); + schema["properties"] = array; + QVERIFY(backendManager.createObjectType(backendName, QStringLiteral("development"), schema)); QVERIFY(backendManager.removeBackend(backendName)); } |