def login(): form = LoginForm(next=request.args.get('next')) if form.validate_on_submit(): u = User.find_by_identity(request.form.get('identity')) if u and u.authenticated(password=request.form.get('password')): # As you can see remember me is always enabled, this was a design # decision I made because more often than not users want this # enabled. This allows for a less complicated login form. # # If however you want them to be able to select whether or not they # should remain logged in then perform the following 3 steps: # 1) Replace 'True' below with: request.form.get('remember', False) # 2) Uncomment the 'remember' field in user/forms.py#LoginForm # 3) Add a checkbox to the login form with the id/name 'remember' if login_user(u, remember=True): u.update_activity_tracking(request.remote_addr) # Handle optionally redirecting to the next URL safely. next_url = request.form.get('next') if next_url: return redirect(safe_next_url(next_url)) return redirect(url_for('create.create_description')) else: flash('Identity or password is incorrect.', 'error') return render_template('user/login.html', form=form)
def test_begin_update_credentials_email_change(self): """ Update credentials but only the e-mail address. """ self.login() user = {'current_password': '******', 'email': '*****@*****.**'} response = self.client.post(url_for('user.update_credentials'), data=user, follow_redirects=True) assert_status_with_message(200, response, 'Your sign in settings have been updated.') old_user = User.find_by_identity('*****@*****.**') assert old_user is None new_user = User.find_by_identity('*****@*****.**') assert new_user is not None
def test_deliver_password_reset_email(self, token): """ Deliver a password reset email. """ with mail.record_messages() as outbox: user = User.find_by_identity('*****@*****.**') deliver_password_reset_email(user.id, token) assert len(outbox) == 1 assert token in outbox[0].body
def token(db): """ Serialize a JWS token. :param db: Pytest fixture :return: JWS token """ user = User.find_by_identity('*****@*****.**') return user.serialize_token()
def test_login_activity(self, users): """ Login successfully and update the activity stats. """ user = User.find_by_identity('*****@*****.**') old_sign_in_count = user.sign_in_count response = self.login() new_sign_in_count = user.sign_in_count assert response.status_code == 200 assert (old_sign_in_count + 1) == new_sign_in_count
def test_password_reset(self, users, token): """ Reset successful. """ reset = {'password': '******', 'reset_token': token} response = self.client.post(url_for('user.password_reset'), data=reset, follow_redirects=True) assert_status_with_message(200, response, 'Your password has been reset.') admin = User.find_by_identity('*****@*****.**') assert admin.password != 'newpassword'
def test_klingon_locale(self, users): """ Klingon locale works successfully. """ user = User.find_by_identity('*****@*****.**') user.locale = 'kl' user.save() self.login() response = self.client.get(url_for('billing.purchase_coins')) # Klingon for "Card". assert_status_with_message(200, response, 'Chaw')
def ensure_identity_exists(form, field): """ Ensure an identity exists. :param form: wtforms Instance :param field: Field being passed in :return: None """ user = User.find_by_identity(field.data) if not user: raise ValidationError('Unable to locate account.')
def test_invoice_create(self, users, mock_stripe): """ Successfully create an invoice item. """ user = User.find_by_identity('*****@*****.**') invoice = Invoice() invoice.create(user=user, currency='usd', amount='4000', credits=25, coupon=None, token='cus_000') assert user.credits == 45
def test_cancel_subscription(self, subscriptions, mock_stripe): """ User subscription gets cancelled. """ user = User.find_by_identity('*****@*****.**') params = {'id': user.id} self.login() response = self.client.post(url_for('admin.users_cancel_subscription'), data=params, follow_redirects=True) assert_status_with_message( 200, response, 'Subscription has been cancelled for Subby') assert user.cancelled_subscription_on is not None
def test_welcome_with_existing_username(self, users): """ Create username failure due to username already existing. """ self.login() u = User.find_by_identity('*****@*****.**') u.username = '******' u.save() user = {'username': '******'} response = self.client.post(url_for('user.welcome'), data=user, follow_redirects=True) assert_status_with_message(200, response, 'You already picked a username.')
def test_signup(self, users): """ Signup successfully. """ old_user_count = User.query.count() user = {'email': '*****@*****.**', 'password': '******'} response = self.client.post(url_for('user.signup'), data=user, follow_redirects=True) assert_status_with_message(200, response, 'Awesome, thanks for signing up!') new_user_count = User.query.count() assert (old_user_count + 1) == new_user_count new_user = User.find_by_identity('*****@*****.**') assert new_user.password != 'password'
def subscriptions(db): """ Create subscription fixtures. :param db: Pytest fixture :return: SQLAlchemy database session """ subscriber = User.find_by_identity('*****@*****.**') if subscriber: subscriber.delete() db.session.query(Subscription).delete() params = { 'role': 'admin', 'email': '*****@*****.**', 'name': 'Subby', 'password': '******', 'payment_id': 'cus_000' } subscriber = User(**params) # The account needs to be commit before we can assign a subscription to it. db.session.add(subscriber) db.session.commit() # Create a subscription. params = {'user_id': subscriber.id, 'plan': 'pro'} subscription = Subscription(**params) db.session.add(subscription) # Create a credit card. params = { 'user_id': subscriber.id, 'brand': 'Visa', 'last4': '4242', 'exp_date': datetime.date(2015, 6, 1) } credit_card = CreditCard(**params) db.session.add(credit_card) db.session.commit() return db
def bulkprocess(): from perciapp.blueprints.user.models import User data = request.get_json() print('bulk POST request received:') print() print(data) print() title = data['title'] gender = data['gender'] category = data['category'] subcategory = data['subcategory'] detail1 = data['detail1'] detail2 = data['detail2'] detail3 = data['detail3'] detail4 = data['detail4'] detail5 = data['detail5'] user = User.find_by_identity('*****@*****.**') params = { 'user_id': user.id, 'title': title, 'gender': gender, 'category': category, 'subcategory': subcategory, 'detail1': detail1, 'detail2': detail2, 'detail3': detail3, 'detail4': detail4, 'detail5': detail5, 'description': 'coming soon' } create = Create(**params) create.save_and_update_user(user) firsts = [ 'sent1', 'sent1_2', 'sent1_3', 'sent1_4', 'sent1_5', 'sent1_6', 'sent1_7', 'sent1_8', 'sent1_9', 'sent1_10', 'sent1_11', 'sent1_12', 'sent1_13', 'sent1_14', 'sent1_15' ] seconds = [ 'sent2', 'sent2_2', 'sent2_3', 'sent2_4', 'sent2_5', 'sent2_6', 'sent2_7', 'sent2_8', 'sent2_9', 'sent2_10', 'sent2_11', 'sent2_12', 'sent2_13', 'sent2_14', 'sent2_15' ] thirds = [ 'sent3', 'sent3_2', 'sent3_3', 'sent3_4', 'sent3_5', 'sent3_6', 'sent3_7', 'sent3_8', 'sent3_9', 'sent3_10', 'sent3_11', 'sent3_12', 'sent3_13', 'sent3_14', 'sent3_15' ] project_id = "perciapp" topic_id = "description-order" publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path(project_id, topic_id) for i in firsts: data = str(create.id) data = data.encode("utf-8") future = publisher.publish(topic_path, data, label=i, id=str(create.id)) print(future.result()) topic_id = "description-order-sent-2" publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path(project_id, topic_id) for i in seconds: data = str(create.id) data = data.encode("utf-8") future = publisher.publish(topic_path, data, label=i, id=str(create.id)) print(future.result()) topic_id = "description-order-sent-3" publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path(project_id, topic_id) for i in thirds: data = str(create.id) data = data.encode("utf-8") future = publisher.publish(topic_path, data, label=i, id=str(create.id)) print(future.result()) print() print(f"Published sent_gen messages to {topic_path}.") print() #Send edit Pub/Sub message topic_id = "description-order-edit" publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path(project_id, topic_id) data = str(create.id) data = data.encode("utf-8") future = publisher.publish(topic_path, data) print(future.result()) print() print(f"Published edit_sent message to {topic_path}.") print() #wait for import time start = time.time() # repeatedly pull description until it has generated description = Create.query.get(create.id) output = [description.description] while output == ['coming soon']: time.sleep(10) db.session.commit() description = Create.query.get(create.id) output = [description.description] now = time.time() print(description.title + ' bulk process waiting:' + str(int(now - start))) now = time.time() if now - start > 300: break from flask import jsonify return jsonify(title=title, description=output), 201