From f341dbcce5b28a0ad15d41fea52f396b968cae16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20L=C3=B3pez=20Zubieta?= Date: Sat, 28 Oct 2017 13:56:10 -0500 Subject: [PATCH 1/4] Adapting updater to work with github releases. --- src/AppGui.cpp | 10 +-- src/QSimpleUpdater/src/Updater.cpp | 61 ++++++++++++++++--- tests/FilesCacheTests.cpp | 0 .../{tst_filescache.cpp => FilesCacheTests.h} | 0 tests/UpdaterTests.cpp | 0 tests/main.cpp | 0 6 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 tests/FilesCacheTests.cpp rename tests/{tst_filescache.cpp => FilesCacheTests.h} (100%) create mode 100644 tests/UpdaterTests.cpp create mode 100644 tests/main.cpp diff --git a/src/AppGui.cpp b/src/AppGui.cpp index bf033f68..5bd3ae75 100644 --- a/src/AppGui.cpp +++ b/src/AppGui.cpp @@ -24,6 +24,8 @@ #include "MacUtils.h" #endif +#define GITHUB_UPDATE_URL "https://api.github.com/repos/mooltipass/moolticute/releases" + #if defined(Q_OS_WIN) #define MC_UPDATE_URL "https://calaos.fr/mooltipass/windows/updater.json" #elif defined(Q_OS_MAC) @@ -521,10 +523,10 @@ void AppGui::checkUpdate(bool displayMessage) return; auto u = QSimpleUpdater::getInstance(); - u->setModuleVersion(MC_UPDATE_URL, APP_VERSION); - u->setNotifyOnUpdate(MC_UPDATE_URL, true); - u->setDownloaderEnabled(MC_UPDATE_URL, true); - u->setNotifyOnFinish(MC_UPDATE_URL, displayMessage); + u->setModuleVersion(GITHUB_UPDATE_URL, APP_VERSION); + u->setNotifyOnUpdate(GITHUB_UPDATE_URL, true); + u->setDownloaderEnabled(GITHUB_UPDATE_URL, true); + u->setNotifyOnFinish(GITHUB_UPDATE_URL, displayMessage); u->checkForUpdates(MC_UPDATE_URL); diff --git a/src/QSimpleUpdater/src/Updater.cpp b/src/QSimpleUpdater/src/Updater.cpp index 6c95a4fb..327dac7c 100644 --- a/src/QSimpleUpdater/src/Updater.cpp +++ b/src/QSimpleUpdater/src/Updater.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -361,18 +362,58 @@ void Updater::onReply (QNetworkReply* reply) return; } - /* Get the platform information */ - QJsonObject updates = document.object().value ("updates").toObject(); - QJsonObject platform = updates.value (platformKey()).toObject(); + /* Get latest GitHub Release */ + QJsonArray githubReleases = document.array(); + QJsonObject latestGithubRelese = githubReleases.at(0).toObject(); + QString tagName = latestGithubRelese.value("tag_name").toString(); + QString htmlUrl = latestGithubRelese.value("html_url").toString(); + QString body = latestGithubRelese.value("body").toString(); - /* Get update information */ - m_openUrl = platform.value ("open-url").toString(); - m_changelog = platform.value ("changelog").toString(); - m_downloadUrl = platform.value ("download-url").toString(); - m_latestVersion = platform.value ("latest-version").toString(); + QString releaseFileSuffix; + if (platformKey().compare("osx") == 0) + releaseFileSuffix = ".dmg"; - /* Compare latest and current version */ - setUpdateAvailable (latestVersion() != moduleVersion()); + if (platformKey().compare("linux") == 0) + releaseFileSuffix = ".deb"; + + if (platformKey().compare("windows") == 0) + releaseFileSuffix = ".exe"; + + if (releaseFileSuffix.isEmpty()) + { + qWarning() << "Automatic updates are not suppurted in this platform " << platformKey(); + setUpdateAvailable(false); + emit checkingFinished (url()); + return; + } + + QJsonArray githubReleaseAssets = latestGithubRelese.value("assets").toArray(); + bool releaseFound = false; + QString releaseName; + QString downloadUrl; + for (QJsonValue jsonValue : githubReleaseAssets) + { + QJsonObject releaseAsset = jsonValue.toObject(); + releaseName = releaseAsset.value("name").toString(); + + if (releaseName.endsWith(releaseFileSuffix)) + { + releaseFound = true; + downloadUrl = releaseAsset.value("browser_download_url").toString(); + break; + } + } + if (releaseFound) + { + m_openUrl = htmlUrl; + m_changelog = body; + m_downloadUrl = downloadUrl; + m_latestVersion = tagName; + qDebug() << m_downloadUrl; + + /* Compare latest and current version */ + setUpdateAvailable (latestVersion() != moduleVersion()); + } emit checkingFinished (url()); } diff --git a/tests/FilesCacheTests.cpp b/tests/FilesCacheTests.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/tst_filescache.cpp b/tests/FilesCacheTests.h similarity index 100% rename from tests/tst_filescache.cpp rename to tests/FilesCacheTests.h diff --git a/tests/UpdaterTests.cpp b/tests/UpdaterTests.cpp new file mode 100644 index 00000000..e69de29b diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 00000000..e69de29b From d59f4eec7fca0b4a47ab95c78418fad69582d0d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20L=C3=B3pez=20Zubieta?= Date: Sat, 28 Oct 2017 13:57:16 -0500 Subject: [PATCH 2/4] Refactor tests and add updater tests. --- tests/FilesCacheTests.cpp | 33 +++++++++++++++++++++++++++++++++ tests/FilesCacheTests.h | 37 ++----------------------------------- tests/UpdaterTests.cpp | 24 ++++++++++++++++++++++++ tests/UpdaterTests.h | 11 +++++++++++ tests/main.cpp | 26 ++++++++++++++++++++++++++ tests/tests.pro | 11 ++++++++--- 6 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 tests/UpdaterTests.h diff --git a/tests/FilesCacheTests.cpp b/tests/FilesCacheTests.cpp index e69de29b..b036463a 100644 --- a/tests/FilesCacheTests.cpp +++ b/tests/FilesCacheTests.cpp @@ -0,0 +1,33 @@ +#include "FilesCacheTests.h" + +FilesCacheTests::FilesCacheTests() +{ +} + +void FilesCacheTests::testSaveAndLoadFileNames() +{ + QList testFiles; + for (int i = 0; i< 3; i++) + { + QVariantMap item; + item.insert("revision", 0); + item.insert("name", QString("file %1").arg(i)); + item.insert("size", 1024*i); + testFiles << item; + } + + FilesCache cache; + cache.setDbChangeNumber(0); + cache.setCardCPZ("cbe9cad108aad501"); + + bool result = cache.save(testFiles); + QVERIFY(result); + + QList fileInCache = cache.load(); + + QVERIFY(fileInCache.size() == 3); + for (int i = 0; i < testFiles.size(); i ++) + QVERIFY(testFiles.at(i) == fileInCache.at(i)); + + QVERIFY(cache.erase()); +} diff --git a/tests/FilesCacheTests.h b/tests/FilesCacheTests.h index 41cf6c31..cfc6af1c 100644 --- a/tests/FilesCacheTests.h +++ b/tests/FilesCacheTests.h @@ -4,50 +4,17 @@ #include #include "../src/FilesCache.h" -class TestsFilesCache : public QObject +class FilesCacheTests : public QObject { Q_OBJECT public: - TestsFilesCache(); + FilesCacheTests(); private Q_SLOTS: void testSaveAndLoadFileNames(); }; -TestsFilesCache::TestsFilesCache() -{ -} - -void TestsFilesCache::testSaveAndLoadFileNames() -{ - QList testFiles; - for (int i = 0; i< 3; i++) - { - QVariantMap item; - item.insert("revision", 0); - item.insert("name", QString("file %1").arg(i)); - item.insert("size", 1024*i); - testFiles << item; - } - - FilesCache cache; - cache.setDbChangeNumber(0); - cache.setCardCPZ("cbe9cad108aad501"); - - bool result = cache.save(testFiles); - QVERIFY(result); - - QList fileInCache = cache.load(); - - QVERIFY(fileInCache.size() == 3); - for (int i = 0; i < testFiles.size(); i ++) - QVERIFY(testFiles.at(i) == fileInCache.at(i)); - - QVERIFY(cache.erase()); -} -QTEST_APPLESS_MAIN(TestsFilesCache) -#include "tst_filescache.moc" diff --git a/tests/UpdaterTests.cpp b/tests/UpdaterTests.cpp index e69de29b..399258b3 100644 --- a/tests/UpdaterTests.cpp +++ b/tests/UpdaterTests.cpp @@ -0,0 +1,24 @@ +#include "UpdaterTests.h" + +#include + +#include "../src/QSimpleUpdater/include/QSimpleUpdater.h" + +void UpdaterTests::testCheckForUpdates() +{ + QString githubApiReleasesUrl ="https://api.github.com/repos/mooltipass/moolticute/releases"; + QSimpleUpdater *updater = QSimpleUpdater::getInstance(); + + updater->setModuleVersion(githubApiReleasesUrl, "0.0"); + updater->setNotifyOnUpdate(githubApiReleasesUrl, true); + updater->setDownloaderEnabled(githubApiReleasesUrl, true); + updater->setNotifyOnFinish(githubApiReleasesUrl, "All done"); + + updater->checkForUpdates(githubApiReleasesUrl); + + QSignalSpy spyChecking(updater, &QSimpleUpdater::checkingFinished); + spyChecking.wait(5000); + + QSignalSpy spyDownload(updater, &QSimpleUpdater::downloadFinished); + spyDownload.wait(30000); +} diff --git a/tests/UpdaterTests.h b/tests/UpdaterTests.h new file mode 100644 index 00000000..0cb3ecb1 --- /dev/null +++ b/tests/UpdaterTests.h @@ -0,0 +1,11 @@ +#include +#include + +#include + +class UpdaterTests : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void testCheckForUpdates(); +}; diff --git a/tests/main.cpp b/tests/main.cpp index e69de29b..c910a0bc 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -0,0 +1,26 @@ +#include + +#include "FilesCacheTests.h" +#include "UpdaterTests.h" + +#define TEST_CLASS(TestClass, status) \ +{ \ + TestClass tc; \ + status |= QTest::qExec(&tc, argc, argv);\ +} + + +// Note: This is equivalent to QTEST_APPLESS_MAIN for multiple test classes. +int main(int argc, char** argv) +{ + QApplication app(argc, argv); + app.setAttribute(Qt::AA_Use96Dpi, true); + + int status = 0; + TEST_CLASS(FilesCache, status); + TEST_CLASS(UpdaterTests, status); + + return status; +} + + diff --git a/tests/tests.pro b/tests/tests.pro index ac5280a1..f58a4b5e 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -27,14 +27,19 @@ DEFINES += QT_DEPRECATED_WARNINGS # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 +include (../src/QSimpleUpdater/QSimpleUpdater.pri) SOURCES += \ - tst_filescache.cpp \ ../src/SimpleCrypt/SimpleCrypt.cpp \ - ../src/FilesCache.cpp + ../src/FilesCache.cpp \ + main.cpp \ + FilesCacheTests.cpp \ + UpdaterTests.cpp HEADERS += \ ../src/SimpleCrypt/SimpleCrypt.h \ - ../src/FilesCache.h + ../src/FilesCache.h \ + UpdaterTests.h \ + FilesCacheTests.h DEFINES += SRCDIR=\\\"$$PWD/\\\" From d8ab6c948f55386c904aa44e4842f768c9bba3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20L=C3=B3pez=20Zubieta?= Date: Sat, 28 Oct 2017 19:28:35 -0400 Subject: [PATCH 3/4] fix typos --- src/AppGui.cpp | 2 +- tests/tests.pro | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AppGui.cpp b/src/AppGui.cpp index 5bd3ae75..aa33d662 100644 --- a/src/AppGui.cpp +++ b/src/AppGui.cpp @@ -528,7 +528,7 @@ void AppGui::checkUpdate(bool displayMessage) u->setDownloaderEnabled(GITHUB_UPDATE_URL, true); u->setNotifyOnFinish(GITHUB_UPDATE_URL, displayMessage); - u->checkForUpdates(MC_UPDATE_URL); + u->checkForUpdates(GITHUB_UPDATE_URL); //Recheck in at least 30minutes plus some random time if (!displayMessage) diff --git a/tests/tests.pro b/tests/tests.pro index f58a4b5e..27b7842f 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -10,7 +10,7 @@ QT -= gui QMAKE_CXXFLAGS += -std=c++0x -TARGET = tst_testsfilescache +TARGET = tests CONFIG += console CONFIG -= app_bundle From 9eeb6d1a18c4ba5929d8516534569230ed40b549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20L=C3=B3pez=20Zubieta?= Date: Sat, 28 Oct 2017 19:29:21 -0400 Subject: [PATCH 4/4] Don't open release url if a download url was found. --- src/QSimpleUpdater/src/Updater.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/QSimpleUpdater/src/Updater.cpp b/src/QSimpleUpdater/src/Updater.cpp index 327dac7c..9640510c 100644 --- a/src/QSimpleUpdater/src/Updater.cpp +++ b/src/QSimpleUpdater/src/Updater.cpp @@ -442,15 +442,14 @@ void Updater::setUpdateAvailable (const bool available) box.setDefaultButton (QMessageBox::Yes); if (box.exec() == QMessageBox::Yes) { - if (!openUrl().isEmpty()) + if (downloadUrl().isEmpty()) QDesktopServices::openUrl (QUrl (openUrl())); - else if (downloaderEnabled()) { + if (downloaderEnabled()) { m_downloader->setUrlId (url()); m_downloader->setFileName (downloadUrl().split ("/").last()); m_downloader->startDownload (QUrl (downloadUrl())); } - else QDesktopServices::openUrl (QUrl (downloadUrl())); }