Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Amplication): Update Generated Code for demo-service #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BCRYPT_SALT=10
COMPOSE_PROJECT_NAME=amp_clsnjtzkx0gyaj401m9rrwx78
COMPOSE_PROJECT_NAME=amp_clso7jmz80k4lj401qbjtswe1
DB_NAME=my-db
DB_PASSWORD=admin
DB_PORT=5432
Expand Down
2 changes: 1 addition & 1 deletion server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ services:
- ${PORT}:3000
environment:
BCRYPT_SALT: ${BCRYPT_SALT}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
JWT_SECRET_KEY: ${JWT_SECRET_KEY}
JWT_EXPIRATION: ${JWT_EXPIRATION}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
depends_on:
- migrate
restart: on-failure
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@light-management/server",
"name": "@demo-service/server",
"private": true,
"scripts": {
"start": "nest start",
Expand Down
17 changes: 17 additions & 0 deletions server/src/address/address.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as common from "@nestjs/common";
import * as swagger from "@nestjs/swagger";
import * as nestAccessControl from "nest-access-control";
import { AddressService } from "./address.service";
import { AddressControllerBase } from "./base/address.controller.base";

@swagger.ApiTags("addresses")
@common.Controller("addresses")
export class AddressController extends AddressControllerBase {
constructor(
protected readonly service: AddressService,
@nestAccessControl.InjectRolesBuilder()
protected readonly rolesBuilder: nestAccessControl.RolesBuilder
) {
super(service, rolesBuilder);
}
}
210 changes: 210 additions & 0 deletions server/src/address/base/address.controller.base.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import { Test } from "@nestjs/testing";
import {
INestApplication,
HttpStatus,
ExecutionContext,
CallHandler,
} from "@nestjs/common";
import request from "supertest";
import { ACGuard } from "nest-access-control";
import { DefaultAuthGuard } from "../../auth/defaultAuth.guard";
import { ACLModule } from "../../auth/acl.module";
import { AclFilterResponseInterceptor } from "../../interceptors/aclFilterResponse.interceptor";
import { AclValidateRequestInterceptor } from "../../interceptors/aclValidateRequest.interceptor";
import { map } from "rxjs";
import { AddressController } from "../address.controller";
import { AddressService } from "../address.service";

const nonExistingId = "nonExistingId";
const existingId = "existingId";
const CREATE_INPUT = {
address_1: "exampleAddress_1",
address_2: "exampleAddress_2",
city: "exampleCity",
createdAt: new Date(),
id: "exampleId",
state: "exampleState",
updatedAt: new Date(),
zip: 42,
};
const CREATE_RESULT = {
address_1: "exampleAddress_1",
address_2: "exampleAddress_2",
city: "exampleCity",
createdAt: new Date(),
id: "exampleId",
state: "exampleState",
updatedAt: new Date(),
zip: 42,
};
const FIND_MANY_RESULT = [
{
address_1: "exampleAddress_1",
address_2: "exampleAddress_2",
city: "exampleCity",
createdAt: new Date(),
id: "exampleId",
state: "exampleState",
updatedAt: new Date(),
zip: 42,
},
];
const FIND_ONE_RESULT = {
address_1: "exampleAddress_1",
address_2: "exampleAddress_2",
city: "exampleCity",
createdAt: new Date(),
id: "exampleId",
state: "exampleState",
updatedAt: new Date(),
zip: 42,
};

const service = {
createAddress() {
return CREATE_RESULT;
},
addresses: () => FIND_MANY_RESULT,
address: ({ where }: { where: { id: string } }) => {
switch (where.id) {
case existingId:
return FIND_ONE_RESULT;
case nonExistingId:
return null;
}
},
};

const basicAuthGuard = {
canActivate: (context: ExecutionContext) => {
const argumentHost = context.switchToHttp();
const request = argumentHost.getRequest();
request.user = {
roles: ["user"],
};
return true;
},
};

const acGuard = {
canActivate: () => {
return true;
},
};

const aclFilterResponseInterceptor = {
intercept: (context: ExecutionContext, next: CallHandler) => {
return next.handle().pipe(
map((data) => {
return data;
})
);
},
};
const aclValidateRequestInterceptor = {
intercept: (context: ExecutionContext, next: CallHandler) => {
return next.handle();
},
};

describe("Address", () => {
let app: INestApplication;

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
{
provide: AddressService,
useValue: service,
},
],
controllers: [AddressController],
imports: [ACLModule],
})
.overrideGuard(DefaultAuthGuard)
.useValue(basicAuthGuard)
.overrideGuard(ACGuard)
.useValue(acGuard)
.overrideInterceptor(AclFilterResponseInterceptor)
.useValue(aclFilterResponseInterceptor)
.overrideInterceptor(AclValidateRequestInterceptor)
.useValue(aclValidateRequestInterceptor)
.compile();

app = moduleRef.createNestApplication();
await app.init();
});

test("POST /addresses", async () => {
await request(app.getHttpServer())
.post("/addresses")
.send(CREATE_INPUT)
.expect(HttpStatus.CREATED)
.expect({
...CREATE_RESULT,
createdAt: CREATE_RESULT.createdAt.toISOString(),
updatedAt: CREATE_RESULT.updatedAt.toISOString(),
});
});

test("GET /addresses", async () => {
await request(app.getHttpServer())
.get("/addresses")
.expect(HttpStatus.OK)
.expect([
{
...FIND_MANY_RESULT[0],
createdAt: FIND_MANY_RESULT[0].createdAt.toISOString(),
updatedAt: FIND_MANY_RESULT[0].updatedAt.toISOString(),
},
]);
});

test("GET /addresses/:id non existing", async () => {
await request(app.getHttpServer())
.get(`${"/addresses"}/${nonExistingId}`)
.expect(HttpStatus.NOT_FOUND)
.expect({
statusCode: HttpStatus.NOT_FOUND,
message: `No resource was found for {"${"id"}":"${nonExistingId}"}`,
error: "Not Found",
});
});

test("GET /addresses/:id existing", async () => {
await request(app.getHttpServer())
.get(`${"/addresses"}/${existingId}`)
.expect(HttpStatus.OK)
.expect({
...FIND_ONE_RESULT,
createdAt: FIND_ONE_RESULT.createdAt.toISOString(),
updatedAt: FIND_ONE_RESULT.updatedAt.toISOString(),
});
});

test("POST /addresses existing resource", async () => {
const agent = request(app.getHttpServer());
await agent
.post("/addresses")
.send(CREATE_INPUT)
.expect(HttpStatus.CREATED)
.expect({
...CREATE_RESULT,
createdAt: CREATE_RESULT.createdAt.toISOString(),
updatedAt: CREATE_RESULT.updatedAt.toISOString(),
})
.then(function () {
agent
.post("/addresses")
.send(CREATE_INPUT)
.expect(HttpStatus.CONFLICT)
.expect({
statusCode: HttpStatus.CONFLICT,
});
});
});

afterAll(async () => {
await app.close();
});
});
Loading