cangku/frontend/test/authSessionPolicy.test.mjs

36 lines
1.0 KiB
JavaScript
Raw Permalink 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.

import assert from 'node:assert/strict';
import test from 'node:test';
import {
getRequestErrorMessage,
isAuthSessionFailure,
} from '../src/api/authSessionPolicy.js';
test('recognizes explicit application authentication failures', () => {
assert.equal(isAuthSessionFailure({
status: 401,
data: { code: 'AUTH_TOKEN_INVALID', error: '无效的认证令牌' },
}), true);
assert.equal(isAuthSessionFailure({
status: 401,
data: { error: '未提供认证令牌' },
}), true);
});
test('does not treat an upstream 401 as an expired application session', () => {
const response = {
status: 401,
data: { error: 'AccessKey已过期,请重新获取' },
};
assert.equal(isAuthSessionFailure(response), false);
assert.equal(getRequestErrorMessage({ response }), 'AccessKey已过期,请重新获取');
});
test('does not treat gateway failures as authentication failures', () => {
assert.equal(isAuthSessionFailure({
status: 502,
data: { code: 'ALIBABA_1688_IMAGE_SEARCH_FAILED' },
}), false);
});