Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

JET token authentication in Django UTC

 def test_api_jwt(self):

    url = reverse('api-jwt-auth')
    u = user_model.objects.create_user(username='user', email='user@foo.com', password='pass')
    u.is_active = False
    u.save()

    resp = self.client.post(url, {'email':'user@foo.com', 'password':'pass'}, format='json')
    self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)

    u.is_active = True
    u.save()

    resp = self.client.post(url, {'username':'user@foo.com', 'password':'pass'}, format='json')
    self.assertEqual(resp.status_code, status.HTTP_200_OK)
    self.assertTrue('token' in resp.data)
    token = resp.data['token']
    #print(token)

    verification_url = reverse('api-jwt-verify')
    resp = self.client.post(verification_url, {'token': token}, format='json')
    self.assertEqual(resp.status_code, status.HTTP_200_OK)

    resp = self.client.post(verification_url, {'token': 'abc'}, format='json')
    self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)

    client = APIClient()
    client.credentials(HTTP_AUTHORIZATION='JWT ' + 'abc')
    resp = client.get('/api/v1/account/', data={'format': 'json'})
    self.assertEqual(resp.status_code, status.HTTP_401_UNAUTHORIZED)
    client.credentials(HTTP_AUTHORIZATION='JWT ' + token)
    resp = client.get('/api/v1/account/', data={'format': 'json'})
    self.assertEqual(resp.status_code, status.HTTP_200_OK)
Comment

JET token authentication in Django UTC-1

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from django.contrib.auth.models import User


class AuthViewsTests(APITestCase):

    def setUp(self):
        self.username = 'usuario'
        self.password = 'contrasegna'
        self.data = {
            'username': self.username,
            'password': self.password
        }

    def test_current_user(self):

        # URL using path name
        url = reverse('tokenAuth')

        # Create a user is a workaround in order to authentication works
        user = User.objects.create_user(username='usuario', email='usuario@mail.com', password='contrasegna')
        self.assertEqual(user.is_active, 1, 'Active User')

        # First post to get token
        response = self.client.post(url, self.data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)
        token = response.data['token']

        # Next post/get's will require the token to connect
        self.client.credentials(HTTP_AUTHORIZATION='JWT {0}'.format(token))
        response = self.client.get(reverse('currentUser'), data={'format': 'json'})
        self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)
Comment

PREVIOUS NEXT
Code Example
Python :: ** (ArgumentError) lists in Phoenix.HTML and templates may only contain integers representing bytes, binaries or other lists, got invalid entry: 
Python :: poython command options 
Python :: python read vcf file line by line 
Python :: how to check if two buttons were pressed python 
Python :: print without parenthesis 
Python :: how to subtract up everything in a list python 
Python :: python if else 
Python :: MEMORY MANAGEMENT SYSTEM IN PYTHON 
Python :: Python Multiline docstring example 
Python :: what does features = data.drop(["Survived", "Sex", "Embarked"], axis=1) do in python 
Python :: coger elementos de un string python expresiones regulares 
Python :: count how many loops that printed in python 
Python :: bucket dataframe into ranges 
Python :: Requests-html absolute url 
Python :: print 1 side of a dictionary python 
Python :: email python library get all messages 
Python :: et.dump export file to xml write method 
Python :: unhapppy man with monwy 
Python :: python why is it important to check the __name__ 
Python :: add hours to date time in python 
Python :: how to add 2 integers in python 
Python :: OpenCV(3.4.11) Error: Assertion failed (_img.rows * _img.cols == vecSize) in CvCascadeImageReader::PosReader::get 
Python :: remove cooldown discord python 
Python :: programe to find contagious sum of sequence 
Python :: how to convert array value to integer in python 
Python :: Herons rule python 
Python :: get false positives from confusoin matrix 
Python :: fouier transformation in python open cv 
Python :: check labels with handles in ax 
Python :: python merge two byte files 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =