def test_login_and_logout(self): factory = APIRequestFactory() request = factory.post( "/api/v1/auth/login", { "username": "******", "password": "******" }, format="json", ) response = obtain_auth_token(request) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIn("token", response.data) # Logout this token token = response.data["token"] request = factory.get("/api/v1/auth/logout", HTTP_AUTHORIZATION="Token {}".format(token)) view = Logout.as_view() response = view(request) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIsNone(response.data) # Retry same token and fails as unauthorized request = factory.get("/api/v1/auth/logout", HTTP_AUTHORIZATION="Token {}".format(token)) view = Logout.as_view() response = view(request) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include from rest_framework.authtoken import views from api.views import Login, Logout urlpatterns = [ path('admin/', admin.site.urls), # Parte de la ruta para los métodos de la api (versión 1) path('api/v1/', include(('api.urls', 'api'))), # Endpoint para obtener el token del usuario según unas credenciales. path('generate_token/', views.obtain_auth_token), # Interfaz de prueba para logarse visualmente y no mediante el endpoint de generate_token path('login/', Login.as_view(), name='Login'), # Endpoint para destruir el token de identificación del usuario y así deslogarse. path('logout/', Logout.as_view(), name='Logout'), ]
from django.conf.urls import url from api.views import Join, Login, Logout, Detail, Update urlpatterns = [ url(r'^user/join/', Join.as_view()), url(r'^user/login/', Login.as_view()), url(r'^user/logout/', Logout.as_view()), url(r'^user/detail/', Detail.as_view()), url(r'^user/update/(?P<type>.+)', Update.as_view()), ]
"""login_rest URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include from rest_framework.authtoken import views import api from api.views import Login, Logout, UserAPI urlpatterns = [ path('admin/', admin.site.urls), path('api/1.0/', include(('api.urls', 'api'))), path('api_generate_token/', views.obtain_auth_token), path('login/', Login.as_view(), name='login'), path('logout/', Logout.as_view()), path('api/1.0/create_user/', UserAPI.as_view(), name="api_create_user") ]
1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.conf import settings from django.conf.urls.static import static from django.contrib import admin from django.urls import path, include from django.views.generic import TemplateView from rest_framework import routers from api.views import CustomAuthToken, Logout, Register # Routers provide an easy way of automatically determining the URL conf. from api.viewsets import CategoryViewSet, PostViewSet router = routers.DefaultRouter() router.register(r'categories', CategoryViewSet, base_name="category") router.register(r'posts', PostViewSet, base_name="post") urlpatterns = [ path('admin/', admin.site.urls), path('', TemplateView.as_view(template_name="index.html")), path('api/v1/web-auth/', include('rest_framework.urls'), name="web-auth"), path('api/v1/login', CustomAuthToken.as_view(), name="login"), path('api/v1/register', Register.as_view(), name="register"), path('api/v1/', include(router.urls)), path('api/v1/logout/', Logout.as_view(), name="logout"), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) if settings.DEBUG: urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
"""backend URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include from api.views import SignUp, Logout, UserData urlpatterns = [ path('admin/', admin.site.urls), path('backend/register/', SignUp.as_view(), name='register'), path('backend/userdata/', UserData.as_view(), name='userdata'), path('rest-auth/logout/', Logout.as_view(), name='logout'), path('rest-auth/', include('rest_auth.urls')), ]