Exemplo n.º 1
0
def load_users_from_json():
    """
    Importing JSON data to table users
    """
    json_filename = 'db/json/users.json'
    with open(json_filename, 'r', encoding='utf-8') as f:
        json_object = json.load(f)
        users = []
        for user in json_object['users']:
            # Each user is a dict
            users.append(
                User(provider='myapp',
                     social_id=User.generate_social_id(),
                     email_address=user.get('email_address'),
                     password=user.get('password')))
    # Add data to users
    db.session.add_all(users)
    # Flush the remaining changes and commit the transaction
    db.session.commit()
    # Close the Session
    db.session.close()
Exemplo n.º 2
0
 def test_signin(self):
     """
     Verify route /sign-in
     http://localhost:5000/oauth2/sign-in
     """
     # Add an existing user so we can test the sign in
     user = User(provider='myapp',
                 social_id='1',
                 email_address='*****@*****.**',
                 password='******')
     db.session.add(user)
     db.session.commit()
     response = self.client.post(url_for('oauth2.signin'),
                                 data={
                                     'email_address': '*****@*****.**',
                                     'password': '******'
                                 },
                                 follow_redirects=True)
     # self.assertTrue(response.status_code == 302)
     self.assertTrue(b'Signed in successfully' in response.data)
Exemplo n.º 3
0
def authorized_type(provider, user_type):
    remote_app = OAuth2Client.get_provider(provider)
    provider, social_id, email_address, username = remote_app.authorized(
        user_type=user_type)
    if provider is not None and social_id is not None:
        # If the social user is not known, add to our database.
        user = User.query.filter_by(provider=provider).filter_by(
            social_id=social_id).first()
        if user is None:
            user = User(provider=provider,
                        social_id=social_id,
                        email_address=email_address,
                        username=username,
                        user_type=user_type)
            db.session.add(user)
            db.session.commit()
            print("new")
        # Flask-Login login_user() function to record the user is logged in
        # for the user session.
        print("auth:", user_type)
        login_user(user)
        flash('Signed in successfully.', 'info')
        data = user_exists('canteen', social_id, user_type)
        if (data):
            session.update(data)
            if (user_type == 'customer'):
                return redirect(url_for('canteen.customer_owner_index'))
            if (user_type == 'owner'):
                return redirect(url_for('canteen.canteen_owner_owner_index'))
        else:
            session['username'] = username
            session['social_id'] = social_id
            session['email_address'] = email_address
            # return redirect(url_for('canteen.%s_form'%user_type))
            return redirect(url_for('canteen.%s_form' % user_type))
    else:

        flash('Authentication failed!', 'error')
        return redirect(url_for('main.index'))
Exemplo n.º 4
0
def authorized(provider):
    remote_app = OAuth2Client.get_provider(provider)
    provider, social_id, email_address, username = remote_app.authorized()
    if provider is not None and social_id is not None:
        # If the social user is not known, add to our database.
        user = User.query.filter_by(provider=provider).filter_by(
            social_id=social_id).first()
        if user is None:
            user = User(provider=provider,
                        social_id=social_id,
                        email_address=email_address,
                        username=username)
            db.session.add(user)
            db.session.commit()
        # Flask-Login login_user() function to record the user is logged in
        # for the user session.
        login_user(user)
        flash('Signed in successfully.', 'info')
        return redirect(url_for('main.index'))

    else:
        flash('Authentication failed!', 'error')
        return redirect(url_for('main.index'))
Exemplo n.º 5
0
 def test_password_salts_are_random(self):
     user1 = User(password='******')
     user2 = User(password='******')
     self.assertTrue(user1.password_hash != user2.password_hash)
Exemplo n.º 6
0
 def test_password_verification(self):
     user = User(password='******')
     self.assertTrue(user.verify_password('Password!'))
     self.assertFalse(user.verify_password('WrongPassword!'))
Exemplo n.º 7
0
 def test_no_password_getter(self):
     user = User(password='******')
     with self.assertRaises(AttributeError):
         user.password
Exemplo n.º 8
0
 def test_password_setter(self):
     user = User(password='******')
     self.assertTrue(user.password_hash is not None)