import request from "../../../app/tests/request"; import withDatabase from "../../../utils/tests/withDatabase"; import { ANOTHER_USER_ID, A_USER_ID, A_DATE, ANOTHER_DATE, } from "../../../utils/tests/testData"; import { aPostSuggestionsBody, aSuggestion, persistASuggestion, someSuggestions, } from "./postSuggestions.driver"; import { A_USER_BEARER } from "../../../utils/tests/jwtUtils"; import { getSuggestionsMetadataRepository } from "../repositories/suggestionsMetadata.repository"; import { getSuggestionsDataRepository } from "../repositories/suggestionsData.repository"; describe("POST /suggestions", () => { withDatabase(); it("Given wrong authorization token Then returns unauthorized", async () => { const response = await request .post("/suggestions") .set("Authorization", `Bearer SOME_FAKE_BEARER_TOKEN`); expect(response.status).toBe(401); }); describe("New version", () => { it("Given no suggestion data Then request is successful", async () => { const response = await request .post("/suggestions") .set("Authorization", A_USER_BEARER) .send({ data: [], metadata: [] }); expect(response.status).toEqual(200); }); it("Given some suggestions Then they are persisted successfully", async () => { const response = await request .post("/suggestions") .set("Authorization", A_USER_BEARER) .send(aPostSuggestionsBody()); expect(response.status).toEqual(200); expect(await getSuggestionsMetadataRepository().find()).toMatchObject([ { character: 30, filename: "some filename", git_branch: "some git branch", git_index_sha1: "some git index sha1", git_repo: "some git repo", id: 1, language: "some language", line: 5, offset: 180, suggestionId: 123, time: A_DATE, user: "A_USER_ID", }, { character: 14, filename: "different filename", git_branch: "some git branch", git_index_sha1: "some git index sha1", git_repo: "some git repo", id: 2, language: "different language", line: 72, offset: 928, suggestionId: 124, time: ANOTHER_DATE, user: "A_USER_ID", }, ]); expect(await getSuggestionsDataRepository().find()).toMatchObject([ { id: 1, original_prefix: "some original_prefix", original_suffix: "some original_suffix", snapshot_prefix: "some snapshot_prefix", snapshot_suffix: "some snapshot_suffix", }, { id: 2, original_prefix: "different original_prefix", original_suffix: "different original_suffix", snapshot_prefix: "different snapshot_prefix", snapshot_suffix: "different snapshot_suffix", }, ]); }); }); describe("Old version", () => { it("Given no suggestions Then request is successful", async () => { const response = await request .post("/suggestions") .set("Authorization", A_USER_BEARER) .send({ suggestions: [] }); expect(response.status).toEqual(200); }); it("Given some suggestions Then they are persisted successfully", async () => { const response = await request .post("/suggestions") .set("Authorization", A_USER_BEARER) .send({ suggestions: someSuggestions(), }); expect(response.status).toEqual(200); expect(await getSuggestionsMetadataRepository().count()).toBe(2); expect(await getSuggestionsDataRepository().count()).toBe(2); }); it("Given existing suggestions when post more suggestions Then they are added", async () => { await persistASuggestion(ANOTHER_USER_ID); const response = await request .post("/suggestions") .set("Authorization", A_USER_BEARER) .send({ suggestions: someSuggestions(), }); expect(response.status).toEqual(200); expect(await getSuggestionsMetadataRepository().find()).toMatchObject( [aSuggestion(ANOTHER_USER_ID), ...someSuggestions(A_USER_ID)].map( (it) => ({ suggestionId: it.id, filename: it.filename, language: it.language, time: it.time, user: it.user, }) ) ); expect(await getSuggestionsDataRepository().find()).toMatchObject( [aSuggestion(ANOTHER_USER_ID), ...someSuggestions(A_USER_ID)].map( (it) => ({ original_prefix: it.original_prefix, original_suffix: it.original_suffix, snapshot_prefix: it.snapshot_prefix, snapshot_suffix: it.snapshot_suffix, }) ) ); }); }); });