def test_delete_task(self): factory = APIRequestFactory() view = TaskDetail.as_view() project = Project.objects.create(title='TestProject') tasks = Task.objects.bulk_create([(Task(id=1, title='Test1', description='d1', project=project)), (Task(id=2, title='Test2', description='d2', project=project)), (Task(id=3, title='Test3', description='d3', project=project))]) task = tasks[0] request = factory.delete('/api/tasks/1') force_authenticate(request, user=self.test_user, token=self.full_access_token) response = view(request, pk='1') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(Task.objects.all()), 2) self.assertNotIn(task, Task.objects.all())
def test_update_task(self): factory = APIRequestFactory() view = TaskDetail.as_view() project = Project.objects.create(id=1, title='TestProject 1') Task.objects.create(title='TestTask', description='test', project_id=project.id, id=1) task_data = { 'title': 'NewTask', 'description': 'new one', 'project_id': project.id } request = factory.put('/api/tasks/1', task_data) force_authenticate(request, user=self.test_user, token=self.full_access_token) response = view(request, pk='1') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['title'], task_data['title']) self.assertEqual(response.data['description'], task_data['description'])
def test_assign_task_to_user_after_task_updating(self): factory = APIRequestFactory() view = TaskDetail.as_view() project = Project.objects.create(id=1, title='TestProject 1') task = Task.objects.create(title='TestTask', project_id=project.id, id=1) user = User.objects.create(username='******', password='******') task_data = { 'title': task.title, 'description': task.description, 'project_id': project.id, 'user_id': user.id } request = factory.put('/api/tasks/1', task_data) force_authenticate(request, user=self.test_user, token=self.full_access_token) response = view(request, pk='1') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['user']['id'], user.id)
def test_get_task_detail(self): factory = APIRequestFactory() view = TaskDetail.as_view() project = Project.objects.create(title='TestProject 1', id=1) task = Task.objects.create(id=1, title='Test', description='test', project_id=project.id) request = factory.get('/api/tasks/1') force_authenticate(request, user=self.test_user, token=self.full_access_token) response = view(request, pk='1') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(task.title, response.data['title']) self.assertEqual(task.description, response.data['description']) force_authenticate(request, user=self.test_user, token=self.read_access_token) response = view(request, pk='1') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(task.title, response.data['title']) self.assertEqual(task.description, response.data['description'])
def test_task_not_found(self): factory = APIRequestFactory() view = TaskDetail.as_view() request = factory.put('/api/tasks/10') force_authenticate(request, user=self.test_user, token=self.full_access_token) response = view(request, pk='10') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) request = factory.delete('/api/tasks/10') force_authenticate(request, user=self.test_user, token=self.full_access_token) response = view(request, pk='10') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_delete_task_fail_by_user_with_read_scope_only(self): factory = APIRequestFactory() view = TaskDetail.as_view() project = Project.objects.create(title='TestProject') Task.objects.bulk_create([(Task(id=1, title='Test1', project=project)), (Task(id=2, title='Test2', project=project)), (Task(id=3, title='Test3', project=project))]) request = factory.delete('/api/tasks/1') force_authenticate(request, user=self.test_user, token=self.read_access_token) response = view(request) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
from django.conf.urls import patterns, include, url # Function based API views # from api.views import task_list, task_detail # Class based API views from api.views import TaskList, TaskDetail urlpatterns = patterns('', # Regular URLs # url(r'^tasks/$', task_list, name='task_list'), # url(r'^tasks/(?P<pk>[0-9]+)$', task_detail, name='task_detail'), # Class based URLs, url( r'^tasks/$', TaskList.as_view(), name = 'task_list' ), url( r'^tasks/(?P<pk>[0-9]+)$', TaskDetail.as_view(), name = 'task_detail' ), )
], extra={'model': Text, 'serializer_class': TextSerializer}))), url(r'^users/$', UserList.as_view(), name='user-list'), url(r'^users/(?P<pk>\d+)/$', UserDetail.as_view(), name='user-detail'), url(r'^users/(?P<pk>\d+)/', include(APIView.urlpatterns([ {'func': 'communities', 'serializer_class': CommunitySerializer}, {'func': 'can_edit', 'func_args': '(?P<doc_pk>\d+)/'}, { 'func': 'memberships', 'serializer_class': MembershipSerializer }, { 'func': 'memberships', 'func_args': '(?P<community_pk>\d+)/', 'serializer_class': MembershipSerializer, }, ], extra={'model': APIUser, 'serializer_class': APIUserSerializer}))), url(r'^tasks/(?P<pk>\d+)/$', TaskDetail.as_view()), url(r'^memberships/$', MembershipList.as_view()), url(r'^memberships/(?P<pk>\d+)/$', MembershipDetail.as_view()), url(r'^memberships/(?P<pk>\d+)/', include(APIView.urlpatterns([ {'func': 'tasks', 'serializer_class': TaskSerializer}, {'func': 'has_task', 'func_args': '(?P<doc_pk>\d+)/'}, {'func': 'assign'}, ], extra={ 'model': Membership, 'serializer_class': MembershipSerializer }))), url(r'^roles/(?P<pk>\d+)/$', RoleDetail.as_view()), url(r'^revision/(?P<pk>\d+)/', include(APIView.urlpatterns([ {}, { 'methods': ['post', 'put'],
# urlpatterns = patterns('', # # # Regular URLs # # url(r'^tasks/$', task_list, name='task_list'), # # url(r'^tasks/(?P<pk>[0-9]+)$', task_detail, name='task_detail'), # # # Class based URLs, # url( r'^tasks/$', TaskList.as_view(), name = 'task_list' ), # url( r'^tasks/(?P<pk>[0-9]+)$', TaskDetail.as_view(), name = 'task_detail' ), # url( r'^tasks/task_id/(?P<task_id>.+)/$', TaskIDList.as_view()), # The API URLs are now determined automatically by the router. # Additionally, we include the login URLs for the browseable API. # urlpatterns = [ # url(r'^tasks/$', TaskView.as_view(), name='task-list'), # ] #router = DefaultRouter() #router.register(r'tasks', TaskViewSet) urlpatterns = patterns('', url( r'^tasks/task_id/(?P<task_id>.+)/$', TaskDetail.as_view()), url( r'^tasks/to_unit/(?P<to_unit>.+)/$', UnitList.as_view()), url( r'^tasks/$', TaskList.as_view()), #url( r'^tasks/task_id/(?P<pk>.+)/$', TaskDetail.as_view()), #url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) )
}, { 'func': 'memberships', 'serializer_class': MembershipSerializer }, { 'func': 'memberships', 'func_args': '(?P<community_pk>\d+)/', 'serializer_class': MembershipSerializer, }, ], extra={ 'model': APIUser, 'serializer_class': APIUserSerializer }))), url(r'^tasks/(?P<pk>\d+)/$', TaskDetail.as_view()), url(r'^memberships/$', MembershipList.as_view()), url(r'^memberships/(?P<pk>\d+)/$', MembershipDetail.as_view()), url( r'^memberships/(?P<pk>\d+)/', include( APIView.urlpatterns([ { 'func': 'tasks', 'serializer_class': TaskSerializer }, { 'func': 'has_task', 'func_args': '(?P<doc_pk>\d+)/' }, {
from django.urls import path from api.views import TasksList, ApiOverView, TaskDetail urlpatterns = [ path("", ApiOverView), path("task-list/", TasksList.as_view()), path("task-list/<int:id>", TaskDetail.as_view()), ]
from django.urls import path from api.views import TaskList from api.views import TaskDetail app_name = 'api' urlpatterns = [ path('', TaskList.as_view(), name='task-list'), path('<int:pk>/', TaskDetail.as_view(), name='task-detail'), ]
from django.urls import path from api import views from api.views import ListsList, ListDetail, GenericListList, GenericListDetail, TaskList, TaskDetail, GenericTaskList, GenericTaskDetail urlpatterns = [ path('login/', views.login), path('register/', views.register), path('list_cbv/', ListsList.as_view()), path('list_cbv/<int:pk>/', ListDetail.as_view()), path('list_fbv/', views.lists_list), path('list_fbv/<int:pk>/', views.lists_detail), path('list_gen/', GenericListList.as_view()), path('list_gen/<int:pk>/', GenericListDetail.as_view()), path('task_cbv/', TaskList.as_view()), path('task_cbv/<int:pk>/', TaskDetail.as_view()), path('task_fbv/', views.task_list), path('task_fbv/<int:pk>/', views.task_detail), path('task_gen/', GenericTaskList.as_view()), path('task_gen/<int:pk>/', GenericTaskDetail.as_view()), ]
# urlpatterns = patterns('', # # # Regular URLs # # url(r'^tasks/$', task_list, name='task_list'), # # url(r'^tasks/(?P<pk>[0-9]+)$', task_detail, name='task_detail'), # # # Class based URLs, # url( r'^tasks/$', TaskList.as_view(), name = 'task_list' ), # url( r'^tasks/(?P<pk>[0-9]+)$', TaskDetail.as_view(), name = 'task_detail' ), # url( r'^tasks/task_id/(?P<task_id>.+)/$', TaskIDList.as_view()), # The API URLs are now determined automatically by the router. # Additionally, we include the login URLs for the browseable API. # urlpatterns = [ # url(r'^tasks/$', TaskView.as_view(), name='task-list'), # ] #router = DefaultRouter() #router.register(r'tasks', TaskViewSet) urlpatterns = patterns( '', url(r'^tasks/task_id/(?P<task_id>.+)/$', TaskDetail.as_view()), url(r'^tasks/to_unit/(?P<to_unit>.+)/$', UnitList.as_view()), url(r'^tasks/$', TaskList.as_view()), #url( r'^tasks/task_id/(?P<pk>.+)/$', TaskDetail.as_view()), #url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')))