Skip to content

Commit

Permalink
Merge pull request #79 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Jan 12, 2024
2 parents 2a7d8c2 + 20e6eb4 commit ff37fd2
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ dist
.pnp.*

lib
src/__tests__/mock/json/*.json
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
"@rollup/plugin-typescript": "^11.1.5",
"@types/crypto-js": "^4.2.1",
"@types/jest": "^29.5.9",
"@types/jsonfile": "^6.1.4",
"@types/wechat-miniprogram": "^3.4.7",
"axios": "^1.6.2",
"axios-mock-adapter": "^1.22.0",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.10",
"dotenv": "^16.3.1",
"jest": "^29.7.0",
"jsonfile": "^6.1.0",
"nyc": "^15.1.0",
"rimraf": "^5.0.5",
"rollup": "^4.3.0",
Expand Down
41 changes: 4 additions & 37 deletions src/__tests__/fake/authenticate-gitee.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
import { giteeRequest, mock } from '../helper/helper';
import { giteeRequest } from '../helper/helper';
import { mockGiteeUser } from '../mock/mock-gitee-user';

describe('Test Use Fake Authenticate Gitee', () => {
beforeAll(() => {
mock.onGet('https://gitee.com/api/v5/user').reply(async () => {
return [200, {
"id": 1000001,
"login": "***",
"name": "***",
"avatar_url": "https://foruda.gitee.com/avatar/***/***.png",
"url": "https://gitee.com/api/v5/users/***",
"html_url": "https://gitee.com/***",
"remark": "",
"followers_url": "https://gitee.com/api/v5/users/***/followers",
"following_url": "https://gitee.com/api/v5/users/***/following_url{/other_user}",
"gists_url": "https://gitee.com/api/v5/users/***/gists{/gist_id}",
"starred_url": "https://gitee.com/api/v5/users/***/starred{/owner}{/repo}",
"subscriptions_url": "https://gitee.com/api/v5/users/***/subscriptions",
"organizations_url": "https://gitee.com/api/v5/users/***/orgs",
"repos_url": "https://gitee.com/api/v5/users/***/repos",
"events_url": "https://gitee.com/api/v5/users/***/events{/privacy}",
"received_events_url": "https://gitee.com/api/v5/users/***/received_events",
"type": "User",
"blog": null,
"weibo": null,
"bio": "",
"public_repos": 0,
"public_gists": 0,
"followers": 0,
"following": 0,
"stared": 0,
"watched": 0,
"created_at": "2020-02-26T13:20:27+08:00",
"updated_at": "2024-01-10T22:27:59+08:00",
"email": null
}];
});
});
describe('Test Fake Authenticate Gitee', () => {
test('Test Authenticate Gitee', async () => {
mockGiteeUser();
const res = await giteeRequest.authenticate();
expect(res).toEqual({
id: 1000001,
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/fake/authenticate-github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { githubRequest } from "../helper/helper";
import { mockGithubUser } from "../mock/mock-github-user";

describe('Test Fake Authenticate Github', () => {
test('Test Authenticate Github', async () => {
mockGithubUser();
const res = await githubRequest.authenticate();
expect(res).toEqual({
login: '***',
id: 1000001,
node_id: '***',
avatar_url: 'https://avatars.githubusercontent.com/u/***?v=4',
gravatar_id: '',
url: 'https://api.github.com/users/***',
html_url: 'https://github.com/***',
followers_url: 'https://api.github.com/users/***/followers',
following_url: 'https://api.github.com/users/***/following{/other_user}',
gists_url: 'https://api.github.com/users/***/gists{/gist_id}',
starred_url: 'https://api.github.com/users/***/starred{/owner}{/repo}',
subscriptions_url: 'https://api.github.com/users/***/subscriptions',
organizations_url: 'https://api.github.com/users/***/orgs',
repos_url: 'https://api.github.com/users/***/repos',
events_url: 'https://api.github.com/users/***/events{/privacy}',
received_events_url: 'https://api.github.com/users/***/received_events',
type: 'User',
site_admin: false,
name: '***',
company: null,
blog: '***.github.io',
location: '***',
email: null,
hireable: null,
bio: '***',
twitter_username: null,
public_repos: 0,
public_gists: 0,
followers: 0,
following: 0,
created_at: '2017-12-04T12:29:35Z',
updated_at: '2023-12-17T04:01:22Z'
});
});
});
51 changes: 51 additions & 0 deletions src/__tests__/fake/authenticate-gitlab.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { gitlabRequest } from "../helper/helper";
import { mockGitlabUser } from "../mock/mock-gitlab-user";

describe('Test Fake Authenticate Gitlab', () => {
test('Test Authenticate Gitlab', async () => {
mockGitlabUser();
const res = await gitlabRequest.authenticate();
expect(res).toEqual({
id: 100001,
username: '***',
name: '***',
state: 'active',
locked: false,
avatar_url: 'https://secure.gravatar.com/avatar/***?s=80&d=identicon',
web_url: 'https://gitlab.com/***',
created_at: '2023-12-21T02:56:43.269Z',
bio: '',
location: '',
public_email: null,
skype: '',
linkedin: '',
twitter: '',
discord: '',
website_url: '',
organization: '',
job_title: '',
pronouns: null,
bot: true,
work_information: null,
local_time: null,
last_sign_in_at: null,
confirmed_at: '2023-12-21T02:56:43.230Z',
last_activity_on: '2024-01-12',
email: '***@noreply.gitlab.com',
theme_id: 0,
color_scheme_id: 0,
projects_limit: 0,
current_sign_in_at: null,
identities: [],
can_create_group: true,
can_create_project: true,
two_factor_enabled: false,
external: false,
private_profile: false,
commit_email: '***@noreply.gitlab.com',
shared_runners_minutes_limit: null,
extra_shared_runners_minutes_limit: null,
scim_identities: []
});
});
});
34 changes: 34 additions & 0 deletions src/__tests__/fake/user-repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { User } from "../helper/user-repository";
import { initGiteeJSONFile, mockGiteeCreate, mockGiteeFind, mockGiteeFindById } from "../mock/mock-gitee-api";

describe('Test Fake User Repository', () => {
beforeAll(async ()=>{
await initGiteeJSONFile();
});

test('Test find User', async () => {
await mockGiteeFind();
const detail = await User.find();
expect(detail.length).toEqual(0);
});

test('Test create & findById User', async () => {
await mockGiteeCreate();
await User.create({
name: 'test-user',
age: 18
});

await mockGiteeFind();
const findResult = await User.find();
expect(findResult.length).toEqual(1);

await mockGiteeFindById(findResult[0].id);
const findByIdResult = await User.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.created_by).not.toBeNull();
expect(typeof findByIdResult.created_by.user_id).toBe('number');
expect(typeof findByIdResult.created_by.username).toBe('string');
expect(typeof findByIdResult.created_by.avatar_url).toBe('string');
});
});
16 changes: 10 additions & 6 deletions src/__tests__/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ dayjs.extend(timezone);
// 设置默认时区
dayjs.tz.setDefault('Asia/Shanghai');

export const GITEE_NUMBER = process.env.TEST_GITEE_NUMBER as string;
export const GITEE_NUMBER = process.env.TEST_GITEE_NUMBER || '1';

export const GITHUB_NUMBER = process.env.TEST_GITHUB_NUMBER as string;

export const GITLAB_NUMBER = process.env.TEST_GITLAB_NUMBER as string;

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

export const GITEE_OWNER = process.env.TEST_GITEE_OWNER || 'test-owner';

export const GITEE_REPO = process.env.TEST_GITEE_REPO || 'test-repo';

export const giteeRequest = createRequest({
httpLib: 'axios',
Expand All @@ -30,15 +34,15 @@ export const giteeRequest = createRequest({
accessToken: process.env.TEST_GITEE_TOKEN as string,

platform: 'gitee',
owner: process.env.TEST_GITEE_OWNER as string,
repo: process.env.TEST_GITEE_REPO as string,
owner: GITEE_OWNER,
repo: GITEE_REPO,

useEncrypt: true,
encryptFn: (data: string) => {
return CryptoJS.AES.encrypt(data, TEST_ENCRYPT_KEY).toString();
return CryptoJS.AES.encrypt(data, ENCRYPT_KEY).toString();
},
decryptFn: (data: string) => {
return CryptoJS.AES.decrypt(data, TEST_ENCRYPT_KEY).toString(CryptoJS.enc.Utf8);
return CryptoJS.AES.decrypt(data, ENCRYPT_KEY).toString(CryptoJS.enc.Utf8);
}
});

Expand Down
Empty file.
42 changes: 42 additions & 0 deletions src/__tests__/mock/mock-gitee-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import dayjs from "dayjs";
import { GITEE_NUMBER, GITEE_OWNER, GITEE_REPO, mock } from "../helper/helper";
import jsonfile from 'jsonfile';

const filepath = 'src/__tests__/mock/json/gitee.json';

export async function initGiteeJSONFile() {
await jsonfile.writeFile(filepath, []);
}

export async function mockGiteeFind() {
const result = await jsonfile.readFile(filepath);
mock.onGet(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(200, result);
}

export async function mockGiteeFindById(id: number) {
mock.onGet(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/${id}`).reply(async (config) => {
const result = await jsonfile.readFile(filepath);
return [200, result.find((item: any) => item.id === id)];
});
}

export async function mockGiteeCreate() {
mock.onPost(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(async (config) => {
const result = await jsonfile.readFile(filepath);
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)
await jsonfile.writeFile(filepath, result, {spaces: 2});
return [200, data];
});
}
35 changes: 35 additions & 0 deletions src/__tests__/mock/mock-gitee-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { mock } from "../helper/helper";

export function mockGiteeUser() {
mock.onGet('https://gitee.com/api/v5/user').reply(200, {
"id": 1000001,
"login": "***",
"name": "***",
"avatar_url": "https://foruda.gitee.com/avatar/***/***.png",
"url": "https://gitee.com/api/v5/users/***",
"html_url": "https://gitee.com/***",
"remark": "",
"followers_url": "https://gitee.com/api/v5/users/***/followers",
"following_url": "https://gitee.com/api/v5/users/***/following_url{/other_user}",
"gists_url": "https://gitee.com/api/v5/users/***/gists{/gist_id}",
"starred_url": "https://gitee.com/api/v5/users/***/starred{/owner}{/repo}",
"subscriptions_url": "https://gitee.com/api/v5/users/***/subscriptions",
"organizations_url": "https://gitee.com/api/v5/users/***/orgs",
"repos_url": "https://gitee.com/api/v5/users/***/repos",
"events_url": "https://gitee.com/api/v5/users/***/events{/privacy}",
"received_events_url": "https://gitee.com/api/v5/users/***/received_events",
"type": "User",
"blog": null,
"weibo": null,
"bio": "",
"public_repos": 0,
"public_gists": 0,
"followers": 0,
"following": 0,
"stared": 0,
"watched": 0,
"created_at": "2020-02-26T13:20:27+08:00",
"updated_at": "2024-01-10T22:27:59+08:00",
"email": null
});
}
Loading

0 comments on commit ff37fd2

Please sign in to comment.