def test_employee_can_get_correct_nomination_status_when_outside_enrollment_period( ldap_mocked_app_with_users, is_enrolled, body): # noqa: E501 dbsession = get_dbsession(ldap_mocked_app_with_users) with transaction.manager: period = Period( name=TEST_PERIOD_NAME, enrollment_start_utc=days_from_utcnow(-2), entry_start_utc=days_from_utcnow(-1), approval_start_utc=days_from_utcnow(2), approval_end_utc=days_from_utcnow(3), ) dbsession.add(period) if is_enrolled: nominee = Nominee(username=TEST_EMPLOYEE_USERNAME, period=period) dbsession.add(nominee) app = successfully_login(ldap_mocked_app_with_users, TEST_EMPLOYEE_USERNAME) csrf_token = app.cookies[ANGULAR_2_XSRF_TOKEN_COOKIE_NAME] response = app.get("/api/v1/self-nominate", headers={ANGULAR_2_XSRF_TOKEN_HEADER_NAME: csrf_token}) assert response.json_body["heading"] == ENROLLMENT_INACTIVE_TEMPLATE[ "heading"].format(period_name=TEST_PERIOD_NAME) assert response.json_body["body"] == body assert (response.json_body["buttonText"] == ENROLLMENT_INACTIVE_TEMPLATE["buttonText"]) assert (response.json_body["buttonLink"] == ENROLLMENT_INACTIVE_TEMPLATE["buttonLink"]) assert (response.json_body["canNominate"] == ENROLLMENT_INACTIVE_TEMPLATE["canNominate"])
def add_test_period_with_template( dbsession, subperiod, template_id, period_id=TEST_PERIOD_ID, period_name=TEST_PERIOD_NAME, offset_from_utc_now_days=0, add_nominees=True, days_in=1, ): days_away = partial(days_from_utcnow, offset=offset_from_utc_now_days) times = generate_period_dates(subperiod, days_away, days_in) with transaction.manager: period = Period(id=period_id, name=period_name, template_id=template_id, **times) dbsession.add(period) with transaction.manager: if add_nominees: for nominee_username in TEST_NOMINEES: dbsession.add( Nominee(period_id=period_id, username=nominee_username)) return period_id
def nominate_everyone(ctx): engine = ctx.obj[ENGINE_KEY] session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) with transaction.manager: period = Period.get_current_period(dbsession) for user, nominee in (dbsession.query(User, Nominee).outerjoin( Nominee, Nominee.username == User.username).all()): if not nominee: dbsession.add(Nominee(user=user, period=period))
def test_employee_cannot_self_nominate_if_already_nominated( app_in_enrollment_subperiod): # noqa: E501 dbsession = get_dbsession(app_in_enrollment_subperiod) with transaction.manager: period = dbsession.query(Period).first() nominee = Nominee(period=period, username=TEST_EMPLOYEE_USERNAME) dbsession.add(nominee) app = successfully_login(app_in_enrollment_subperiod, TEST_EMPLOYEE_USERNAME) csrf_token = app.cookies[ANGULAR_2_XSRF_TOKEN_COOKIE_NAME] response = app.post( "/api/v1/self-nominate", headers={ANGULAR_2_XSRF_TOKEN_HEADER_NAME: csrf_token}, expect_errors=True, ) assert response.status_code == 400 assert "You are already enrolled" in response.json_body["message"]
def self_nominate(request): """ If the current period cycle is in the enrollment state, update `request.user` status for the current period to ENROLLED. Returns ------- JSON-serialisable payload that includes: * Message to display to `request.user` on their current nomination status for the current period. * Whether to display a button, with an associated URL and display text """ location = get_config_value(request.registry.settings, constants.HOMEBASE_LOCATION_KEY) current_period = Period.get_current_period(request.dbsession, options=joinedload("nominees")) if not current_period: raise HTTPNotFound(explanation="The feedback process is closed for " "the meantime. Please contact your " "manager for more details.") elif current_period.subperiod(location) != Period.ENROLLMENT_SUBPERIOD: display_end_date = current_period.entry_start_utc.strftime( constants.DEFAULT_DISPLAY_DATETIME_FORMAT) raise HTTPNotFound(explanation="The enrollment period closed on " "%s" % display_end_date) username = request.user.username if username in (n.username for n in current_period.nominees): raise HTTPBadRequest(explanation="You are already enrolled " "for the current period %s" % current_period.name) period_name = current_period.name with transaction.manager: request.dbsession.add(Nominee(period=current_period, username=username)) return interpolate_template(ENROLLMENT_SUCCESS_TEMPLATE, period_name=period_name)
def test_employee_can_get_correct_nomination_status_when_already_enrolled( app_in_enrollment_subperiod): # noqa: E501 dbsession = get_dbsession(app_in_enrollment_subperiod) with transaction.manager: period = dbsession.query(Period).first() nominee = Nominee(period=period, username=TEST_EMPLOYEE_USERNAME) dbsession.add(nominee) app = successfully_login(app_in_enrollment_subperiod, TEST_EMPLOYEE_USERNAME) csrf_token = app.cookies[ANGULAR_2_XSRF_TOKEN_COOKIE_NAME] response = app.get("/api/v1/self-nominate", headers={ANGULAR_2_XSRF_TOKEN_HEADER_NAME: csrf_token}) assert response.json_body["heading"] == ENROLLMENT_EXISTS_TEMPLATE[ "heading"].format(period_name=TEST_PERIOD_NAME) assert response.json_body["body"] == ENROLLMENT_EXISTS_TEMPLATE[ "body"].format(period_name=TEST_PERIOD_NAME) assert response.json_body["buttonText"] == ENROLLMENT_EXISTS_TEMPLATE[ "buttonText"] assert response.json_body["buttonLink"] == ENROLLMENT_EXISTS_TEMPLATE[ "buttonLink"] assert (response.json_body["canNominate"] == ENROLLMENT_EXISTS_TEMPLATE["canNominate"])
def add_test_data_for_stats(_dbsession, current_subperiod=Period.APPROVAL_SUBPERIOD, days_in=1): """Not trying to test mutability of existing rows so no need to track ids, so just need to make sure we have 2 separate periods with multiple stats each """ template_id = add_test_template(_dbsession) current_period_id = add_test_period_with_template( _dbsession, current_subperiod, template_id, period_id=TEST_PERIOD_ID, period_name=TEST_PERIOD_NAME, add_nominees=False, days_in=days_in, ) previous_period_id = add_test_period_with_template( _dbsession, Period.APPROVAL_SUBPERIOD, template_id, period_id=TEST_PREVIOUS_PERIOD_ID, period_name=TEST_PREVIOUS_PERIOD_NAME, offset_from_utc_now_days=-400, add_nominees=False, days_in=days_in, ) # TEST_MANAGER only manages nominee 1 and 2 nominee1 = partial(Nominee, username=TEST_STATS_NOMINATED_USERS[0]) nominee2 = partial(Nominee, username=TEST_STATS_NOMINATED_USERS[1]) nominee3 = partial(Nominee, username=TEST_STATS_NOMINATED_USERS[2]) random_username = lambda: fake.profile()["username"] # noqa: E731 with transaction.manager: # nominee 1 participated in everything _generate_num_of_forms( _dbsession, previous_period_id, 7, to_username=random_username, from_username=TEST_EMPLOYEE_USERNAME, ) _dbsession.add(nominee1(period_id=previous_period_id)) _generate_num_of_forms( _dbsession, previous_period_id, 3, to_username=TEST_EMPLOYEE_USERNAME, from_username=random_username, ) _generate_num_of_forms( _dbsession, current_period_id, 2, to_username=random_username, from_username=TEST_EMPLOYEE_USERNAME, ) _dbsession.add(nominee1(period_id=current_period_id)) _generate_num_of_forms( _dbsession, current_period_id, TEST_NUM_FORMS_RECEIVED, to_username=TEST_EMPLOYEE_USERNAME, from_username=random_username, ) # nominee 2: _generate_num_of_forms( _dbsession, previous_period_id, 2, to_username=random_username, from_username=TEST_EMPLOYEE_2_USERNAME, ) # Did not request any feedback so not nomination # Did not give any feedback so no forms created # Did request any feedback so nomination _dbsession.add(nominee2(period_id=current_period_id)) # nominee 3 participated in everything but isn't managed _generate_num_of_forms( _dbsession, previous_period_id, 3, to_username=random_username, from_username=TEST_TALENT_MANAGER_USERNAME, ) _dbsession.add(nominee3(period_id=previous_period_id)) _generate_num_of_forms( _dbsession, previous_period_id, 8, to_username=TEST_TALENT_MANAGER_USERNAME, from_username=random_username, ) _generate_num_of_forms( _dbsession, current_period_id, 10, to_username=random_username, from_username=TEST_TALENT_MANAGER_USERNAME, ) _dbsession.add(nominee3(period_id=current_period_id)) _generate_num_of_forms( _dbsession, current_period_id, 2, to_username=TEST_TALENT_MANAGER_USERNAME, from_username=random_username, ) # summary done for TEST_TALENT_MANAGER_USERNAME manager_summary = FeedbackForm( to_username=TEST_TALENT_MANAGER_USERNAME, # noqa: E501 from_username=TEST_MANAGER_USERNAME, period_id=current_period_id, is_summary=True, ) answers = [ FeedbackAnswer( question_id=QUESTION_IDS_AND_TEMPLATES[0][0], content=TEST_SUMMARY_1 + "Foo", ), FeedbackAnswer( question_id=QUESTION_IDS_AND_TEMPLATES[1][0], content=TEST_SUMMARY_2 + "Foo ★", ), FeedbackAnswer( question_id=QUESTION_IDS_AND_TEMPLATES[2][0], content=TEST_SUMMARY_3 + "Foo", ), ] manager_summary.answers = answers _dbsession.add(manager_summary) # there is also a summary done for TEST_EMPLOYEE_2 manager_form = FeedbackForm( to_username=TEST_EMPLOYEE_2_USERNAME, from_username=TEST_MANAGER_USERNAME, period_id=current_period_id, is_summary=True, ) answers = [ FeedbackAnswer(question_id=QUESTION_IDS_AND_TEMPLATES[0][0], content=TEST_SUMMARY_1), FeedbackAnswer(question_id=QUESTION_IDS_AND_TEMPLATES[1][0], content=TEST_SUMMARY_2), FeedbackAnswer(question_id=QUESTION_IDS_AND_TEMPLATES[2][0], content=TEST_SUMMARY_3), ] manager_form.answers = answers _dbsession.add(manager_form) # other employee is nominated and summarised other_nominee = Nominee(username=TEST_STATS_NOMINATED_USERS[3], period_id=current_period_id) _dbsession.add(other_nominee) other_manager_form = FeedbackForm( to_username=TEST_OTHER_EMPLOYEE_USERNAME, from_username=TEST_OTHER_MANAGER_USERNAME, period_id=current_period_id, is_summary=True, ) answers = [ FeedbackAnswer(question_id=QUESTION_IDS_AND_TEMPLATES[0][0], content=TEST_SUMMARY_1), FeedbackAnswer(question_id=QUESTION_IDS_AND_TEMPLATES[1][0], content=TEST_SUMMARY_2), FeedbackAnswer(question_id=QUESTION_IDS_AND_TEMPLATES[2][0], content=TEST_SUMMARY_3), ] other_manager_form.answers = answers _dbsession.add(other_manager_form)