geopro/tests/net/test_auth.cpp

65 lines
2.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// net 层端到端登录连通测试(真实站点)。
// 复刻已实测通过的流程getImageCode -> verifyCodeCheck -> RSA -> login2
// 关键在 ApiClient 内单一 QNetworkAccessManager 共享 JSESSIONID 会话。
// 异步化fetchCaptchaAsync/loginAsync 返回自管理句柄QSignalSpy::wait 驱动事件循环等待。
// 需要 QCoreApplication 提供事件循环。网络不可达时本用例会失败(属环境问题,非逻辑问题)。
#include <gtest/gtest.h>
#include <QCoreApplication>
#include <QSignalSpy>
#include <fstream>
#include <sstream>
#include <string>
#include "ApiClient.hpp"
#include "AuthLoads.hpp"
#include "AuthService.hpp"
namespace {
// 读取整个文件内容PEM 公钥)。
std::string slurp(const char* path) {
std::ifstream f(path);
std::stringstream s;
s << f.rdbuf();
return s.str();
}
} // namespace
TEST(AuthLiveTest, FullLoginFlowReturnsToken) {
int argc = 0;
char** argv = nullptr;
QCoreApplication app(argc, argv);
geopro::net::ApiClient api("http://tenant.geomative.cn/pop-api");
geopro::net::AuthService auth(
api, slurp("D:/Git/lanbingtech/geopro/resources/rsa_public_key.pem"));
auto* cl = auth.fetchCaptchaAsync();
QSignalSpy capDone(cl, &geopro::net::CaptchaLoad::done);
QSignalSpy capFail(cl, &geopro::net::CaptchaLoad::failed);
ASSERT_TRUE(capDone.wait(10000) || capFail.count() > 0);
ASSERT_EQ(capDone.count(), 1)
<< (capFail.count() ? capFail.takeFirst().at(0).toString().toStdString() : "captcha failed");
auto cap = capDone.takeFirst().at(0).value<geopro::net::AuthService::Captcha>();
ASSERT_FALSE(cap.codeId.isEmpty());
// 后端已把验证码从明文 code 改为图片 image实测自动化无法识别图内字符 → 跳过 live 登录断言。
if (cap.code.isEmpty()) {
ASSERT_FALSE(cap.image.isEmpty()) << "既无明文 code 也无图片 image验证码接口异常";
GTEST_SKIP() << "后端验证码改为图片(data.image),无明文 code无法自动登录验证";
}
auto* ll = auth.loginAsync("sydk", "123456", cap.code, cap.codeId);
QSignalSpy loginDone(ll, &geopro::net::LoginLoad::done);
QSignalSpy loginFail(ll, &geopro::net::LoginLoad::failed);
ASSERT_TRUE(loginDone.wait(10000) || loginFail.count() > 0);
EXPECT_EQ(loginDone.count(), 1)
<< (loginFail.count() ? loginFail.takeFirst().at(0).toString().toStdString() : "");
if (loginDone.count()) {
auto token = loginDone.takeFirst().at(0).toString();
EXPECT_TRUE(token.startsWith("Geomative ")) << token.toStdString();
}
}