From 685605a5bcc151f97a7e32399cc86e1a3fd83d81 Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Sat, 11 Nov 2023 11:21:37 +0900 Subject: [PATCH 1/3] :white_check_mark: Implement settings related API tests --- backend/setup/tests.py | 65 +++++++++++++++++++++++++++++++++++++- backend/user/test_views.py | 1 - 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/backend/setup/tests.py b/backend/setup/tests.py index 7ce503c2..6f528b5a 100644 --- a/backend/setup/tests.py +++ b/backend/setup/tests.py @@ -1,3 +1,66 @@ from django.test import TestCase +from rest_framework import status + +from user.models import User + + +class SettingsTest(TestCase): + + def setUp(self): + self.user = User.objects.create_user( + email='test_email@gmail.com', + password='test_password', + nickname='test_nickname' + ) + self.tokens = self.login_and_get_tokens() + self.access_token = self.tokens.get('access') + self.refresh_token = self.tokens.get('refresh') + + def login_and_get_tokens(self): + data = { + 'email': 'test_email@gmail.com', + 'password': 'test_password', + } + response = self.client.post('/user/login/', data) + if response.status_code == status.HTTP_200_OK: + tokens = response.json() + return tokens + return None + + def test_settings_backup_success(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + data = { + 'display_mode': 0, + 'default_menu': 1 + } + # when attempting backup for the first time + response = self.client.post('/setting/backup/', data, **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + # when attempting backup repetitively + data = { + 'display_mode': 1, + 'default_menu': 0 + } + response = self.client.post('/setting/backup/', data, **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_get_settings_success(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + response = self.client.get('/setting/backup/', **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.json() + self.assertEqual(data['display_mode'], 0) # 0 is the default value + self.assertEqual(data['default_menu'], 0) # 0 is the default value + + + + + + + -# Create your tests here. diff --git a/backend/user/test_views.py b/backend/user/test_views.py index 115fcef5..fe46003b 100644 --- a/backend/user/test_views.py +++ b/backend/user/test_views.py @@ -1,6 +1,5 @@ from django.test import TestCase from rest_framework import status -from rest_framework.utils import json from user.models import User, EmailVerification From 36270587ea065a2973a5c596ba7725fd8fb66077 Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Sat, 11 Nov 2023 12:00:09 +0900 Subject: [PATCH 2/3] :white_check_mark: Implement favorite symbol backup API test --- backend/entry/tests.py | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/backend/entry/tests.py b/backend/entry/tests.py index 7ce503c2..3e781165 100644 --- a/backend/entry/tests.py +++ b/backend/entry/tests.py @@ -1,3 +1,56 @@ from django.test import TestCase +from rest_framework import status -# Create your tests here. +from entry.models import Symbol +from user.models import User + + +class SymbolTest(TestCase): + + def setUp(self): + self.user = User.objects.create_user( + email='test_email@gmail.com', + password='test_password', + nickname='test_nickname' + ) + self.tokens = self.login_and_get_tokens() + self.access_token = self.tokens.get('access') + self.refresh_token = self.tokens.get('refresh') + + Symbol.objects.create(id=501, text="test1", category=1, created_by=self.user) + Symbol.objects.create(id=502, text="test2", category=2, created_by=self.user) + Symbol.objects.create(id=503, text="test3", category=3, created_by=self.user) + + def login_and_get_tokens(self): + data = { + 'email': 'test_email@gmail.com', + 'password': 'test_password', + } + response = self.client.post('/user/login/', data) + if response.status_code == status.HTTP_200_OK: + tokens = response.json() + return tokens + return None + + def test_favorite_backup_success(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + # Add some favorites to the user's list + response = self.client.post('/symbol/favorite/backup/?id=501,502,503', **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_get_favorite_symbols_success(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + expected_response = { + 'id': 501 + } + # Add favorite symbol to check + response = self.client.post('/symbol/favorite/backup/?id=501', **headers) + + response = self.client.get('/symbol/favorite/backup/', **headers) + data = response.json() + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(data.get('results')[0], expected_response) \ No newline at end of file From c5aa88993df9b9c2860559ddb1c17bc829d902d4 Mon Sep 17 00:00:00 2001 From: YeonJeong Kim Date: Sat, 11 Nov 2023 12:31:30 +0900 Subject: [PATCH 3/3] :white_check_mark: Implement user-created-symbol related APIs test --- backend/entry/tests.py | 50 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/backend/entry/tests.py b/backend/entry/tests.py index 3e781165..9745c43b 100644 --- a/backend/entry/tests.py +++ b/backend/entry/tests.py @@ -1,3 +1,4 @@ +from django.core.files.uploadedfile import SimpleUploadedFile from django.test import TestCase from rest_framework import status @@ -51,6 +52,53 @@ def test_get_favorite_symbols_success(self): response = self.client.post('/symbol/favorite/backup/?id=501', **headers) response = self.client.get('/symbol/favorite/backup/', **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.json() + self.assertEqual(data.get('results')[0], expected_response) + + def test_enable_my_symbols_success(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + # Enable user-created symbols + response = self.client.post('/symbol/enable/?id=501', **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(data.get('results')[0], expected_response) \ No newline at end of file + + def test_enable_my_symbols_fail_no_such_symbol(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + # Enable user-created symbols + response = self.client.post('/symbol/enable/?id=505', **headers) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + def test_get_my_symbols_success(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + # Get all of the user-created symbols + self.client.post('/symbol/enable/?id=501', **headers) # since ONLY enabled symbols can be retrieved + response = self.client.get('/symbol/', **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.json() + self.assertEqual(len(data.get('my_symbols')), 1) # since only one symbol is enabled + + def test_get_my_specific_symbol_success(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + # Get all of the user-created symbols + self.client.post('/symbol/enable/?id=501', **headers) # since ONLY enabled symbols can be retrieved + response = self.client.get('/symbol/501/', **headers) + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.json() + self.assertIsNotNone(data.get('my_symbol')) + + def test_get_my_specific_symbol_fail_invalid_symbol(self): + headers = { + 'HTTP_AUTHORIZATION': f'Bearer {self.access_token}' + } + # Get all of the user-created symbols + response = self.client.get('/symbol/501/', **headers) + # Since symbol 501 is not valid yet + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)