diff --git a/src/entities/auth/index.ts b/src/entities/auth/index.ts index 527187f9e..5fc9669e5 100644 --- a/src/entities/auth/index.ts +++ b/src/entities/auth/index.ts @@ -31,3 +31,5 @@ export { refreshMiddleware } from './api/refreshMiddleware'; export { RegistrationLabel } from './ui/RegistrationLabel/RegistrationLabel'; export { RegistrationLabelSkeleton } from './ui/RegistrationLabel/RegistrationLabel.skeleton'; export { TelegramWidget } from './ui/TelegramWidget/TelegramWidget'; +export { authMockProfilesByAccessToken } from './api/__mocks__'; +export { getMockAuthProfile } from './api/__mocks__'; diff --git a/src/features/question/deleteQuestion/api/__mocks__/deleteQuestionMock.ts b/src/features/question/deleteQuestion/api/__mocks__/deleteQuestionMock.ts index cd74f6b3e..dba165f77 100644 --- a/src/features/question/deleteQuestion/api/__mocks__/deleteQuestionMock.ts +++ b/src/features/question/deleteQuestion/api/__mocks__/deleteQuestionMock.ts @@ -1,20 +1,91 @@ -import { http } from 'msw'; +import { DefaultBodyType, http, HttpResponse } from 'msw'; +import { getMockAuthProfile } from '@/entities/auth'; +import { collectionsMock } from '@/entities/collection'; import { questionsMock } from '@/entities/question'; import { deleteQuestionApiUrls } from '../../model/constants/deleteQuestionConstants'; +import { DeleteQuestionError } from '../../model/types/deleteQuestionTypes'; -export const deleteQuestionMock = http.delete( - process.env.API_URL + deleteQuestionApiUrls.deleteQuestion, - ({ params }) => { - const questionId = Number(params.questionId); +export const deleteQuestionMock = http.delete< + { questionId: string }, + DefaultBodyType, + ApiErrorData +>(process.env.API_URL + deleteQuestionApiUrls.deleteQuestion, ({ params, request }) => { + const questionId = Number(params.questionId); - const index = questionsMock.data.findIndex((question) => question.id === questionId); + const profileMockResponse = getMockAuthProfile(request); - if (index !== 1) { - questionsMock.data.splice(index, 1); - } + if (!profileMockResponse) { + return HttpResponse.json( + { + message: 'auth.auth.unauthorized', + statusCode: 401, + description: 'Authentication failed', + }, + { status: 401 }, + ); + } - return new Response(); - }, -); + if (!profileMockResponse.isVerified) { + return HttpResponse.json( + { + message: 'auth.user.verified', + statusCode: 403, + description: 'Route is available for verified users!', + }, + { status: 403 }, + ); + } + + const currentQuestion = questionsMock.data.find((question) => question.id === questionId); + + if (!currentQuestion) { + return HttpResponse.json( + { + message: 'question.question.not_found', + statusCode: 404, + description: 'Question not found', + }, + { status: 404 }, + ); + } + + const isAdmin = profileMockResponse.userRoles.some((role) => role.name === 'admin'); + const isAuthor = profileMockResponse.userRoles.some((role) => role.name === 'author'); + const isOwner = currentQuestion.createdBy.id === profileMockResponse.id; + + if (!isAdmin && !(isAuthor && isOwner)) { + return HttpResponse.json( + { + message: 'auth.roles.author_can_change_only_own', + statusCode: 403, + description: 'Author can change only own data', + }, + { status: 403 }, + ); + } + + const hasQuestionInCollection = collectionsMock.data.some((collection) => + collection.questions?.some((question) => question.id === currentQuestion.id), + ); + + if (hasQuestionInCollection) { + return HttpResponse.json( + { + message: 'question.collection.question_in_collection', + statusCode: 409, + description: 'Question is in collection and cannot be deleted', + }, + { status: 409 }, + ); + } + + const questionIndex = questionsMock.data.findIndex((question) => question.id === questionId); + + if (questionIndex !== -1) { + questionsMock.data.splice(questionIndex, 1); + } + + return new HttpResponse(null, { status: 200 }); +});