Skip to content

Commit

Permalink
fix: remove hostInject and change host check
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiming committed Mar 17, 2018
1 parent 0f987bc commit e851175
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 46 deletions.
5 changes: 1 addition & 4 deletions src/controllers/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { IInitialize, IPageInfo, IAssetsInfo, IExit } from '../interfaces/Log';
import { Description, ResType } from 'routing-controllers-openapi-v3';
import { LogService } from '../services/LogService';
import { IToken, IPageId } from '../interfaces/Helper';
import hostInject from '../middlewares/hostInject';
import sessionInject from '../middlewares/sessionInject';
import { Context } from 'koa';

Expand All @@ -16,14 +15,12 @@ export class LogController {
@Description('建立会话')
@ResType(IToken)
@Post('/initialize')
@UseBefore(hostInject())
async initialize(
@Ctx() ctx: Context,
@Body() body: IInitialize,
@State('hostId') hostId: number,
@HeaderParam('Authorization') token?: string
): Promise<IToken> {
return await this.logService.initialize(body, ctx.ip, hostId, token);
return await this.logService.initialize(body, ctx.ip, token);
}

@Description(`页面数据`)
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/Log.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Session from '../entities/Session';
import Visiter from '../entities/Visiter';
import Page from '../entities/Page';
import Asset from '../entities/Asset';
import { IsString, IsNumber, IsOptional, IsArray, IsDateString } from 'class-validator';
import { Type } from 'class-transformer';

export class IInitialize implements Partial<Session>, Partial<Visiter> {
export class IInitialize implements Partial<Visiter> {
@IsString() referrer: string;
@IsString() lang: string;
@IsString() ua: string;
@IsString() os: string;
@IsString() host: string;
}

export class IPageInfo implements Partial<Page> {
Expand Down
30 changes: 0 additions & 30 deletions src/middlewares/hostInject.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/services/LogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export class LogService {
hostRepository: HostRepository = getCustomRepository(HostRepository);
assetRepository: AssetRepository = getCustomRepository(AssetRepository);

async initialize(body: IInitialize, ip: string, hostId: number, preToken?: string): Promise<IToken> {
const host = await this.hostRepository.findOneById(hostId);
async initialize(body: IInitialize, ip: string, preToken?: string): Promise<IToken> {
const website = body.host;

const host = await this.hostRepository.findOne({ website });

if (!host) {
throw new BadRequestError('Host is not registed');
Expand Down
10 changes: 2 additions & 8 deletions test/controllers/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ describe('LogController', () => {
});

assert(res.status === 400);
assert(res.body.message === 'Request Host Not Found');
});

it('建立会话,无 TOKEN', async () => {
const res = await request(Test.Instance.app)
.post('/log/initialize')
.set('t-host', host.website)
.send({
referrer: 'https://ruiming.me',
lang: 'zh-cn',
host: host.website,
ua: faker.internet.userAgent(),
os: 'linux'
});
Expand All @@ -52,10 +51,10 @@ describe('LogController', () => {
const res = await request(Test.Instance.app)
.post('/log/initialize')
.set('authorization', token)
.set('t-host', host.website)
.send({
referrer: 'https://ruiming.me',
lang: 'zh-cn',
host: host.website,
ua: faker.internet.userAgent(),
os: 'linux'
});
Expand All @@ -69,7 +68,6 @@ describe('LogController', () => {
it('发送页面数据,没有 TOKEN', async () => {
const res = await request(Test.Instance.app)
.post('/log/page')
.set('t-host', host.website)
.send(Test.Instance.mockPage());

assert(res.status === 400);
Expand All @@ -79,7 +77,6 @@ describe('LogController', () => {
const res = await request(Test.Instance.app)
.post('/log/page')
.set('authorization', token)
.set('t-host', host.website)
.send(
Test.Instance.mockPage({
url: 'https://ruiming.me'
Expand All @@ -96,7 +93,6 @@ describe('LogController', () => {
const res = await request(Test.Instance.app)
.post('/log/assets')
.set('authorization', token)
.set('t-host', host.website)
.send({
pageId,
assets: [Test.Instance.mockAsset({ name: name1 }), Test.Instance.mockAsset({ name: name2 })]
Expand All @@ -115,7 +111,6 @@ describe('LogController', () => {
const res = await request(Test.Instance.app)
.post('/log/page')
.set('authorization', token)
.set('t-host', host.website)
.send(
Test.Instance.mockPage({
url: 'https://ruiming.me',
Expand All @@ -132,7 +127,6 @@ describe('LogController', () => {
const res = await request(Test.Instance.app)
.post('/log/exit')
.set('authorization', token)
.set('t-host', host.website)
.send({
pageId: nextPageId,
exitTime: new Date()
Expand Down

0 comments on commit e851175

Please sign in to comment.