<s> """<STR_LIT>""" <EOL> from datetime import timedelta <EOL> from time import sleep <EOL> from django . contrib . auth . models import User <EOL> from rest_framework import status <EOL> from rest_framework . test import APITestCase <EOL> from rest_framework_expiring_authtoken . models import ExpiringToken <EOL> class ObtainExpiringTokenViewTestCase ( APITestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . username = '<STR_LIT:test>' <EOL> self . email = '<STR_LIT>' <EOL> self . password = '<STR_LIT:test>' <EOL> self . user = User . objects . create_user ( <EOL> username = self . username , <EOL> email = self . email , <EOL> password = self . password <EOL> ) <EOL> def test_post ( self ) : <EOL> """<STR_LIT>""" <EOL> token = ExpiringToken . objects . create ( user = self . user ) <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT:username>' : self . username , <EOL> '<STR_LIT:password>' : self . password <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( token . key , response . data [ '<STR_LIT>' ] ) <EOL> def test_post_create_token ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT:username>' : self . username , <EOL> '<STR_LIT:password>' : self . password <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> token = ExpiringToken . objects . first ( ) <EOL> self . assertEqual ( token . user , self . user ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , token . key ) <EOL> def test_post_no_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( response . data , <EOL> { <EOL> '<STR_LIT:username>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT:password>' : [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> def test_post_wrong_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT:username>' : self . username , <EOL> '<STR_LIT:password>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( response . data , <EOL> { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' <EOL> ] <EOL> } <EOL> ) <EOL> def test_post_expired_token ( self ) : <EOL> """<STR_LIT>""" <EOL> token = ExpiringToken . objects . create ( user = self . user ) <EOL> key_1 = token . key <EOL> with self . settings ( EXPIRING_TOKEN_LIFESPAN = timedelta ( milliseconds = <NUM_LIT:1> ) ) : <EOL> sleep ( <NUM_LIT> ) <EOL> response = self . client . post ( <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT:username>' : self . username , <EOL> '<STR_LIT:password>' : self . password <EOL> } <EOL> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> token = ExpiringToken . objects . first ( ) <EOL> key_2 = token . key <EOL> self . assertEqual ( token . user , self . user ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , token . key ) <EOL> self . assertTrue ( key_1 != key_2 ) </s>
