Skip to content

Commit afcd3e1

Browse files
committed
use jwt instead of self-written format for tokens
1 parent 119cd0e commit afcd3e1

File tree

10 files changed

+314
-227
lines changed

10 files changed

+314
-227
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
]
3434
},
3535
"resolutions": {
36-
"@preact/signals": "^2.5.1",
3736
"@preact/compat": "^18.3.1",
37+
"@preact/signals": "^2.5.1",
3838
"backbone.marionette@npm:3.5.1/backbone": "^1.6.0",
3939
"backbone.marionette@npm:3.5.1/underscore": "^1.13.6",
4040
"braces": "^3.0.3",

packages/core/test/report.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { resolveConfig } from "../src/index.js";
77
import { AllureReport } from "../src/report.js";
88
import { AllureServiceClientMock } from "./utils.js";
99

10+
// JWT payload: { "iss": "allure-service", "url": "https://service.allurereport.org", "projectId": "test-project-id" }
11+
const validAccessToken =
12+
"header.eyJpc3MiOiJhbGx1cmUtc2VydmljZSIsInVybCI6Imh0dHBzOi8vc2VydmljZS5hbGx1cmVyZXBvcnQub3JnIiwicHJvamVjdElkIjoidGVzdC1wcm9qZWN0LWlkIn0.signature";
13+
1014
vi.mock("@allurereport/service", async (importOriginal) => {
1115
const utils = await import("./utils.js");
1216

@@ -215,8 +219,7 @@ describe("report", () => {
215219
const allureReport = new AllureReport({
216220
...config,
217221
allureService: {
218-
url: fixtures.reportUrl,
219-
project: "test-project",
222+
accessToken: validAccessToken,
220223
},
221224
});
222225

packages/core/test/utils.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ export class AllureServiceMock {}
55
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
66
export const AllureServiceClientMock = vi.fn(function () {});
77

8-
AllureServiceClientMock.prototype.login = vi.fn();
9-
10-
AllureServiceClientMock.prototype.logout = vi.fn();
8+
AllureServiceClientMock.prototype.decodeToken = vi.fn();
119

1210
AllureServiceClientMock.prototype.profile = vi.fn();
1311

14-
AllureServiceClientMock.prototype.createProject = vi.fn();
12+
AllureServiceClientMock.prototype.generateNewAccessToken = vi.fn();
1513

1614
AllureServiceClientMock.prototype.projects = vi.fn();
1715

18-
AllureServiceClientMock.prototype.deleteProject = vi.fn();
16+
AllureServiceClientMock.prototype.project = vi.fn();
1917

20-
AllureServiceClientMock.prototype.appendHistory = vi.fn();
18+
AllureServiceClientMock.prototype.createProject = vi.fn();
19+
20+
AllureServiceClientMock.prototype.deleteProject = vi.fn();
2121

2222
AllureServiceClientMock.prototype.downloadHistory = vi.fn();
2323

2424
AllureServiceClientMock.prototype.createReport = vi.fn();
2525

26+
AllureServiceClientMock.prototype.completeReport = vi.fn();
27+
28+
AllureServiceClientMock.prototype.deleteReport = vi.fn();
29+
2630
AllureServiceClientMock.prototype.addReportAsset = vi.fn();
2731

2832
AllureServiceClientMock.prototype.addReportFile = vi.fn();
29-
30-
AllureServiceClientMock.prototype.completeReport = vi.fn();

packages/sandbox/allurerc.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ export default defineConfig({
115115
matcher: ({ labels }) => labels.some(({ name, value }) => name === "env" && value === "bar"),
116116
},
117117
},
118+
allureService: {
119+
accessToken: "eyJhbGciOiJIUzI1NiJ9.eyJwcm9qZWN0SWQiOiIwOTU1NDZlNS05ZmVkLTQ4MmItYTdkZC0yNGEwMDFmYmI4MzciLCJ1cmwiOiJodHRwOi8vbG9jYWxob3N0Ojk5OTEiLCJpYXQiOjE3Njc4NjEzNDcsImlzcyI6ImFsbHVyZS1zZXJ2aWNlIn0.xXRc3x3VBzKTnzZCtnW-dy23adWpG0puOROSOKG1wM4"
120+
}
118121
});

packages/service/src/service.ts

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,27 @@ export class AllureServiceClient {
1515
throw new Error("Allure service access token is required");
1616
}
1717

18-
const [prefix, body] = atob(config.accessToken).split(":");
18+
const { iss, projectId, url: baseUrl } = this.decodeToken(config.accessToken) ?? {};
1919

20-
if (prefix !== "allure-service") {
21-
throw new Error("Invalid access token");
22-
}
23-
24-
const { projectId, url: baseUrl } = JSON.parse(atob(body));
25-
26-
if (!baseUrl || !projectId) {
20+
if (iss !== "allure-service" || !baseUrl || !projectId) {
2721
throw new Error("Invalid access token");
2822
}
2923

3024
this.#url = baseUrl;
3125
this.#client = createServiceHttpClient(this.#url, config.accessToken);
3226
}
3327

28+
decodeToken(token: string) {
29+
try {
30+
const base64Url = token.split(".")[1];
31+
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
32+
33+
return JSON.parse(atob(base64));
34+
} catch {
35+
return undefined;
36+
}
37+
}
38+
3439
/**
3540
* Returns user profile and current project
3641
*/
@@ -71,34 +76,16 @@ export class AllureServiceClient {
7176
return this.#client.get<{ project: { id: string; name: string } }>(`/projects/${uuid}`);
7277
}
7378

74-
/**
75-
* Creates a new project
76-
* @param payload
77-
*/
78-
async createProject(payload: { name: string }) {
79-
const { project } = await this.#client.post<{ project: { id: string; name: string } }>("/projects", {
80-
body: payload,
81-
});
82-
83-
return project;
84-
}
85-
86-
/**
87-
* Deletes a project
88-
* @param payload
89-
*/
90-
async deleteProject(payload: { id: string }) {
91-
return this.#client.delete(`/projects/${payload.id}`);
92-
}
93-
9479
/**
9580
* Downloads history data for a specific branch
9681
* @param payload
9782
*/
9883
async downloadHistory(payload: { branch: string; limit?: number }) {
9984
const { branch, limit } = payload;
10085
const { history } = await this.#client.get<{ history: HistoryDataPoint[] }>(
101-
limit ? `/projects/history/${encodeURIComponent(branch)}?limit=${limit}` : `/projects/history/${encodeURIComponent(branch)}`,
86+
limit
87+
? `/projects/history/${encodeURIComponent(branch)}?limit=${encodeURIComponent(limit.toString())}`
88+
: `/projects/history/${encodeURIComponent(branch)}`,
10289
);
10390

10491
return history;

packages/service/test/history.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ import { HttpClientMock } from "./utils.js";
77

88
const { AllureServiceClient: AllureServiceClientClass } = await import("../src/service.js");
99

10+
// Static JWT token fixture
11+
// JWT payload: { "iss": "allure-service", "url": "https://service.allurereport.org", "projectId": "test-project-id" }
12+
const validAccessToken =
13+
"header.eyJpc3MiOiJhbGx1cmUtc2VydmljZSIsInVybCI6Imh0dHBzOi8vc2VydmljZS5hbGx1cmVyZXBvcnQub3JnIiwicHJvamVjdElkIjoidGVzdC1wcm9qZWN0LWlkIn0.signature";
14+
1015
const fixtures = {
11-
project: "project",
16+
accessToken: validAccessToken,
17+
project: "test-project-id",
1218
url: "https://service.allurereport.org",
1319
branch: "main",
1420
historyDataPoint: {
@@ -42,7 +48,7 @@ describe("AllureRemoteHistory", () => {
4248
let history: AllureRemoteHistory;
4349

4450
beforeEach(() => {
45-
serviceClient = new AllureServiceClientClass({ url: fixtures.url, project: fixtures.project });
51+
serviceClient = new AllureServiceClientClass({ accessToken: fixtures.accessToken });
4652
history = new AllureRemoteHistory({
4753
allureServiceClient: serviceClient,
4854
branch: fixtures.branch,
@@ -69,9 +75,7 @@ describe("AllureRemoteHistory", () => {
6975

7076
const result = await history.readHistory();
7177

72-
expect(HttpClientMock.prototype.get).toHaveBeenCalledWith(
73-
`/projects/${fixtures.project}/${fixtures.branch}/history`,
74-
);
78+
expect(HttpClientMock.prototype.get).toHaveBeenCalledWith(`/projects/history/${fixtures.branch}`);
7579
expect(result).toEqual([fixtures.historyDataPoint]);
7680
});
7781

@@ -100,9 +104,7 @@ describe("AllureRemoteHistory", () => {
100104

101105
const result = await history.readHistory();
102106

103-
expect(HttpClientMock.prototype.get).toHaveBeenCalledWith(
104-
`/projects/${fixtures.project}/${fixtures.branch}/history?limit=10`,
105-
);
107+
expect(HttpClientMock.prototype.get).toHaveBeenCalledWith(`/projects/history/${fixtures.branch}?limit=10`);
106108
expect(result).toEqual([fixtures.historyDataPoint]);
107109
});
108110

0 commit comments

Comments
 (0)