#include "testenv.h" #include "msaauthservice.h" #include class TestMsaRedirect : public QObject { Q_OBJECT private slots: void authorizationUrlCarriesClientAndRedirect() { const QUrl url = MsaAuthService::authorizationUrl(); QCOMPARE(url.scheme(), QStringLiteral("https")); QCOMPARE(url.host(), QStringLiteral("login.live.com")); const QUrlQuery query(url); QCOMPARE(query.queryItemValue("client_id"), MsaAuthService::clientId()); QCOMPARE(query.queryItemValue("response_type"), QStringLiteral("code")); QVERIFY(!query.queryItemValue("scope").isEmpty()); // Код возвращается на тот адрес, который потом распознаёт matchRedirect. const QUrl redirect(query.queryItemValue("redirect_uri", QUrl::FullyDecoded)); QString code; QVERIFY(MsaAuthService::matchRedirect(QUrl(redirect.toString() + "?code=abc"), &code, nullptr)); QCOMPARE(code, QStringLiteral("abc")); } void matchRedirect_data() { QTest::addColumn("url"); QTest::addColumn("matches"); QTest::addColumn("code"); QTest::addColumn("error"); QTest::newRow("code in query") << "https://login.live.com/oauth20_desktop.srf?code=M.C123-abc&lc=1049" << true << "M.C123-abc" << ""; QTest::newRow("code in fragment") << "https://login.live.com/oauth20_desktop.srf#code=FRAG&state=1" << true << "FRAG" << ""; QTest::newRow("encoded code") << "https://login.live.com/oauth20_desktop.srf?code=a%2Bb%3D" << true << "a+b=" << ""; QTest::newRow("error with description") << "https://login.live.com/oauth20_desktop.srf?error=access_denied&error_description=The%20user%20denied" << true << "" << "The user denied"; QTest::newRow("error only") << "https://login.live.com/oauth20_desktop.srf?error=server_error" << true << "" << "server_error"; QTest::newRow("host case-insensitive") << "https://LOGIN.LIVE.COM/oauth20_desktop.srf?code=x" << true << "x" << ""; QTest::newRow("redirect without params") << "https://login.live.com/oauth20_desktop.srf" << true << "" << ""; QTest::newRow("authorize page") << "https://login.live.com/oauth20_authorize.srf?code=x" << false << "" << ""; QTest::newRow("foreign host") << "https://evil.example/oauth20_desktop.srf?code=x" << false << "" << ""; QTest::newRow("empty") << "" << false << "" << ""; } void matchRedirect() { QFETCH(QString, url); QFETCH(bool, matches); QFETCH(QString, code); QFETCH(QString, error); QString actualCode = QStringLiteral("untouched"); QString actualError = QStringLiteral("untouched"); QCOMPARE(MsaAuthService::matchRedirect(QUrl(url), &actualCode, &actualError), matches); if (matches) { QCOMPARE(actualCode, code); QCOMPARE(actualError, error); } else { QCOMPARE(actualCode, QStringLiteral("untouched")); } } }; BILAUNCHER_TEST_MAIN(TestMsaRedirect) #include "tst_msaredirect.moc"