Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoXiCheng committed Jan 28, 2024
1 parent b91f346 commit d7b6671
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 380 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-crud",
"version": "1.1.0",
"version": "1.1.1",
"description": "Lightweight Data Repository Based on Git Issue API",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/authenticate-gitee.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { USE_API, giteeRequest } from './helper/helper';
import { mockGiteeUser } from './mock/mock-git-user';
import { GiteeMock } from './mock/gitee-mock';

describe('Test Authenticate Gitee', () => {
beforeAll(()=>{
if (USE_API) return;
mockGiteeUser();
new GiteeMock().setUpMock();
});

test('Test Authenticate Gitee', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/authenticate-github.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GithubUser } from "../index";
import { USE_API, githubRequest } from "./helper/helper";
import { mockGithubUser } from "./mock/mock-git-user";
import { GithubMock } from "./mock/github-mock";

describe('Test Authenticate Github', () => {
beforeAll(() => {
if (USE_API) return;
mockGithubUser();
new GithubMock().setUpMock();
});

test('Test Authenticate Github', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/authenticate-gitlab.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { USE_API, gitlabRequest } from "./helper/helper";
import { mockGitlabUser } from "./mock/mock-git-user";
import { GitlabMock } from "./mock/gitlab-mock";

describe('Test Authenticate Gitlab', () => {
beforeAll(() => {
if (USE_API) return;
mockGitlabUser();
new GitlabMock().setUpMock();
});

test('Test Authenticate Gitlab', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/book-repository.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import dayjs from "dayjs";
import { PlainObject } from "../index";
import { BookModel } from "./helper/book-model";
import { USE_API, mock } from "./helper/helper";
import { USE_API } from "./helper/helper";
import { Book } from "./helper/book-repository";
import { setupGithubMock } from "./mock/mock-github-api";
import { GithubMock } from "./mock/github-mock";


describe('Test Book Storage', () => {
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('Test Book Storage', () => {
if (USE_API) {
await Book.deleteAll();
} else {
setupGithubMock();
new GithubMock().setUpMock();
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/chat-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PlainObject } from "../index";
import { ChatModel } from "./helper/chat-model";
import { Chat } from "./helper/chat-repository";
import { USE_API } from "./helper/helper";
import { setupGitlabMock} from "./mock/mock-gitlab-api";
import { GitlabMock } from "./mock/gitlab-mock";


describe('Use Gitlab Test Chat Storage', () => {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('Use Gitlab Test Chat Storage', () => {
if (USE_API) {
await Chat.deleteAll();
} else {
setupGitlabMock();
new GitlabMock().setUpMock();
}
});

Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/mock/base-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { writeJSONSync } from "../helper/helper";

export abstract class BaseMock {
constructor(filename: string) {
writeJSONSync(filename, []);
}

setUpMock() {
this.mockUser();
this.mockCreate();
this.mockFind();
this.mockFindById();
this.mockUpdateById();
this.mockDeleteById();
this.mockDetail();
}

abstract mockUser(): void;
abstract mockCreate(): void;
abstract mockFind(): void;
abstract mockFindById(): void;
abstract mockUpdateById(): void;
abstract mockDeleteById(): void;
abstract mockDetail(): void;
}
111 changes: 111 additions & 0 deletions src/__tests__/mock/gitee-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import dayjs from "dayjs";
import { OfficialUrl } from "../../enums";
import { GITEE_NUMBER, GITEE_OWNER, GITEE_REPO, mock, readJSONSync, writeJSONSync } from "../helper/helper";
import { BaseMock } from "./base-mock";

const filename = "temp-gitee.json";

export class GiteeMock extends BaseMock {
private baseURL = OfficialUrl.gitee;

constructor() {
super(filename)
}

mockUser(): void {
mock?.onGet(`${this.baseURL}/api/v5/user`).reply(async () => {
return [200, readJSONSync('mock-gitee-user.json')];
});
}
mockCreate(): void {
mock?.onPost(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
id: Math.round(Math.random() * 1000000),
body: JSON.parse(config.data).body,
user: {
id: 100001,
login: "***",
name: "***",
avatar_url: "https://foruda.gitee.com/avatar/***/***.png",
},
created_at: dayjs().format(),
updated_at: dayjs().format()
};
result.push(data);
writeJSONSync(filename, result);
return [200, data];
});
}
mockFind(): void {
mock?.onGet(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params) {
Object.values(config.params).forEach((item) => {
expect(item).not.toBeNull();
});
}
if (config.params?.since) {
return [200, result.filter((item: any) => dayjs(item.created_at).isAfter(dayjs(config.params.since)))];
}
if (config.params?.order) {
return [200, config.params.order == 'desc' ? result.reverse() : result];
}
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];
});
}
mockFindById(): void {
mock?.onGet(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\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(/\/issues\/comments\/(\d+)/)?.[1];
const target = result.find((item: any) => item.id == id);
if (!target) {
return [404, { message: '404 Not Found' }];
}
return [200, target];
});
}
mockUpdateById(): void {
mock?.onPatch(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\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(/\/issues\/comments\/(\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)];
});
}
mockDeleteById(): void {
mock?.onDelete(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\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(/\/issues\/comments\/(\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];
});
}
mockDetail(): void {
mock?.onGet(new RegExp(`${this.baseURL}/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_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
.
This regular expression is constructed from a
environment variable
.
return [200, readJSONSync('mock-gitee-detail.json')];
});
}

}
113 changes: 113 additions & 0 deletions src/__tests__/mock/github-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import dayjs from "dayjs";
import { OfficialUrl } from "../../enums";
import { GITHUB_NUMBER, GITHUB_OWNER, GITHUB_REPO, mock, readJSONSync, writeJSONSync } from "../helper/helper";
import { BaseMock } from "./base-mock";

const filename = "temp-github.json";
export class GithubMock extends BaseMock {
private baseURL = OfficialUrl.github;
constructor() {
super(filename)
}
mockUser(): void {
mock?.onGet(`${this.baseURL}/user`).reply(async () => {
return [200, readJSONSync('mock-github-user.json')];
});
}
mockCreate(): void {
mock?.onPost(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
id: Math.round(Math.random() * 1000000),
body: JSON.parse(config.data).body,
user: {
id: 100001,
login: "***",
name: "***",
avatar_url: "https://foruda.github.com/avatar/***/***.png",
},
created_at: dayjs().format(),
updated_at: dayjs().format()
};
result.push(data);
writeJSONSync(filename, result);
return [200, data];
});
}
mockFind(): void {
mock?.onGet(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params) {
Object.values(config.params).forEach((item) => {
expect(item).not.toBeNull();
})
}
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];
});
}
mockFindById(): void {
mock?.onGet(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\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(/\/issues\/comments\/(\d+)/)?.[1];
const target = result.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/issues/comments#get-an-issue-comment"
}];
}
return [200, target];
});
}
mockUpdateById(): void {
mock?.onPatch(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\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(/\/issues\/comments\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/issues/comments#update-an-issue-comment"
}];
}
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)];
});
}
mockDeleteById(): void {
mock?.onDelete(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\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(/\/issues\/comments\/(\d+)/)?.[1];
const target = raw.find((item: any) => item.id == id);
if (!target) {
return [404, {
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/issues/comments#delete-an-issue-comment"
}];
}
const remain = raw.find((item: any) => item.id != id);
writeJSONSync(filename, remain ? remain : []);
return [204];
});
}
mockDetail(): void {
mock?.onGet(new RegExp(`${this.baseURL}/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_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
.
This regular expression is constructed from a
environment variable
.
return [200, readJSONSync('mock-github-detail.json')];
});
}
}
Loading

0 comments on commit d7b6671

Please sign in to comment.