Skip to content

Commit

Permalink
test: add get me integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelTshDev committed Apr 16, 2024
1 parent d5c2cb1 commit ed0e358
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
27 changes: 20 additions & 7 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"jwt-decode": "^3.1.2",
"pg": "^8.11.3",
"redis": "^4.6.10",
"sinon": "^17.0.1",
"source-map-support": "^0.5.21",
"swagger-ui-dist": "^5.9.0",
"swagger-ui-express": "^5.0.0",
Expand Down Expand Up @@ -110,11 +109,12 @@
"husky": "^8.0.3",
"lodash.kebabcase": "^4.1.1",
"mocha": "^10.2.0",
"nock": "^13.3.4",
"nock": "^13.5.4",
"nyc": "^15.1.0",
"plop": "^4.0.0",
"prettier": "^3.0.3",
"supertest": "^6.3.3",
"sinon": "^17.0.1",
"supertest": "^6.3.4",
"typescript": "^5.2.2"
}
}
30 changes: 29 additions & 1 deletion src/tests/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ import "mocha";
import { use } from "chai";
import chaiAsPromised from "chai-as-promised";
import { DataSource } from "typeorm";
import sinon from "sinon";
import "express-async-errors";
import { createContainer } from "../container";
import { config } from "../config/db";
import "express-async-errors";
import * as auth0Module from "../middleware/auth0";

use(chaiAsPromised);

const clearDb = async (_dataSource: DataSource) => {};
const clearDb = async (dataSource: DataSource) => {
const entities = dataSource.entityMetadatas;

await dataSource.manager.transaction(async (transactionalEntityManager) => {
// disable checking relations
await transactionalEntityManager.query("SET session_replication_role = replica;");

await Promise.all(entities.map((entity) => transactionalEntityManager.query(`DELETE FROM "${entity.tableName}"`)));

// enable checking relations
await transactionalEntityManager.query("SET session_replication_role = origin;");
});
};

before(async () => {
const dbConnection = await new DataSource({
Expand All @@ -19,6 +33,20 @@ before(async () => {

global.dbConnection = dbConnection;
await dbConnection.dropDatabase();
sinon.stub(auth0Module, "validateAccessToken").callsFake(() => (req, res, next) => {
// eslint-disable-next-line no-param-reassign
req.auth = {
payload: {
me: {
email: "test@integration.com",
},
},
header: {},
token: "",
};

return next();
});

global.container = await createContainer();
});
Expand Down
33 changes: 33 additions & 0 deletions src/tests/shared/get-me.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import request from "supertest";
import { StatusCodes } from "http-status-codes";
import { expect } from "chai";
import { dataSource } from "../../config/db";
import { UserEntity } from "../../app/features/example/models/user.entity";

describe("GET /api/example/me", () => {
const authToken = "Bearer example.token";
const userRepository = dataSource.getRepository(UserEntity);

it("should return proper profile data", async () => {
const expectedUserEntity = await userRepository.save({
firstName: "Test",
lastName: "Integration",
email: "test@integration.com",
});

await request(await global.container.cradle.app)
.get("/api/example/me")
.set("Authorization", authToken)
.expect(StatusCodes.OK)
.expect((response) => {
expect(response.body).to.deep.equal(expectedUserEntity);
});
});

it("should return Not Found if there is no such user in database", async () => {
await request(await global.container.cradle.app)
.get("/api/example/me")
.set("Authorization", authToken)
.expect(StatusCodes.NOT_FOUND);
});
});

0 comments on commit ed0e358

Please sign in to comment.