File: testutils.cpp

package info (click to toggle)
libquotient 0.9.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,588 kB
  • sloc: xml: 39,103; cpp: 25,226; sh: 97; makefile: 10
file content (47 lines) | stat: -rw-r--r-- 1,860 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
// SPDX-FileCopyrightText: 2022 Kitsune Ral <kitsune-ral@users.sf.net>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "testutils.h"

#include <Quotient/connection.h>
#include <Quotient/networkaccessmanager.h>

#include <QtTest/QSignalSpy>

using Quotient::Connection;

bool waitForSignal(auto objPtr, auto signal)
{
    return QSignalSpy(std::to_address(objPtr), signal).wait(60000);
}

std::shared_ptr<Quotient::Connection> Quotient::createTestConnection(QLatin1StringView localUserName,
                                                                     QLatin1StringView secret,
                                                                     QLatin1StringView deviceName)
{
    static constexpr auto homeserverAddr = "localhost:1234"_L1;
    auto* const nam = NetworkAccessManager::instance();
    QObject::connect(nam, &QNetworkAccessManager::sslErrors, nam,
                     [](QNetworkReply* reply) { reply->ignoreSslErrors(); });

    auto c = std::make_shared<Connection>();
    c->enableEncryption(true);
    const QString userId{ u'@' % localUserName % u':' % homeserverAddr };
    c->setHomeserver(QUrl(u"https://" % homeserverAddr));
    if (!waitForSignal(c, &Connection::loginFlowsChanged)
        || !c->supportsPasswordAuth()) {
        qCritical().noquote() << "Can't use password login at" << homeserverAddr
                              << "- check that the homeserver is running";
        return nullptr;
    }
    c->loginWithPassword(localUserName, secret, deviceName);
    if (!waitForSignal(c, &Connection::connected)) {
        qCritical().noquote()
            << "Could not achieve the logged in state for" << userId
            << "- check the credentials in the test code and at the homeserver";
        return nullptr;
    }
    return c;
}