Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ppl-fasilkom-ui
2021
Kelas D
PT Gizi Sehat - Dietela
Dietela Backend
Commits
75847ac8
Commit
75847ac8
authored
May 01, 2021
by
Glenda Emanuella Sutanto
Browse files
Refactor Sprint 2 Glenda
parent
00220d46
Changes
3
Hide whitespace changes
Inline
Side-by-side
authentication/tests.py
View file @
75847ac8
...
...
@@ -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
)
...
...
payment/tests.py
View file @
75847ac8
...
...
@@ -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
)
payment/views.py
View file @
75847ac8
...
...
@@ -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
()
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment