def db_creation_list_month_year_endpoint( db_creation_shifts_list_endpoint, user_object, contract_object, create_n_shift_objects, ): """ This fixture creates 2 shifts for the standart user on the 2nd of February 2019 :param db_creation_shifts_list_endpoint: :param user_object: :param contract_object: :param create_n_shift_objects: :return: """ # dependency of db_creation_shifts_list_endpoint creates 2 shifts for user_object on 29th of January # and 2 shifts for diff user_object also on 29th of January # We now create 2 shift on 2nd of February for user_object _started = datetime.datetime(2019, 2, 2, 14) _stopped = datetime.datetime(2019, 2, 2, 16) create_n_shift_objects( (1, 3), user=user_object, contract=contract_object, started=_started, stopped=_stopped, )
def valid_shift_json(user_object, contract_object): """ This fixture provides a valid (according to the ShiftSerializer) JSON dictionary. :param user_object: :param contract_object: :return: Dict """ started = datetime.datetime(2019, 1, 29, 14).isoformat() stopped = datetime.datetime(2019, 1, 29, 16).isoformat() created_at = datetime.datetime(2019, 1, 29, 16).isoformat() modified_at = created_at user = user_object.id contract = contract_object.id _type = "st" note = "something was strange" tags = json.dumps(["tag1", "tag2"]) data = { "started": started, "stopped": stopped, "contract": contract, "type": _type, "note": note, "tags": tags, "user": user, "created_by": user, "modified_by": user, "created_at": created_at, "modified_at": modified_at, } return data
def stopped_before_started_json(valid_shift_json): """ This fixture creates an invalid according to the ShiftSerializer) JSON dictionary where the started datatime is after the stopped datetime. :param valid_shift_json: :return: Dict """ valid_shift_json["started"] = datetime.datetime(2019, 1, 29, 16) valid_shift_json["stopped"] = datetime.datetime(2019, 1, 29, 14) return valid_shift_json
def shift_starts_ends_after_contract_json(valid_shift_json): """ This fixture creates an invalid according to the ShiftSerializer) JSON dictionary. Here the started and stopped of the shift are set after the contract ends. The contract object has for end_date the 31st of January in 2019. :param valid_shift_json: :return: Dict """ valid_shift_json["started"] = datetime.datetime(2019, 2, 19, 14) valid_shift_json["stopped"] = datetime.datetime(2019, 2, 19, 16) return valid_shift_json
def valid_contract_json(user_object): """ This fixture provides a valid (according to the ContractSerializer) JSON dictionary. :param user_object: :param user_object: :return: Dict """ name = "Test Contract" hours = 20.0 start_date = datetime.date(2019, 1, 1).isoformat() end_date = datetime.date(2019, 1, 31).isoformat() user = user_object.id created_at = datetime.datetime(2018, 12, 31, hour=10).isoformat() modified_at = created_at data = { "name": name, "hours": hours, "start_date": start_date, "end_date": end_date, "user": user, "created_by": user, "modified_by": user, "created_at": created_at, "modified_at": modified_at, } return data
def valid_contract_json(user_object): """ This fixture provides a valid (according to the ContractSerializer) JSON dictionary. :param user_object: :param user_object: :return: Dict """ name = "Test Contract" minutes = 1200 start_date = datetime.date(2019, 1, 1).isoformat() end_date = datetime.date(2019, 1, 31).isoformat() user = str(user_object.id) created_at = datetime.datetime(2018, 12, 31, hour=10).isoformat() modified_at = created_at data = { "name": name, "minutes": minutes, "start_date": start_date, "end_date": end_date, "user": user, "created_by": user, "modified_by": user, "created_at": created_at, "modified_at": modified_at, "carryover_target_date": start_date, "initial_carryover_minutes": 0, } return data
def create_n_report_objects(): """ This fixture resembles a report object factory. Shifts are distinguised by id, there is no specific need for the start_stop mechanism. Nonetheless in terms of consistency this mechanism is kept as in the user_conftest.py. :return: Function """ month_year = datetime.date(2019, 1, 1) hours = datetime.timedelta(0) created_at = datetime.datetime(2019, 1, 1, 16).isoformat() modified_at = created_at def create_reports(start_stop, user, contract, month_year=month_year): lst = [] for i in range(*start_stop): report = Report.objects.create( month_year=month_year, hours=hours, contract=contract, user=user, created_by=user, modified_by=user, created_at=created_at, modified_at=modified_at, ) lst.append(report) return lst return create_reports
def _make_aware(time_input): """ Convert naive date object into timezone aware datetime. """ return datetime.datetime( time_input.year, time_input.month, time_input.day, tzinfo=TIMEZONE, )
def clockedinshift_object(user_object, contract_object): return ClockedInShift.objects.create( started=datetime.datetime(2019, 2, 11, 14, tzinfo=utc), user=user_object, contract=contract_object, created_by=user_object, modified_by=user_object, )
def stopped_on_next_day_json(valid_shift_json): """ This fixture creates an invalid according to the ShiftSerializer) JSON dictionary where the stopped datetime is on the next day after the started datetime. :param valid_shift_json: :return: Dict """ valid_shift_json["stopped"] = datetime.datetime(2019, 1, 30, 1) return valid_shift_json
def create_n_shift_objects(): """ This fixture resembles a shift object factory. Shifts are distinguised by id, there is no specific need for the start_stop mechanism. Nonetheless in terms of consistency this mechanism is kept as in the user_conftest.py. :return: Function """ _started = datetime.datetime(2019, 1, 29, 14) _stopped = datetime.datetime(2019, 1, 29, 16) created_at = datetime.datetime(2019, 1, 29, 16).isoformat() modified_at = created_at _type = "st" note = "something was strange" tags = ["tag1, tag2"] def create_shifts(start_stop, user, contract, started=_started, stopped=_stopped): lst = [] for i in range(*start_stop): shift = Shift.objects.create( started=started, stopped=stopped, created_at=created_at, modified_at=modified_at, type=_type, note=note, user=user, created_by=user, modified_by=user, contract=contract, ) shift.tags.add(*tags) lst.append(shift) return lst return create_shifts
def formatdate_time(venue_data): ''' trim out date information from csv file date format output d_local2utc -> input for local2UTC function format (python datetime type) ''' day = venue_data[1:9] time = venue_data[12:] hr = int(time[0:2]) minute = int(time[3:5]) sec = int(time[6:8]) year = int('20'+day[6:8]) month = int(day[0:2]) date = int(day[3:5]) #combine = ((year, month, date, hr, minute, sec)) d_local2utc = datetime.datetime(year, month, date, hr, minute, sec) # becomes this fmt 2014-06-16 15:59:18.371930 return d_local2utc # , combine
def valid_clockedinshift_json(user_object, contract_object): """ This fixture provides a valid (according to ClockedInShiftSerializer) JSON dictionary for a clocked-in shift which is created manually. :param user_object: :param contract_object: :return: """ user = str(user_object.id) data = { "started": datetime.datetime(2019, 1, 29, 14, tzinfo=utc).isoformat(), "contract": str(contract_object.id), "user": user, "created_by": user, "modified_by": user, } return data
def validate_jwt_with_real_token(cognito_settings): """Test validate_jwt """ region = cognito_settings["cognito.region"] userpool_id = cognito_settings["cognito.userpool.id"] userpool_iss = auth.cognito_userpool_iss(region, userpool_id) userpool_keys = auth.cognito_userpool_keys(userpool_iss) ########################################################################### # Test case: invalid key passed, msg = auth.validate_jwt(ACCESS_TOKEN_INVALID_KEY, userpool_iss, userpool_keys) assert passed is False assert msg == "Obtained keys are wrong" ########################################################################### # Test case: expired token # http://stackoverflow.com/questions/1357711/pytz-utc-conversion#1357711 d_valid = datetime.datetime(2017, 10, 9, 10, 26, 7) # 1 hr before expiring tz_valid = timezone("Australia/Melbourne") d_valid_tz = tz_valid.normalize(tz_valid.localize(d_valid)) d_valid_utc = d_valid_tz.astimezone(utc) # Ensure it has expired d_token = datetime.datetime.utcfromtimestamp( jwt.get_unverified_claims(ACCESS_TOKEN_EXPIRED)["exp"]) assert d_token < datetime.datetime.utcnow() # Check it was the expected expiry assert d_valid_utc < tz_valid.normalize( tz_valid.localize(d_token)).astimezone(utc) passed, msg = auth.validate_jwt(ACCESS_TOKEN_EXPIRED, userpool_iss, userpool_keys) assert passed is False assert msg == "Failed to decode token: Signature has expired."
if abbrev not in _cache: _cache[abbrev] = [] _cache[abbrev].append(tz.zone) if __name__ == '__main__': cal = pdt.Calendar() sys.stdout.write('{}\n'.format(cal.parse(sys.argv[1]))) local = current_tz() now = datetime.datetime.now(local) other_tz = pytz.timezone('US/Pacific') now_utc = now.astimezone(pytz.utc) then_parsed = datetime.datetime(*cal.parse(sys.argv[1])[0][:6]) then = other_tz.localize(then_parsed) h, m, s, t = until(now_utc, then.astimezone(pytz.utc)) sys.stdout.write('It is currently {} in {}.\n'.format( now.strftime('%d %b %H:%M:%S %Z'), local.zone)) sys.stdout.write('It is currently {} in {}.\n'.format( now.astimezone(other_tz).strftime('%d %b %H:%M:%S %Z'), other_tz.zone)) sys.stdout.write('Time until {}: '.format( then.strftime('%d %b %H:%M:%S %Z'))) if h > 0: sys.stdout.write('{} hours '.format(h)) sys.stdout.write('{} minutes {} seconds '.format(m, s))
def update_clockedinshift_json(valid_clockedinshift_json): valid_clockedinshift_json["started"] = datetime.datetime( 2019, 1, 29, 16, tzinfo=utc).isoformat() return valid_clockedinshift_json
abbrev = now.astimezone(tz).tzname() if abbrev not in _cache: _cache[abbrev] = [] _cache[abbrev].append(tz.zone) if __name__ == '__main__': cal = pdt.Calendar() sys.stdout.write('{}\n'.format(cal.parse(sys.argv[1]))) local = current_tz() now = datetime.datetime.now(local) other_tz = pytz.timezone('US/Pacific') now_utc = now.astimezone(pytz.utc) then_parsed = datetime.datetime(*cal.parse(sys.argv[1])[0][:6]) then = other_tz.localize(then_parsed) h, m, s, t = until(now_utc, then.astimezone(pytz.utc)) sys.stdout.write('It is currently {} in {}.\n'.format( now.strftime('%d %b %H:%M:%S %Z'), local.zone)) sys.stdout.write('It is currently {} in {}.\n'.format( now.astimezone(other_tz).strftime('%d %b %H:%M:%S %Z'), other_tz.zone)) sys.stdout.write('Time until {}: '.format( then.strftime('%d %b %H:%M:%S %Z'))) if h > 0: sys.stdout.write('{} hours '.format(h)) sys.stdout.write('{} minutes {} seconds '.format(m, s))