def test_create_user_follow_request(self, mock): user_1 = User(fb_id='123', email=None, name='Jake') user_2 = User(fb_id='123', email=None, name='Jane') db.session.add(user_1) db.session.add(user_2) db.session.commit() mock.return_value = user_1 resp, status_code = user_service.create_user_follow_request({"id": 2}) assert status_code == 201
def test_get_all_users(self, mock): user_1 = User(fb_id='123', email=None, name='Jake') user_2 = User(fb_id='123', email=None, name='Jane') db.session.add(user_1) db.session.add(user_2) db.session.commit() mock.return_value = User(fb_id='123', email=None, name='Albert') user_list, status_code = user_service.get_all_users() expected = [{'email': None, 'name': 'Jane', 'id': 2, 'relationship_status': None}, {'email': None, 'name': 'Jake', 'id': 1, 'relationship_status': None}] assert user_list == expected assert status_code == 200
def save_new_user(data): user = User.query.filter_by(email=data['email']).first() if not user: new_user = User(first_name=data['first_name'], last_name=data['last_name'], user_name=data['user_name'], email=data['email'], password_hash=data['password'], confirmation_code=data['confirmation_code'], confirmation_time=None, insert_ts=datetime.datetime.utcnow()) save_changes(new_user) response_object = { 'status': 'success', 'status_code': 00, 'message': 'Successfully registered.' } return response_object, 201 else: response_object = { 'status': 'fail', 'status_code': 2, 'message': 'User already exists. Please Log in.' } return response_object, 409
def test_get_user_subscribers(self, mock): user_1 = User(fb_id='123', email=None, name='Jake') user_2 = User(fb_id='123', email=None, name='Jane') user_1.followers.append(user_2) db.session.add(user_1) db.session.add(user_2) db.session.commit() mock.side_effect = [user_2, user_1] user_service.confirm_user_follow_request({"id": 1}) resp, status_code = user_service.get_user_subscribers() expected = [{'follower_id': 2, 'name': 'Jane'}] assert resp == expected assert status_code == 200
def test_encode_auth_token(self): user = User(email='*****@*****.**', password='******', registered_on=datetime.datetime.utcnow()) db.session.add(user) db.session.commit() auth_token = user.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes))
def test_user_create(self): user = User(email='*****@*****.**', name="Test Test", fb_id="12345678") db.session.add(user) db.session.commit() self.assertTrue(user.id == 1) self.assertTrue(user.email == "*****@*****.**") self.assertTrue(user.name == "Test Test")
def test_get_user_subscribedto(self, mock): user_1 = User(fb_id='123', email=None, name='Jake') user_2 = User(fb_id='123', email=None, name='Jane') user_1.followers.append(user_2) db.session.add(user_1) db.session.add(user_2) db.session.commit() mock.return_value = user_2 user_service.confirm_user_follow_request({"id": 1}) resp, status_code = user_service.get_user_subscribedto() expected = [{ "name": "Jake", "user_id": 1 }] assert resp == expected assert status_code == 200
def test_decode_auth_token(self): user = User(email='*****@*****.**', password='******', registered_on=datetime.datetime.utcnow()) db.session.add(user) db.session.commit() auth_token = User.encode_auth_token(user.id) self.assertTrue( User.decode_auth_token(auth_token.decode('utf-8')) == user.id)
def test_save_new_poll(self, mock): user_1 = User(fb_id='123', email=None, name='Jake') user_2 = User(fb_id='123', email=None, name='Jane') user_1.followers.append(user_2) db.session.add(user_1) db.session.add(user_2) db.session.commit() mock.return_value = user_1 resp, status_code = poll_service.save_new_poll(1, { "prompt": "test", "form_type": "multChoice", "resp_struct": None }) expected = {"id": 1} assert expected == resp assert status_code == 201
def test_encode_auth_token(self): user = User(email='*****@*****.**', last_name='Arthur', name='Oliver', password='******', registered_on=datetime.datetime.utcnow()) db.session.add(user) db.session.commit() auth_token = User.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes))
def test_get_a_user(self): user_1 = User(fb_id='123', email=None, name='Jake') db.session.add(user_1) db.session.commit() resp, status_code = user_service.get_a_user(1) expected = {'fb_id': '123', 'email': None, 'id': 1, 'name': 'Jake'} assert resp == expected assert status_code == 200
def save_new_user(data): user = mongo.db.user.find_one({'email': data['email']}) if user is None: new_user = User(email=data['email'], username=data['username'], password=base64.b64encode(data['password'].encode()), registered_on=datetime.datetime.utcnow()) mongo.db.user.insert_one(new_user.__dict__) return True else: return False
def post(self): """Creates a new User """ data = request.json new_user = User( email=data['email'], username=data['username'], password=data['password'], registered_on=datetime.datetime.utcnow() ) save_new_item(new_user) return Response(status=201)
def create_user(self, data): new_user = User(public_id=str(uuid.uuid4()), email=data['email']) if save(new_user): response = {'status': 'success', 'message': 'User created'} return response, 201 else: response = { 'status': 'failure', 'message': 'Failed to create user' } return response, 409
def create_user(): """ Management Script for creating users """ print("=== Create User ===") questions = [ inquirer.Text("name", message="Full Name"), inquirer.Text("username", message="Username"), inquirer.Password("password", message="Password"), inquirer.Password("password_repeat", message="Confirm Password"), inquirer.Text("tribe_id", message="Tribe ID (optional)"), inquirer.List( "role", message="Role", choices=[UserRoles.USER, UserRoles.ADMIN, UserRoles.TRIBE_ADMIN]) ] answers = inquirer.prompt(questions) name = answers.get("name") username = answers.get("username") password = answers.get("password") password_repeat = answers.get("password_repeat") tribe_id = answers.get("tribe_id") role = answers.get("role") if not tribe_id: tribe_id = None if password_repeat != password: print("Passwords do not match!") return user = User(tribe_id=tribe_id, public_id=str(uuid4()), username=username, name=name, role=role, created_on=datetime.utcnow()) user.password = password print("Do you want to add this user to the database?") print(user) answers = inquirer.prompt([inquirer.Confirm("confirm", message="Confirm")]) confirm = answers.get("confirm") if not confirm: return db.session.add(user) db.session.commit() print(f"Successfully added user: {user}")
def save_new_user(data): user = User.query.filter_by(email=data['email']).first() if not user: new_user = User(public_id=str(uuid.uuid4()), email=data['email'], username=data['username'], password=data['password'], registered_on=datetime.datetime.utcnow()) save_changes(new_user) return generate_token(new_user) else: response_object = { 'status': 'fail', 'message': 'User already exists. Please Log in.', } return response_object, 409
def save_new_user(data): """ Creates a new user """ user = User.query.filter_by(email=data['email']).first() if not user: new_user = User(email=data['email'], username=data['username'], password=data['password'], registered_on=datetime.datetime.utcnow()) save_changes(new_user) response = { 'status': 'success', 'message': 'User successfully created' } return response, 201 else: response = {'status': 'fail', 'message': 'User already exists'} return response, 409
def setUp(self): super().setUp() self.user = User(email='*****@*****.**', password='******', registered_on=datetime.datetime.utcnow()) db.session.add(self.user) self.book = Book(name='Test') db.session.add(self.user) db.session.add(self.book) db.session.commit() self.ratings_len = 5 self.ratings = [ Rating(user_id=self.user.id, book_id=self.book.id, value=randint(1, 5)) for _ in range(self.ratings_len) ] db.session.add_all(self.ratings) db.session.commit()
def create_new_user(data): user = User.query.filter_by(email=data['email']).first() if not user: new_user = User(email=data['email'], last_name=data['last_name'], name=data['name'], password=data['password'], public_id=str(uuid.uuid4()), registered_on=datetime.datetime.utcnow()) save_changes(new_user) return generate_token(new_user) else: response_object = { 'status': 'error', 'message': 'User already exists, Please log in' } return response_object, 409
def test_get_user_polls(self): user_1 = User(fb_id='123', email=None, name='Jake') poll_1_date = datetime.datetime(2019, 12, 5, 4, 10, 12, 400736) poll_2_date = datetime.datetime(2019, 12, 5, 4, 10, 12, 402306) poll_1 = Poll(owner_id=1, prompt="test1", form_type="multChoice", resp_struct=None, created_date=poll_1_date) poll_2 = Poll(owner_id=1, prompt="test2", form_type="multChoice", resp_struct=None, created_date=poll_2_date) db.session.add(user_1) db.session.add(poll_1) db.session.add(poll_2) db.session.commit() resp, status_code = poll_service.get_user_polls(1) expected = [{ 'id': 1, 'is_open': True, 'resp_struct': None, 'owner_id': 1, 'created_date': poll_1_date, 'prompt': 'test1', 'form_type': 'multChoice' }, { 'id': 2, 'is_open': True, 'resp_struct': None, 'owner_id': 1, 'created_date': poll_2_date, 'prompt': 'test2', 'form_type': 'multChoice' }] assert expected == resp assert status_code == 200
def setUp(self): super().setUp() self.user = User( email='*****@*****.**', password='******', registered_on=datetime.datetime.utcnow() ) self.writer = Writer( first_name='Test', last_name='Test' ) self.writer_books = [Book(name=f'Book{i}', authors=[self.writer]) for i in range(10)] db.session.add(self.user) db.session.add(self.writer) db.session.add_all(self.writer_books) db.session.commit() self.ratings = [Rating(user_id=self.user.id, book_id=book.id, value=randint(1, 5)) for book in self.writer_books] db.session.add_all(self.ratings) db.session.commit()
def save_new_user(data): try: access_token = data['accessToken'] debug_token_request = requests.get( current_app.config['DEBUG_TOKEN_URL'].format( access_token=access_token)) debug_token_json = debug_token_request.json() user_id = debug_token_json['data']['user_id'] user = User.query.filter_by(fb_id=user_id).first() # if user exists, just return new JWT if user: jwt = create_access_token(user.id, expires_delta=False) return dict(token=jwt), 201 user_details_request = requests.get( url=current_app.config['USER_DETAIL_URL'].format( access_token=access_token, user_id=user_id)) user_detail_json = user_details_request.json() name = user_detail_json['name'] email = None if 'email' not in user_detail_json else user_detail_json[ 'email'] fb_id = user_detail_json['id'] user = User(fb_id=fb_id, name=name, email=email) db.session.add(user) db.session.commit() jwt = create_access_token(user.id) return dict(token=jwt, expires_delta=False), 201 except Exception as e: raise ValueError(e) response_object = {'status': 'error', 'message': 'Internal Error'} return response_object, 500
def save_new_user(data): user = User.query.filter_by(email=data['email']).first() if not user: new_user = User(public_id=str(uuid.uuid4()), email=data['email'], username=data['username'], name=data['name'], admin=data.get('admin', False), password=data.get('password', None)) save_changes(new_user) response_object = { 'status': 'success', 'message': 'Successfully registered.' } return response_object, 201 else: response_object = { 'status': 'fail', 'message': 'User already exists. Please Log in.', } return response_object, 409
def save_new_user(data): user = User.query.filter_by(email=data["email"]).first() if not user: new_user = User( public_id = str(uuid.uuid4()), first_name = data["firstName"], last_name = data["lastName"], email = data["email"], username = data["username"], password = data["password"], contact_no = data["contactNo"], registered_on = datetime.datetime.utcnow(), admin = False ) Helper.save_changes(new_user) return Helper.generate_token("User", new_user) else: return Helper.return_resp_obj("fail", "User already exists. Please log in instead.", None, 409)
def save_new_user(data): user = User.query.filter_by(email=data["email"]).first() if not user: new_user = User( public_id=str(uuid.uuid4()), email=data["email"], username=data["username"], name=data["name"], admin=data.get("admin", False), password=data.get("password", None), ) save_changes(new_user) response_object = { "status": "success", "message": "Successfully registered.", } return response_object, 201 else: response_object = { "status": "fail", "message": "User already exists. Please Log in.", } return response_object, 409
def test_update_poll(self): user_1 = User(fb_id='123', email=None, name='Jake') poll_1_date = datetime.datetime(2019, 12, 5, 4, 10, 12, 400736) poll_2_date = datetime.datetime(2019, 12, 5, 4, 10, 12, 402306) poll_1 = Poll(owner_id=1, prompt="test1", form_type="multChoice", resp_struct=None, created_date=poll_1_date) poll_2 = Poll(owner_id=1, prompt="test2", form_type="multChoice", resp_struct=None, created_date=poll_2_date) db.session.add(user_1) db.session.add(poll_1) db.session.add(poll_2) db.session.commit() resp, status_code = poll_service.update_poll(1, 1, {'is_open': False}) assert status_code == 200 resp, status_code = poll_service.get_poll_by_id(1, 1) expected = { 'id': 1, 'is_open': False, 'resp_struct': None, 'owner_id': 1, 'created_date': poll_1_date, 'prompt': 'test1', 'form_type': 'multChoice' } assert resp == expected assert status_code == 200
def create_user(data): user = User.query.filter_by(telegram_id=data['telegram_id']).first() if not user: new_user = User(telegram_id=data['telegram_id'], first_name=data['first_name'], last_name=data['last_name'], registration_id=data['registration_id'], is_admin=data['is_admin']) save_changes(new_user) response_object = { 'status': 'success', 'message': 'You are now registered with Mento.', 'telegram_id': new_user.telegram_id, } return response_object, 201 else: if not user.is_admin and data["is_admin"]: user.first_name = data['first_name'] user.last_name = data['last_name'] user.registration_id = data['registration_id'] user.is_admin = data['is_admin'] save_changes(user) response_object = { 'status': 'success', 'message': 'You now have the admin priviledge.', 'telegram_id': user.telegram_id, } return response_object, 201 user.first_name = data['first_name'] user.last_name = data['last_name'] user.registration_id = data['registration_id'] save_changes(user) response_object = { 'status': 'success', 'message': 'You have successfully updated your details.', } return response_object, 201
def test_registered_with_already_registered_user(self): """ Test registration with already registered email""" user = User( email='*****@*****.**', username='******', password='******', registered_on=datetime.datetime.utcnow() ) mongo.db.user.save(user.__dict__) with self.client: response = self.client.post( '/auth/register', data=json.dumps(dict( email='*****@*****.**', password='******', )), content_type='application/json' ) data = json.loads(response.data.decode()) self.assertTrue(data['status'] == 'fail') self.assertTrue( data['message'] == 'User already exists. Please Log in.') self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 409)