Skip to content

Commit 4e869f1

Browse files
committed
production: * hot fix backend tests
1 parent b0c15d4 commit 4e869f1

File tree

13 files changed

+226
-130
lines changed

13 files changed

+226
-130
lines changed

server/tests/api/1-dislike-post/post.api.spec.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,23 @@ const loginEndpoint = joinPath([
2929

3030
const postApiPath = joinPath([config.ENV.APP.API_PATH, ApiPath.POSTS]);
3131

32-
const postIdEndpoint = joinPath(
32+
const postIdEndpoint = joinPath([
3333
config.ENV.APP.API_PATH,
3434
ApiPath.POSTS,
3535
PostsApiPath.$ID
36-
);
36+
]);
3737

38-
const postReactEndpoint = joinPath(
38+
const postReactEndpoint = joinPath([
3939
config.ENV.APP.API_PATH,
4040
ApiPath.POSTS,
4141
PostsApiPath.REACT
42-
);
42+
]);
4343

4444
describe(`${postApiPath} routes`, () => {
45-
const { app, knex } = buildApp();
46-
const { select, insert } = getCrudHandlers(knex);
45+
const { getApp, getKnex } = buildApp();
46+
const { select, insert } = getCrudHandlers(getKnex);
47+
48+
const app = getApp();
4749

4850
let token;
4951

@@ -64,13 +66,13 @@ describe(`${postApiPath} routes`, () => {
6466
token = loginResponse.json().token;
6567
});
6668

67-
describe(`${postReactEndpoint} (${HttpMethod.PUT}) endpoint`, async () => {
68-
const { id: postId } = await select({
69-
table: DatabaseTableName.COMMENTS,
70-
limit: KNEX_SELECT_ONE_RECORD
71-
});
72-
69+
describe(`${postReactEndpoint} (${HttpMethod.PUT}) endpoint`, () => {
7370
it(`should return ${HttpCode.OK} with liked post`, async () => {
71+
const { id: postId } = await select({
72+
table: DatabaseTableName.POSTS,
73+
limit: KNEX_SELECT_ONE_RECORD
74+
});
75+
7476
const getPostBeforeLikeResponse = await app
7577
.inject()
7678
.get(postIdEndpoint.replace(':id', postId))
@@ -93,6 +95,11 @@ describe(`${postApiPath} routes`, () => {
9395
});
9496

9597
it(`should return ${HttpCode.OK} with removed user's like post`, async () => {
98+
const { id: postId } = await select({
99+
table: DatabaseTableName.POSTS,
100+
limit: KNEX_SELECT_ONE_RECORD
101+
});
102+
96103
const getPostBeforeLikeResponse = await app
97104
.inject()
98105
.get(postIdEndpoint.replace(':id', postId))
@@ -115,6 +122,11 @@ describe(`${postApiPath} routes`, () => {
115122
});
116123

117124
it(`should return ${HttpCode.OK} with disliked post`, async () => {
125+
const { id: postId } = await select({
126+
table: DatabaseTableName.POSTS,
127+
limit: KNEX_SELECT_ONE_RECORD
128+
});
129+
118130
const getPostBeforeLikeResponse = await app
119131
.inject()
120132
.get(postIdEndpoint.replace(':id', postId))
@@ -137,6 +149,11 @@ describe(`${postApiPath} routes`, () => {
137149
});
138150

139151
it(`should return ${HttpCode.OK} with removed user's dislike post`, async () => {
152+
const { id: postId } = await select({
153+
table: DatabaseTableName.POSTS,
154+
limit: KNEX_SELECT_ONE_RECORD
155+
});
156+
140157
const getPostBeforeLikeResponse = await app
141158
.inject()
142159
.get(postIdEndpoint.replace(':id', postId))
@@ -159,6 +176,11 @@ describe(`${postApiPath} routes`, () => {
159176
});
160177

161178
it(`should return ${HttpCode.OK} with switched like to dislike post`, async () => {
179+
const { id: postId } = await select({
180+
table: DatabaseTableName.POSTS,
181+
limit: KNEX_SELECT_ONE_RECORD
182+
});
183+
162184
const getPostBeforeLikeResponse = await app
163185
.inject()
164186
.get(postIdEndpoint.replace(':id', postId))
@@ -198,6 +220,11 @@ describe(`${postApiPath} routes`, () => {
198220
});
199221

200222
it(`should return ${HttpCode.OK} with switched dislike to like post`, async () => {
223+
const { id: postId } = await select({
224+
table: DatabaseTableName.POSTS,
225+
limit: KNEX_SELECT_ONE_RECORD
226+
});
227+
201228
const getPostBeforeLikeResponse = await app
202229
.inject()
203230
.get(postIdEndpoint.replace(':id', postId))

server/tests/api/10-show-who-liked-comment/post-comment.api.spec.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ const commentApiPath = joinPath([config.ENV.APP.API_PATH, ApiPath.COMMENTS]);
3030

3131
const postApiPath = joinPath([config.ENV.APP.API_PATH, ApiPath.POSTS]);
3232

33-
const postIdEndpoint = joinPath(
33+
const postIdEndpoint = joinPath([
3434
config.ENV.APP.API_PATH,
3535
ApiPath.POSTS,
3636
PostsApiPath.$ID
37-
);
37+
]);
3838

3939
const commentReactEndpoint = joinPath([
4040
config.ENV.APP.API_PATH,
@@ -49,8 +49,10 @@ const commentIdEndpoint = joinPath([
4949
]);
5050

5151
describe(`${commentApiPath} and ${postApiPath} routes`, () => {
52-
const { app, knex } = buildApp();
53-
const { select, insert } = getCrudHandlers(knex);
52+
const { getApp, getKnex } = buildApp();
53+
const { select, insert } = getCrudHandlers(getKnex);
54+
55+
const app = getApp();
5456

5557
let token;
5658
let userId;
@@ -93,12 +95,12 @@ describe(`${commentApiPath} and ${postApiPath} routes`, () => {
9395
.body({ postId: secondCommentId, isLike: false });
9496
});
9597

96-
describe(`${commentIdEndpoint} (${HttpMethod.GET}) endpoint`, async () => {
97-
const [{ id: firstCommentId }, { id: secondCommentId }] = await select({
98-
table: DatabaseTableName.COMMENTS
99-
});
100-
98+
describe(`${commentIdEndpoint} (${HttpMethod.GET}) endpoint`, () => {
10199
it(`should return ${HttpCode.OK} with likes and dislikes of comment`, async () => {
100+
const [{ id: firstCommentId }, { id: secondCommentId }] = await select({
101+
table: DatabaseTableName.COMMENTS
102+
});
103+
102104
const firstResponse = await app
103105
.inject()
104106
.get(commentIdEndpoint.replace(':id', firstCommentId))
@@ -131,13 +133,13 @@ describe(`${commentApiPath} and ${postApiPath} routes`, () => {
131133
});
132134
});
133135

134-
describe(`${postIdEndpoint} (${HttpMethod.GET}) endpoint`, async () => {
135-
const [
136-
{ id: firstCommentId, firstPostId },
137-
{ id: secondCommentId, secondPostId }
138-
] = await select({ table: DatabaseTableName.COMMENTS });
139-
136+
describe(`${postIdEndpoint} (${HttpMethod.GET}) endpoint`, () => {
140137
it(`should return ${HttpCode.OK} with likes and dislikes of post's comment`, async () => {
138+
const [
139+
{ id: firstCommentId, firstPostId },
140+
{ id: secondCommentId, secondPostId }
141+
] = await select({ table: DatabaseTableName.COMMENTS });
142+
141143
const firstPostResponse = await app
142144
.inject()
143145
.get(postIdEndpoint.replace(':id', firstPostId))

server/tests/api/11-update-profile/user.api.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ const imagesEndpoint = joinPath([
4343
]);
4444

4545
describe(`${userApiPath} routes`, () => {
46-
const { app, knex } = buildApp();
47-
const { insert } = getCrudHandlers(knex);
46+
const { getApp, getKnex } = buildApp();
47+
const { insert } = getCrudHandlers(getKnex);
48+
49+
const app = getApp();
4850

4951
let tokenMainUser;
5052
let tokenMinorUser;
@@ -80,7 +82,7 @@ describe(`${userApiPath} routes`, () => {
8082
it(`should return ${HttpCode.FORBIDDEN} with attempt to update user by not own one`, async () => {
8183
const updatedMainUser = {
8284
...userMain,
83-
[UserPayloadKey.USERNAME]: faker.name.firstName()
85+
[UserPayloadKey.USERNAME]: faker.person.firstName()
8486
};
8587

8688
const updateUserResponse = await app
@@ -127,7 +129,7 @@ describe(`${userApiPath} routes`, () => {
127129
const { id: imageId } = uploadImageResponse.json();
128130
const updatedMainUser = {
129131
...userMain,
130-
[UserPayloadKey.USERNAME]: faker.name.firstName(),
132+
[UserPayloadKey.USERNAME]: faker.person.firstName(),
131133
imageId
132134
};
133135
const response = await app

server/tests/api/12-set-user-status/auth-user.api.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ const authUserEndpoint = joinPath([
3939
]);
4040

4141
describe(`${userApiPath} and ${authApiPath} routes`, () => {
42-
const { app, knex } = buildApp();
43-
const { insert } = getCrudHandlers(knex);
42+
const { getApp, getKnex } = buildApp();
43+
const { insert } = getCrudHandlers(getKnex);
44+
45+
const app = getApp();
4446

4547
let tokenMainUser;
4648
let tokenMinorUser;

server/tests/api/13-reset-set-password/password.api.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { beforeAll, describe, expect, it } from '@jest/globals';
33

44
import { ApiPath } from '#libs/enums/enums.js';
55
import { config } from '#libs/packages/config/config.js';
6+
import { HttpCode } from '#libs/packages/http/http.js';
67
import { joinPath } from '#libs/packages/path/path.js';
78
import { AuthApiPath } from '#packages/auth/auth.js';
8-
import { HttpCode } from '#packages/http/http.js';
99
import { PasswordApiPath } from '#packages/password/password.js';
1010
import {
1111
UserPayloadKey,
@@ -41,8 +41,10 @@ const setPasswordEndpoint = joinPath([
4141
]);
4242

4343
describe(`${passwordEndpoint} routes`, () => {
44-
const { app, knex } = buildApp();
45-
const { insert } = getCrudHandlers(knex);
44+
const { getApp, getKnex } = buildApp();
45+
const { insert } = getCrudHandlers(getKnex);
46+
47+
const app = getApp();
4648

4749
let user;
4850

server/tests/api/2-update-post/post.api.spec.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ const loginEndpoint = joinPath([
3030

3131
const postApiPath = joinPath([config.ENV.APP.API_PATH, ApiPath.POSTS]);
3232

33-
const postIdEndpoint = joinPath(
33+
const postIdEndpoint = joinPath([
3434
config.ENV.APP.API_PATH,
3535
ApiPath.POSTS,
3636
PostsApiPath.$ID
37-
);
37+
]);
3838

3939
describe(`${postApiPath} routes`, () => {
40-
const { app, knex } = buildApp();
41-
const { select, insert } = getCrudHandlers(knex);
40+
const { getApp, getKnex } = buildApp();
41+
const { select, insert } = getCrudHandlers(getKnex);
42+
43+
const app = getApp();
4244

4345
let tokenMainUser;
4446
let tokenMinorUser;
@@ -69,13 +71,13 @@ describe(`${postApiPath} routes`, () => {
6971
tokenMinorUser = loginMinorUserResponse.json().token;
7072
});
7173

72-
describe(`${postIdEndpoint} (${HttpMethod.PUT}) endpoint`, async () => {
73-
const post = await select({
74-
table: DatabaseTableName.POSTS,
75-
limit: KNEX_SELECT_ONE_RECORD
76-
});
77-
74+
describe(`${postIdEndpoint} (${HttpMethod.PUT}) endpoint`, () => {
7875
it(`should return ${HttpCode.FORBIDDEN} with attempt to update post by not own user`, async () => {
76+
const post = await select({
77+
table: DatabaseTableName.POSTS,
78+
limit: KNEX_SELECT_ONE_RECORD
79+
});
80+
7981
const testUpdatedPost = {
8082
...post,
8183
[PostPayloadKey.BODY]: faker.lorem.paragraph()
@@ -101,6 +103,11 @@ describe(`${postApiPath} routes`, () => {
101103
});
102104

103105
it(`should return ${HttpCode.OK} with updated post`, async () => {
106+
const post = await select({
107+
table: DatabaseTableName.POSTS,
108+
limit: KNEX_SELECT_ONE_RECORD
109+
});
110+
104111
const testUpdatedPost = {
105112
...post,
106113
[PostPayloadKey.BODY]: faker.lorem.paragraph()

server/tests/api/3-delete-post/post.api.spec.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,23 @@ const loginEndpoint = joinPath([
2929

3030
const postApiPath = joinPath([config.ENV.APP.API_PATH, ApiPath.POSTS]);
3131

32-
const postIdEndpoint = joinPath(
32+
const postIdEndpoint = joinPath([
3333
config.ENV.APP.API_PATH,
3434
ApiPath.POSTS,
3535
PostsApiPath.$ID
36-
);
36+
]);
3737

38-
const postsEndpoint = joinPath(
38+
const postsEndpoint = joinPath([
3939
config.ENV.APP.API_PATH,
4040
ApiPath.POSTS,
4141
PostsApiPath.ROOT
42-
);
42+
]);
4343

4444
describe(`${postApiPath} routes`, () => {
45-
const { app, knex } = buildApp();
46-
const { select, insert } = getCrudHandlers(knex);
45+
const { getApp, getKnex } = buildApp();
46+
const { select, insert } = getCrudHandlers(getKnex);
47+
48+
const app = getApp();
4749

4850
let tokenMainUser;
4951
let tokenMinorUser;
@@ -74,13 +76,13 @@ describe(`${postApiPath} routes`, () => {
7476
tokenMinorUser = loginMinorUserResponse.json().token;
7577
});
7678

77-
describe(`${postIdEndpoint} (${HttpMethod.DELETE}) endpoint`, async () => {
78-
const postToDelete = await select({
79-
table: DatabaseTableName.COMMENTS,
80-
limit: KNEX_SELECT_ONE_RECORD
81-
});
82-
79+
describe(`${postIdEndpoint} (${HttpMethod.DELETE}) endpoint`, () => {
8380
it(`should return ${HttpCode.FORBIDDEN} with attempt to delete post by not own user`, async () => {
81+
const postToDelete = await select({
82+
table: DatabaseTableName.POSTS,
83+
limit: KNEX_SELECT_ONE_RECORD
84+
});
85+
8486
const deletePostResponse = await app
8587
.inject()
8688
.delete(postIdEndpoint.replace(':id', postToDelete.id))
@@ -100,6 +102,11 @@ describe(`${postApiPath} routes`, () => {
100102
});
101103

102104
it(`should return ${HttpCode.OK} with soft deleted post`, async () => {
105+
const postToDelete = await select({
106+
table: DatabaseTableName.POSTS,
107+
limit: KNEX_SELECT_ONE_RECORD
108+
});
109+
103110
const deletePostResponse = await app
104111
.inject()
105112
.delete(postIdEndpoint.replace(':id', postToDelete.id))
@@ -127,13 +134,13 @@ describe(`${postApiPath} routes`, () => {
127134
});
128135
});
129136

130-
describe(`${postsEndpoint} (${HttpMethod.GET}) endpoint`, async () => {
131-
const postToDelete = await select({
132-
table: DatabaseTableName.COMMENTS,
133-
limit: KNEX_SELECT_ONE_RECORD
134-
});
135-
137+
describe(`${postsEndpoint} (${HttpMethod.GET}) endpoint`, () => {
136138
it(`should return ${HttpCode.OK} with ignoring soft deleted post`, async () => {
139+
const postToDelete = await select({
140+
table: DatabaseTableName.POSTS,
141+
limit: KNEX_SELECT_ONE_RECORD
142+
});
143+
137144
await app
138145
.inject()
139146
.delete(postIdEndpoint.replace(':id', postToDelete.id))

0 commit comments

Comments
 (0)