def test_jwt_token(self): """ creates a dummy mechanic through mechanic signup should get a valid response on signup tries to access secure api without token should get an error response saying jwt token required then tries to access secure api with a valid token should get a valid response(200) of the api :return: None """ self.client.post('/api/mechanic/signup', self.mechanic, content_type="application/json") user = User.objects.get(email=self.mechanic['email']) res = self.client.get('/api/mechanic') self.assertNotEqual(res.status_code, 200) self.assertEqual(res.json()['message'], messages.JWT_REQUIRED) jwt_token = get_jwt(user) auth_headers = {'HTTP_AUTHORIZATION': 'Bearer ' + jwt_token} res = self.client.get('/api/mechanic', **auth_headers) self.assertEqual(res.status_code, 200) self.assertEqual(res.json()['mechanics'][0], Mechanic.objects.get(user=user).to_dict())
def setUp(self): """ stores a sample request body for mechanic signup creates a dummy mechanic, a dummy user, a dummy vehicle and corresponding auth tokens stores a sample request body for contact mechanic :return: None """ self.client = Client() self.mechanic = get_sample_mechanic_data() self.client.post('/api/mechanic/signup', self.mechanic, content_type="application/json") self.user = User.objects.create(id=2, email='*****@*****.**', number='9123456700', password=bcrypt.hashpw( 'user'.encode('utf-8'), bcrypt.gensalt()).decode(), role=User.ROLE_CHOICES.USER, created_on=timezone.now()) user_jwt_token = get_jwt(self.user) self.user_auth_headers = { 'HTTP_AUTHORIZATION': 'Bearer ' + user_jwt_token } mechanic_jwt_token = get_jwt( User.objects.get(email=self.mechanic['email'])) self.mechanic_auth_headers = { 'HTTP_AUTHORIZATION': 'Bearer ' + mechanic_jwt_token } self.vehicle = Vehicle.objects.create(pincode='1234', vin='9NFXO86WBWA082766', year='2020', status='ACTIVE', owner=self.user) self.contact_mechanic_request_body = { 'mechanic_api': 'https://www.google.com', 'repeat_request_if_failed': True, 'number_of_repeats': 5, 'mechanic_code': self.mechanic['mechanic_code'], 'vin': self.vehicle.vin, 'problem_details': 'My Car is not working', }
def setUp(self): """ stores a sample request body for mechanic signup creates a dummy mechanic corresponding auth tokens :return: None """ self.client = Client() self.mechanic = get_sample_mechanic_data() self.client.post('/api/mechanic/signup', self.mechanic, content_type="application/json") self.user = User.objects.get(email=self.mechanic['email']) jwt_token = get_jwt(self.user) self.auth_headers = {'HTTP_AUTHORIZATION': 'Bearer ' + jwt_token}
def test_expired_jwt_token(self): """ creates a dummy mechanic through mechanic signup should get a valid response on signup tries to access secure api with an expired token should get an error response saying token expired :return: None """ self.client.post('/api/mechanic/signup', self.mechanic, content_type="application/json") user = User.objects.get(email=self.mechanic['email']) res = self.client.get('/api/mechanic') self.assertNotEqual(res.status_code, 200) jwt_token = get_jwt(user, exp=datetime.utcnow() - timedelta(hours=3)) auth_headers = {'HTTP_AUTHORIZATION': 'Bearer ' + jwt_token} res = self.client.get('/api/mechanic', **auth_headers) self.assertNotEqual(res.status_code, 200) self.assertEqual(res.json()['message'], messages.TOKEN_EXPIRED)