def setUp(self): self.app = app self.ctx = self.app.app_context() self.ctx.push() db.drop_all() db.create_all() # Creating the 1st user. This user will issue a request user = User(username=self.default_username) user.set_password_hash(self.default_password) db.session.add(user) db.session.commit() self.client = TestClient(self.app, user.generate_auth_token(), '') # Create a request for 1st user request_data = { 'meal_type': 'Vietnamese', 'meal_time': 'Dinner', 'location_string': 'San Francisco' } rv, json = self.client.post(API_VERSION + '/requests/', data=request_data) self.request_location = rv.headers['Location'] # Create the 2nd user. This user will make proposal for the request by # 1st user user_2 = User(username='******') user_2.set_password_hash('123456') db.session.add(user_2) db.session.commit() self.client = TestClient(self.app, user_2.generate_auth_token(), '')
def create_user(): data = request.get_json() if data['username']: username = data['username'] if data['email']: email = data['email'] if data['password']: password = data['password'] new_user = User( username=username, email=email, ) new_user.set_password(password) user_schema = UserSchema() user = user_schema.dump(new_user) return make_response( jsonify({ "message": "Resource added successfully", "success": True }), 201)
def setUp(self): self.app = app self.ctx = self.app.app_context() self.ctx.push() db.drop_all() db.create_all() user = User(username=self.default_username) user.set_password_hash(self.default_password) db.session.add(user) db.session.commit() self.client = TestClient(self.app, user.generate_auth_token(), '')
def test_password_hashing(): with app.app_context(): db.create_all() new_user = User(username='******', email='*****@*****.**') password = '******' new_user.set_password(password) new_user.save() assert new_user.check_password('password') == True
def add_user(): try: username = request.json['username'] if User.query.filter_by(username=username).first(): raise ValidationError('Username already existed!') except KeyError as e: raise ValidationError('Invalid user info: missing ' + e.args[0]) user = User() user.import_data(request.json) db.session.add(user) db.session.commit() return {}, 201, {'Location': user.get_url()}
def client(): test_client = app.test_client() db.drop_all() db.create_all() user = User(username='******') user.set_password_hash('123456') db.session.add(user) db.session.commit() yield test_client """ tearDown code """ db.session.remove() db.drop_all()
def test_user_creation(): with app.app_context(): db.create_all() username = '******' email = '*****@*****.**' password = '******' new_user = User(username=username, email=email) new_user.set_password(password) new_user.save() user = User.query.filter_by(username=username).first() assert user.username == username
def oauth_callback(provider): if provider not in app.config['OAUTH_CREDENTIALS']: response = jsonify({ 'status': 401, 'error': 'provider not found', 'message': '{0} is not supported'.format(provider) }) response.status_code = 401 return response oauth = OAuthSignIn.get_provider(provider) social_id, username, email = oauth.callback() if username is not None: user_data = { 'username': username, 'email': email, 'photo_url': '', 'password': os.urandom(24) } tmp_user = User.query.filter_by(username=user_data['username']).first() if tmp_user is None: new_user = User() new_user.import_data(user_data) db.session.add(new_user) db.session.commit() tmp_user = new_user return jsonify({'token': tmp_user.generate_auth_token()}) response = jsonify({ 'status': 401, 'error': 'access denied', 'message': 'user does not authorized access' }) response.status_code = 401 return response
def create_new_user(): """ Create user endpoint --- parameters:- in: bodyname: bodyschema:id: UserSignuprequired:- username- password- emailproperties:username:type: stringdescription: Unique username of the userdefault: "Johndoe"password:type: stringdescription: Password of the user 127default: "somethingstrong"email:type: stringdescription: email of the userdefault: "*****@*****.**"responses:201:description: User successfully createdschema:id: UserSignUpSchemaproperties:code:type: string422:description: Invalid input argumentsschema:id: invalidInputproperties:code:type: stringmessage:type: string""" data = request.get_json() new_user = User(username=data['username'], email=data['email'], passwd_hash=data['passwd_hash']) new_user.hash_password(new_user.passwd_hash) new_user.create() user_schema = UserOutputSchema() user = user_schema.dump(new_user) print(user) return make_response( jsonify({ "message": "User Resource Created Successfully", "Success": True, "user": user }))
def add_user(username, password): user = User(username=username) user.set_password_hash(password) db.session.add(user) db.session.commit() print "Added {0}!".format(user.username)
from main.models.users import User from main.utils.database import db from main import app from main.config import TestConfig app.config.from_object(TestConfig) db.init_app(app) with app.app_context(): db.create_all() new_user = User(username='******', email='*****@*****.**') new_user.set_password('testpassword') new_user.save() def test_password_reset(): old_password = '******' new_password = '******' new_user.reset_password(old_password, new_password) assert new_user.check_password('newpassword') == True