64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { SessionManager } from "../src/auth/SessionManager";
|
|
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: 2,
|
|
retryDelayMs: 0
|
|
};
|
|
|
|
describe("SessionManager", () => {
|
|
it("logs in and stores B1SESSION and ROUTEID cookies", async () => {
|
|
const http = {
|
|
post: vi.fn().mockResolvedValue({
|
|
data: { SessionId: "fallback", SessionTimeout: 30 },
|
|
headers: {
|
|
"set-cookie": ["B1SESSION=abc123; Path=/b1s/v1", "ROUTEID=.node1; Path=/b1s/v1"]
|
|
}
|
|
})
|
|
};
|
|
|
|
const sessionManager = new SessionManager(http as never, config);
|
|
const session = await sessionManager.getSession();
|
|
|
|
expect(http.post).toHaveBeenCalledWith(
|
|
"/Login",
|
|
{
|
|
CompanyDB: "SBODEMOUS",
|
|
UserName: "manager",
|
|
Password: "secret",
|
|
Language: undefined
|
|
},
|
|
{ skipAuth: true }
|
|
);
|
|
expect(session.b1session).toBe("abc123");
|
|
expect(session.routeId).toBe(".node1");
|
|
expect(sessionManager.buildCookieHeader(session)).toBe("B1SESSION=abc123; ROUTEID=.node1");
|
|
});
|
|
|
|
it("logs out and clears the current session", async () => {
|
|
const http = {
|
|
post: vi
|
|
.fn()
|
|
.mockResolvedValueOnce({
|
|
data: { SessionId: "abc123" },
|
|
headers: {}
|
|
})
|
|
.mockResolvedValueOnce({ data: {}, headers: {} })
|
|
};
|
|
|
|
const sessionManager = new SessionManager(http as never, config);
|
|
await sessionManager.getSession();
|
|
await sessionManager.logout();
|
|
|
|
expect(http.post).toHaveBeenLastCalledWith("/Logout", undefined, { skipAuth: false });
|
|
expect(sessionManager.currentSession).toBeUndefined();
|
|
});
|
|
});
|