def register_participant(): hunt_id = request.args['hunt_id'] hunt = Hunt.find_by_id(g.db, hunt_id) if hunt: form = ParticipantForm(request.form) if form.validate(): email = form.email.data logger.info( 'Participant registration form validated for hunt, "%s", and' ' email, %s.\nPreparing to validate participant against hunt' ' participation rules.', hunt.name, email) participant_valid, err_msg = validate_participant( g.db, email, hunt_id, hunt.participant_rule) if participant_valid: logger.info( 'The registering participant, %s, has been' ' validated against the hunt participation rules.' ' Preparing to find email in participant database' ' table.', email) if not get_participant(g.db, email, hunt_id): logger.info( 'Preparing to save new participant with email, %s,' ' to hunt, %s', email, hunt.name) create_new_participant(g.db, form, hunt_id) scavenger_info = {'email': email, 'name': form.name.data} session.update(scavenger_info) admin_settings = get_settings(g.db, hunt_id=hunt_id) logger.info( "Retrieved settings associated with hunt with id, %s: %s", hunt_id, admin_settings) try: lrs = WaxCommunicator(admin_settings, request.host_url, hunt, None, scavenger_info=scavenger_info) except Exception as e: logger.exception( "Error instantiating WaxCommunicator while registering" " participant: %s", e) raise e try: lrs.send_began_hunt_statement() except Exception as e: logger.exception("Error sending began hunt statement: %s", e) raise e logger.info( "name and email set to %s, and %s\n" "preparing requested item information.", session['name'], email) redirect_url = get_intended_url(session, hunt_id) return make_response(redirect(redirect_url)) else: logger.info( 'participant attempted to register for' ' hunt with invalid form information.\n' 'Error message: %s\n. Form data: %s', err_msg, request.form) return err_msg else: # i don't think this can happen ever in the app logger.warning( 'A user attempted to register for hunt with id, %s,' ' but the hunt could not be found. Form data: %s', hunt_id, request.form) abort(400)
def test_validate_participant_anyone_can_participate(self, get_db): valid, err_msg = utils.validate_participant( get_db(), example_email(), 1, 'anyone') self.assertTrue(valid)
def register_participant(): hunt_id = request.args['hunt_id'] hunt = Hunt.find_by_id(g.db, hunt_id) if hunt: form = ParticipantForm(request.form) if form.validate(): email = form.email.data logger.info( 'Participant registration form validated for hunt, "%s", and' ' email, %s.\nPreparing to validate participant against hunt' ' participation rules.', hunt.name, email) participant_valid, err_msg = validate_participant( g.db, email, hunt_id, hunt.participant_rule) if participant_valid: logger.info('The registering participant, %s, has been' ' validated against the hunt participation rules.' ' Preparing to find email in participant database' ' table.', email) if not get_participant(g.db, email, hunt_id): logger.info( 'Preparing to save new participant with email, %s,' ' to hunt, %s', email, hunt.name) create_new_participant(g.db, form, hunt_id) scavenger_info = {'email': email, 'name': form.name.data} session.update(scavenger_info) admin_settings = get_settings(g.db, hunt_id=hunt_id) logger.info( "Retrieved settings associated with hunt with id, %s: %s", hunt_id, admin_settings) try: lrs = WaxCommunicator( admin_settings, request.host_url, hunt, None, scavenger_info=scavenger_info) except Exception as e: logger.exception( "Error instantiating WaxCommunicator while registering" " participant: %s", e) raise e try: lrs.send_began_hunt_statement() except Exception as e: logger.exception( "Error sending began hunt statement: %s", e) raise e logger.info( "name and email set to %s, and %s\n" "preparing requested item information.", session['name'], email) redirect_url = get_intended_url(session, hunt_id) return make_response(redirect(redirect_url)) else: logger.info('participant attempted to register for' ' hunt with invalid form information.\n' 'Error message: %s\n. Form data: %s', err_msg, request.form) return err_msg else: # i don't think this can happen ever in the app logger.warning('A user attempted to register for hunt with id, %s,' ' but the hunt could not be found. Form data: %s', hunt_id, request.form) abort(400)
def test_validate_participant_by_whitelist(self, get_db, get_participant): # mock will be truthy when returned from the check for participant valid, _ = utils.validate_participant( get_db(), example_email(), 1, 'by_whitelist') self.assertTrue(valid)
def test_validate_participant_by_whitelist_with_invalid_email( self, get_db, get_participant): get_participant.return_value = None invalid, _ = utils.validate_participant( get_db(), example_email(), 1, 'by_whitelist') self.assertFalse(invalid)
def test_validate_participant_by_domain(self, get_db, get_hunt_domain): domain = get_hunt_domain.return_value = 'example.com' email = '{}@{}'.format(identifier(), domain) valid, _ = utils.validate_participant(get_db(), email, 1, 'by_domain') self.assertTrue(valid)
def test_validate_participate_by_domain_invalid_domain(self, get_db): different_domain_email = '{}@not.example.com'.format(identifier()) invalid, _ = utils.validate_participant( get_db(), different_domain_email, 1, 'by_domain') self.assertFalse(invalid)
def test_validate_participant_anyone_can_participate(self, get_db): valid, err_msg = utils.validate_participant(get_db(), example_email(), 1, 'anyone') self.assertTrue(valid)
def test_validate_participant_by_whitelist_with_invalid_email( self, get_db, get_participant): get_participant.return_value = None invalid, _ = utils.validate_participant(get_db(), example_email(), 1, 'by_whitelist') self.assertFalse(invalid)
def test_validate_participant_by_whitelist(self, get_db, get_participant): # mock will be truthy when returned from the check for participant valid, _ = utils.validate_participant(get_db(), example_email(), 1, 'by_whitelist') self.assertTrue(valid)
def test_validate_participate_by_domain_invalid_domain(self, get_db): different_domain_email = '{}@not.example.com'.format(identifier()) invalid, _ = utils.validate_participant(get_db(), different_domain_email, 1, 'by_domain') self.assertFalse(invalid)