Fakultas Ilmu Komputer UI

Commit 0ec47cc4 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

Merge branch 'refactor-sprint-2-glenda' into 'staging'

Refactor Sprint 2 Glenda

See merge request !53
parents 00220d46 75847ac8
Pipeline #74127 passed with stages
in 8 minutes and 51 seconds
......@@ -18,6 +18,9 @@ class UserModelTests(APITestCase):
@classmethod
def setUpTestData(cls):
cls.REGISTRATION_URL = "/auth/registration/"
cls.LINK_DATA_URL = "/auth/link-data/"
cls.diet_profile_1 = DietProfile.objects.create(
name="test",
email="test@test.com",
......@@ -153,6 +156,7 @@ class UserModelTests(APITestCase):
self.assertEqual('admin', admin_user_data.get('role'))
def test_post_registration_user_succeed(self):
url = self.REGISTRATION_URL
data = {
'name': 'tes',
'email': 'abc123@gmail.com',
......@@ -160,7 +164,7 @@ class UserModelTests(APITestCase):
'password2': '2828abab',
}
response = self.client.post('/auth/registration/', data, format='json')
response = self.client.post(url, data, format='json')
json_response = json.loads(response.content)
self.assertEqual(json_response.get('user').get('email'), 'abc123@gmail.com')
self.assertEqual(json_response.get('user').get('role'), 'client')
......@@ -169,17 +173,19 @@ class UserModelTests(APITestCase):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_post_registration_user_failed_because_invalid_email(self):
url = self.REGISTRATION_URL
data = {
'email': 'abc123gmail.com',
'password1': '2828abab',
'password2': '2828abab',
}
response = self.client.post('/auth/registration/', data, format='json')
response = self.client.post(url, data, format='json')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_registration_user_failed_because_different_password(self):
url = self.REGISTRATION_URL
data = {
'name': 'tes',
'email': 'abc123@gmail.com',
......@@ -187,11 +193,12 @@ class UserModelTests(APITestCase):
'password2': '2828abab',
}
response = self.client.post('/auth/registration/', data, format='json')
response = self.client.post(url, data, format='json')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_registration_user_failed_because_email_already_registered(self):
url = self.REGISTRATION_URL
data = {
'name': 'tes',
'email': self.custom_user_1.email,
......@@ -199,7 +206,7 @@ class UserModelTests(APITestCase):
'password2': '2828abaab',
}
response = self.client.post('/auth/registration/', data, format='json')
response = self.client.post(url, data, format='json')
self.assertEqual(CustomUser.objects.count(), self.num_of_custom_user)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......@@ -212,13 +219,14 @@ class UserModelTests(APITestCase):
self.assertEqual(serializer.update(None, {}), None)
def test_link_user_and_diet_profile_and_cart_succeed(self):
url = self.LINK_DATA_URL
data = {
'email': self.custom_user_2.email,
'diet_profile_id': self.diet_profile_2.id,
'cart_id': self.cart.id,
}
response = self.client.post('/auth/link-data/', data, format='json')
response = self.client.post(url, data, format='json')
self.assertEqual(json.loads(response.content).get('user').get('email'), \
self.custom_user_2.email)
self.assertEqual(json.loads(response.content).get('diet_profile').get('id'), \
......@@ -227,35 +235,38 @@ class UserModelTests(APITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_link_user_and_diet_profile_failed_because_email_doesnt_exist(self):
url = self.LINK_DATA_URL
data = {
'email': 'emaill@gmail.com',
'diet_profile_id': self.diet_profile_2.id,
'cart_id': self.cart.id,
}
response = self.client.post('/auth/link-data/', data, format='json')
response = self.client.post(url, data, format='json')
self.assertEqual(json.loads(response.content).get('message'), 'User is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_link_user_and_diet_profile_failed_because_diet_profile_doesnt_exist(self):
url = self.LINK_DATA_URL
data = {
'email': self.custom_user_2.email,
'diet_profile_id': -1,
'cart_id': self.cart.id,
}
response = self.client.post('/auth/link-data/', data, format='json')
response = self.client.post(url, data, format='json')
self.assertEqual(json.loads(response.content).get('message'), 'Diet profile is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_link_user_and_diet_profile_failed_because_cart_doesnt_exist(self):
url = self.LINK_DATA_URL
data = {
'email': self.custom_user_2.email,
'diet_profile_id': self.diet_profile_2.id,
'cart_id': -1,
}
response = self.client.post('/auth/link-data/', data, format='json')
response = self.client.post(url, data, format='json')
self.assertEqual(json.loads(response.content).get('message'), 'Cart is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......
......@@ -107,7 +107,7 @@ class CartTests(APITestCase):
self.assertEqual(Cart.objects.count(), 4)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_set_transaction_status_with_status_code_200(self):
def test_set_transaction_status_succeed_with_status_code_200(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
'cart_id': self.cart_3.id,
......@@ -117,7 +117,7 @@ class CartTests(APITestCase):
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_set_transaction_status_with_status_code_201(self):
def test_set_transaction_status_succeed_with_status_code_201(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
'cart_id': self.cart_3.id,
......@@ -127,7 +127,7 @@ class CartTests(APITestCase):
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_set_transaction_status_with_other_status_code(self):
def test_set_transaction_status_succeed_with_other_status_code(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
'cart_id': self.cart_3.id,
......@@ -137,7 +137,18 @@ class CartTests(APITestCase):
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_midtrans_redirection(self):
def test_set_transaction_status_failed_because_cart_does_not_exist(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
'cart_id': -1,
'status_code': 200
}
response = self.client.post(url, data, format='json')
self.assertEqual(json.loads(response.content).get('message'), 'Cart is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_midtrans_redirection_succeed(self):
url = self.MIDTRANS_REDIRECTION_URL
request_params = {
'order_id': self.cart_3.id,
......@@ -148,6 +159,18 @@ class CartTests(APITestCase):
response = self.client.get(url, request_params)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_midtrans_redirection_failed_because_cart_does_not_exist(self):
url = self.MIDTRANS_REDIRECTION_URL
request_params = {
'order_id': -1,
'status_code': 201,
'transaction_status': 'pending'
}
response = self.client.get(url, request_params)
self.assertEqual(json.loads(response.content).get('message'), 'Cart is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
@patch('payment.views.json.loads')
class TestPaymentIntegration(TestCase):
......@@ -198,3 +221,13 @@ class TestPaymentIntegration(TestCase):
self.assertNotIn('token', json_response)
self.assertNotIn('redirect_url', json_response)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_post_payment_failed_because_cart_does_not_exist(self, mock_json_loads):
mock_json_return_value = {
'message': 'Cart is not found.'
}
mock_json_loads.return_value = mock_json_return_value
response = self.client.post(self.PAYMENT_URL, {'cart_id': -1}, format='json')
json_response = json.loads(response.content)
self.assertEqual(json.loads(response.content).get('message'), 'Cart is not found.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......@@ -64,7 +64,10 @@ class PaymentViewSet(viewsets.ViewSet):
def create(self, request):
cart_id = request.data['cart_id']
cart = Cart.objects.get(pk=cart_id)
try:
cart = Cart.objects.get(pk=cart_id)
except Cart.DoesNotExist:
return Response({'message': 'Cart is not found.'}, status=status.HTTP_400_BAD_REQUEST)
payload = self.create_payload(cart)
response = requests.post(self.MIDTRANS_URL, payload, headers=self.POST_PAYMENT_HEADERS)
json_response = json.loads(response.content)
......@@ -82,7 +85,11 @@ class TransactionStatusViewSet(viewsets.ViewSet):
cart_id = request.data['cart_id']
transaction_status_code = request.data['status_code']
cart = Cart.objects.get(pk=cart_id)
try:
cart = Cart.objects.get(pk=cart_id)
except Cart.DoesNotExist:
return Response({'message': 'Cart is not found.'}, status=status.HTTP_400_BAD_REQUEST)
if transaction_status_code == 200:
cart.transaction_status = TRANSACTION_STATUS.SUCCESS
elif transaction_status_code == 201:
......@@ -101,7 +108,11 @@ class MidtransRedirectionViewSet(viewsets.ViewSet):
transaction_status = request.query_params['transaction_status']
cart_id = request.query_params['order_id']
cart = Cart.objects.get(pk=cart_id)
try:
cart = Cart.objects.get(pk=cart_id)
except Cart.DoesNotExist:
return Response({'message': 'Cart is not found.'}, status=status.HTTP_400_BAD_REQUEST)
cart.transaction_status = transaction_status
cart.save()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment