Fakultas Ilmu Komputer UI

Commit 4d46b325 authored by Yasmin Adelia Puti Chaidir's avatar Yasmin Adelia Puti Chaidir
Browse files

Merge branch 'PBI-4-IUMK' of...

Merge branch 'PBI-4-IUMK' of https://gitlab.cs.ui.ac.id/ppl-fasilkom-ui/2021/DD/justika-pengembangan-platform-untuk-umkm/justika-umkm-legal-backend into PBI-4-IUMK
parents 52932297 e803386b
Pipeline #72631 passed with stages
in 2 minutes and 22 seconds
# Generated by Django 3.1.7 on 2021-04-01 13:09
# Generated by Django 3.2 on 2021-04-25 07:56
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = []
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
......@@ -55,6 +59,13 @@ class Migration(migrations.Migration):
),
("kode_kbli", models.CharField(max_length=5)),
("catatan", models.TextField()),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
from django.db import models
from account.models import Account
class Iumk(models.Model):
......@@ -26,5 +27,6 @@ class Iumk(models.Model):
jumlah_karyawan = models.CharField(max_length=20, choices=JUMLAH_KARYAWAN_CHOICES)
kode_kbli = models.CharField(max_length=5)
catatan = models.TextField()
user = models.ForeignKey(Account, on_delete=models.CASCADE)
REQUIRED_FIELD = ["nama_usaha", "status_entitas", "alamat", "barang_jasa"]
REQUIRED_FIELD = ["nama_usaha", "status_entitas", "alamat", "barang_jasa", "user"]
......@@ -17,6 +17,7 @@ class IumkSerializer(serializers.ModelSerializer):
"jumlah_karyawan",
"kode_kbli",
"catatan",
"user",
)
......@@ -43,6 +44,7 @@ class CreateIumkSerializer(IumkSerializer):
"jumlah_karyawan",
"kode_kbli",
"catatan",
"user",
)
extra_kwargs = {
"jumlah_karyawan": {"required": False},
......
......@@ -2,6 +2,8 @@ from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIClient
from iumk.models import Iumk
from account.models import Account
from rest_framework.authtoken.models import Token
class TestIumk(TestCase):
......@@ -10,33 +12,29 @@ class TestIumk(TestCase):
self.nama_usaha = "justika"
self.reverse_path = "iumk:create_iumk"
self.account = Account.objects.create(
email="sigendud@gmail.com",
password="sebuahPassword22",
phone_number="8123456789",
is_staff=False,
)
Iumk.objects.create(
nama_usaha="justika",
status_entitas="PT",
alamat="dimana hayo",
barang_jasa="konsultasi",
user=self.account,
)
self.access_token, created = Token.objects.get_or_create(user=self.account)
def test_iumk(self):
justika = Iumk.objects.get(nama_usaha="justika")
self.assertTrue(isinstance(justika, Iumk))
def test_create_iumk_success(self):
response = self.url.post(
reverse(self.reverse_path),
{
"nama_usaha": self.nama_usaha,
"status_entitas": "PT",
"alamat": "Jl. dimana hayo",
"barang_jasa": "konsultasi",
"jumlah_karyawan": "1-10",
"kode_kbli": "01111",
"catatan": "sebuah catatan",
},
)
self.assertEqual(response.status_code, 201)
def test_kode_kbli_must_be_numeric(self):
self.url.credentials(HTTP_AUTHORIZATION="Token " + self.access_token.key)
response = self.url.post(
reverse(self.reverse_path),
{
......@@ -56,6 +54,7 @@ class TestIumk(TestCase):
)
def test_kode_kbli_must_five_digit_long(self):
self.url.credentials(HTTP_AUTHORIZATION="Token " + self.access_token.key)
response = self.url.post(
reverse(self.reverse_path),
{
......@@ -73,3 +72,51 @@ class TestIumk(TestCase):
{"non_field_errors": ["Kode KBLI harus terdiri dari 5 angka!"]},
response.json(),
)
class TestIumkView(TestCase):
def setUp(self):
self.url = APIClient()
self.reverse_path = "iumk:create_iumk"
self.account = Account.objects.create(
email="sigendud@gmail.com",
password="sebuahPassword22",
phone_number="8123456789",
is_staff=False,
)
self.access_token, created = Token.objects.get_or_create(user=self.account)
def test_create_iumk_fail_without_token(self):
response = self.url.post(
reverse(self.reverse_path),
{
"nama_usaha": "Iumk Sigendud",
"status_entitas": "PT",
"alamat": "Jalan Terbang Layang",
"barang_jasa": "Perangkat Lunak",
"jumlah_karyawan": "11-50",
"kode_kbli": "12121",
"catatan": "Ini contoh catatan",
},
)
self.assertEqual(response.status_code, 401)
def test_create_iumk_with_token(self):
self.url.credentials(HTTP_AUTHORIZATION="Token " + self.access_token.key)
response = self.url.post(
reverse(self.reverse_path),
{
"nama_usaha": "Iumk Sigendud",
"status_entitas": "PT",
"alamat": "Jalan Terbang Layang",
"barang_jasa": "Perangkat Lunak",
"jumlah_karyawan": "11-50",
"kode_kbli": "12121",
"catatan": "Ini contoh catatan",
},
)
self.assertEqual(response.status_code, 201)
from rest_framework import generics
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework import status
from rest_framework.response import Response
from iumk.serializers import CreateIumkSerializer
class IumkCreateView(generics.CreateAPIView):
serializer_class = CreateIumkSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
def get_serializer(self, *args, **kwargs):
serializer_class = self.get_serializer_class()
kwargs["context"] = self.get_serializer_context()
draft_request_data = self.request.data.copy()
draft_request_data["user"] = self.request.user.pk
kwargs["data"] = draft_request_data
return serializer_class(*args, **kwargs)
def perform_create(self, serializer):
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
obj = serializer.save()
obj.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
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