Files
sap-bo/tests/systemInfo.test.ts
T
JiriUhlir 94ab66a430 Add GET /api/system/info and return SAP version from login
- capture Version + SessionTimeout from the Service Layer login
  response (SessionManager.loginInfo, survives logout) and return
  them from POST /api/session/login
- new GET /api/system/info: entity sets (service document),
  UserFieldsMD/UserTablesMD/UserObjectsMD customizations and
  CompanyService_GetAdminInfo; unreadable sections come back as
  { error } instead of failing the whole request
- OpenAPI: new System tag, examples for both endpoints
- tests for getSystemInfo section isolation and loginInfo retention

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 08:26:00 +02:00

107 lines
3.5 KiB
TypeScript

import axios, { AxiosAdapter, AxiosResponse } from "axios";
import { describe, expect, it } from "vitest";
import { SapBusinessOneServiceLayer } from "../src/SapBusinessOneServiceLayer";
import { SapB1Config } from "../src/config";
const config: SapB1Config = {
baseUrl: "https://sap.example.local:50000",
companyDB: "SBODEMOUS",
username: "manager",
password: "secret",
timeout: 30_000,
rejectUnauthorized: true,
retryCount: 0,
retryDelayMs: 0
};
function response(data: unknown, requestConfig: Parameters<AxiosAdapter>[0], status = 200): AxiosResponse {
return {
data,
status,
statusText: "OK",
headers: {},
config: requestConfig
};
}
describe("getSystemInfo", () => {
it("collects version, entity sets and customization metadata, isolating section errors", async () => {
const http = axios.create({
adapter: async (requestConfig) => {
const url = String(requestConfig.url);
if (url === "/Login") {
return {
...response({ SessionId: "abc123", Version: "1000230", SessionTimeout: 30 }, requestConfig),
headers: { "set-cookie": ["B1SESSION=abc123; Path=/b1s/v1"] }
};
}
if (url === "/Logout") {
return response({}, requestConfig);
}
if (url === "/") {
return response(
{ value: [{ name: "Items", url: "Items" }, { name: "BusinessPartners", url: "BusinessPartners" }] },
requestConfig
);
}
if (url === "UserFieldsMD") {
return response({ value: [{ TableName: "OCRD", FieldID: 0, Name: "MyField", Type: "db_Alpha" }] }, requestConfig);
}
if (url === "UserTablesMD") {
return response({ value: [{ TableName: "MY_TABLE", TableDescription: "Custom table" }] }, requestConfig);
}
if (url === "UserObjectsMD") {
return response({ value: [] }, requestConfig);
}
if (url === "CompanyService_GetAdminInfo") {
return Promise.reject({
isAxiosError: true,
message: "Forbidden",
response: { status: 403, data: { error: { message: { value: "No permission" } } } }
});
}
throw new Error(`Unexpected request URL: ${url}`);
}
});
const sap = new SapBusinessOneServiceLayer(config, { http });
const info = await sap.getSystemInfo();
expect(info.serviceLayer).toEqual({ version: "1000230", sessionTimeoutMinutes: 30 });
expect(info.entitySets).toEqual(["BusinessPartners", "Items"]);
expect(info.userFields).toEqual([{ TableName: "OCRD", FieldID: 0, Name: "MyField", Type: "db_Alpha" }]);
expect(info.userTables).toEqual([{ TableName: "MY_TABLE", TableDescription: "Custom table" }]);
expect(info.userObjects).toEqual([]);
expect(info.adminInfo).toEqual({ error: "No permission" });
});
it("keeps login info available after logout so the login endpoint can report the version", async () => {
const http = axios.create({
adapter: async (requestConfig) => {
if (requestConfig.url === "/Login") {
return {
...response({ SessionId: "abc123", Version: "1000230", SessionTimeout: 30 }, requestConfig),
headers: { "set-cookie": ["B1SESSION=abc123; Path=/b1s/v1"] }
};
}
return response({}, requestConfig);
}
});
const sap = new SapBusinessOneServiceLayer(config, { http });
await sap.login();
await sap.logout();
expect(sap.client.loginInfo).toEqual({ version: "1000230", sessionTimeoutMinutes: 30 });
});
});