From d027e5fd4ae04b07d0b15791516d050db01bbd4d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9EBAS8243=E2=80=9C?= <gerd.embruch@uni-hamburg.de>
Date: Thu, 1 Aug 2024 12:41:33 +0200
Subject: [PATCH] fixed tests by adding findRecordByID

---
 __tests__/ai/__snapshots__/chat.test.js.snap | 12 ++++++------
 __tests__/ai/chat.test.js                    |  7 ++++---
 controllers/AI.js                            |  1 -
 controllers/User.js                          |  2 +-
 4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/__tests__/ai/__snapshots__/chat.test.js.snap b/__tests__/ai/__snapshots__/chat.test.js.snap
index 966387e..653f19a 100644
--- a/__tests__/ai/__snapshots__/chat.test.js.snap
+++ b/__tests__/ai/__snapshots__/chat.test.js.snap
@@ -35,6 +35,12 @@ exports[`ai chat with model > given a chatId was sended > should respond with a
 }
 `;
 
+exports[`ai chat with model > given invalid chatId sended > should respond with a proper body 1`] = `
+{
+  "message": "No chat history with ID 66a2af22df510b35b401f5ba found.",
+}
+`;
+
 exports[`ai chat with model > given no chatId sended > should respond with a proper body 1`] = `
 {
   "answer": "It seems you're asking when the pain of mocking (in the context of software development) subsides. Well, with practice and experience, you'll find that your code becomes more robust, and the process of writing unit tests using mocking libraries becomes more natural. The initial discomfort will give way to a sense of confidence and efficiency as you master the art of mocking.",
@@ -96,9 +102,3 @@ exports[`ai chat with model > given required fields are missing > should respond
   },
 }
 `;
-
-exports[`ai chat with model > given valid chatId sended > should respond with a proper body 1`] = `
-{
-  "message": "No chat history with ID 66a2af22df510b35b401f5ba found.",
-}
-`;
diff --git a/__tests__/ai/chat.test.js b/__tests__/ai/chat.test.js
index e45d270..985c791 100644
--- a/__tests__/ai/chat.test.js
+++ b/__tests__/ai/chat.test.js
@@ -123,6 +123,7 @@ vi.mock('../../utils/handleDB.js', async (importOriginal) => {
     ...await importOriginal(),
     dbConnection: vi.fn(() => 'mocked'),
     findOneRecord: vi.fn(() => mockedVals.foundUser),
+    findRecordByID: vi.fn(() => mockedVals.filledChatRecord),
     createRecord: vi.fn(() => mockedVals.emptyChatRecord),
   };
 });
@@ -152,7 +153,7 @@ describe('ai chat with model', () => {
   describe('given a chatId was sended', () => {
     beforeAll(async () => {
 
-      dbService.findOneRecord.mockImplementationOnce(() => mockedVals.filledChatRecord);
+      // dbService.findOneRecord.mockImplementationOnce(() => mockedVals.filledChatRecord);
 
       response = await supertest(app)
         .post(ROUTE)
@@ -170,10 +171,10 @@ describe('ai chat with model', () => {
 
   // ############################
 
-  describe('given valid chatId sended', () => {
+  describe('given invalid chatId sended', () => {
     beforeAll(async () => {
 
-      dbService.findOneRecord.mockImplementationOnce(() => null);
+      dbService.findRecordByID.mockImplementationOnce(() => null);
 
       response = await supertest(app)
         .post(ROUTE)
diff --git a/controllers/AI.js b/controllers/AI.js
index a683661..0fd16bb 100644
--- a/controllers/AI.js
+++ b/controllers/AI.js
@@ -99,7 +99,6 @@ export const getChat = async (req, res, next) => {
   try {
     // fetch chat record
     const record = await findRecordByID(Chat, req.body.chatId);
-
     if (!record) {
       return res.status(404).json({ message: `No chat history with ID ${req.body.chatId} found.` });
     }
diff --git a/controllers/User.js b/controllers/User.js
index 96c8bf2..ac1df7c 100644
--- a/controllers/User.js
+++ b/controllers/User.js
@@ -8,7 +8,7 @@ import { prefillDocumentObject, hideConfidentialFields } from '../utils/handleSc
 export const createUser = async (req, res, next) => {
   try {
     // autoverify if user-agent is Artillery
-    const isArtilleryAgent = req.get('user-agent').includes('Artillery');
+    const isArtilleryAgent = (req.get('user-agent') && req.get('user-agent').includes('Artillery') ? true : false);
     if (isArtilleryAgent) {
       req.body.verified = true;
     }
-- 
GitLab