def test_user_retrieve(self): user = mixer.blend('user.User') view = UserViewSet.as_view({'get': 'retrieve'}) factory = APIRequestFactory() path = reverse('user-detail', kwargs={'pk': user.id}) req = factory.get(path, format='json') resp = view(req, pk=user.id) assert resp.status_code == status.HTTP_200_OK assert resp.data == user.to_dict(UserSerializer.Meta.fields)
def test_user_destroy(self): user = mixer.blend('user.User') assert User.objects.count() == 1 view = UserViewSet.as_view({'delete': 'destroy'}) factory = APIRequestFactory() path = reverse('user-detail', kwargs={'pk': user.id}) req = factory.delete(path, format='json') resp = view(req, pk=user.id) assert resp.status_code == status.HTTP_204_NO_CONTENT assert User.objects.count() == 0
def test_user_list(self): user_nums = 5 mixer.cycle(user_nums).blend('user.User') view = UserViewSet.as_view({'get': 'list'}) factory = APIRequestFactory() path = reverse('user-list') req = factory.get(path, format='json') resp = view(req) assert len(resp.data) == user_nums assert resp.status_code == status.HTTP_200_OK
def test_user_partial_update(self): user = mixer.blend('user.User') view = UserViewSet.as_view({'put': 'partial_update'}) factory = APIRequestFactory() path = reverse('user-detail', kwargs={'pk': user.id}) req = factory.put(path, data={"email": "*****@*****.**"}, format='json') resp = view(req, pk=user.id) assert resp.status_code == status.HTTP_200_OK assert resp.data['email'] == "*****@*****.**"
def test_user_create(self): view = UserViewSet.as_view({'post': 'create'}) factory = APIRequestFactory() path = reverse('user-list') with mixer.ctx(commit=False): user = mixer.blend('user.User') user_data = user.to_dict() req = factory.post(path, data=user_data, format='json') resp = view(req) assert User.objects.count() == 1 assert resp.status_code == status.HTTP_201_CREATED
def test_user_update(self): user = mixer.blend('user.User') view = UserViewSet.as_view({'put': 'update'}) factory = APIRequestFactory() path = reverse('user-detail', kwargs={'pk': user.id}) with mixer.ctx(commit=False): fake_user = mixer.blend('user.User') fake_user_data = fake_user.to_dict( ['password', 'username', 'email']) req = factory.put(path, data=fake_user_data, format='json') resp = view(req, pk=user.id) resp_data = resp.data.copy() resp_data.pop('id') assert resp.status_code == status.HTTP_200_OK assert resp_data == fake_user_data
def preload_latest_activity(hub_ids, ordering): from user.views import UserViewSet from reputation.serializers import DynamicContributionSerializer hub_ids_str = hub_ids request_path = '/api/user/following_latest_activity/' if STAGING: http_host = 'staging-backend.researchhub.com' protocol = 'https' elif PRODUCTION: http_host = 'backend.researchhub.com' protocol = 'https' else: http_host = 'localhost:8000' protocol = 'http' query_string = f'?page=1&hub_ids={hub_ids_str}' http_meta = { 'QUERY_STRING': query_string, 'HTTP_HOST': http_host, 'HTTP_X_FORWARDED_PROTO': protocol, } cache_key = get_cache_key('contributions', hub_ids_str) user_view = UserViewSet() http_req = HttpRequest() http_req.META = http_meta http_req.path = request_path req = Request(http_req) user_view.request = req latest_activities = user_view._get_latest_activity_queryset( hub_ids_str, ordering ) page = user_view.paginate_queryset(latest_activities) context = user_view._get_latest_activity_context() serializer = DynamicContributionSerializer( page, _include_fields=[ 'contribution_type', 'created_date', 'id', 'source', 'unified_document', 'user' ], context=context, many=True, ) serializer_data = serializer.data paginated_response = user_view.get_paginated_response( serializer_data ) cache.set( cache_key, paginated_response.data, timeout=60*60*24 ) return paginated_response.data
from django.urls import path # Local Application Imports from user.views import UserViewSet, UserSingleViewSet app_name = 'user' urlpatterns = [ path('users/', UserViewSet.as_view(), name='users'), path('users/<int:pk>', UserSingleViewSet.as_view(), name='users') ]
from django.urls import path from user.views import UserViewSet urlpatterns = [ path('authenticate', UserViewSet.as_view({ 'post': 'login', })), path('user', UserViewSet.as_view({ 'post': 'create', })), path( 'user', UserViewSet.as_view({ 'put': 'update', 'delete': 'delete', 'get': 'retrieve' })), ]