Skip to content

Commit

Permalink
Fix oauth unit test, handle status.php request
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Dec 9, 2020
1 parent 78a0060 commit ac45ee8
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions test/testoauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ class OAuthTestCase : public QObject
Q_OBJECT
DesktopServiceHook desktopServiceHook;
public:
enum State { StartState, BrowserOpened, TokenAsked, CustomState } state = StartState;
enum State { StartState,
StatusPhpState,
BrowserOpened,
TokenAsked,
CustomState } state = StartState;
Q_ENUM(State);
bool replyToBrowserOk = false;
bool gotAuthOk = false;
Expand All @@ -112,9 +116,12 @@ class OAuthTestCase : public QObject
account->setUrl(sOAuthTestServer);
account->setCredentials(new FakeCredentials{fakeQnam});
fakeQnam->setParent(this);
fakeQnam->setOverride([this](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *device) {
if (req.url().path().endsWith(".well-known/openid-configuration"))
fakeQnam->setOverride([this](QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *device) {
if (req.url().path().endsWith(".well-known/openid-configuration")) {
return this->wellKnownReply(op, req);
} else if (req.url().path().endsWith("status.php")) {
return this->statusPhpReply(op, req);
}
OC_ASSERT(device);
OC_ASSERT(device->bytesAvailable() > 0); // OAuth2 always sends around POST data.
return this->tokenReply(op, req);
Expand All @@ -130,7 +137,7 @@ class OAuthTestCase : public QObject
}

virtual void openBrowserHook(const QUrl &url) {
QCOMPARE(state, StartState);
QCOMPARE(state, StatusPhpState);
state = BrowserOpened;
QCOMPARE(url.path(), QString(sOAuthTestServer.path() + "/index.php/apps/oauth2/authorize"));
QVERIFY(url.toString().startsWith(sOAuthTestServer.toString()));
Expand Down Expand Up @@ -170,6 +177,18 @@ class OAuthTestCase : public QObject
return new FakePostReply(op, req, std::move(payload), fakeQnam);
}

virtual QNetworkReply *statusPhpReply(QNetworkAccessManager::Operation op, const QNetworkRequest &req)
{
OC_ASSERT(state == StartState);
state = StatusPhpState;
OC_ASSERT(op == QNetworkAccessManager::GetOperation);
OC_ASSERT(req.url().toString().startsWith(sOAuthTestServer.toString()));
OC_ASSERT(req.url().path() == sOAuthTestServer.path() + "/status.php");
std::unique_ptr<QBuffer> payload(new QBuffer());
payload->setData(statusPhpPayload());
return new FakePostReply(op, req, std::move(payload), fakeQnam);
}

virtual QNetworkReply *wellKnownReply(QNetworkAccessManager::Operation op, const QNetworkRequest &req)
{
return new FakeErrorReply(op, req, fakeQnam, 404);
Expand All @@ -187,6 +206,19 @@ class OAuthTestCase : public QObject
return jsondata.toJson();
}

virtual QByteArray statusPhpPayload() const
{
QJsonDocument jsondata(QJsonObject {
{ "installed", true },
{ "maintenance", false },
{ "needsDbUpgrade", false },
{ "version", "10.5.0.10" },
{ "versionstring", "10.5.0" },
{ "edition", "Enterprise" },
{ "productname", "ownCloud" } });
return jsondata.toJson();
}

virtual void oauthResult(OAuth::Result result, const QString &user, const QString &token , const QString &refreshToken) {
QCOMPARE(state, TokenAsked);
QCOMPARE(result, OAuth::LoggedIn);
Expand Down

0 comments on commit ac45ee8

Please sign in to comment.