Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Xicheng Guo committed Jan 15, 2024
1 parent cba0eb0 commit ee843f4
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 191 deletions.
189 changes: 0 additions & 189 deletions src/__tests__/book-repository.test.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/__tests__/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const GITEE_NUMBER = process.env.TEST_GITEE_NUMBER || '1';

export const GITHUB_NUMBER = process.env.TEST_GITHUB_NUMBER || '1';

export const GITLAB_NUMBER = process.env.TEST_GITLAB_NUMBER as string;
export const GITLAB_NUMBER = process.env.TEST_GITLAB_NUMBER || '1';

export const ENCRYPT_KEY = process.env.TEST_ENCRYPT_KEY || 'test-encrypt-key';

Expand All @@ -32,6 +32,8 @@ export const GITHUB_OWNER = process.env.TEST_GITHUB_OWNER || 'test-owner';

export const GITHUB_REPO = process.env.TEST_GITHUB_REPO || 'test-repo';

export const GITLAB_PROJECT_ID = process.env.TEST_GITLAB_PROJECT_ID || 'test-project-id';

export let mock: MockAdapter | null = null;
if (!USE_API) {
mock = new MockAdapter(axios);
Expand Down Expand Up @@ -71,9 +73,11 @@ export const githubRequest = createRequest({
export const gitlabRequest = createRequest({
httpLib: 'axios',
httpClient: axios,

accessToken: process.env.TEST_GITLAB_TOKEN as string,

platform: 'gitlab',
projectId: process.env.TEST_GITLAB_PROJECT_ID as string
projectId: GITLAB_PROJECT_ID
});

export function readJSONSync(filename: string) {
Expand Down
64 changes: 64 additions & 0 deletions src/__tests__/mock/json/mock-gitlab-detail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"id": 100001,
"iid": 2,
"project_id": 100001,
"title": "For *** Test New",
"description": "test",
"state": "opened",
"created_at": "2024-01-02T02:27:57.658Z",
"updated_at": "2024-01-15T08:06:48.811Z",
"closed_at": null,
"closed_by": null,
"labels": [],
"milestone": null,
"assignees": [],
"author": {
"id": 100001,
"username": "***",
"name": "***",
"state": "active",
"locked": false,
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/***/avatar.png",
"web_url": "https://gitlab.com/***"
},
"type": "ISSUE",
"assignee": null,
"user_notes_count": 1,
"merge_requests_count": 0,
"upvotes": 0,
"downvotes": 0,
"due_date": null,
"confidential": false,
"discussion_locked": null,
"issue_type": "issue",
"web_url": "https://gitlab.com/***/***/-/issues/2",
"time_stats": {
"time_estimate": 0,
"total_time_spent": 0,
"human_time_estimate": null,
"human_total_time_spent": null
},
"task_completion_status": {
"count": 0,
"completed_count": 0
},
"blocking_issues_count": 0,
"has_tasks": true,
"task_status": "0 of 0 checklist items completed",
"_links": {
"self": "https://gitlab.com/api/v4/projects/***/issues/2",
"notes": "https://gitlab.com/api/v4/projects/***/issues/2/notes",
"award_emoji": "https://gitlab.com/api/v4/projects/***/issues/2/award_emoji",
"project": "https://gitlab.com/api/v4/projects/***",
"closed_as_duplicate_of": null
},
"references": {
"short": "#2",
"relative": "#2",
"full": "***/***#2"
},
"severity": "UNKNOWN",
"subscribed": true,
"moved_to_id": null,
"service_desk_reply_to": null
}
103 changes: 103 additions & 0 deletions src/__tests__/mock/mock-gitlab-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import dayjs from "dayjs";
import { GITLAB_NUMBER, GITLAB_PROJECT_ID, mock, readJSONSync, writeJSONSync } from "../helper/helper";

const filename = "temp-gitlab.json";

export async function initGitlabJSONFile() {
writeJSONSync(filename, []);
}

export async function mockGitlabFind() {
mock?.onGet(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params?.since) {
return [200, result.filter((item: any) => dayjs(item.created_at).isAfter(dayjs(config.params.since)))];
}
if (config.params?.page && config.params?.per_page) {
const start = (config.params.page - 1) * config.params.per_page;
const end = config.params.page * config.params.per_page;
return [200, result.slice(start, end)];
}
return [200, result];
});
}

export async function mockGitlabFindById() {
mock?.onGet(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes/\\d+`)).reply(async (config) => {

Check failure

Code scanning / CodeQL

Regular expression injection High test

This regular expression is constructed from a
environment variable
.
This regular expression is constructed from a
environment variable
.
const result = readJSONSync(filename);
const id = config.url?.match(/\/notes\/(\d+)/)?.[1];
const target = result.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "404 Not found"
}];
}
return [200, target];
});
}

export async function mockGitlabCreate() {
mock?.onPost(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
id: Math.round(Math.random() * 1000000),
body: JSON.parse(config.data).body,
system: false,
author: {
id: 100001,
username: "***",
name: "***",
avatar_url: "https://foruda.gitlab.com/avatar/***/***.png",
},
created_at: dayjs().format(),
updated_at: dayjs().format()
};
result.push(data);
writeJSONSync(filename, result);
return [200, data];
});
}

export async function mockGitlabUpdateById() {
mock?.onPut(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes/\\d+`)).reply(async (config) => {

Check failure

Code scanning / CodeQL

Regular expression injection High test

This regular expression is constructed from a
environment variable
.
This regular expression is constructed from a
environment variable
.
const raw = readJSONSync(filename);
const id = config.url?.match(/\/notes\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "404 Not found"
}];
}
raw.forEach((item: any) => {
if (item.id == id) {
item.body = JSON.parse(config.data).body;
item.updated_at = dayjs().format();
}
});
writeJSONSync(filename, raw);
const resAfter = readJSONSync(filename);
return [200, resAfter.find((item: any) => item.id == id)];
});
}

export async function mockGitlabDeleteById() {
mock?.onDelete(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes/\\d+`)).reply(async (config) => {

Check failure

Code scanning / CodeQL

Regular expression injection High test

This regular expression is constructed from a
environment variable
.
This regular expression is constructed from a
environment variable
.
const raw = readJSONSync(filename);
const id = config.url?.match(/\/notes\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "404 Not found"
}];
}
const remain = raw.find((item: any) => item.id != id);
writeJSONSync(filename, remain ? remain : []);
return [204];
});
}

export async function mockGitlabDetail() {
mock?.onGet(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}`)).reply(async (config) => {

Check failure

Code scanning / CodeQL

Regular expression injection High test

This regular expression is constructed from a
environment variable
.
This regular expression is constructed from a
environment variable
.
return [200, readJSONSync('mock-gitlab-detail.json')];
});
}
Loading

0 comments on commit ee843f4

Please sign in to comment.