Fakultas Ilmu Komputer UI

Commit 2a9e8aa5 authored by Arga Ghulam Ahmad's avatar Arga Ghulam Ahmad
Browse files

Extract login process to a new method on FeedbackTests

Remove duplications on FeedbackTests.
parent bbe25ae3
from django.contrib import admin
from core.models.accounts import Company, Supervisor, Student
from core.models.feedbacks import Feedback
from core.models.vacancies import Vacancy
admin.site.register(Company)
admin.site.register(Student)
admin.site.register(Supervisor)
admin.site.register(Vacancy)
admin.site.register(Feedback)
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-10-03 13:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0013_auto_20170602_1130'),
]
operations = [
migrations.CreateModel(
name='Feedback',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('title', models.CharField(blank=True, default=b'', max_length=100)),
('content', models.TextField()),
],
options={
'ordering': ['created'],
},
),
]
from django.db import models
class Feedback(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
content = models.TextField()
class Meta:
ordering = ['created']
from rest_framework import serializers
from core.models.feedbacks import Feedback
class FeedbackSerializer(serializers.ModelSerializer):
class Meta:
model = Feedback
fields = ['id', 'title', 'content']
# __init__.py
from core.tests.test_accounts import LoginTests, RegisterTests
from core.tests.test_vacancies import ApplicationTests, BookmarkApplicationTests, CompanyListsTests
from core.tests.test_feedbacks import FeedbacksTests
import requests_mock
from rest_framework import status
from rest_framework.test import APITestCase
from core.models.feedbacks import Feedback
class FeedbacksTests(APITestCase):
def login(self, m):
# Login
m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG',
json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'},
status_code=200)
m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
"username": "dummy.mahasiswa",
"nama": "Dummy Mahasiswa",
"state": 1,
"kode_org": "01.00.12.01:mahasiswa",
"kodeidentitas": "1234567890",
"nama_role": "mahasiswa"
}, status_code=200)
m.get(
'https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG',
json={
"kota_lahir": "kota_kota",
"tgl_lahir": "2017-12-31",
"program": [{
"nm_org": "Ilmu Informasi",
"angkatan": "2017"
}]
}, status_code=200)
login_url = '/api/login/'
self.client.post(login_url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'},
format='json')
@requests_mock.Mocker()
def test_feedback_list_with_more_than_10_feedback_and_with_page_number(self, m):
self.login(m)
# test_feedback_list_with_more_than_10_feedback_and_with_page_number
for i in range(15):
Feedback.objects.create(content="a feedback", title="a title")
feedbacks_url = '/api/feedbacks/'
feedbacks_url_page_1 = '/api/feedbacks/?page=1'
feedbacks_url_page_2 = '/api/feedbacks/?page=2'
response1 = self.client.get(feedbacks_url_page_1)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data['count'], 15)
self.assertIn(feedbacks_url_page_2, response1.data['next'])
self.assertEqual(response1.data['previous'], None)
self.assertEqual(len(response1.data['results']), 10)
response2 = self.client.get(feedbacks_url_page_2)
self.assertEqual(response2.status_code, status.HTTP_200_OK)
self.assertEqual(response2.data['count'], 15)
self.assertIn(feedbacks_url, response2.data['previous'])
self.assertEqual(response2.data['next'], None)
self.assertEqual(len(response2.data['results']), 5)
@requests_mock.Mocker()
def test_feedback_list_with_more_than_10_feedback_and_without_page_number(self, m):
self.login(m)
# test_feedback_list_with_more_than_10_feedback_and_without_page_number
for i in range(15):
Feedback.objects.create(content="a feedback", title="a title")
feedbacks_url = '/api/feedbacks/'
feedbacks_url_page_2 = '/api/feedbacks/?page=2'
response1 = self.client.get(feedbacks_url)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data['count'], 15)
self.assertIn(feedbacks_url_page_2, response1.data['next'])
self.assertEqual(response1.data['previous'], None)
self.assertEqual(len(response1.data['results']), 10)
response2 = self.client.get(feedbacks_url_page_2)
self.assertEqual(response2.status_code, status.HTTP_200_OK)
self.assertEqual(response2.data['count'], 15)
self.assertIn(feedbacks_url, response2.data['previous'])
self.assertEqual(response2.data['next'], None)
self.assertEqual(len(response2.data['results']), 5)
@requests_mock.Mocker()
def test_feedback_list_with_less_than_or_equals_10_feedbacks_and_with_zero_feedback_and_without_page_number(self,
m):
self.login(m)
# test_feedback_list_with_more_than_10_feedback_and_without_page_number
feedbacks_url = '/api/feedbacks/'
response1 = self.client.get(feedbacks_url)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data['count'], 0)
self.assertEqual(response1.data['next'], None)
self.assertEqual(response1.data['previous'], None)
self.assertEqual(len(response1.data['results']), 0)
@requests_mock.Mocker()
def test_feedback_list_with_less_than_or_equals_10_feedbacks_and_with_more_than_zero_feedback_and_with_page_number(
self, m):
self.login(m)
# test_feedback_list_with_less_than_10_feedbacks_and_with_more_than_zero_feedback_and_with_page_number
for i in range(5):
Feedback.objects.create(content="a feedback", title="a title")
feedbacks_url_page_1 = '/api/feedbacks/?page=1'
response1 = self.client.get(feedbacks_url_page_1)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data['count'], 5)
self.assertEqual(response1.data['previous'], None)
self.assertEqual(response1.data['next'], None)
self.assertEqual(len(response1.data['results']), 5)
@requests_mock.Mocker()
def test_feedback_list_with_less_than_or_equals_10_feedbacks_and_with_more_than_zero_feedback_and_without_page_number(
self,
m):
self.login(m)
# test_feedback_list_with_less_than_10_feedbacks_and_with_more_than_zero_feedback_and_without_page_number
for i in range(5):
Feedback.objects.create(content="a feedback", title="a title")
feedbacks_url = '/api/feedbacks/'
response1 = self.client.get(feedbacks_url)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data['count'], 5)
self.assertEqual(response1.data['previous'], None)
self.assertEqual(response1.data['next'], None)
self.assertEqual(len(response1.data['results']), 5)
@requests_mock.Mocker()
def test_feedback_list_with_less_than_or_equals_10_feedbacks_and_with_zero_feedback_and_with_page_number(self, m):
self.login(m)
# test_feedback_list_with_less_than_10_feedbacks_and_with_zero_feedback_and_with_page_number
feedbacks_url = '/api/feedbacks/?page=1'
response1 = self.client.get(feedbacks_url)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data['count'], 0)
self.assertEqual(response1.data['next'], None)
self.assertEqual(response1.data['previous'], None)
self.assertEqual(len(response1.data['results']), 0)
@requests_mock.Mocker()
def test_feedbacks_create_with_title_and_content(self, m):
self.login(m)
# test_feedbacks_create_with_title_and_content
feedbacks_url = '/api/feedbacks/'
response = self.client.post(feedbacks_url,
{"content": "a content", "title": "a title"})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["title"], "a title")
self.assertEqual(response.data["content"], "a content")
@requests_mock.Mocker()
def test_feedbacks_create_without_title_and_with_content(self, m):
self.login(m)
# test_feedbacks_create_without_title_and_with_content
feedbacks_url = '/api/feedbacks/'
response = self.client.post(feedbacks_url,
{"content": "a content"})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['detail'], "Please send a json object that have title and content property")
@requests_mock.Mocker()
def test_feedbacks_create_without_content_and_with_title(self, m):
self.login(m)
# test_feedbacks_create_without_content_and_with_title
feedbacks_url = '/api/feedbacks/'
response = self.client.post(feedbacks_url,
{"title": "a title"})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['detail'], "Please send a json object that have title and content property")
@requests_mock.Mocker()
def test_feedbacks_create_without_content_and_without_title(self, m):
self.login(m)
# test_feedbacks_create_without_content_and_without_title
feedbacks_url = '/api/feedbacks/'
response = self.client.post(feedbacks_url,
{})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['detail'], "Please send a json object that have title and content property")
@requests_mock.Mocker()
def test_feedbacks_delete_with_exist_data_and_with_data_id(self, m):
self.login(m)
# test_feedbacks_delete_with_exist_data_and_with_data_id
new_feedback = Feedback(title="a title", content="a content")
new_feedback.save()
feedback_id = new_feedback.id
feedbacks_url = '/api/feedbacks/' + str(feedback_id) + '/'
response = self.client.delete(feedbacks_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["detail"], "Feedback with id {} was removed".format(feedback_id))
@requests_mock.Mocker()
def test_feedbacks_delete_with_exist_data_and_with_data_id(self, m):
self.login(m)
# test_feedbacks_delete_with_exist_data_and_with_data_id
new_feedback = Feedback(title="a title", content="a content")
new_feedback.save()
feedback_id = new_feedback.id
feedbacks_url = '/api/feedbacks/' + str(feedback_id) + '/'
response = self.client.delete(feedbacks_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["detail"], "Feedback with id {} was removed".format(feedback_id))
@requests_mock.Mocker()
def test_feedbacks_delete_with_not_exist_data_and_with_data_id(self, m):
self.login(m)
# test_feedbacks_delete_with_not_exist_data_and_with_data_id
new_feedback = Feedback(title="a title", content="a content")
new_feedback.save()
feedback_id = new_feedback.id
new_feedback.delete()
feedbacks_url = '/api/feedbacks/' + str(feedback_id) + '/'
response = self.client.delete(feedbacks_url)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["detail"], "Feedback with id {} doesn't exist".format(feedback_id))
@requests_mock.Mocker()
def test_feedbacks_delete_without_data_id(self, m):
self.login(m)
# test_feedbacks_delete_without_data_id
feedbacks_url = '/api/feedbacks/'
response = self.client.delete(feedbacks_url)
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
from rest_framework import viewsets, status
from rest_framework.response import Response
from core.lib.permissions import IsAdminOrStudent
from core.models.feedbacks import Feedback
from core.serializers.feedbacks import FeedbackSerializer
class FeedbackViewSet(viewsets.GenericViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` feedback actions.
Additionally we also provide an extra `highlight` action.
"""
queryset = Feedback.objects.all()
serializer_class = FeedbackSerializer
permission_classes = [IsAdminOrStudent]
def list(self, request):
"""
Get list of feedbacks
---
"""
feedbacks = Feedback.objects.all()
page = self.paginate_queryset(feedbacks)
serialized_page_data = FeedbackSerializer(page, many=True, context={'request': request}).data
print (serialized_page_data)
return self.get_paginated_response(serialized_page_data)
def create(self, request):
"""
Create a feedback
---
"""
if 'title' in request.data and 'content' in request.data:
new_feedback = Feedback(title=request.data['title'], content=request.data['content'])
new_feedback.save()
serialized_new_feedback = FeedbackSerializer(new_feedback, context={'request': request})
serialized_new_feedback_data = serialized_new_feedback.data
print(serialized_new_feedback_data)
return Response(serialized_new_feedback_data)
else:
message = {"detail": "Please send a json object that have title and content property"}
print(message)
return Response(message, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, pk):
"""
Remove feedback {id}
---
"""
if Feedback.objects.filter(id=pk).count() != 0:
print (request.data)
delete_feedback = Feedback.objects.get(id=pk)
delete_feedback.delete()
message = {"detail": "Feedback with id {} was removed".format(pk)}
print(message)
return Response(message, status=status.HTTP_200_OK)
else:
message = {"detail": "Feedback with id {} doesn't exist".format(pk)}
print(message)
return Response(message, status=status.HTTP_400_BAD_REQUEST)
......@@ -26,6 +26,7 @@ from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSe
CompanyRegisterViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, StudentApplicationViewSet, \
CompanyApplicationViewSet, CompanyVacanciesViewSet, ApplicationViewSet
from core.views.feedbacks import FeedbackViewSet
schema_view = get_swagger_view()
router = routers.DefaultRouter()
......@@ -37,6 +38,7 @@ router.register(r'login', LoginViewSet)
router.register(r'register', CompanyRegisterViewSet)
router.register(r'vacancies', VacancyViewSet)
router.register(r'applications', ApplicationViewSet)
router.register(r'feedbacks', FeedbackViewSet)
# router.register(r'students/(?P<student_id>\d+)/profile', StudentProfileViewSet)
router.register(r'students/(?P<student_id>\d+)/bookmarked-vacancies', BookmarkedVacancyByStudentViewSet,
base_name='bookmarked-vacancy-list')
......
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