Fakultas Ilmu Komputer UI

Commit 1af68df1 authored by PRIMO GIANCARLO UNEPUTTY's avatar PRIMO GIANCARLO UNEPUTTY
Browse files

Merge branch 'PBI-20-Jual_Beli_Investasi' into 'staging'

Pbi 20 jual beli investasi - fixing bugs & adding tests

See merge request ppl-fasilkom-ui/ppl-reguler-ganjil-2021-2021/walkiddie-toys/walkiddie-toys-backend!37
parents 02ff9851 7cdfd70f
......@@ -4,7 +4,6 @@ from toko.serializers import PengadaanSerializer
from .models import Investasi
class InvestasiSerializer(serializers.ModelSerializer):
investor = serializers.ReadOnlyField(source='investor.email')
uang_investasi = serializers.SerializerMethodField('get_investasi_dalam_bentuk_uang')
@classmethod
......@@ -28,3 +27,5 @@ class InvestasiSerializer(serializers.ModelSerializer):
class InvestasiReadSerializer(InvestasiSerializer):
pengadaan = PengadaanSerializer(read_only=True)
investor = serializers.ReadOnlyField(source='investor.email', allow_null=True)
pembeli = serializers.ReadOnlyField(source='pembeli.email', allow_null=True)
......@@ -101,7 +101,25 @@ class InvestasiTest(TestCase):
self.assertNotEqual(response.status_code, 200)
def test_update_investasi(self):
def test_update_investasi_of_different_user(self):
investasi = Investasi(investor=self.user, pengadaan=self.pengadaan, nominal=1)
investasi.save()
user = UserAccount(email='a@a.id', first_name='a', last_name='a', role='investor')
user.set_password('a')
user.save()
token = self._get_token(user.email, 'a')
response = self.client.put(
'/api/investasi/{}'.format(investasi.pk),
{**self.investasi_data, "nominal": 13000},
content_type="application/json",
HTTP_AUTHORIZATION="JWT {}".format(token)
)
self.assertNotEqual(response.status_code, 200)
def test_update_investasi_success(self):
investasi = Investasi(investor=self.user, pengadaan=self.pengadaan, nominal=1)
investasi.save()
......@@ -116,6 +134,34 @@ class InvestasiTest(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['nominal'], 13000)
def test_delete_investasi_sedang_dijual(self):
investasi = Investasi(
investor=self.user, pengadaan=self.pengadaan, nominal=1, status_investasi='DJL'
)
investasi.save()
token = self._get_token(self.user_email, self.user_pass)
response = self.client.delete(
'/api/investasi/{}'.format(investasi.pk),
content_type="application/json",
HTTP_AUTHORIZATION="JWT {}".format(token)
)
self.assertNotEqual(response.status_code, 204)
def test_delete_investasi_success(self):
investasi = Investasi(investor=self.user, pengadaan=self.pengadaan, nominal=1)
investasi.save()
token = self._get_token(self.user_email, self.user_pass)
response = self.client.delete(
'/api/investasi/{}'.format(investasi.pk),
content_type="application/json",
HTTP_AUTHORIZATION="JWT {}".format(token)
)
self.assertEqual(response.status_code, 204)
def test_jual_investasi_of_different_user(self):
investasi = Investasi(
investor=self.user, pengadaan=self.pengadaan, nominal=1, status_investasi='DMK'
......@@ -158,6 +204,9 @@ class InvestasiTest(TestCase):
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['pembeli'], None)
self.assertEqual(response.json()['statusPembelian'], 'BDB')
self.assertEqual(response.json()['statusInvestasi'], 'DJL')
response = self.client.get(
'/api/investasi/penjualan/',
......@@ -202,6 +251,9 @@ class InvestasiTest(TestCase):
HTTP_AUTHORIZATION="JWT {}".format(token)
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['pembeli'], None)
self.assertEqual(response.json()['statusPembelian'], 'BDB')
self.assertEqual(response.json()['statusInvestasi'], 'DMK')
response = self.client.get(
'/api/investasi/penjualan/',
......@@ -355,8 +407,9 @@ class InvestasiTest(TestCase):
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['pembeli'], user.pk)
self.assertEqual(response.json()['pembeli'], user.email)
self.assertEqual(response.json()['statusPembelian'], 'MPA')
self.assertEqual(response.json()['statusInvestasi'], 'SDB')
def test_cancel_beli_investasi_fail(self):
investasi = Investasi(investor=self.user, pengadaan=self.pengadaan, nominal=1)
......@@ -414,6 +467,7 @@ class InvestasiTest(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['pembeli'], None)
self.assertEqual(response.json()['statusPembelian'], 'BDB')
self.assertEqual(response.json()['statusInvestasi'], 'DJL')
def test_get_investasi_has_field_uang_investasi(self):
investasi = Investasi(investor=self.user, pengadaan=self.pengadaan, nominal=1)
......
#pylint: disable=too-many-ancestors
from rest_framework.exceptions import ValidationError
from rest_framework import generics
from rest_framework import permissions
from djangorestframework_camel_case.render import (CamelCaseJSONRenderer,
CamelCaseBrowsableAPIRenderer)
from djangorestframework_camel_case.parser import (CamelCaseJSONParser,
......@@ -17,6 +17,10 @@ class IsOwner(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
return obj.investor == request.user
class IsPembeli(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
return obj.pembeli == request.user
class InvestasiList(generics.ListCreateAPIView):
def get_queryset(self):
return Investasi.objects.filter(investor=self.request.user)
......@@ -46,11 +50,17 @@ class InvestasiDetail(generics.RetrieveUpdateDestroyAPIView):
return Investasi.objects.filter(investor=self.request.user)
def get_serializer_class(self):
if self.request.method == "POST" or self.request.method == "PUT":
if self.request.method in ["PATCH", "PUT", "DELETE"]:
return InvestasiSerializer
return InvestasiReadSerializer
serializer_class = InvestasiReadSerializer
def perform_destroy(self, instance):
if instance.status_investasi == DIMILIKI:
instance.delete()
else:
error_msg = {'error': "Tidak dapat menghapus investasi yang sedang dijual"}
raise ValidationError(error_msg)
permission_classes = [permissions.IsAuthenticated, IsOwner]
parser_classes = (CamelCaseJSONParser, CamelCaseFormParser, CamelCaseMultiPartParser, )
renderer_classes = (CamelCaseJSONRenderer, CamelCaseBrowsableAPIRenderer, )
......@@ -75,10 +85,14 @@ class InvestasiDijualDetail(generics.RetrieveAPIView):
class JualInvestasi(generics.UpdateAPIView):
def get_queryset(self):
return Investasi.objects.filter(investor=self.request.user)
return Investasi.objects.filter(investor=self.request.user, status_investasi=DIMILIKI)
def perform_update(self, serializer):
serializer.save(pembeli=None, status_investasi=DIJUAL)
serializer.save(
pembeli=None,
status_pembelian=BELUM_DIBELI,
status_investasi=DIJUAL
)
serializer_class = InvestasiReadSerializer
permission_classes = [permissions.IsAuthenticated, IsOwner]
......@@ -103,7 +117,7 @@ class CancelJualInvestasi(generics.UpdateAPIView):
class BeliInvestasi(generics.UpdateAPIView):
def get_queryset(self):
return Investasi.objects.filter(status_investasi=DIJUAL)
return Investasi.objects.filter(pembeli=None, status_investasi=DIJUAL)
def perform_update(self, serializer):
serializer.save(
......@@ -119,7 +133,10 @@ class BeliInvestasi(generics.UpdateAPIView):
class CancelBeliInvestasi(generics.UpdateAPIView):
def get_queryset(self):
return Investasi.objects.filter(status_investasi=SEDANG_DIBELI)
return Investasi.objects.filter(
pembeli=self.request.user,
status_investasi=SEDANG_DIBELI
)
def perform_update(self, serializer):
serializer.save(
......@@ -129,6 +146,6 @@ class CancelBeliInvestasi(generics.UpdateAPIView):
)
serializer_class = InvestasiReadSerializer
permission_classes = [permissions.IsAuthenticated]
permission_classes = [permissions.IsAuthenticated, IsPembeli]
parser_classes = (CamelCaseJSONParser, CamelCaseFormParser, CamelCaseMultiPartParser, )
renderer_classes = (CamelCaseJSONRenderer, CamelCaseBrowsableAPIRenderer, )
Supports Markdown
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