def test_create_cultural_heritage_item_with_time(self): item_data = { "title": "Very emotional thresh hook", 'start_year': 1512, 'end_year': 1571 } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] response = self.client.get( self.cultural_heritage_item_url + str(id) + '/', format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertAlmostEqual(float(response_content['start_year']), 1512) self.assertAlmostEqual(float(response_content['end_year']), 1571)
def test_create_cultural_heritage_item_with_comment(self): item_data = { "title": "Very emotional thresh hook", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) response_content = json.loads(smart_text(response.content)) id = response_content['id'] self.assertEqual(response.status_code, 201) text = 'That is a nice heritage item' item_data = { 'comment': { 'text': text, } } response = self.client.post( self.cultural_heritage_item_url + str(id) + '/comment', item_data, format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( self.cultural_heritage_item_url + str(id) + '/', format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['comments']), 1) self.assertEqual(response_content['comments'][0]['text'], text) self.assertEqual(response_content['id'], id)
def test_recommendation_with_time(self): item_data = { "title": "Draven support", 'tags': [{ 'name': 'lol' }, { 'name': 'support' }], 'longitude': '23.523', 'latitude': '43.232', 'start_year': 1500, 'end_year': 1700, } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] item_data = { "title": "zoe support", 'tags': [{ 'name': 'lol' }, { 'name': 'support' }], 'longitude': '40.523', 'latitude': '52.232', } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = { "title": "istanbul galata tower", 'longitude': '25.523', 'latitude': '48.232', 'start_year': 1500, 'end_year': 1700, } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = { "item_id": id, } response = self.client.get('/cultural_heritage_item/recommendation', item_data, format='json') response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['results'][0]['title'], 'istanbul galata tower')
def test_signup_default_profile_picture(self): """ Ensure JWT signup works using JSON POST . """ data = { 'email': "*****@*****.**", 'username': "******", 'password': self.password, 'confirm_password': self.password, } response = self.client.post(self.sigun_url, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 201) self.login_data_with_username['username_or_email'] = "Suleiman" response = self.client.post( self.login_url, json.dumps(self.login_data_with_username), content_type='application/json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) token = response_content['token'] response = APIClient().get('/api/auth/me/', HTTP_AUTHORIZATION='JWT ' + token) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 200) self.assertEqual(response_content['username'], "Suleiman") self.assertEqual(response_content['email'], "*****@*****.**") self.assertTrue( response_content['profile_picture'] in settings.PROFILE_PICTURES)
def test_cultural_heritage_item_favorited_amount(self): item_data = { "title": "Ahri mid montage", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) response_content = json.loads(smart_text(response.content)) id = response_content['id'] self.assertEqual(response.status_code, 201) response = self.client.post( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) self.assertEqual(response.status_code, 201) response = self.client2.post( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( self.cultural_heritage_item_url + str(id), format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['favorited_amount'], 2)
def test_account_update_with_firstname_and_picture(self): response = self.client.post( self.login_url, json.dumps(self.login_data_with_username), content_type='application/json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) token = response_content['token'] data = { 'profile_picture': 'http://i.imgur.com/3OLTFVq.jpg', 'firstname': 'hayri', } response = APIClient().patch(self.update_profile + self.username + '/', json.dumps(data), content_type='application/json', HTTP_AUTHORIZATION='JWT ' + token) self.assertEqual(response.status_code, 200) response = APIClient().get('/api/auth/me', HTTP_AUTHORIZATION='JWT ' + token) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['profile_picture'], 'http://i.imgur.com/3OLTFVq.jpg') self.assertEqual(response_content['firstname'], 'hayri')
def test_get_cultural_heritage_item(self): title = 'Very emotional thresh hook' item_data = { "title": title, } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = { "title": "Draven montage", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( self.cultural_heritage_item_url + "?limit=1", format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['results']), 1) response = self.client.get( self.cultural_heritage_item_url + "?limit=1&offset=1", format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['results']), 1)
def test_list_user_items_with_another_user(self): title = 'Very emotional thresh hook' item_data = { "title": title, } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( self.my_items_url, format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['results'][0]['title'], title) self.client.logout() self.client.login(username=self.username2, password=self.password2) response = self.client.get( self.my_items_url, format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['results']), 0)
def test_get_tags(self): item_data = { "title": "Space needle", 'tags': [{ 'name': 'place1', }, { 'name': 'Seattle1' }, { 'name': 'space1' }, { 'name': 'Needle1' }, { 'name': 'downtown1' }] } item_data2 = { "title": "Discovery park", 'tags': [{ 'name': 'place', }, { 'name': 'Seattle' }, { 'name': 'space' }, { 'name': 'Needle' }, { 'name': 'downtown' }] } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response = self.client.post( self.cultural_heritage_item_url, item_data2, format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( self.tags_url, format='json', ) # test endpoint flexibility response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content), 10) response = self.client.get( '/tags', format='json', ) response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content), 10)
def test_item_visit_time(self): title = 'Very emotional thresh hook' item_data = { "title": title, "tags": [ { "name": "talha", "name": "thresh", } ] } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] item_data = { 'cultural_heritage_item': id, 'duration': 5 } response = self.client.put( '/user/visit_time', item_data, format='json', ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 200) self.assertEqual(response_content['duration'], 5) response = self.client.put( '/user/visit_time', item_data, format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['duration'], 10) response = self.client2.put( '/user/visit_time', item_data, format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['duration'], 5)
def test_cultural_heritage_search_with_guest_user(self): item_data = { "title": "Draven support", 'tags': [{ 'name': 'adc', }, { 'name': 'meta' }], 'place_name': 'meta', 'latitude': '22.12', 'longitude': '23.14' } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = { "title": "Zoe support", 'latitude': '26.12', 'longitude': '27.14' } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = { "title": "Tresh hook meta", 'tags': [{ 'name': 'Meta' }, { 'name': 'support' }] } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) self.client.logout() response = self.client.get(self.cultural_heritage_item_url + 'search/' + 'adc') response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['results']), 1) response = self.client.get(self.cultural_heritage_item_url + 'search/' + 'meta') response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['results']), 3)
def test_me_endpoint_with_loggedin_user(self): response = self.client.post( self.login_url, json.dumps(self.login_data_with_username), content_type='application/json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) token = response_content['token'] response = APIClient().get('/api/auth/me/', HTTP_AUTHORIZATION='JWT ' + token) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 200) self.assertEqual(response_content['username'], self.username) self.assertEqual(response_content['email'], self.email)
def test_get_cultural_heritage_item_by_id_with_image_media_items(self): item_data = { "title": "Very emotional thresh hook", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] # First image item image_item_data1 = { 'images': [ { 'url': 'http://i.imgur.com/3OLTFVq.jpg', 'main': True }, ] } response = self.client.post( self.cultural_heritage_item_url + str(id) + '/image', image_item_data1, format='json', ) self.assertEqual(response.status_code, 201) # Second image item image_item_data2 = { 'images': [{ 'url': 'http://i.imgur.com/3OL28374TFVq.jpg', }] } response = self.client.post( self.cultural_heritage_item_url + str(id) + '/image', image_item_data2, format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( self.cultural_heritage_item_url + str(id), format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['images']), 2)
def authenticate(self, request): auth = get_authorization_header(request).split() auth_header_prefix = settings.JWT_AUTH_HEADER_PREFIX.lower() if not auth or smart_text(auth[0].lower()) != auth_header_prefix: raise exceptions.AuthenticationFailed() if len(auth) == 1: msg = 'Invalid Authorization header. No credentials provided.' raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: msg = ('Invalid Authorization header. Credentials string ' 'should not contain spaces.') raise exceptions.AuthenticationFailed(msg) try: payload = jwt_decode_handler(auth[1]) except jwt.ExpiredSignature: msg = 'Signature has expired.' raise exceptions.AuthenticationFailed(msg) except jwt.DecodeError: msg = 'Error decoding signature.' raise exceptions.AuthenticationFailed(msg) user = self.authenticate_credentials(payload) return (user, auth[1])
def test_item_popularity_num_favorites(self): response = self.client.post(self.cultural_heritage_item_url, {'title': 'Lee Sin'}, format='json') self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) response = self.client.post( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) self.assertEqual(response.status_code, 201) item = Cultural_Heritage.objects.all()[0] expected_popularity = log(1 + COEFF_NUM_FAVORITES) self.assertAlmostEqual(popularity_score(item), expected_popularity) response = self.client.delete( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) self.assertEqual(response.status_code, 204) item = Cultural_Heritage.objects.all()[0] self.assertEqual(popularity_score(item), 0)
def test_account_update_with_new_password(self): response = self.client.post( self.login_url, json.dumps(self.login_data_with_username), content_type='application/json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) token = response_content['token'] data = { 'old_password': '******', 'password': '******', 'confirm_password': '******' } response = APIClient().patch(self.update_profile + self.username + '/', json.dumps(data), content_type='application/json', HTTP_AUTHORIZATION='JWT ' + token) self.assertEqual(response.status_code, 200) self.login_data_with_username['password'] = '******' response = self.client.post( self.login_url, json.dumps(self.login_data_with_username), content_type='application/json', ) self.assertEqual(response.status_code, 200)
def test_item_popularity_num_comments(self): response = self.client.post(self.cultural_heritage_item_url, {'title': 'Lee Sin'}, format='json') self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] item = Cultural_Heritage.objects.all()[0] self.assertEqual(popularity_score(item), 0) num_comments = 6 for i in range(num_comments): response = self.client.post( self.cultural_heritage_item_url + str(id) + '/comment', {'comment': { 'text': 'Comment {}'.format(i) }}, format='json') self.assertEqual(response.status_code, 201) item = Cultural_Heritage.objects.all()[0] expected_popularity = log(1 + num_comments * COEFF_NUM_COMMENTS) self.assertAlmostEqual(popularity_score(item), expected_popularity)
def test_favorite_cultural_heritage_item_twice(self): item_data = { "title": "Ahri mid montage", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) response_content = json.loads(smart_text(response.content)) id = response_content['id'] self.assertEqual(response.status_code, 201) self.client.post( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) # Here we ensure that the same user cannot favorite the same item more than once. response = self.client.post( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) self.assertEqual(response.status_code, 400)
def test_create_cultural_heritage_item_with_tags(self): item_data = { "title": "Space needle", 'tags': [{ 'name': 'place', }, { 'name': 'Seattle' }, { 'name': 'space' }, { 'name': 'Needle' }, { 'name': 'downtown' }] } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 201) id = response_content['id'] response = self.client.get( self.cultural_heritage_item_url + str(id), format='json', ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response_content['tags']), 5)
def test_delete_cultural_heritage_item(self): title = 'Very emotional thresh hook' item_data = { "title": title, } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] response = self.client.delete( self.cultural_heritage_item_url + str(id) + '/', format='json', ) self.assertEqual(response.status_code, 204) response = self.client.get( self.cultural_heritage_item_url + str(id) + '/', format='json', ) self.assertEqual(response.status_code, 404)
def test_create_cultural_heritage_item_with_many_images_at_once(self): item_data = { "title": "Space needle", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] image_item_data = { 'images': [ { 'url': 'http://i.imgur.com/1113OLTFsdfVq.jpg', }, { 'url': 'http://i.imgur.com/111r3OLTF21Vq.jpg', }, { 'url': 'http://i.imgur.com/111e3OLT3213FVq.jpg', }, { 'url': 'http://i.imgur.com/1113wO12LTFVq.jpg', }, { 'url': 'http://i.imgur.com/1113weOLTFVq.jpg', }, ] } response = self.client.post( self.cultural_heritage_item_url + str(id) + '/image', image_item_data, format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( self.cultural_heritage_item_url + str(id), format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['images']), 5)
def test_update_cultural_heritage_item(self): title = 'Very emotional thresh hook' item_data = { "title": title, "tags": [{ "name": "talha", "name": "thresh", }] } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] item_data = { "comments": [], "description": "new description", "id": id, "images": [], "public_accessibility": True, "tags": [{ "name": "tag" }], "title": "Title 7jaaa", # "user": 2 Since we wont change the user } response = self.client.patch( self.cultural_heritage_item_url + str(id) + '/', item_data, format='json', ) self.assertEqual(response.status_code, 200) response = self.client.get( self.cultural_heritage_item_url + str(id) + '/', format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['title'], 'Title 7jaaa') self.assertEqual(response_content['tags'][0]['name'], 'tag')
def test_nearby_search(self): item_data = { "title": "Draven montage", "longitude": "23.123523", "latitude": "21.123523", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = {"title": "Elo"} response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = { "title": "thresh montage", "longitude": "50.123523", "latitude": "23.123523", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) item_data = { "title": "Ahri montage", "longitude": "-87.123523", "latitude": "-85.123523", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) location_data = { 'longitude': "12.51231", 'latitude': "11.51231", } response = self.client.get('/nearby_items', location_data, format='json') response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['results'][0]['title'], 'Draven montage') self.assertEqual(response_content['results'][1]['title'], 'thresh montage') self.assertEqual(response_content['results'][2]['title'], 'Ahri montage')
def test_get_favorite_items(self): item_data = { "title": "Ahri mid montage", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 201) response = self.client.post( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) self.assertEqual(response.status_code, 201) item_data = { "title": "Urgot top montage", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) response = self.client.post( '/user/cultural_heritage_item/' + str(response_content['id']) + '/favorite/', format='json', ) self.assertEqual(response.status_code, 201) response = self.client.get( '/user/cultural_heritage_item/favorite/', format='json', ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 200) self.assertEqual(len(response_content['results']), 2) self.assertEqual(response_content['results'][0]['title'], 'Ahri mid montage')
def test_recommendation_response_not_contains_the_same_item(self): item_data = { "title": "Draven support", } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] item_data = { "item_id": id, } response = self.client.get('/cultural_heritage_item/recommendation', item_data, format='json') response_content = json.loads(smart_text(response.content)) self.assertEqual(len(response_content['results']), 0)
def test_get_cultural_heritage_item_by_id(self): title = 'Very emotional thresh hook' item_data = {"title": title, 'place_name': 'Seattle'} response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) self.assertEqual(response.status_code, 201) response_content = json.loads(smart_text(response.content)) id = response_content['id'] response = self.client.get( self.cultural_heritage_item_url + str(id), format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['title'], title) self.assertEqual(response_content['place_name'], 'Seattle') self.assertEqual(response_content['id'], id)
def test_account_update_regex(self): response = self.client.post( self.login_url, json.dumps(self.login_data_with_username), content_type='application/json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) token = response_content['token'] response = APIClient().patch('/api/auth/me/' + self.username + '/', HTTP_AUTHORIZATION='JWT ' + token) self.assertEqual(response.status_code, 200)
def test_jwt_login_json_with_username(self): """ Ensure JWT login view using JSON POST works with only username and password. """ response = self.client.post(self.login_url, json.dumps(self.login_data_with_username), content_type='application/json') self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) decoded_payload = utils.jwt_decode_handler(response_content['token']) self.assertEqual(decoded_payload['username'], self.username)
def test_post_json_failing_jwt_auth(self): """ Ensure POSTing json over JWT auth without correct credentials fails """ response = self.client.post('/jwt/', content_type='application/json') response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') expected_error = ['Incorrect authentication credentials.'] self.assertEqual(response_content['errors'], expected_error)
def test_cultural_heritage_item_favorited_amount_field_not_editable(self): item_data = { "title": "Ahri mid montage", "favorited_amonut": 10, } response = self.client.post( self.cultural_heritage_item_url, item_data, format='json', ) response_content = json.loads(smart_text(response.content)) id = response_content['id'] self.assertEqual(response.status_code, 201) response = self.client.get( self.cultural_heritage_item_url + str(id), format='json', ) self.assertEqual(response.status_code, 200) response_content = json.loads(smart_text(response.content)) self.assertEqual(response_content['favorited_amount'], 0)
def post(self, request, *args, **kwargs): try: request_json = json.loads(smart_text(request.body)) except ValueError: return self.render_bad_request_response() form = JSONWebTokenForm(request_json) if not form.is_valid(): return self.render_bad_request_response({'errors': form.errors}) context_dict = { 'token': form.object['token'] } return self.render_response(context_dict)
def test_jwt_login_json(self): """ Ensure JWT login view using JSON POST works. """ response = self.client.post( '/auth-token/', json.dumps(self.data), content_type='application/json' ) response_content = json.loads(smart_text(response.content)) decoded_payload = utils.jwt_decode_handler(response_content['token']) self.assertEqual(response.status_code, 200) self.assertEqual(decoded_payload['username'], self.username)
def test_post_invalid_jwt_header_failing_jwt_auth(self): """ Ensure POSTing over JWT auth without correct credentials fails """ auth = 'Bearer abc abc' response = self.client.post( '/jwt/', content_type='application/json', HTTP_AUTHORIZATION=auth ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') expected_error = ['Invalid Authorization header. Credentials string should not contain spaces.'] self.assertEqual(response_content['errors'], expected_error)
def test_post_invalid_token_failing_jwt_auth(self): """ Ensure POSTing over JWT auth with invalid token fails """ auth = 'Bearer abc123' response = self.client.post( '/jwt/', content_type='application/json', HTTP_AUTHORIZATION=auth ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') expected_error = ['Error decoding signature.'] self.assertEqual(response_content['errors'], expected_error)
def test_post_json_passing_jwt_auth(self): """ Ensure POSTing form over JWT auth with correct credentials passes and does not require CSRF """ payload = utils.jwt_payload_handler(self.user) token = utils.jwt_encode_handler(payload) auth = 'Bearer {0}'.format(token) response = self.client.post( '/jwt/', content_type='application/json', HTTP_AUTHORIZATION=auth ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 200) self.assertEqual(response_content['username'], self.username)
def test_post_expired_token_failing_jwt_auth(self): """ Ensure POSTing over JWT auth with expired token fails """ payload = utils.jwt_payload_handler(self.user) payload['exp'] = 1 token = utils.jwt_encode_handler(payload) auth = 'Bearer {0}'.format(token) response = self.client.post( '/jwt/', content_type='application/json', HTTP_AUTHORIZATION=auth ) response_content = json.loads(smart_text(response.content)) self.assertEqual(response.status_code, 401) self.assertEqual(response['WWW-Authenticate'], 'JWT realm="api"') expected_error = ['Signature has expired.'] self.assertEqual(response_content['errors'], expected_error)
def test_jwt_login_with_expired_token(self): """ Ensure JWT login view works even if expired token is provided """ payload = utils.jwt_payload_handler(self.user) payload['exp'] = 1 token = utils.jwt_encode_handler(payload) auth = 'Bearer {0}'.format(token) response = self.client.post( '/auth-token/', json.dumps(self.data), content_type='application/json', HTTP_AUTHORIZATION=auth ) response_content = json.loads(smart_text(response.content)) decoded_payload = utils.jwt_decode_handler(response_content['token']) self.assertEqual(response.status_code, 200) self.assertEqual(decoded_payload['username'], self.username)
def post(self, request): request_json = json.loads(smart_text(request.body)) print request_json token = request_json['token'] new_payload = self.refresh(token) return Response(json.dumps(new_payload), mimetype='application/json')