Ejemplo n.º 1
0
    def setUp(self):
        global pulse_cfg

        self.proc = None
        self.publisher = None
        self.guardian = PulseGuardian(warn_queue_size=TEST_WARN_SIZE,
                                      del_queue_size=TEST_DELETE_SIZE,
                                      emails=False)

        dbinit.init_and_clear_db()

        self.consumer_cfg = pulse_cfg.copy()
        self.consumer_cfg['applabel'] = str(uuid.uuid1())
        # Configure/create the test user to be used for message consumption.
        self.consumer_cfg['user'] = CONSUMER_USER
        self.consumer_cfg['password'] = CONSUMER_PASSWORD

        self.user = User.new_user(email=CONSUMER_EMAIL, admin=False)
        # As a default owner for pulse_user in some tests where an owner is not
        # provided.
        self.admin = User.new_user(email=ADMIN_EMAIL, admin=True)

        db_session.add(self.user)
        db_session.commit()

        self.pulse_user = PulseUser.new_user(
            username=CONSUMER_USER,
            password=CONSUMER_PASSWORD,
            owners=self.user)

        db_session.add(self.pulse_user)
        db_session.commit()
Ejemplo n.º 2
0
def dummy_data():
    # Dummy test users
    User.new_user(email='[email protected]')
    users = User.query.all()

    for i in xrange(4):
        RabbitMQAccount.new_user(
            username='dummy{0}'.format(i),
            password='dummy',
            owners=users[0])

    rabbitmq_accounts = RabbitMQAccount.query.all()

    # And some dummy queues
    dummy_queue = Queue(name='dummy-empty-queue', size=0, owner=rabbitmq_accounts[0])
    db_session.add(dummy_queue)
    db_session.commit()

    dummy_queue = Queue(name='dummy-non-empty-queue', size=config.warn_queue_size/5, owner=rabbitmq_accounts[0])
    db_session.add(dummy_queue)
    db_session.commit()

    dummy_queue = Queue(name='dummy-warning-queue', size=config.warn_queue_size + 1, owner=rabbitmq_accounts[1])
    db_session.add(dummy_queue)
    db_session.commit()

    dummy_queue = Queue(name='dummy-deletion-queue', size=int(config.del_queue_size * 1.2), owner=rabbitmq_accounts[2])
    db_session.add(dummy_queue)
    db_session.commit()

    # Test admin user
    User.new_user(email='[email protected]', admin=True)

    logger.info('Finished generating dummy data.')
Ejemplo n.º 3
0
    def setup_3_users(self):
        mif = "[email protected]"
        maf = "[email protected]"
        self.curr_email = CONSUMER_EMAIL
        self.all_emails = [mif, maf, self.curr_email]

        mif_u = User.new_user(email=mif, admin=False)
        maf_u = User.new_user(email=maf, admin=False)
        self.curr_user = User.new_user(email=self.curr_email, admin=False)
        self.all_users = [mif_u, maf_u, self.curr_user]
Ejemplo n.º 4
0
def auth_handler():
    # The request has to have an assertion for us to verify
    if 'assertion' not in request.form:
        return jsonify(ok=False, message="Assertion parameter missing")

    # Send the assertion to Mozilla's verifier service.
    data = dict(assertion=request.form['assertion'],
                audience=config.persona_audience)
    resp = requests.post(config.persona_verifier, data=data, verify=True)

    # Did the verifier respond?
    if resp.ok:
        # Parse the response
        verification_data = resp.json()
        if verification_data['status'] == 'okay':
            email = verification_data['email']
            session['email'] = email
            session['logged_in'] = True

            user = User.query.filter(User.email == email).first()
            if user is None:
                user = User.new_user(email=email)

            if user.pulse_users:
                return jsonify(ok=True, redirect='/')

            return jsonify(ok=True, redirect='/register')

    # Oops, something failed. Abort.
    error_msg = "Couldn't connect to the Persona verifier ({0})".format(
        config.persona_verifier)
    logging.error(error_msg)
    return jsonify(ok=False, message=error_msg)
Ejemplo n.º 5
0
def callback_handling():
    """
    Callback from Auth0

    Auth0 will call back to this endpoint once it has acquired a user in some
    capacity (from Google, or their email).  Here we verify that what we got
    back is consistent with Auth0, then match that against our internal DB and
    either find the user, or create it.
    """

    # Validate what we got in the request against the Auth0 backend.
    code = request.args.get('code')
    json_header = {'content-type': 'application/json'}
    token_url = "https://{domain}/oauth/token".format(domain=config.auth0_domain)
    token_payload = {
      'client_id': config.auth0_client_id,
      'client_secret': config.auth0_client_secret,
      'redirect_uri': config.auth0_callback_url,
      'code': code,
      'grant_type': 'authorization_code'
    }
    token_info = requests.post(token_url,
                               data=json.dumps(token_payload),
                               headers=json_header).json()
    user_url = "https://{domain}/userinfo?access_token={access_token}".format(
        domain=config.auth0_domain, access_token=token_info['access_token'])
    resp = requests.get(user_url)

    # Failure to authenticate is handled in the Auth0 lock, so if the user
    # say, gives the wrong code for their email, the Auth0 lock will notify
    # them.  We don't get a response in the callback here until the user has
    # satisfied Auth0.  The only error we will see is if we can't reach Auth0
    # for verification.
    if resp.ok:
        # Parse the response
        user_info = resp.json()

        # find the user in our DB, or create it.
        email = user_info['email']
        session['email'] = email
        session['logged_in'] = True

        user = User.query.filter(User.email == email).first()
        if user is None:
            user = User.new_user(email=email)

        if user.pulse_users:
            return redirect('/')

        return redirect('/register')

    # Oops, something failed. Abort.
    error_msg = "Error verifying with Auth0 ({})".format(config.auth0_domain)

    logging.error(error_msg)
    logging.error(resp.text)
    # Add this message to the "flash message" list so that the '/' template
    # can display it.
    flash(error_msg)
    return redirect('/')
Ejemplo n.º 6
0
    def test_user(self):
        user = User.new_user(email='[email protected]', admin=False)
        db_session.add(user)
        db_session.commit()

        rabbitmq_account = RabbitMQAccount.new_user(username='dummy',
                                                    password='DummyPassword',
                                                    owners=user,
                                                    create_rabbitmq_user=False)
        db_session.add(rabbitmq_account)
        db_session.commit()

        self.assertTrue(user in User.query.all())

        # Emails are normalized by putting them lower-case
        self.assertEqual(
            User.query.filter(User.email == '[email protected]').first(), user)
        self.assertEqual(
            RabbitMQAccount.query.filter(
                RabbitMQAccount.username == 'dummy').first(),
            rabbitmq_account)
        self.assertEqual(
            RabbitMQAccount.query.filter(
                RabbitMQAccount.username == 'DUMMY').first(),
            None)
Ejemplo n.º 7
0
    def test_user_set_admin(self):
        user = User.new_user(email='[email protected]', admin=False)
        db_session.add(user)
        db_session.commit()

        user = User.query.filter(User.email == '[email protected]').first()
        user.set_admin(True)

        userDb = User.query.filter(User.email == '[email protected]').first()

        self.assertTrue(userDb.admin)
Ejemplo n.º 8
0
def load_fake_account(fake_account):
    """Load fake user and setup session."""

    # Set session user.
    session['userinfo'] = {'email': fake_account}
    session['fake_account'] = True

    # Check if user already exists in the database, creating it if not.
    g.user = User.query.filter(User.email == fake_account).first()
    if g.user is None:
        g.user = User.new_user(email=fake_account)
Ejemplo n.º 9
0
def load_user():
    """Loads the currently logged-in user (if any) to the request context."""

    # Check if fake account is set and load user.
    if fake_account:
        load_fake_account(fake_account)
        return

    email = session.get('email')
    if not email:
        g.user = None
        return

    g.user = User.query.filter(User.email == session.get('email')).first()
    if not g.user:
        g.user = User.new_user(email=email)
Ejemplo n.º 10
0
    def setUp(self):
        global pulse_cfg

        self.proc = None
        self.publisher = None
        self.management_api = PulseManagementAPI(
            management_url='http://{}:{}/api'.format(
                pulse_cfg['host'], pulse_cfg['management_port']),
            user=pulse_cfg['user'],
            password=pulse_cfg['password']
        )
        self.guardian = PulseGuardian(self.management_api,
                                      warn_queue_size=TEST_WARN_SIZE,
                                      del_queue_size=TEST_DELETE_SIZE,
                                      emails=False)

        # Hack in a test config.
        dbinit.pulse_management = self.management_api
        dbinit.init_and_clear_db()

        self.consumer_cfg = pulse_cfg.copy()
        self.consumer_cfg['applabel'] = str(uuid.uuid1())
        # Configure/create the test user to be used for message consumption.
        self.consumer_cfg['user'] = CONSUMER_USER
        self.consumer_cfg['password'] = CONSUMER_PASSWORD

        self.user = User.new_user(email=CONSUMER_EMAIL, admin=False)

        db_session.add(self.user)
        db_session.commit()

        self.pulse_user = PulseUser.new_user(
            username=CONSUMER_USER,
            password=CONSUMER_PASSWORD,
            owner=self.user,
            management_api=self.management_api)

        db_session.add(self.pulse_user)
        db_session.commit()
Ejemplo n.º 11
0
    def test_user(self):
        user = User.new_user(email='[email protected]', admin=False)
        db_session.add(user)
        db_session.commit()

        pulse_user = PulseUser.new_user(username='dummy',
                                        password='DummyPassword',
                                        owner=user,
                                        management_api=None)
        db_session.add(pulse_user)
        db_session.commit()

        self.assertTrue(user in User.query.all())

        # Emails are normalized by putting them lower-case
        self.assertEqual(
            User.query.filter(User.email == '[email protected]').first(), user)
        self.assertEqual(
            PulseUser.query.filter(PulseUser.username == 'dummy').first(),
            pulse_user)
        self.assertEqual(
            PulseUser.query.filter(PulseUser.username == 'DUMMY').first(),
            None)