class UserRegistration(Resource): ''' creates a user in the database ''' def __init__(self): self.data_base = User() def post(self): ''' adds user to the user list ''' data, errors = UserSchema().load(request.get_json()) if errors: return errors if self.data_base.find_if_exists('username', data['username']): return 'username already taken' if self.data_base.find_if_exists('email', data['email']): return 'email already in use' user = { 'firstname': data['firstname'], 'lastname': data['lastname'], 'email': data['email'], 'username': data['username'] if data['username'] else data['email'], 'password': data['password'], } user = self.data_base.add_user(user) response = UserSchema(exclude=['password']).dump(user)[0] return {'data': response, 'message': 'Successfully created user'}, 201
def post(self): data = request.get_json() email = InputValidator.valid_email(data['email'].strip()) password = data['password'].strip() payload = ['email', 'password'] for item in data.keys(): if item not in payload: return { "message": f"The field {item} is not a valid field" }, 400 if email and password: if User.fetch_single_user(email): current_user = User.fetch_single_user(email) if User.verify_hash(password, current_user["password"]): access_token = create_access_token( identity=email, expires_delta=datetime.timedelta(hours=24)) return { 'mesage': f'Logged in as {current_user["email"]}', 'access_token': access_token, }, 200 return {'message': 'Invalid password'}, 400 return {'message': 'User does not exist'}, 404 return {"message": "Enter email and password to login"}, 400
def setUp(self): app = create_app('testing') self.app_context = app.app_context() self.app_context.push() self.client = app.test_client() self.data_base = User() migrate() self.sample_user = dict(firstname="test_first", lastname="test_last", email="*****@*****.**", username="******", password="******")
def setup(self): self.app = create_app("config.TestingConfig").test_client() test_user_data = { "email": "*****@*****.**", "username": "******", "password": "******" } test_user = User(test_user_data['email'], test_user_data['username'], test_user_data['password']) test_user.signup() access_token = create_access_token(identity=test_user_data['username'], expires_delta=False) self.headers = {'Authorization': 'Bearer ' + access_token}
class Userlogin(Resource): ''' define user login ''' def __init__(self): self.data_base = User() def post(self): ''' logins user ''' data, errors = UserSchema(only=('username', 'password')).load(request.get_json()) if errors: return errors users = self.data_base.for_where('username', data['username']) if not users: return {'error_message': 'not users'}, 404 user = users[0] return { 'access_token': create_access_token(UserSchema(exclude=['password']).dump(user)[0], expires_delta=False), 'refresh_token': create_refresh_token( UserSchema(exclude=['password']).dump(user)[0]), 'user': UserSchema(exclude=['id', 'password']).dump(user)[0], 'message': 'Successfully logged in' }, 200
class UserTest(unittest.TestCase): ''' test ''' def setUp(self): app = create_app('testing') self.app_context = app.app_context() self.app_context.push() self.client = app.test_client() self.data_base = User() migrate() self.sample_user = dict(firstname="test_first", lastname="test_last", email="*****@*****.**", username="******", password="******") def tearDown(self): truncate() def test_it_registers_user(self): # act response = self.client.post('api/v1/auth/signup', json=self.sample_user) # assert self.assertEqual(201, response.status_code) self.assertEqual(self.sample_user['username'], response.get_json()['data']['username']) self.assertEqual(self.sample_user['email'], response.get_json()['data']['email']) self.assertNotIn('password', response.get_json()['data']) def test_it_logs_in_user(self): # setup self.data_base.add_user(self.sample_user) # act response = self.client.post('api/v1/auth/login', json=self.sample_user) # assert data = response.get_json() self.assertEqual(response.status_code, 200) self.assertEqual(data['message'], 'Successfully logged in') self.assertIn('access_token', data) self.assertIn('refresh_token', data)
def post(self): data = request.get_json() email = InputValidator.valid_email(data['email'].strip()) username = data['username'].strip() password = data['password'].strip() payload = ['email', 'username', 'password'] for item in data.keys(): if item not in payload: return { "message": f"The field {item} is not a valid field" }, 400 if not email: return {"message": "Please enter a valid email"}, 400 elif not username: return {"message": "Please enter a username"}, 400 elif not password: return {"message": "please enter password"}, 400 else: if not User.fetch_single_user(email): new_user = User(email=data['email'], password=User.generate_hash(data['password'])) new_user.save_user() return {"message": f'User {data["email"]} was created'}, 201 return {"message": f"User {email} already exists"}, 400 return { "message": 'Please enter an email address and password', }, 400
def __init__(self): self.data_base = User()