def test_create_token(self): """Basic usage of create_token.""" token_1 = auth.create_token('*****@*****.**', 'login') self.assertTrue(token_1) token_2 = auth.create_token('*****@*****.**', 'unsubscribe') self.assertTrue(token_2) self.assertNotEqual(token_1, token_2) token_3 = auth.create_token('*****@*****.**', 'login') self.assertTrue(token_3) self.assertNotEqual(token_1, token_3)
def employment_vars(user): """Compute vars for a given user for the employment survey. Returns: a dict with all vars required for the template, or None if no email should be sent. """ registered_months_ago = _get_french_months_ago( user.registered_at.ToDatetime()) if not registered_months_ago: logging.warning('User registered only recently (%s)', user.registered_at) return None survey_token = parse.quote( auth.create_token(user.user_id, role='employment-status')) return { 'firstName': user.profile.name, 'registeredMonthsAgo': registered_months_ago, 'seekingUrl': '{}/api/employment-status?user={}&token={}&seeking=1&redirect={}'. format( _BASE_URL, user.user_id, survey_token, parse.quote('https://bayes.typeform.com/to/hn10ya'), ), # TODO(benoit) : Better if seeking is STOP_SEEKING instead of numeric value. 'stopSeekingUrl': '{}/api/employment-status?user={}&token={}&seeking=2&redirect={}'. format( _BASE_URL, user.user_id, survey_token, parse.quote('https://bayes.typeform.com/to/jEnbMx'), ), 'unsubscribeLink': '{}/unsubscribe.html?email={}&auth={}'.format( _BASE_URL, parse.quote(user.profile.email), parse.quote( auth.create_token(user.profile.email, role='unsubscribe'))), }
def network_vars(user): """Compute vars for a given user for the network email. Returns: a dict with all vars required for the template, or None if no email should be sent. """ if not user.projects: logging.info('User has no project') return None project = user.projects[0] registered_months_ago = _get_french_months_ago( user.registered_at.ToDatetime()) if not registered_months_ago: logging.warning('User registered only recently (%s)', user.registered_at) return None in_target_domain = _get_best_in_domain_match( project.target_job.job_group.rome_id) if not in_target_domain: logging.warning('Could not find a target domain (%s)', project.target_job.job_group) return None worst_frustration = next( (f for f in (user_pb2.NO_OFFER_ANSWERS, user_pb2.MOTIVATION) if f in user.profile.frustrations), None) is_hairdresser_or_in_marseille = \ project.target_job.job_group.rome_id.startswith('D') or \ project.mobility.city.departement_id == '13' other_job_in_city = 'coiffeur à Marseille' if is_hairdresser_or_in_marseille: other_job_in_city = 'secrétaire à Lyon' return { 'gender': user_pb2.Gender.Name(user.profile.gender), 'firstName': user.profile.name, 'registeredMonthsAgo': registered_months_ago, 'inTargetDomain': in_target_domain, 'frustration': user_pb2.Frustration.Name(worst_frustration) if worst_frustration else '', 'otherJobInCity': other_job_in_city, 'jobInCity': '%s %s' % (french.lower_first_letter( _genderize_job(project.target_job, user.profile.gender)), french.in_city(strip_district(project.mobility.city.name))), 'emailInUrl': parse.quote(user.profile.email), 'unsubscribeLink': '%s/unsubscribe.html?email=%s&auth=%s' % (_BASE_URL, parse.quote(user.profile.email), parse.quote(auth.create_token(user.profile.email, role='unsubscribe'))), }
def spontaneous_vars(user, previous_email_campaign_id): """Compute vars for a given user for the spontaneous email. Returns: a dict with all vars required for the template, or None if no email should be sent. """ if not user.projects: logging.info('User has no project') return None project = user.projects[0] job_group_info = _JOB_GROUPS_INFO.get_collection(_DB).get( project.target_job.job_group.rome_id) def _should_use_spontaneous(modes): return any(mode.mode == job_pb2.SPONTANEOUS_APPLICATION and mode.percentage > 20 for mode in modes.modes) application_modes = job_group_info.application_modes if not any( _should_use_spontaneous(modes) for modes in application_modes.values()): return None registered_months_ago = _get_french_months_ago( user.registered_at.ToDatetime()) if not registered_months_ago: logging.warning('User registered only recently (%s)', user.registered_at) return None has_read_previous_email = previous_email_campaign_id and any( email.campaign_id == previous_email_campaign_id and email.status in (user_pb2.EMAIL_SENT_OPENED, user_pb2.EMAIL_SENT_CLICKED) for email in user.emails_sent) def _get_rome_value(fieldname): return _ROME_INFO.get_value(project.target_job.job_group.rome_id, fieldname) contact_mode = _get_rome_value('contact_mode') if not contact_mode: logging.error('There is no contact mode for the job group "%s"', project.target_job.job_group.rome_id) return None in_a_workplace = _get_rome_value('in_a_workplace') or '' if not in_a_workplace and contact_mode != 'BY_EMAIL': logging.error( 'There is no "in_a_workplace" field for the job group "%s".', project.target_job.job_group.rome_id) return None like_your_workplace = _get_rome_value('like_your_workplace') or '' if in_a_workplace and not like_your_workplace: logging.error( 'There is no "like_your_workplace" field for the job group "%s".', project.target_job.job_group.rome_id) return None to_the_workplace = _get_rome_value('to_the_workplace') if not to_the_workplace: to_the_workplace = "à l'entreprise" some_companies = _get_rome_value('some_companies') if not some_companies: some_companies = 'des entreprises' what_i_love_about = _get_rome_value('what_i_love_about') or '' if user.profile.gender == user_pb2.FEMININE: what_i_love_about_feminine = _get_rome_value( 'what_i_love_about_feminine') if what_i_love_about_feminine: what_i_love_about = what_i_love_about_feminine if not what_i_love_about and contact_mode == 'BY_EMAIL': logging.error( 'There is no "What I love about" field for the job group "%s".', project.target_job.job_group.rome_id) return None why_specific_company = _get_rome_value('why_specific_company') if not why_specific_company: logging.error( 'There is no "Why this specific company" field for the job group "%s".', project.target_job.job_group.rome_id) return None various_companies = _get_rome_value('various_companies') or '' if project.weekly_applications_estimate == project_pb2.SOME: weekly_application_count = '5' elif project.weekly_applications_estimate > project_pb2.SOME: weekly_application_count = '15' else: weekly_application_count = '' return { 'applicationComplexity': job_pb2.ApplicationProcessComplexity.Name( job_group_info.application_complexity), 'contactMode': contact_mode, 'deepLinkLBB': 'https://labonneboite.pole-emploi.fr/entreprises/commune/{}/rome/' '{}?utm_medium=web&utm_source=bob&utm_campaign=bob-email'.format( project.mobility.city.city_id, project.target_job.job_group.rome_id), 'emailInUrl': parse.quote(user.profile.email), 'experienceAsText': _EXPERIENCE_AS_TEXT.get(project.seniority, 'peu'), 'firstName': user.profile.name, 'gender': user_pb2.Gender.Name(user.profile.gender), 'hasReadPreviousEmail': 'True' if has_read_previous_email else '', 'inWorkPlace': in_a_workplace, 'jobName': french.lower_first_letter( _genderize_job(project.target_job, user.profile.gender)), 'lastName': user.profile.last_name, 'likeYourWorkplace': like_your_workplace, 'registeredMonthsAgo': registered_months_ago, 'someCompanies': some_companies, 'toTheWorkPlace': to_the_workplace, 'unsubscribeLink': '{}/unsubscribe.html?email={}&auth={}'.format( _BASE_URL, parse.quote(user.profile.email), parse.quote( auth.create_token(user.profile.email, role='unsubscribe'))), 'variousCompanies': various_companies, 'weeklyApplicationOptions': weekly_application_count, 'whatILoveAbout': what_i_love_about, 'whySpecificCompany': why_specific_company, }
def test_check_token_wrong_role(self): """check_token fails if wrong role.""" login_token = auth.create_token('*****@*****.**', 'login') with self.assertRaises(ValueError): auth.check_token('*****@*****.**', login_token, 'unsubscribe')
def test_check_token(self): """Basic usage of check_token (round trip with create_token).""" login_token = auth.create_token('*****@*****.**', 'login') auth.check_token('*****@*****.**', login_token, 'login')