Skip to content

Commit

Permalink
style(api): fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Feb 22, 2019
1 parent 4a7541d commit 264c011
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 135 deletions.
18 changes: 10 additions & 8 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,21 @@
"root": "apps/api-e2e",
"sourceRoot": "apps/api-e2e/src",
"projectType": "application",
"prefix": "api",
"schematics": {},
"architect": {
"test": {
"builder": "@nrwl/builders:jest",
"options": {
"jestConfig": "apps/api-e2e/jest.config.js",
"tsConfig": "apps/api-e2e/tsconfig.e2e.json"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "apps/api-e2e/tsconfig.e2e.json",
"exclude": ["**/node_modules/**"]
"exclude": ["**/node_modules/**", "**/*.json"]
}
},
"e2e": {
"builder": "@nrwl/builders:jest",
"options": {
"jestConfig": "apps/api-e2e/jest.config.js",
"tsConfig": "apps/api-e2e/tsconfig.e2e.json"
}
}
}
Expand Down
15 changes: 2 additions & 13 deletions apps/api-e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
module.exports = {
name: 'api-e2e',
testRegex: '.e2e-spec.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
moduleFileExtensions: ['js', 'json', 'ts'],
collectCoverage: true,
coverageReporters: ['html'],
testEnvironment: 'node',
preset: '../../jest.config.js',
testMatch: ['**/+(*.)+(e2e-spec|e2e-test).+(ts|js)?(x)'],
coverageDirectory: '../../coverage/apps/api-e2e',
globals: {
'ts-jest': {
tsConfig: 'apps/api-e2e/tsconfig.e2e.json',
},
},
};
4 changes: 2 additions & 2 deletions apps/api-e2e/src/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
// @ts-ignore
import { AppModule } from '../../api/src/app/app.module';
// import { AppModule } from '../../api/src/app/app.module';

jest.setTimeout(30000);

Expand All @@ -11,7 +11,7 @@ describe('AppController (e2e)', () => {

beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
// imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
Expand Down
78 changes: 78 additions & 0 deletions apps/api-e2e/src/auth/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// import 'jest';
// import * as supertest from 'supertest';
// import { Test } from '@nestjs/testing';
// import { INestApplication } from '@nestjs/common';
//
// import { AuthModule } from '../../src/auth/auth.module';
// import { AuthService } from '../../src/auth/auth.service';
// import { AuthenticateDto } from '../../src/auth/dto/authenticate-dto';
// import { users } from '../../src/auth/users.const';
//
// describe('Auth', () => {
// let app: INestApplication;
//
// //
// // Setup mock data & services
// //
// const regularUser = users.find(user => user.username === 'user@mydomain.com');
// let regularUserJWTToken: any;
// const staffUser = users.find(user => user.username === 'staff@mydomain.com');
// let staffUserJWTToken: any;
//
// beforeAll(async () => {
// //
// // Instantiate nest application
// //
// const module = await Test.createTestingModule({
// imports: [AuthModule],
// }).compile();
//
// app = module.createNestApplication();
// await app.init();
//
// supertest(app.getHttpServer())
// .post('/api/auth/authenticate')
// .send({ authenticateDto: { username: regularUser.username, password: regularUser.password } })
// .end((err, res) => {
// regularUserJWTToken = JSON.parse(res.text).data;
// });
//
// supertest(app.getHttpServer())
// .post('/api/auth/authenticate')
// .send({ authenticateDto: { username: staffUser.username, password: staffUser.password } })
// .end((err, res) => {
// staffUserJWTToken = JSON.parse(res.text).data;
// });
// });
//
// it(`/POST auth`, () => {
// return supertest(app.getHttpServer())
// .post('/api/auth/authenticate')
// .send({ authenticateDto: { username: regularUser.username, password: regularUser.password } })
// .expect(201);
// });
//
// it(`/GET protected data without auth token`, () => {
// return supertest(app.getHttpServer())
// .get('/api/auth/data')
// .expect(401);
// });
//
// it(`/GET protected data with wrong role`, () => {
// return supertest(app.getHttpServer())
// .get('/api/auth/data')
// .set('authorization', `Bearer ${regularUserJWTToken.accessToken}`)
// .expect(403);
// });
//
// it(`/GET protected data with correct role`, () => {
// return supertest(app.getHttpServer())
// .get('/api/auth/data')
// .set('authorization', `Bearer ${staffUserJWTToken.accessToken}`)
// .expect(200);
// });
//
// afterAll(async () => {
// await app.close();
// });
// });
99 changes: 99 additions & 0 deletions apps/api-e2e/src/notifications/notifications.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// import 'jest';
// import * as supertest from 'supertest';
// import { Test } from '@nestjs/testing';
// import { CatsModule } from '../../src/cats/cats.module';
// import { INestApplication } from '@nestjs/common';
// import { AuthService } from '../../src/auth/auth.service';
// import { AccessTokenWithMetadata } from '../../src/auth/interfaces/jwt-accessTokenData.interface';
// import { users } from '../../src/auth/users.const';
// import { ConfigService } from '../../src/common/services/config.service';
// import { CatsService } from '../../src/cats/cats.service';
//
// describe('Notifications', () => {
// let app: INestApplication;
// let userAuthToken: AccessTokenWithMetadata;
// let staffAuthToken: AccessTokenWithMetadata;
// const configService = new ConfigService();
//
// //
// // Set configuration flags
// //
// configService.nodeEnv = 'test';
//
// //
// // Setup mock data & services
// //
// const userJwtPayload = {
// user: users.find(user => user.username === 'user@mydomain.com'),
// };
//
// const staffJwtPayload = {
// user: users.find(user => user.username === 'staff@mydomain.com'),
// };
//
// const catsService = { findAll: () => ['test'] };
//
// beforeAll(async () => {
// //
// // Instantiate nest application
// //
// const module = await Test.createTestingModule({
// imports: [CatsModule],
// })
// .overrideProvider(CatsService)
// .useValue(catsService)
// .compile();
//
// app = module.createNestApplication();
// await app.init();
//
// userAuthToken = await app.get(AuthService).createToken(userJwtPayload);
// staffAuthToken = await app.get(AuthService).createToken(staffJwtPayload);
// });
//
// it(`/GET cats`, () => {
// return supertest(app.getHttpServer())
// .get('/api/cats')
// .expect(200)
// .expect({
// data: catsService.findAll(),
// });
// });
//
// it(`/GET cats with auth token`, () => {
// return supertest(app.getHttpServer())
// .get('/api/cats')
// .set('authorization', `Bearer ${userAuthToken}`)
// .expect(200)
// .expect({
// data: catsService.findAll(),
// });
// });
//
// it(`/GET protected cats without auth token`, () => {
// return supertest(app.getHttpServer())
// .get('/api/cats/protected')
// .expect(401);
// });
//
// it(`/GET protected cats with user auth token`, () => {
// return supertest(app.getHttpServer())
// .get('/api/cats/protected')
// .set('authorization', `Bearer ${userAuthToken.accessToken}`)
// .expect(403);
// });
//
// it(`/GET protected cats with staff auth token`, () => {
// return supertest(app.getHttpServer())
// .get('/api/cats/protected')
// .set('authorization', `Bearer ${staffAuthToken.accessToken}`)
// .expect(200)
// .expect({
// data: catsService.findAll(),
// });
// });
//
// afterAll(async () => {
// await app.close();
// });
// });
3 changes: 1 addition & 2 deletions apps/api-e2e/tsconfig.e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/apps/api-e2e",
"module": "commonjs",
"target": "es6",
"types": ["jest", "node"]
},
"include": ["**/*.ts"]
"include": ["**/*.e2e-spec.ts", "**/*.d.ts"]
}
33 changes: 5 additions & 28 deletions apps/api-e2e/tslint.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
{
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"jsRules": {
"no-unused-expression": true
},
"rules": {
"eofline": false,
"quotemark": [true, "single"],
"indent": false,
"member-access": [false],
"ordered-imports": [false],
"max-line-length": [true, 150],
"member-ordering": [false],
"curly": false,
"interface-name": [false],
"array-type": [false],
"no-console": false,
"no-empty-interface": false,
"no-empty": false,
"arrow-parens": false,
"object-literal-sort-keys": false,
"no-unused-expression": false,
"max-classes-per-file": [false],
"variable-name": [false],
"one-line": [false],
"one-variable-per-declaration": [false]
},
"rulesDirectory": []
"extends": "../../tslint.json",
"rules": {},
"jsRules":{
"object-literal-sort-keys": false
}
}
29 changes: 21 additions & 8 deletions apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ ng serve api --prod
#### Run Prod Mode

```bash
# build first
# clean dist first
npx rimraf dist
# then build
ng build api --prod
# then run
npm run api:start:prod
node dist/apps/api/main.js
```

**API URL:** http://localhost:3000
Expand All @@ -72,7 +74,7 @@ npm run api:start:prod
> build for production env
```bash
npm run api:build
ng build api --prod
```

### Generate
Expand All @@ -92,16 +94,27 @@ nest g class user.entity app/auth --no-spec --dry-run
nest g class auth.exception app/auth --no-spec --dry-run
```

### Test
### Unit Test

> coverage will be generate in coverage/apps/api
```bash
# unit tests
ng test api
# unit test only changed
ng test api --onlyChanged
# generate coverage
ng test api --codeCoverage
# test in CI env
ng test api --runInBand
```

# e2e tests
ng test api-e2e
### E2E Test

```bash
ng e2e api-e2e
# e2e test with watch mode
ng e2e api-e2e --watch
# e2e test in CI env
ng e2e api-e2e --forceExit --detectOpenHandles
```

### Reference
Expand Down
22 changes: 11 additions & 11 deletions apps/api/src/app/auth/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ export const defaultOptions = {
},
};

const createPassportContext = (request, response) => (type, options) =>
new Promise((resolve, reject) =>
passport.authenticate(type, options, (err, user, info) => {
try {
return resolve(options.callback(err, user, info));
} catch (err) {
reject(err);
}
})(request, response, resolve),
);

// TODO like https://github.com/nestjs/nest/blob/master/sample/19-auth/src/auth/guards/jwt-auth.guard.ts
@Injectable()
export class AuthGuard implements CanActivate {
Expand All @@ -30,14 +41,3 @@ export class AuthGuard implements CanActivate {
return true;
}
}

const createPassportContext = (request, response) => (type, options) =>
new Promise((resolve, reject) =>
passport.authenticate(type, options, (err, user, info) => {
try {
return resolve(options.callback(err, user, info));
} catch (err) {
reject(err);
}
})(request, response, resolve),
);
Loading

0 comments on commit 264c011

Please sign in to comment.