Skip to content

Commit

Permalink
fixing all failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carvilsi committed Feb 25, 2024
1 parent 537b1e4 commit 5ddce43
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"description": "A snippet manager service",
"main": "./lib/index.js",
"scripts": {
"test-ci-cd": "cd test/; docker-compose down; sudo docker-compose up --build --force-recreate -d; cd ..; sleep 15; ./node_modules/jest/bin/jest.js --runInBand --runInBand --force-exit test/",
"test-ci-cd": "cd test/; sudo docker-compose up --build --force-recreate -d; cd ..; sleep 15; ./node_modules/jest/bin/jest.js --runInBand --force-exit test/",
"dev": "cd devops/dev; docker-compose up -d; cd -; nodemon -e js,ts -x ts-node -i test/ --files src/index.ts",
"dev-test": "cd devops/dev; docker-compose down; docker-compose up -d; cd -; sleep 15; ./node_modules/jest/bin/jest.js --runInBand --force-exit test/",
"dev-test": "cd devops/dev; docker-compose up -d; cd -; sleep 15; ./node_modules/jest/bin/jest.js --runInBand --force-exit test/",
"build": "tsc",
"start": "node lib/index.js",
"lint": "./node_modules/eslint/bin/eslint.js ./src/ --ext .ts",
"lint-test": "./node_modules/eslint/bin/eslint.js ./test/ --ext .ts",
"lint-fix": "./node_modules/eslint/bin/eslint.js ./src/ --ext .ts --fix",
"lint-test-fix": "./node_modules/eslint/bin/eslint.js ./test/ --ext .ts --fix",
"test": "jest",
"test": "jest --runInBand --force-exit",
"pre-release": "npm audit && ./pre-release.sh",
"release": "./git-tag-n-push.sh && ./docker-build.sh && ./docker-tag-n-push.sh",
"watch": "nodemon -e js,ts -x ts-node -i test/ --files src/index.ts"
Expand Down
45 changes: 25 additions & 20 deletions src/controllers/snippets_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ export async function findSnippetByQuery(
],
};
}
const results = await collection
.find(mongoQuery)
const results = await collection?.find(mongoQuery)

Check warning on line 66 in src/controllers/snippets_controller.ts

View workflow job for this annotation

GitHub Actions / build

Insert `⏎······`
.limit(limitFind)
.toArray();
const snippets: Snippet[] = [];
for (const result of results) {
if (results != null) {
for (const result of results) {
const snippet: Snippet = {

Check warning on line 72 in src/controllers/snippets_controller.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
snippet: result.snippet,

Check warning on line 73 in src/controllers/snippets_controller.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
description: result.description,

Check warning on line 74 in src/controllers/snippets_controller.ts

View workflow job for this annotation

GitHub Actions / build

Replace `········` with `··········`
Expand All @@ -77,6 +77,7 @@ export async function findSnippetByQuery(
};

Check warning on line 77 in src/controllers/snippets_controller.ts

View workflow job for this annotation

GitHub Actions / build

Replace `······` with `········`
snippets.push(snippet);

Check warning on line 78 in src/controllers/snippets_controller.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
}

Check warning on line 79 in src/controllers/snippets_controller.ts

View workflow job for this annotation

GitHub Actions / build

Insert `··`
}
return snippets;
} catch (error) {
errorControllerHandler(error);
Expand All @@ -87,20 +88,24 @@ export async function findSnippetByID(
snippetID: string
): Promise<Snippet | string | undefined> {
try {
const collection = await getCollection(collectionName);
logger.debug(`try to find commands with id: ${snippetID}`);
const objectId = new ObjectId(snippetID);
const result = await collection.findOne({ _id: objectId });
if (result != null) {
const snippet: Snippet = {
snippet: result.snippet,
description: result.description,
_id: result._id,
user: result.user,
};
return snippet;
if (snippetID != null) {
const collection = await getCollection(collectionName);
logger.debug(`try to find commands with id: ${snippetID}`);
const objectId = new ObjectId(snippetID);
const result = await collection?.findOne({ _id: objectId });
if (result != null) {
const snippet: Snippet = {
snippet: result.snippet,
description: result.description,
_id: result._id,
user: result.user,
};
return snippet;
} else {
throw new Error(`snippet not found for ${snippetID}`);
}
} else {
throw new Error(`snippet not found for ${snippetID}`);
throw new Error('bad request');
}
} catch (error) {
errorControllerHandler(error);
Expand All @@ -114,7 +119,7 @@ export async function deleteSnippetByID(
): Promise<boolean | undefined> {
try {
const collection = await getCollection(collectionName);
if (snippetID != null && userID != null) {
if (snippetID != null && userID != null && secret != null) {
const user = await findUserByID(userID, secret);
const command = await findSnippetByID(snippetID);
if (command != null) {
Expand All @@ -128,7 +133,7 @@ export async function deleteSnippetByID(
}
}
const objectId = new ObjectId(snippetID);
const result = await collection.deleteOne({ _id: objectId });
const result = await collection?.deleteOne({ _id: objectId });
if (result != null) {
if (result.acknowledged && result.deletedCount === 1) return true;
} else {
Expand All @@ -138,7 +143,6 @@ export async function deleteSnippetByID(
throw new Error('bad request');
}
} catch (error) {
console.dir(error);
errorControllerHandler(error);
}
}
Expand All @@ -156,7 +160,8 @@ export async function updateSnippet(
id != null &&
userID != null &&
snippetUpdate != null &&
descriptionUpdate != null
descriptionUpdate != null &&
secret != null
) {
const user = await findUserByID(userID, secret);
if (user == null)
Expand Down
3 changes: 1 addition & 2 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function paramCheck(
req: express.Request,
mandatoryParams: Array<string>,
{ check = 'body' }: { check?: string } = {}
): boolean {
): void {
for (const mandatoryParam of mandatoryParams) {
const errMessage = `bad request for endpoint, mandatory: ${mandatoryParam}`;
switch (check) {
Expand All @@ -33,7 +33,6 @@ export function paramCheck(
break;
}
}
return true;
}

export function userLengthCheck(username: string): boolean {
Expand Down
4 changes: 2 additions & 2 deletions test/snippets_create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('snippets create', () => {
const chance = new Chance();
const firstUser = chance.name();
const secondtUser = chance.name();
const firstUserSecret = chance.string();
const secondUserSecret = chance.string();
const firstUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 16 in test/snippets_create.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`
const secondUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 17 in test/snippets_create.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`

beforeAll(async () => {
let res = await request(testGlobals.__PYWLL_SERVER_URL__)
Expand Down
13 changes: 10 additions & 3 deletions test/snippets_delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('snippets delete', () => {
const chance = new Chance();
const firstUser = chance.name();
const secondtUser = chance.name();
const firstUserSecret = chance.string();
const secondUserSecret = chance.string();
const firstUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 18 in test/snippets_delete.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`
const secondUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 19 in test/snippets_delete.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`
const fakeSnippetID = testGlobals.__FAKE_ID__;

beforeAll(async () => {
Expand All @@ -42,6 +42,9 @@ describe('snippets delete', () => {
expect(res.statusCode).toBe(200);
expect(res.text.length).toBe(26);
secondUserID = JSON.parse(res.text);
console.log('================');
console.log(`secondUserSecret: ${secondUserSecret}`);
console.log(`secondUserID: ${secondUserID}`);
res = await request(testGlobals.__PYWLL_SERVER_URL__)
.post('/snippet')
.send(snippetObj)
Expand All @@ -61,16 +64,20 @@ describe('snippets delete', () => {
expect(res.statusCode).toBe(200);
expect(res.text.length).toBe(26);
secondUserSnippetID = JSON.parse(res.text);
console.log('-----------------');
console.log(`secondUserSnippetID: ${secondUserSnippetID}`);
});

test('should delete a snippet by id and for second user', async () => {
console.log('WTFFFFFFFFFFFFF');
let res = await request(testGlobals.__PYWLL_SERVER_URL__)
.delete(
`/snippet/${secondUserSnippetID}/${secondUserID}/${secondUserSecret}`
)
.set('Accept', 'application/json');
expect(res.statusCode).toBe(200);
console.dir(res.text);
let response = JSON.parse(res.text);
expect(res.statusCode).toBe(200);
expect(response).toBe(true);
res = await request(testGlobals.__PYWLL_SERVER_URL__)
.get('/snippet/find')
Expand Down
4 changes: 2 additions & 2 deletions test/snippets_read.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('snippets read (find)', () => {
const chance = new Chance();
const firstUser = chance.name();
const secondtUser = chance.name();
const firstUserSecret = chance.string();
const secondUserSecret = chance.string();
const firstUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 17 in test/snippets_read.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`
const secondUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 18 in test/snippets_read.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`
const fakeSnippetID = testGlobals.__FAKE_ID__;

beforeAll(async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/snippets_update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { beforeAll, describe, expect, test } from '@jest/globals';
import request from 'supertest';
import testGlobals from './test_globals';

describe('snippets CRUD', () => {
describe('snippets update', () => {
let firstUserSnippetID: string;
let firstUserID: string;
let secondUserID: string;
const snippetObj = testGlobals.__SNIPPET_OBJECT__;
const chance = new Chance();
const firstUser = chance.name();
const secondtUser = chance.name();
const firstUserSecret = chance.string();
const secondUserSecret = chance.string();
const firstUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 17 in test/snippets_update.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`
const secondUserSecret = chance.string({ pool: 'abcdef01234456789'});

Check warning on line 18 in test/snippets_update.test.ts

View workflow job for this annotation

GitHub Actions / build

Insert `·`
const fakeSnippetID = testGlobals.__FAKE_ID__;
const newCommand = 'ls -la';
const newDescription = 'list with hidden files and details';
Expand Down

0 comments on commit 5ddce43

Please sign in to comment.