|
|
@@ -8,30 +8,12 @@ use \WikipaliApi\Model\PostApiV3AdminCreditSettlementsRequest;
|
|
|
* v3 管理后台 · AI 对话积分:/v3/admin/credit-overview、credit-accounts、credit-ledger、
|
|
|
* credit-adjustments、credit-settlements(仅 root / administrator)。
|
|
|
*
|
|
|
- * 测试账号是普通用户时只测 401 / 403;是管理员时再测读、422 与写。
|
|
|
+ * 两个账号:401 / 403 用普通测试账号;读与写用单独配置的管理员账号(admin_token(),没配就 SKIP)。
|
|
|
+ * 管理员在生产上只读:写入用例(POST,含 422 校验)走 admin_write_token(),
|
|
|
+ * 只在 config.php 的 admin_write_servers 白名单服务器上跑。
|
|
|
* 写都是幂等的:结算重复执行结果覆盖;给自己 +1 再 -1 用固定的 ref,每台服务器只入账一次、净额为 0。
|
|
|
*/
|
|
|
|
|
|
-/** 测试账号是不是管理员(看 /v3/me 的 roles) */
|
|
|
-function credit_admin(string $token): bool
|
|
|
-{
|
|
|
- $me = raw('GET', '/v3/me', null, null, $token);
|
|
|
- assert_status(200, $me, 'me');
|
|
|
- $roles = $me['body']['data']['roles'] ?? [];
|
|
|
-
|
|
|
- return (bool) array_intersect(['administrator', 'root'], $roles);
|
|
|
-}
|
|
|
-
|
|
|
-function admin_token_or_skip(): string
|
|
|
-{
|
|
|
- $token = required_token();
|
|
|
- if (! credit_admin($token)) {
|
|
|
- skip('测试账号不是管理员');
|
|
|
- }
|
|
|
-
|
|
|
- return $token;
|
|
|
-}
|
|
|
-
|
|
|
/* ---- 无 token → 401 ---- */
|
|
|
|
|
|
test('GET /v3/admin/credit-overview(无 token)→ 401', function () {
|
|
|
@@ -48,11 +30,7 @@ test('POST /v3/admin/credit-settlements(无 token)→ 401', function () {
|
|
|
/* ---- 普通用户 → 403 ---- */
|
|
|
|
|
|
test('GET /v3/admin/credit-accounts(非管理员)→ 403', function () {
|
|
|
- $token = required_token();
|
|
|
- if (credit_admin($token)) {
|
|
|
- skip('测试账号是管理员');
|
|
|
- }
|
|
|
- $res = request(fn () => api(AdminApi::class, $token)->getApiV3AdminCreditAccountsWithHttpInfo());
|
|
|
+ $res = request(fn () => api(AdminApi::class, required_token())->getApiV3AdminCreditAccountsWithHttpInfo());
|
|
|
assert_status(403, $res, 'credit-accounts');
|
|
|
assert_problem($res, 'credit-accounts');
|
|
|
});
|
|
|
@@ -60,7 +38,7 @@ test('GET /v3/admin/credit-accounts(非管理员)→ 403', function () {
|
|
|
/* ---- 管理员:读 ---- */
|
|
|
|
|
|
test('GET /v3/admin/credit-overview → 200', function () {
|
|
|
- $res = request(fn () => api(AdminApi::class, admin_token_or_skip())->getApiV3AdminCreditOverviewWithHttpInfo());
|
|
|
+ $res = request(fn () => api(AdminApi::class, admin_token())->getApiV3AdminCreditOverviewWithHttpInfo());
|
|
|
assert_status(200, $res, 'credit-overview');
|
|
|
foreach (['today', 'earn_since', 'policy_version', 'policy', 'totals', 'recent_days', 'last_settlement'] as $key) {
|
|
|
assert_has_key($key, $res['body']['data'], 'credit-overview.data');
|
|
|
@@ -69,7 +47,7 @@ test('GET /v3/admin/credit-overview → 200', function () {
|
|
|
});
|
|
|
|
|
|
test('GET /v3/admin/credit-accounts → 200 分页', function () {
|
|
|
- $res = request(fn () => api(AdminApi::class, admin_token_or_skip())->getApiV3AdminCreditAccountsWithHttpInfo(per_page: 5));
|
|
|
+ $res = request(fn () => api(AdminApi::class, admin_token())->getApiV3AdminCreditAccountsWithHttpInfo(per_page: 5));
|
|
|
assert_status(200, $res, 'credit-accounts');
|
|
|
assert_has_key('total', $res['body']['meta'], 'credit-accounts.meta');
|
|
|
foreach ($res['body']['data'] as $row) {
|
|
|
@@ -80,17 +58,17 @@ test('GET /v3/admin/credit-accounts → 200 分页', function () {
|
|
|
});
|
|
|
|
|
|
test('GET /v3/admin/credit-ledger(只给 account_id)→ 422', function () {
|
|
|
- $res = request(fn () => api(AdminApi::class, admin_token_or_skip())->getApiV3AdminCreditLedgerWithHttpInfo(
|
|
|
+ $res = request(fn () => api(AdminApi::class, admin_token())->getApiV3AdminCreditLedgerWithHttpInfo(
|
|
|
filter_account_id: '00000000-0000-4000-8000-000000000000',
|
|
|
));
|
|
|
assert_status(422, $res, 'credit-ledger');
|
|
|
assert_field_error('filter.account_type', $res, 'credit-ledger');
|
|
|
});
|
|
|
|
|
|
-/* ---- 管理员:写(幂等) ---- */
|
|
|
+/* ---- 管理员:写(幂等;只在 admin_write_servers 上跑) ---- */
|
|
|
|
|
|
test('POST /v3/admin/credit-settlements → 201(可重复执行)', function () {
|
|
|
- $res = request(fn () => api(AdminApi::class, admin_token_or_skip())->postApiV3AdminCreditSettlementsWithHttpInfo(
|
|
|
+ $res = request(fn () => api(AdminApi::class, admin_write_token())->postApiV3AdminCreditSettlementsWithHttpInfo(
|
|
|
new PostApiV3AdminCreditSettlementsRequest(),
|
|
|
));
|
|
|
assert_status(201, $res, 'credit-settlements');
|
|
|
@@ -100,13 +78,13 @@ test('POST /v3/admin/credit-settlements → 201(可重复执行)', function
|
|
|
test('POST /v3/admin/credit-settlements(未来日期)→ 422', function () {
|
|
|
$body = new PostApiV3AdminCreditSettlementsRequest();
|
|
|
$body->setDate(gmdate('Y-m-d', time() + 86400 * 2));
|
|
|
- $res = request(fn () => api(AdminApi::class, admin_token_or_skip())->postApiV3AdminCreditSettlementsWithHttpInfo($body));
|
|
|
+ $res = request(fn () => api(AdminApi::class, admin_write_token())->postApiV3AdminCreditSettlementsWithHttpInfo($body));
|
|
|
assert_status(422, $res, 'credit-settlements');
|
|
|
assert_field_error('date', $res, 'credit-settlements');
|
|
|
});
|
|
|
|
|
|
test('POST /v3/admin/credit-adjustments:给自己 +1 再 -1(固定 ref,只入账一次)', function () {
|
|
|
- $token = admin_token_or_skip();
|
|
|
+ $token = admin_write_token();
|
|
|
$me = raw('GET', '/v3/me', null, null, $token);
|
|
|
assert_status(200, $me, 'me');
|
|
|
$me = $me['body']['data']['id'];
|
|
|
@@ -129,7 +107,7 @@ test('POST /v3/admin/credit-adjustments(delta 为 0、缺原因)→ 422', fu
|
|
|
$body->setUser('nobody');
|
|
|
$body->setDelta(0);
|
|
|
$body->setNote('');
|
|
|
- $res = request(fn () => api(AdminApi::class, admin_token_or_skip())->postApiV3AdminCreditAdjustmentsWithHttpInfo($body));
|
|
|
+ $res = request(fn () => api(AdminApi::class, admin_write_token())->postApiV3AdminCreditAdjustmentsWithHttpInfo($body));
|
|
|
assert_status(422, $res, 'credit-adjustments');
|
|
|
assert_field_error('delta', $res, 'credit-adjustments');
|
|
|
});
|