Python rest_framework_jwt.views.obtain_jwt_token() Examples

The following are 1 code examples of rest_framework_jwt.views.obtain_jwt_token(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework_jwt.views , or try the search function .
Example #1
Source File: tests.py    From certificate-generator-server-archive with GNU General Public License v3.0 5 votes vote down vote up
def test_if_student_was_create(self):
        # Registering a new user
        payload = {
            "name": "test",
            "user": {
                "username": "test",
                "password": "password",
                "email": "test@test.com"
            }
        }
        request = self.factory.post('/api/user', payload, format='json')
        response = views.StudentCreation.as_view()(request)
        self.assertEqual(response.status_code, 201)

        # Checking whether the actual user object was created or not
        user = User.objects.get(username='test')
        self.assertEqual(user.username, 'test')
        self.assertEqual(user.email, 'test@test.com')

        # Checking if password is stored as a hash or plain
        user = User.objects.get(username='test')
        self.assertNotEqual(user.password, "password")

        # Trying a login protected route for student
        payload = {
            "username": "test",
            "password": "password"
        }
        request = self.factory.post('/api-token-auth/', payload, format='json')
        response = obtain_jwt_token(request)
        self.assertEqual(response.status_code, 200)
        response.render()
        token = json.loads(response.content)['token']
        self.client.credentials(HTTP_AUTHORIZATION='JWT {}'.format(token))
        response = self.client.get('/api/get_certificates/')
        self.assertEqual(response.status_code, 200)