def test_signupWithInvalidPassword(self): """ Ensure JWT signup fails with password less than 7 chars """ data = { 'email': "*****@*****.**", 'username': "******", 'password': "******", 'confirm_password': "******", } response = self.client.post(self.sigun_url, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400) # Ensure JWT fails with password that does not have any capital letter data['password'] = "******" data['confirm_password'] = "******" response = self.client.post(self.sigun_url, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400) data['password'] = "******" data['confirm_password'] = "******" response = self.client.post(self.sigun_url, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400)
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_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_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 render_bad_request_response(self, error_dict=None): if error_dict is None: error_dict = self.error_response_dict json_context = json.dumps(error_dict, cls=self.json_encoder_class) return HttpResponseBadRequest(json_context, content_type='application/json')
def test_account_update_with_seized_username_and_valid_token(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 = {'email': '*****@*****.**'} response = APIClient().patch(self.update_profile + 'regularuser' + '/', json.dumps(data), content_type='application/json', HTTP_AUTHORIZATION='JWT ' + token) self.assertEqual(response.status_code, 404)
def test_signupWithSameEmail(self): """ Ensure JWT signup fails using JSON POST when the email is already taken . """ data = { 'email': "*****@*****.**", 'username': "******", 'password': self.password, 'confirm_password': self.password, } response = self.client.post(self.sigun_url, json.dumps(data), content_type='application/json') response = self.client.post(self.sigun_url, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400)
def render_bad_request_response(self, error_dict=None): if error_dict is None: error_dict = self.error_response_dict json_context = json.dumps(error_dict, cls=self.json_encoder_class) return HttpResponseBadRequest( json_context, content_type='application/json')
def test_jwt_login_json_missing_fields(self): """ Ensure JWT login view using JSON POST fails if missing fields. """ response = self.client.post(self.login_url, json.dumps({'username': self.username}), content_type='application/json') self.assertEqual(response.status_code, 400)
def test_jwt_login_json_missing_fields(self): """ Ensure JWT login view using JSON POST fails if missing fields. """ response = self.client.post( '/auth-token/', json.dumps({'username': self.username}), content_type='application/json' ) self.assertEqual(response.status_code, 400)
def test_jwt_login_json_bad_creds(self): """ Ensure JWT login view using JSON POST fails if bad credentials are used. """ data = {'username': self.username, 'password': '******'} response = self.client.post(self.login_url, json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 400)
def test_signup_regex(self): data = { 'email': "*****@*****.**", 'username': "******", 'password': self.password, 'confirm_password': self.password, } response = self.client.post('/api/auth/signup', json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 201)
def test_jwt_login_json_bad_creds(self): """ Ensure JWT login view using JSON POST fails if bad credentials are used. """ self.data['password'] = '******' response = self.client.post('/auth-token/', json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 400)
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 create(self, request, *args, **kwargs): try: for image_item_data in request.data['images']: request.data[ 'cultural_heritage_item'] = self.current_heritage_item.pk for k, v in image_item_data.items(): request.data[k] = v result = super(image_media_item, self).create(request, *args, **kwargs) return result except BaseException as e: return Response(json.dumps(str(e)), status=status.HTTP_400_BAD_REQUEST)
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 alertaalmacen(request): cell = worksheet.findall("Almacen") cont = 0 for i in cell: cont = cont + 1 data = json.dumps(cont) return HttpResponse(data, content_type="application/json")
def lista(request): f = Firebase('https://monitoreo.firebaseio.com/eventos/') result = f.push({'ya': 'uuu'}) print 'Result', result data_dict = ValuesQuerySetToDict('url') data = json.dumps(data_dict) return HttpResponse(data, content_type="application/json")
def dispatch(self, request, *args, **kwargs): try: request.user, request.token = self.authenticate(request) except exceptions.AuthenticationFailed as e: response = HttpResponse(json.dumps({'errors': [str(e)]}), status=401, content_type='application/json') response['WWW-Authenticate'] = self.authenticate_header(request) return response return super(JSONWebTokenAuthMixin, self).dispatch(request, *args, **kwargs)
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 get(self, request): id = request.user.id print 'User....',id print Alumno.objects.all() user = AuthUser.objects.get(id=id) nivel = user.nivel.id nivel_nombre = user.nivel.nombre colegio = user.colegio.nombre first_name = user.first_name data = json.dumps({'first_name':first_name,'username': request.user.username,'id':id,'nivel':nivel,'nivel_nombre':nivel_nombre,'colegio':colegio}) return HttpResponse(data)
def test_account_update_with_email_and_not_username(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 = {'email': '*****@*****.**', 'username': '******'} 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['email'], '*****@*****.**') self.assertEqual(response_content['username'], 'heisenberg1')
def test_jwt_refresh_json_no_orig_iat(self): """ Ensure JWT refresh view using JSON POST fails if no orig_iat is present on the payload. """ self.payload.pop('orig_iat') data = {'token': utils.jwt_encode_handler(self.payload)} response = self.client.post('/refresh-token/', json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400)
def vendidogrupo(request): print 'POST..', json.loads(request.body) x = json.loads(request.body) ''' f = open('/var/www/html/error.html', 'a') f.write(x) f.close() ''' data = json.dumps('x') return HttpResponse(data, content_type="application/json")
def test_jwt_login_json_bad_creds(self): """ Ensure JWT login view using JSON POST fails if bad credentials are used. """ self.data['password'] = '******' response = self.client.post( '/auth-token/', json.dumps(self.data), content_type='application/json' ) self.assertEqual(response.status_code, 400)
def test_signup(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)
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_signupWithProfilePicture(self): """ Ensure JWT signup works using JSON POST . """ data = { 'email': "*****@*****.**", 'username': "******", 'password': self.password, 'confirm_password': self.password, 'profile_picture': 'http://i.imgur.com/3OLTFVq.jpg', } response = self.client.post(self.sigun_url, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 201)
def post(self, request): print 'aceptaser', json.loads(request.body)[0]['id'] id_socia = Socia.objects.get(user_id=request.user.id).id ser = Servicio.objects.get(id=json.loads(request.body)[0]['id']) numero_notificacion = Cliente.objects.get( user_id=ser.cliente.user.id).numero_notificacion noticliente = ser.cliente.numero_notificacion ser.socia_id = id_socia ser.estado_id = 2 ser.save() header = { "Content-Type": "application/json; charset=utf-8", "Authorization": "Basic OGQyNTllMmUtMmY2Ny00ZGQxLWEzNWMtMjM5NTdlNjM0ZTc3" } payload = { "app_id": "6d06ccb5-60c3-4a76-83d5-9363fbf6b40a", "include_player_ids": [numero_notificacion], "contents": { "en": "Tienes una nueva chica que te atendera" }, "data": { 'aceptaservicio': json.loads(request.body)[0]['id'] } } req = requests.post("https://onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload)) print(req.status_code, req.reason) x = { 'servicio': json.loads(request.body)[0]['id'], 'detalle': 'socia-cliente' } c = simplejson.dumps(x) return HttpResponse(c, content_type="application/json")
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 dispatch(self, request, *args, **kwargs): try: request.user, request.token = self.authenticate(request) except exceptions.AuthenticationFailed as e: response = HttpResponse( json.dumps({'errors': [str(e)]}), status=401, content_type='application/json' ) response['WWW-Authenticate'] = self.authenticate_header(request) return response return super(JSONWebTokenAuthMixin, self).dispatch( request, *args, **kwargs)
def centro(request, id): cell = worksheet.find(id) date = datetime.now() - timedelta(hours=5) fila = cell.row print 'fila', fila worksheet.update_cell(fila, 5, 'centro') worksheet.update_cell(fila, 10, str(date)) data = json.dumps('x') return HttpResponse(data, content_type="application/json")
def test_jwt_refresh_json_inactive_user(self): """ Ensure JWT refresh view using JSON POST fails if the user is inactive """ self.user.is_active = False self.user.save() data = {'token': utils.jwt_encode_handler(self.payload)} response = self.client.post('/refresh-token/', json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400)
def nuevos(request): cell = worksheet.findall("Nuevo") x = [] data = dict((i.row, i.value) for i in cell) for i in data: print x.insert(i, worksheet.row_values(i)) print x data = json.dumps(x) return HttpResponse(data, content_type="application/json")
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(self.login_url, json.dumps(self.login_data_with_username), content_type='application/json', HTTP_AUTHORIZATION=auth) 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_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')
def post(self, request): data = json.dumps({'username': request.user.username}) return HttpResponse(data)
def render_response(self, context_dict): json_context = json.dumps(context_dict, cls=self.json_encoder_class) return HttpResponse(json_context, content_type='application/json')