/*
   SPDX-FileCopyrightText: 2020-2026 Laurent Montel <montel@kde.org>

   SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "addusertorolejobtest.h"

#include "restapimethod.h"
#include "role/addusertorolejob.h"
#include "ruqola_restapi_helper.h"

#include <QJsonDocument>
#include <QTest>

using namespace Qt::Literals::StringLiterals;
QTEST_GUILESS_MAIN(AddUserToRoleJobTest)
using namespace RocketChatRestApi;
AddUserToRoleJobTest::AddUserToRoleJobTest(QObject *parent)
    : QObject(parent)
{
}

void AddUserToRoleJobTest::shouldHaveDefaultValue()
{
    AddUserToRoleJob job;
    RuqolaRestApiHelper::verifyDefaultValue(&job);
    QVERIFY(job.requireHttpAuthentication());
    QVERIFY(!job.hasQueryParameterSupport());
    QVERIFY(job.username().isEmpty());
    QVERIFY(job.roleId().isEmpty());
    QVERIFY(job.roleName().isEmpty());
    QVERIFY(!job.useRC80());
}

void AddUserToRoleJobTest::shouldGenerateRequest()
{
    AddUserToRoleJob job;
    QNetworkRequest request = QNetworkRequest(QUrl());
    RuqolaRestApiHelper::verifyAuthentication(&job, request);
    QCOMPARE(request.url(), QUrl(u"http://www.kde.org/api/v1/roles.addUserToRole"_s));
    QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), u"application/json"_s);
}

void AddUserToRoleJobTest::shouldGenerateJson()
{
    {
        AddUserToRoleJob job;
        // Before RC 8.0.0
        job.setUseRC80(false);

        const QString username = u"foo1"_s;
        job.setUsername(username);
        const QString rolename = u"role1"_s;
        job.setRoleName(rolename);
        QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"roleName":"%1","username":"%2"})").arg(rolename, username).toLatin1());
    }
    {
        AddUserToRoleJob job;
        job.setUseRC80(true);

        const QString username = u"foo1"_s;
        job.setUsername(username);
        const QString rolename = u"role1"_s;
        job.setRoleName(rolename);
        const QString roleId = u"roleId1"_s;
        job.setRoleId(roleId);
        QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral(R"({"roleId":"%1","username":"%2"})").arg(roleId, username).toLatin1());
    }
}

void AddUserToRoleJobTest::shouldNotStarting()
{
    AddUserToRoleJob job;
    job.setUseRC80(false);

    RestApiMethod method;
    method.setServerUrl(u"http://www.kde.org"_s);
    job.setRestApiMethod(&method);

    QNetworkAccessManager mNetworkAccessManager;
    job.setNetworkAccessManager(&mNetworkAccessManager);
    QVERIFY(!job.canStart());
    const QString auth = u"foo"_s;
    const QString userId = u"foo"_s;
    job.setAuthToken(auth);
    QVERIFY(!job.canStart());
    job.setUserId(userId);

    QVERIFY(!job.canStart());
    const QString username = u"foo1"_s;
    job.setUsername(username);
    QVERIFY(!job.canStart());
    const QString rolename = u"role1"_s;
    job.setRoleName(rolename);
    QVERIFY(job.canStart());

    job.setUseRC80(true);
    QVERIFY(!job.canStart());

    const QString roleId = u"roleId1"_s;
    job.setRoleId(roleId);
    QVERIFY(job.canStart());
}

#include "moc_addusertorolejobtest.cpp"
