Fakultas Ilmu Komputer UI

Commit 2b4fed41 authored by BIMO IMAN SMARTADI's avatar BIMO IMAN SMARTADI
Browse files

Merge branch 'modify_api_endpoint' into 'PBI-4-menambahkan_informasi_fasilitas_publik'

Modify api endpoint

See merge request !33
parents 492a5334 b022cc6e
Pipeline #42272 passed with stages
in 12 minutes and 23 seconds
......@@ -109,9 +109,13 @@ Make `POST` request to API endpoint `/informasi-lokasi/lokasi/add-fasilitas/*nam
* TEMPAT_PARKIR_DISABILITAS = 'PK'
* RUNNING_TEXT = 'RT'
* TEMPAT_PARKIR_BIASA = 'TB'
* Put as a String separated by space
* example: "KR LF TD"
<br>
* `deskripsi` : Facility description.
* `rating` : rating for the Facility, an integer 1-5.
<br>
<b>Make sure you have token it your request header.</b>
<b>Add this to your request header. `Authorization` with value `token *your_token_from_request*`</b>
......
from django.test import TestCase
from django.test import TestCase, Client
from django.db.utils import IntegrityError
from .models import Lokasi, Fasilitas, Komentar, KURSI_RODA, RUNNING_TEXT
from .serializers import LokasiSerializer
from registrasi.models import BisaGoUser
from django.contrib.auth.models import User
from django.urls import reverse, path, include
import json
import tempfile
# Create your tests here.
class InformasiFasilitasTest(TestCase):
class InformasiFasilitasModelTest(TestCase):
not_null_constraint_failed_message = 'NOT NULL constraint failed'
def test_models_lokasi_not_created(self):
with self.assertRaises(IntegrityError) as cm:
obj = Lokasi(name=None)
obj.save()
self.assertTrue(str(cm.exception).startswith(self.not_null_constraint_failed_message))
self.assertTrue(str(cm.exception).startswith(
self.not_null_constraint_failed_message))
def test_models_create_new_lokasi(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
......@@ -32,7 +36,8 @@ class InformasiFasilitasTest(TestCase):
with self.assertRaises(IntegrityError) as cm:
obj = Fasilitas(lokasi=None)
obj.save()
self.assertTrue(str(cm.exception).startswith(self.not_null_constraint_failed_message))
self.assertTrue(str(cm.exception).startswith(
self.not_null_constraint_failed_message))
def test_models_create_new_fasilitas(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
......@@ -69,7 +74,8 @@ class InformasiFasilitasTest(TestCase):
with self.assertRaises(IntegrityError) as cm:
obj = Komentar(fasilitas=None)
obj.save();
self.assertTrue(str(cm.exception).startswith(self.not_null_constraint_failed_message))
self.assertTrue(str(cm.exception).startswith(
self.not_null_constraint_failed_message))
def test_models_create_new_komentar(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
......@@ -118,4 +124,296 @@ def test_view_lokasi(self):
no_telp='081212123131',
image=image
)
lokasi.save()
\ No newline at end of file
lokasi.save()
class InformasiFasilitasViewTest(TestCase):
urlpatterns = [
path('informasi-fasilitas/', include('informasi_fasilitas.urls')),
]
def test_LokasiSerializer_valid(self):
serializer = LokasiSerializer(data = {
'name' : 'Mall1',
'latitude' : 0.0,
'longitude' : 0.0,
'alamat' : 'Jl. Raya Bogor no.1, Jakarta',
'no_telp' : '081212123131'
})
self.assertTrue(serializer.is_valid)
def test_can_get_lokasi_list(self):
response = Client().get(reverse('lokasi-list'))
self.assertEqual(response.status_code, 200)
def test_cannot_post_lokasi_list(self):
response = Client().post(reverse('lokasi-list'))
self.assertEqual(response.status_code, 405)
def test_can_get_lokasi_details(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
response = Client().get(reverse('lokasi-details', kwargs={'name':'Mall'}))
self.assertEqual(response.status_code, 200)
def test_cannot_post_lokasi_details(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
response = Client().post(reverse('lokasi-details', kwargs={'name':'Mall'}))
self.assertEqual(response.status_code, 405)
def test_get_lokasi_details_not_found(self):
response = Client().get(reverse('lokasi-details', kwargs={'name':'Mall'}))
self.assertEqual(response.status_code, 404)
def test_can_post_add_lokasi(self):
email='user@gmail.com'
password='hahagotim'
Client().post('/api/register/',
{'name':'name',
'email':email,
'phone_number':000000000,
'password':password})
token_response = Client().post('/api-token-auth/',
{'username' : email, 'password' : password})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
client = Client(HTTP_AUTHORIZATION='token '+token)
response = client.post(reverse('add-lokasi'),
{'name' : 'Mall',
'latitude' : 0.0,
'longitude' : 0.0,
'alamat' : 'Jl. Raya Bogor no.1, Jakarta',
'no_telp' : '081212123131'})
self.assertEqual(response.status_code, 201)
def test_cannot_get_add_lokasi(self):
email='user@gmail.com'
password='hahagotim'
Client().post('/api/register/',
{'name':'name',
'email':email,
'phone_number':000000000,
'password':password})
token_response = Client().post('/api-token-auth/',
{'username' : email, 'password' : password})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
client = Client(HTTP_AUTHORIZATION='token '+token)
response = client.get(reverse('add-lokasi'))
self.assertEqual(response.status_code, 405)
def test_can_get_list_fasilitas(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
user=User(
username='user@gmail.com',
last_name='name',
email='user@gmail.com',
password='hahagotim'
)
user.save()
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
fasilitas=Fasilitas(
lokasi=lokasi,
user=user,
deskripsi="penjelasan panjang",
like=0,
dislike=0,
rating=5,
tag={KURSI_RODA, RUNNING_TEXT},
image=image
)
fasilitas.save()
response = Client().get(reverse('list-fasilitas', kwargs={'nama_lokasi':'Mall'}))
self.assertEqual(response.status_code, 200)
def test_cannot_post_list_fasilitas(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
response = Client().post(reverse('list-fasilitas', kwargs={'nama_lokasi':'Mall'}))
self.assertEqual(response.status_code, 405)
def test_get_list_fasilitas_not_found(self):
response = Client().get(reverse('list-fasilitas', kwargs={'nama_lokasi':'Mall'}))
self.assertEqual(response.status_code, 404)
def test_can_get_detail_fasilitas(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
user=User(
username='user@gmail.com',
last_name='name',
email='user@gmail.com',
password='hahagotim'
)
user.save()
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
fasilitas=Fasilitas(
lokasi=lokasi,
user=user,
deskripsi="penjelasan panjang",
like=0,
dislike=0,
rating=5,
tag={KURSI_RODA, RUNNING_TEXT},
image=image
)
fasilitas.save()
id = fasilitas.id
response = Client().get(reverse('detail-fasilitas', kwargs={'nama_lokasi':'Mall', 'id':id}))
self.assertEqual(response.status_code, 200)
def test_cannot_post_detail_fasilitas(self):
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
user=User(
username='user@gmail.com',
last_name='name',
email='user@gmail.com',
password='hahagotim'
)
user.save()
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
fasilitas=Fasilitas(
lokasi=lokasi,
user=user,
deskripsi="penjelasan panjang",
like=0,
dislike=0,
rating=5,
tag={KURSI_RODA, RUNNING_TEXT},
image=image
)
fasilitas.save()
id = fasilitas.id
response = Client().post(reverse('detail-fasilitas', kwargs={'nama_lokasi':'Mall', 'id':id}))
self.assertEqual(response.status_code, 405)
def test_get_detail_fasilitas_not_found(self):
response = Client().get(reverse('detail-fasilitas', kwargs={'nama_lokasi':'Mall', 'id':0}))
self.assertEqual(response.status_code, 404)
def test_can_post_add_fasilitas(self):
email='user@gmail.com'
password='hahagotim'
Client().post('/api/register/',
{'name':'name',
'email':email,
'phone_number':000000000,
'password':password})
token_response = Client().post('/api-token-auth/',
{'username' : email, 'password' : password})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
client = Client(HTTP_AUTHORIZATION='token '+token)
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
response = client.post(reverse('add-fasilitas', kwargs={'nama_lokasi':'Mall'}),
{'deskripsi' : 'penjelasan fasilitas',
'rating' : 2,
'tag' : 'KR RT'})
self.assertEqual(response.status_code, 201)
def test_cannot_get_add_fasilitas(self):
email='user@gmail.com'
password='hahagotim'
Client().post('/api/register/',
{'name':'name',
'email':email,
'phone_number':000000000,
'password':password})
token_response = Client().post('/api-token-auth/',
{'username' : email, 'password' : password})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
client = Client(HTTP_AUTHORIZATION='token '+token)
image = tempfile.NamedTemporaryFile(suffix=".jpg").name
lokasi=Lokasi(
name='Mall',
latitude=0.0,
longitude=0.0,
alamat='Jl. Raya Bogor no.1, Jakarta',
no_telp='081212123131',
image=image
)
lokasi.save()
response = client.get(reverse('add-fasilitas', kwargs={'nama_lokasi':'Mall'}))
self.assertEqual(response.status_code, 405)
def test_post_add_fasilitas_not_found(self):
email='user@gmail.com'
password='hahagotim'
Client().post('/api/register/',
{'name':'name',
'email':email,
'phone_number':000000000,
'password':password})
token_response = Client().post('/api-token-auth/',
{'username' : email, 'password' : password})
content = json.loads(token_response.content.decode('utf-8'))
token = content['token']
client = Client(HTTP_AUTHORIZATION='token '+token)
response = client.post(reverse('add-fasilitas', kwargs={'nama_lokasi':'Mall'}),
{'deskripsi' : 'penjelasan fasilitas',
'rating' : 2})
self.assertEqual(response.status_code, 404)
\ No newline at end of file
......@@ -4,9 +4,9 @@ from . import views
urlpatterns = [
path('lokasi/list/', views.lokasi_list,name='lokasi-list'),
path('lokasi/detail/<str:name>', views.lokasi_details,name='lokasi-details'),
path('lokasi/detail/<str:name>/', views.lokasi_details,name='lokasi-details'),
path('lokasi/add/', views.add_lokasi,name='add-lokasi'),
path('lokasi/add-fasilitas/<str:nama_lokasi>', views.add_fasilitas, name='add-fasilitas'),
path('lokasi/list-fasilitas/<str:nama_lokasi>', views.list_fasilitas, name='list-fasilitas'),
path('lokasi/detail-fasilitas/<str:nama_lokasi>/<int:id>', views.detail_fasilitas, name='detail-fasilitas'),
path('lokasi/add-fasilitas/<str:nama_lokasi>/', views.add_fasilitas, name='add-fasilitas'),
path('lokasi/list-fasilitas/<str:nama_lokasi>/', views.list_fasilitas, name='list-fasilitas'),
path('lokasi/detail-fasilitas/<str:nama_lokasi>/<int:id>/', views.detail_fasilitas, name='detail-fasilitas'),
]
\ No newline at end of file
......@@ -5,7 +5,8 @@ from django.contrib.auth.models import User
from rest_framework import viewsets
from rest_framework.decorators import api_view, permission_classes, authentication_classes
from rest_framework.authentication import TokenAuthentication
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from .serializers import LokasiSerializer
from .models import Lokasi, Fasilitas, Komentar
......@@ -24,7 +25,7 @@ def lokasi_list(request):
if request.method == 'GET':
lokasi_list = Lokasi.objects.all()
serializer = LokasiSerializer(lokasi_list, many=True)
return JsonResponse(serializer.data, safe=False)
return JsonResponse(serializer.data, safe=False, status = 200)
else:
return JsonResponse({'response' : request_error_message("get")}, status = 400)
......@@ -36,7 +37,7 @@ def lokasi_details(request,name):
if request.method == 'GET':
lokasi = Lokasi.objects.get(name = name)
serializer = LokasiSerializer(lokasi)
return JsonResponse(serializer.data, safe=False)
return JsonResponse(serializer.data, safe=False, status = 200)
else:
return JsonResponse({'response' : request_error_message("get")}, status = 400)
except KeyError as e:
......@@ -45,26 +46,26 @@ def lokasi_details(request,name):
return JsonResponse({'response':'lokasi not found'}, status = 404)
@api_view(['POST'])
@authentication_classes([])
@permission_classes([])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def add_lokasi(request):
try:
if request.method == 'POST':
name = request.POST['name']
latitude = request.POST['latitude']
longitude = request.POST['longitude']
alamat = request.POST['alamat']
no_telp = request.POST['no_telp']
image = request.POST['image']
Lokasi.objects.create(name=name, latitude=latitude, longitude=longitude, alamat=alamat, no_telp=no_telp, image=image)
return JsonResponse({'response' : 'Location added', 'name':name}, status = 201)
data = request.POST
serializer = LokasiSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
else:
return JsonResponse(serializer.errors, status=400)
else:
return JsonResponse({'response' : request_error_message("post")}, status = 400)
except KeyError as e:
return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
@api_view(['GET'])
@authentication_classes([TokenAuthentication,])
@authentication_classes([])
@permission_classes([])
def list_fasilitas(request, nama_lokasi):
try:
......@@ -86,26 +87,28 @@ def list_fasilitas(request, nama_lokasi):
fasilitas_details["tag"] = fasilitas.tag
fasilitas_details["image"] = str(fasilitas.image)
fasilitas_details["is_verified"] = fasilitas.is_verified
return JsonResponse(return_json, status = 201)
return JsonResponse(return_json, status = 200)
except KeyError as e:
return JsonResponse({'response': missing_key_message(str(e))}, status = 500)
except Exception as e:
return JsonResponse({'response': str(e)})
return JsonResponse({'response': str(e)}, status = 404)
@api_view(['POST'])
@authentication_classes([TokenAuthentication,])
@permission_classes([])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAuthenticated])
def add_fasilitas(request, nama_lokasi):
try:
if request.method == 'POST':
lokasi = Lokasi.objects.get(name=nama_lokasi)
user = User.objects.get(email=str(request.user))
deskripsi = request.POST['deskripsi']
image = request.POST['image']
rating = request.POST['rating']
tag = request.POST['tag'].split()
fasilitas = Fasilitas.objects.create(lokasi=lokasi,
user=user,
deskripsi=deskripsi,
image=image)
rating=rating,
tag=tag)
return JsonResponse({'response' : 'fasilitas added', 'id':fasilitas.id}, status = 201)
else:
return JsonResponse({'response' : request_error_message("post")}, status = 400)
......@@ -125,7 +128,7 @@ def detail_fasilitas(request, nama_lokasi, id):
user = fasilitas.user
return_json = {"nama_lokasi": lokasi.name, "deskripsi":fasilitas.deskripsi, "creator":user.last_name, "date_time":fasilitas.date_time,
"like":fasilitas.like, "dislike":fasilitas.dislike, "rating":fasilitas.rating, "tag":fasilitas.tag, "image":str(fasilitas.image), "is_verified":fasilitas.is_verified}
return JsonResponse(return_json, status = 201)
return JsonResponse(return_json, status = 200)
else:
return JsonResponse({'response' : request_error_message("get")}, status = 400)
except KeyError as e:
......
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