diff --git a/project/backend/api/tests.py b/project/backend/api/tests.py index 7a76a5ac..0eb91026 100644 --- a/project/backend/api/tests.py +++ b/project/backend/api/tests.py @@ -147,7 +147,7 @@ def setUp(self): user = User.objects.create_user(id=1, email='test@example.com', username='test@example.com', first_name='User', last_name='Test') # basic_user = BasicUser.objects.create(user=user, bio='Hello') - cont = models.Contributor.objects.create(user=user, bio='Hello') + cont = models.Contributor.objects.create(user=user, bio='Hello',id=1) node = models.Node.objects.create(node_title='test', theorem=None, publish_date="2023-01-01", @@ -175,6 +175,7 @@ def test_search(self): self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User') self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test') self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], 'test@example.com') + self.assertEqual(response.json()['nodes'][0]['authors'][0]['id'], 1) self.assertEqual(response.json()['authors'], []) data = {'query': 'test', 'type': 'author'} response = self.client.get(self.search_url, data, format='json') @@ -183,6 +184,7 @@ def test_search(self): self.assertEqual(response.json()['authors'][0]['name'], 'User') self.assertEqual(response.json()['authors'][0]['surname'], 'Test') self.assertEqual(response.json()['authors'][0]['username'], 'test@example.com') + self.assertEqual(response.json()['authors'][0]['id'], 1) data = {'query': 'test', 'type': 'by'} response = self.client.get(self.search_url, data, format='json') self.assertEqual(response.status_code, 200) @@ -192,6 +194,7 @@ def test_search(self): self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User') self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test') self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], 'test@example.com') + self.assertEqual(response.json()['nodes'][0]['authors'][0]['id'], 1) data = {'query': 'test', 'type': 'all'} response = self.client.get(self.search_url, data, format='json') self.assertEqual(response.status_code, 200) @@ -204,6 +207,7 @@ def test_search(self): self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User') self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test') self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], 'test@example.com') + self.assertEqual(response.json()['nodes'][0]['authors'][0]['id'], 1) class ProfileGETAPITestCase(TestCase): @@ -212,8 +216,9 @@ def setUp(self): user = User.objects.create_user(id=1, email='test@example.com', username='test@example.com', first_name='User', last_name='Test') # basic_user = BasicUser.objects.create(user=user, bio='Hello') - cont = Contributor.objects.create(user=user,bio='Hello') + cont = Contributor.objects.create(user=user,bio='Hello',id=1) node = Node.objects.create(node_title='test', + node_id = 55, theorem=None, publish_date="2023-01-01", is_valid=True, @@ -221,8 +226,10 @@ def setUp(self): Q = Question.objects.create( node=node, asker = cont, - question_content = "TEXT", - answerer = cont + question_content = "QUESTION", + answerer = cont, + answer_content = 'ANSWER', + ) node.contributors.add(cont) self.get_profile_url = reverse('get_profile') @@ -239,15 +246,38 @@ def test_get_user_profile(self): self.assertEqual(response.json()['name'],'User') self.assertEqual(response.json()['surname'], 'Test') self.assertEqual(response.json()['bio'], 'Hello') - self.assertEqual(response.json()['nodes'][0]['id'],1) + self.assertEqual(response.json()['nodes'][0]['id'],55) self.assertEqual(response.json()['nodes'][0]['title'], 'test') self.assertEqual(response.json()['nodes'][0]['date'], '2023-01-01') self.assertEqual(response.json()['nodes'][0]['authors'][0]['name'], 'User') self.assertEqual(response.json()['nodes'][0]['authors'][0]['surname'], 'Test') self.assertEqual(response.json()['nodes'][0]['authors'][0]['username'], 'test@example.com') # TODO TESTS FAIL HERE - # self.assertEqual(response.json()['answered_questions'][0],1) - # self.assertEqual(response.json()['asked_questions'][0], 1) + self.assertEqual(response.json()['answered_questions'][0]['question_content'],'QUESTION') + self.assertEqual(response.json()['answered_questions'][0]['answer_content'],'ANSWER') + self.assertEqual(response.json()['answered_questions'][0]['asker_name'], 'User') + self.assertEqual(response.json()['answered_questions'][0]['asker_surname'], 'Test') + self.assertEqual(response.json()['answered_questions'][0]['asker_id'], 1) + self.assertEqual(response.json()['answered_questions'][0]['asker_mail'], 'test@example.com') + self.assertEqual(response.json()['answered_questions'][0]['node_id'], 55) + self.assertEqual(response.json()['answered_questions'][0]['node_title'], 'test') + self.assertEqual(response.json()['answered_questions'][0]['answerer_name'], 'User') + self.assertEqual(response.json()['answered_questions'][0]['answerer_surname'], 'Test') + self.assertEqual(response.json()['answered_questions'][0]['answerer_id'], 1) + self.assertEqual(response.json()['answered_questions'][0]['answerer_mail'], 'test@example.com') + + self.assertEqual(response.json()['asked_questions'][0]['question_content'], 'QUESTION') + self.assertEqual(response.json()['asked_questions'][0]['answer_content'], 'ANSWER') + self.assertEqual(response.json()['asked_questions'][0]['asker_name'], 'User') + self.assertEqual(response.json()['asked_questions'][0]['asker_surname'], 'Test') + self.assertEqual(response.json()['asked_questions'][0]['asker_id'], 1) + self.assertEqual(response.json()['asked_questions'][0]['asker_mail'], 'test@example.com') + self.assertEqual(response.json()['asked_questions'][0]['node_id'], 55) + self.assertEqual(response.json()['asked_questions'][0]['node_title'], 'test') + self.assertEqual(response.json()['asked_questions'][0]['answerer_name'], 'User') + self.assertEqual(response.json()['asked_questions'][0]['answerer_surname'], 'Test') + self.assertEqual(response.json()['asked_questions'][0]['answerer_id'], 1) + self.assertEqual(response.json()['asked_questions'][0]['answerer_mail'], 'test@example.com') class ProofGETAPITestCase(TestCase): def setUp(self): diff --git a/project/backend/api/views.py b/project/backend/api/views.py index 9e8fb3bc..4162f3a4 100644 --- a/project/backend/api/views.py +++ b/project/backend/api/views.py @@ -136,16 +136,16 @@ def search(request): for cont in contributors: user = User.objects.get(username=cont) cont = Contributor.objects.get(user=user) - res_authors.append({'name': User.objects.get(id=cont.user_id).first_name, - 'surname': User.objects.get(id=cont.user_id).last_name, 'username': cont.user.username}) + res_authors.append({'name': user.first_name, + 'surname': user.last_name, 'username': cont.user.username , 'id': cont.id}) node_infos = [] for node_id in nodes: node = Node.objects.get(node_id=node_id) authors = [] for cont in node.contributors.all(): user = User.objects.get(id=cont.user_id) - authors.append({'name': User.objects.get(id=cont.user_id).first_name, - 'surname': User.objects.get(id=cont.user_id).last_name, 'username': user.username}) + authors.append({'name': user.first_name, + 'surname': user.last_name, 'username': user.username, 'id': cont.id}) node_infos.append({'id': node_id, 'title': node.node_title, 'date': node.publish_date, 'authors': authors}) return JsonResponse({'nodes' : node_infos , 'authors' :res_authors },status=200) @@ -168,10 +168,36 @@ def get_profile(request): nodes.append(node.node_id) user_answered_qs = Question.objects.filter(answerer=cont[0].id) for ans in user_answered_qs: - answered_questions.append(ans.id) - user_asked_qs = Question.objects.filter(asker=user.id) + asker_user = User.objects.get(id=ans.asker.user_id) + answerer_user = User.objects.get(id=ans.answerer.user_id) + answered_questions.append({'id':ans.id, 'ask_date':ans.created_at, 'node_id':ans.node.node_id, 'node_title':ans.node.node_title, 'node_date':ans.node.publish_date, + 'asker_id':ans.asker.id,'asker_name':asker_user.first_name,'asker_surname':asker_user.last_name, + 'asker_mail':asker_user.username,'answerer_name':answerer_user.first_name,'answerer_surname':answerer_user.last_name, + 'answerer_mail':answerer_user.username, + 'answerer_id':ans.answerer.id, 'question_content':ans.question_content, + 'answer_content':ans.answer_content,'answer_date':ans.answered_at,'is_answered':1}) + + user_asked_qs = Question.objects.filter(asker=cont[0].id) for q in user_asked_qs: - asked_questions.append(q.id) + asker_user = User.objects.get(id=q.asker.user_id) + if q.answerer == None: + asked_questions.append( + {'id': q.id, 'ask_date': q.created_at, 'node_id': q.node.node_id, 'node_title': q.node.node_title, + 'node_date': q.node.publish_date, + 'asker_id': q.asker.id, 'asker_name': asker_user.first_name, 'asker_surname': asker_user.last_name, + 'asker_mail': asker_user.username, 'question_content': q.question_content, 'is_answered': 0}) + continue + answerer_user = User.objects.get(id=q.answerer.user_id) + asked_questions.append( + {'id': q.id, 'ask_date': q.created_at, 'node_id': q.node.node_id, 'node_title': q.node.node_title, + 'node_date': q.node.publish_date, + 'asker_id': q.asker.id, 'asker_name': asker_user.first_name, 'asker_surname': asker_user.last_name, + 'asker_mail': asker_user.username, 'answerer_name': answerer_user.first_name, + 'answerer_surname': answerer_user.last_name, + 'answerer_mail': answerer_user.username, + 'answerer_id': q.answerer.id, 'question_content': q.question_content, + 'answer_content': q.answer_content, 'answer_date': q.answered_at, 'is_answered':1}) + node_infos = [] for node_id in nodes: node = Node.objects.get(node_id=node_id) @@ -180,7 +206,7 @@ def get_profile(request): user = User.objects.get(id=cont.user_id) authors.append({'name': user.first_name, 'surname': user.last_name, 'username': user.username}) node_infos.append({'id':node_id,'title':node.node_title,'date':node.publish_date,'authors':authors}) - # TODO QUESTION RETURNS SHOULD BE CHANGED IN THE FUTURE. + return JsonResponse({'name':user.first_name, 'surname':user.last_name, 'bio':basic_user.bio,