def post(self): user_dict = request.get_json() if not user_dict: response = {'user': '******'} return response, HttpStatus.bad_request_400.value errors = user_schema.validate(user_dict) if errors: return errors, HttpStatus.bad_request_400.value user_name = user_dict['username'] existing_user = User.query.filter_by(username=user_name).first() if existing_user is not None: response = {'user': '******'.format(user_name)} return response, HttpStatus.bad_request_400.value try: user = User(username=user_name) error_message, password_ok = \ user.check_password_strength_and_hash_if_ok(user_dict['password']) if password_ok: user.add(user) query = User.query.get(user.id) dump_result = user_schema.dump(query) return dump_result, HttpStatus.created_201.value else: return {"error": error_message}, HttpStatus.bad_request_400.value except SQLAlchemyError as e: db.session.rollback() response = {"error": str(e)} return response, HttpStatus.bad_request_400.value
def test_check_password(self): """ Test if password is checked accuratley """ details = dict( name='tester', email='*****@*****.**', password='******', ) user = User(**details) assert user.check_password(details['password']) assert not user.check_password(details['password'] + "12")
def test_check_password(self): """ Test if password is checked accuratley """ details = dict( name = 'tester', email = '*****@*****.**', password = '******', ) user = User(**details) assert user.check_password(details['password']) assert not user.check_password(details['password']+"12")
def post(self): form = UserCreateForm() if not form.validate_on_submit(): return form.errors, 422 user = User() form.populate_obj(user) user.save() login_user(user) return "Created", 201
def post(self): form = UserCreateForm() if not form.validate_on_submit(): return form.errors, 422 user = User() form.populate_obj(user) user.save() login_user(user) return "Created",201
def create_new_user(session, user_info, secret): email = user_info["email"].strip().lower() existing_user = session.query(User).filter( User.email == email).one_or_none() if existing_user: return {"success": False, "message": "User already exists."} name = user_info["name"].strip().lower() digest = create_hash(name=name, email=email, secret=secret) user = User(email=email, name=name, phone=user_info.get("phone"), hash=digest) session.add(user) session.flush() access_token = encode_auth_token(user_id=user.id) return { "success": True, "message": "User added successfully.", "data": { "user_id": user.id, "access_token": access_token["data"]["access_token"], }, }
def test_authentication(self): """ Test authentication of user """ user, authenticated = User.authenticate( self.demo_user_details["email"], self.demo_user_details["password"]) assert user.id == self.demo_user.id assert authenticated
def newuser(request): id_token = request.POST.get('TokenID') Phone = request.POST.get('PhoneNumber') Cname = request.POST.get('CName') City = request.POST.get('City') GoogleID = "734656398511-bvq0eii3pavpasv7lj8359u6s6n41gvg.apps.googleusercontent.com" url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + id_token response = requests.get(url) try: if (response.json()['iss'] in ('accounts.google.com', 'https://accounts.google.com')) and (response.json()['aud'] == GoogleID): #response['auth'] = os.environ['password'] userid = response.json()['sub'] if (response.json()['email_verified'] == "true"): email = response.json()['email'] Pname = response.json()['name'] PicURL = response.json()['picture'] u = User(Pname=Pname, Cname=Cname, City=City, UID=userid, Email=email, PicUrl=PicURL, PhoneNumber=Phone) return render(request, 'tracker/signedin.html', {}) except: return redirect('home')
def init_data(self): self.demo_user_details = dict( name=u'test', email=u'*****@*****.**', password=u'123456', ) self.demo_user = User(**self.demo_user_details) login_user(self.demo_user) self.expense_details = dict( description="Mangoes", expense_time=int(datetime.datetime.now().strftime("%s")) * 1000, amount=12.00, user=self.demo_user) self.expense = Expense(**self.expense_details) self.comment_details = dict(text="very expensive", user=self.demo_user, expense=self.expense) self.comment = Comment(**self.comment_details) db.session.add(self.comment) db.session.add(self.expense) db.session.add(self.demo_user) db.session.commit()
def test_user_creation(self): """ Test if user details are stored acurately in the database """ details = dict(name='tester', email='*****@*****.**', password='******', age=23, phone="+12377123123", sex_code=MALE) user = User(**details) user.save() user_id = user.id del (user) created_user = User.query.get(user_id) assert created_user assert created_user.name == details['name'] assert created_user.email == details['email'] assert created_user.password assert created_user.age == details['age'] assert created_user.phone == details['phone'] assert created_user.sex_code == details['sex_code']
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash(f'Konto zostało utworzone', 'success') return redirect(url_for('login')) return render_template('register.html', form=form)
def test_user_creation(self): """ Test if user details are stored acurately in the database """ details = dict( name = 'tester', email = '*****@*****.**', password = '******', age = 23, phone = "+12377123123", sex_code = MALE ) user = User(**details) user.save() user_id = user.id del(user) created_user = User.query.get(user_id) assert created_user assert created_user.name == details['name'] assert created_user.email == details['email'] assert created_user.password assert created_user.age == details['age'] assert created_user.phone == details['phone'] assert created_user.sex_code == details['sex_code']
def post(self): form = SessionCreateForm() if not form.validate_on_submit(): return form.errors, 422 user, authenticated = User.authenticate(form.email.data, form.password.data) if user and authenticated: if login_user(user): return 200 return "Login Failed", 401
def register(): if request.method == 'POST': username = request.form['username'] unhashed_password = request.form['password'] user_type = request.form['user_type'] user = User(username=username, unhashed_password=unhashed_password, admin=(True if int(user_type) == 1 else False), user_type=int(user_type)) db.session.add(user) db.session.commit() return redirect(url_for('auth.login')) return render_template('register.html')
def test_authentication(self): """ Test authentication of user """ user,authenticated = User.authenticate(self.demo_user_details["email"],self.demo_user_details["password"]) assert user.id == self.demo_user.id assert authenticated
def test_register_user(self): """Endpoint to test registering a user using User class""" user = User('Rita', 'Rhytah', 'thisisandela', 1)
def post(self, valid_data): user = User() user.username = valid_data['username'] user.email = valid_data['email'] user.password = bcrypt.generate_password_hash('12345') user.save()
def put_user(): data = json.loads(request.data) user = User(name=data["name"]) db.session.add(user) db.session.commit() return json.dumps(user.to_dict())
def post(self, valid_data): username = valid_data['username'] password = valid_data['password'] user, token = User.authorize(username, password) login_user(user) return marshal({'token':token, 'status':'success'},resource_fields)
def post(self, valid_data): username = valid_data['username'] password = valid_data['password'] user, token = User.authorize(username, password) login_user(user) return marshal({'token': token, 'status': 'success'}, resource_fields)