WMS_MAIN/test/app/controller/system.user.loginByCode.test.js

56 lines
1.9 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const { invokeSystemMethod } = require('../../support/system_method_client');
const VALID_LOGIN_CODE = String(process.env.SYSTEM_TEST_LOGIN_CODE || '').trim();
describe('test/app/controller/system.user.loginByCode.test.js', () => {
it('should login successfully when code is valid', async function() {
if (!VALID_LOGIN_CODE) {
this.skip();
}
const res = await invokeSystemMethod('user.loginByCode', {
code: VALID_LOGIN_CODE,
});
assert.equal(res.status, 200);
assert.equal(res.body.flag, 'success');
assert.equal(res.body.code, 200);
assert.equal(res.body.success, true);
assert.equal(typeof res.body.data?.token, 'string');
assert.ok(res.body.data.token.length > 20);
assert.equal(typeof res.body.data?.user?.id, 'number');
});
it('should fail when code is missing', async () => {
const res = await invokeSystemMethod('user.loginByCode', {});
assert.equal(res.status, 200);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 0);
assert.match(String(res.body.message || ''), /code/);
});
it('should fail when code does not exist', async () => {
const code = `INVALID_CODE_${Date.now()}`;
const res = await invokeSystemMethod('user.loginByCode', { code });
assert.equal(res.status, 200);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 0);
assert.match(String(res.body.message || ''), /登录码不存在|登录失败/);
});
it('should fail when code format is valid but not matched', async () => {
const code = 'A1B2C3';
const res = await invokeSystemMethod('user.loginByCode', { code });
assert.equal(res.status, 200);
assert.equal(res.body.flag, 'failure');
assert.equal(res.body.code, 0);
assert.match(String(res.body.message || ''), /登录码不存在|登录失败/);
});
});