Beispiel #1
0
    def test_fill_in_start_end(self):
        # case where neither start nor end is defined; they are returned
        # as is (as None values)
        s = e = None
        assert (None, None) == self.arl_indexer._fill_in_start_end(s, e)

        # case where end is defined but start isn't (invalid); exception raised
        e = datetime.datetime.utcnow()
        with raises(ValueError) as e_info:
            self.arl_indexer._fill_in_start_end(s, e)
        assert arlindexer.ArlIndexer.END_WITHOUT_START_ERR_MSG == e_info.value.args[
            0]

        # case where start is after end (invalid); exception raised
        s = e + datetime.timedelta(1)
        with raises(ValueError) as e_info:
            self.arl_indexer._fill_in_start_end(s, e)
        assert arlindexer.ArlIndexer.START_AFTER_END_ERR_MSG == e_info.value.args[
            0]

        # case where start is before end (valid); they are returned as is
        s = e - datetime.timedelta(1)
        assert s, e == self.arl_indexer._fill_in_start_end(s, e)

        # case where start is defined but not end; end gets set to now
        with timecop.freeze(time.mktime(e.timetuple())):
            assert s, e == self.arl_indexer._fill_in_start_end(s, None)
Beispiel #2
0
    def test_fill_in_start_end(self):
        # case where neither start nor end is defined; they are returned
        # as is (as None values)
        s = e = None
        assert (None, None) == self.arl_indexer._fill_in_start_end(s, e)

        # case where end is defined but start isn't (invalid); exception raised
        e = datetime.datetime.utcnow()
        with raises(ValueError) as e_info:
            self.arl_indexer._fill_in_start_end(s, e)
        assert arlindexer.ArlIndexer.END_WITHOUT_START_ERR_MSG == e_info.value.args[0]

        # case where start is after end (invalid); exception raised
        s = e + datetime.timedelta(1)
        with raises(ValueError) as e_info:
            self.arl_indexer._fill_in_start_end(s, e)
        assert arlindexer.ArlIndexer.START_AFTER_END_ERR_MSG == e_info.value.args[0]

        # case where start is before end (valid); they are returned as is
        s = e - datetime.timedelta(1)
        assert s, e == self.arl_indexer._fill_in_start_end(s, e)

        # case where start is defined but not end; end gets set to now
        with timecop.freeze(time.mktime(e.timetuple())):
            assert s, e == self.arl_indexer._fill_in_start_end(s, None)
Beispiel #3
0
    def test_can_nest_freezes(self):
        secs = time.time()
        secs -= self.SECONDS_IN_A_DAY

        yesterday = date.today() - timedelta(days=1)
        day_before_yesterday = date.today() - timedelta(days=2)

        with timecop.freeze(secs):
            self.assertEqual(yesterday, date.today())

            secs -= self.SECONDS_IN_A_DAY  # take off another day
            with timecop.freeze(secs):
                self.assertEqual(day_before_yesterday, date.today())

            # test again after coming out of looped context
            self.assertEqual(yesterday, date.today())
Beispiel #4
0
 def test_validating_invalid_totp_for_same_secret(self):
     """
     Test case when the same secret is used, but the token differs
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         self.assertFalse(valid_totp(get_totp(secret) + 1, secret))
Beispiel #5
0
 def test_validating_totp_for_same_secret(self):
     """
     Check if validating TOTP generated for the same secret works
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         self.assertTrue(valid_totp(get_totp(secret), secret))
Beispiel #6
0
    def test_can_nest_freezes(self):
        secs = time.time()
        secs -= self.SECONDS_IN_A_DAY

        yesterday = date.today() - timedelta(days=1)
        day_before_yesterday = date.today() - timedelta(days=2)

        with timecop.freeze(secs):
            self.assertEqual(yesterday, date.today())

            secs -= self.SECONDS_IN_A_DAY  # take off another day
            with timecop.freeze(secs):
                self.assertEqual(day_before_yesterday, date.today())

            # test again after coming out of looped context
            self.assertEqual(yesterday, date.today())
Beispiel #7
0
    def test_datetime_utcnow(self):
        now = time.time()

        with timecop.freeze(now):
            time.sleep(0.6)
            self.assertEqual(datetime.datetime.utcfromtimestamp(now),
                             datetime.datetime.utcnow())
Beispiel #8
0
 def test_can_freeze_to_a_timedelta_object(self):
     offset = timedelta(days=-1)
     now = time.time()
     with timecop.freeze(offset):
         self.assertAlmostEqual(now - self.SECONDS_IN_A_DAY,
                                time.time(),
                                delta=0.9)
Beispiel #9
0
 def test_validating_invalid_totp_for_same_secret(self):
     """
     Test case when the same secret is used, but the token differs
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         self.assertFalse(valid_totp(get_totp(secret)+1, secret))
Beispiel #10
0
 def test_validating_totp_for_same_secret(self):
     """
     Check if validating TOTP generated for the same secret works
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         self.assertTrue(valid_totp(get_totp(secret), secret))
Beispiel #11
0
 def test_validating_correct_hotp_as_totp(self):
     """
     Check if valid TOTP will work as HOTP - should not work, unless for
     very big interval number (matching Unix epoch timestamp)
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         self.assertFalse(valid_totp(get_hotp(secret, 1), secret))
Beispiel #12
0
 def test_validating_correct_hotp_as_totp(self):
     """
     Check if valid TOTP will work as HOTP - should not work, unless for
     very big interval number (matching Unix epoch timestamp)
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         self.assertFalse(valid_totp(get_hotp(secret, 1), secret))
Beispiel #13
0
    def test_time_stops_with_freeze(self):
        now = time.time() 

        with timecop.freeze(now):
            # NB: time.sleep() and time.time() resolution should be plenty to detect
            # a sleep even of a few milliseconds but we're trading test run time for 
            # confidence that time is really frozen
            time.sleep(0.6)
            self.assertEqual(now, time.time())  # time ought not to have changed at all
Beispiel #14
0
    def test_yesterday(self):
        secs = time.time()
        secs -= self.SECONDS_IN_A_DAY

        # do it "right"
        yesterday = date.today() - timedelta(days=1)

        with timecop.freeze(secs):
            self.assertEqual(yesterday, date.today())
Beispiel #15
0
    def test_time_stops_with_freeze(self):
        now = time.time() 

        with timecop.freeze(now):
            # NB: time.sleep() and time.time() resolution should be plenty to detect
            # a sleep even of a few milliseconds but we're trading test run time for 
            # confidence that time is really frozen
            time.sleep(0.6)
            self.assertEqual(now, time.time())  # time ought not to have changed at all
Beispiel #16
0
    def test_yesterday(self):
        secs = time.time()
        secs -= self.SECONDS_IN_A_DAY

        # do it "right"
        yesterday = date.today() - timedelta(days=1)

        with timecop.freeze(secs):
            self.assertEqual(yesterday, date.today())
Beispiel #17
0
 def test_generating_current_totp_and_validating(self):
     """
     Check if TOTP generated for current time is the same as manually
     created HOTP for proper interval
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         hotp = get_hotp(secret=secret, intervals_no=int(time.time())//30,)
         totp = get_totp(secret=secret)
         self.assertEqual(hotp, totp)
Beispiel #18
0
 def test_generating_current_totp_as_string(self):
     """
     Check if the TOTP also works seamlessly when generated as string
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         hotp = get_hotp(
             secret=secret,
             intervals_no=int(time.time())//30,
             as_string=True,
         )
         totp = get_totp(secret=secret, as_string=True)
         self.assertEqual(hotp, totp)
Beispiel #19
0
 def test_generating_current_totp_as_string(self):
     """
     Check if the TOTP also works seamlessly when generated as string
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         hotp = get_hotp(
             secret=secret,
             intervals_no=int(time.time()) // 30,
             as_string=True,
         )
         totp = get_totp(secret=secret, as_string=True)
         self.assertEqual(hotp, totp)
Beispiel #20
0
 def test_generating_current_totp_and_validating(self):
     """
     Check if TOTP generated for current time is the same as manually
     created HOTP for proper interval
     """
     secret = b'MFRGGZDFMZTWQ2LK'
     with timecop.freeze(time.time()):
         hotp = get_hotp(
             secret=secret,
             intervals_no=int(time.time()) // 30,
         )
         totp = get_totp(secret=secret)
         self.assertEqual(hotp, totp)
Beispiel #21
0
    def test_validating_totp_with_a_window(self):
        """
        validate if a totp token falls within a certain window
        """
        secret = b'MFRGGZDFMZTWQ2LK'
        with timecop.freeze(time.time()):
            totp = get_totp(secret=secret, clock=(int(time.time()-30)))
            self.assertFalse(valid_totp(totp, secret))
            self.assertTrue(valid_totp(totp, secret, window=1))

            totp = get_totp(secret=secret, clock=(int(time.time()+30)))
            self.assertFalse(valid_totp(totp, secret))
            self.assertTrue(valid_totp(totp, secret, window=1))

            totp = get_totp(secret=secret, clock=(int(time.time()-60)))
            self.assertFalse(valid_totp(totp, secret))
            self.assertFalse(valid_totp(totp, secret, window=1))
            self.assertTrue(valid_totp(totp, secret, window=2))
Beispiel #22
0
    def test_validating_totp_with_a_window(self):
        """
        validate if a totp token falls within a certain window
        """
        secret = b'MFRGGZDFMZTWQ2LK'
        with timecop.freeze(time.time()):
            totp = get_totp(secret=secret, clock=(int(time.time() - 30)))
            self.assertFalse(valid_totp(totp, secret))
            self.assertTrue(valid_totp(totp, secret, window=1))

            totp = get_totp(secret=secret, clock=(int(time.time() + 30)))
            self.assertFalse(valid_totp(totp, secret))
            self.assertTrue(valid_totp(totp, secret, window=1))

            totp = get_totp(secret=secret, clock=(int(time.time() - 60)))
            self.assertFalse(valid_totp(totp, secret))
            self.assertFalse(valid_totp(totp, secret, window=1))
            self.assertTrue(valid_totp(totp, secret, window=2))
Beispiel #23
0
    def test_generating_totp_at_specific_clock(self):
        """
        check if the totp can be generated for a specific clock
        which is basically the same as hotp
        """
        secret = b'MFRGGZDFMZTWQ2LK'
        with timecop.freeze(time.time()):
            hotp = get_hotp(secret=secret, intervals_no=int(time.time())//30,)
            totp = get_totp(secret=secret, clock=None)
            self.assertEqual(hotp, totp)

            # hotp intervals minus 1
            hotp = get_hotp(
                secret=secret,
                intervals_no=int(time.time())//30-1,
            )
            # totp 30 seconds in the past
            totp = get_totp(secret=secret, clock=(int(time.time())-30))
            self.assertEqual(hotp, totp)
Beispiel #24
0
    def test_generating_totp_at_specific_clock(self):
        """
        check if the totp can be generated for a specific clock
        which is basically the same as hotp
        """
        secret = b'MFRGGZDFMZTWQ2LK'
        with timecop.freeze(time.time()):
            hotp = get_hotp(
                secret=secret,
                intervals_no=int(time.time()) // 30,
            )
            totp = get_totp(secret=secret, clock=None)
            self.assertEqual(hotp, totp)

            # hotp intervals minus 1
            hotp = get_hotp(
                secret=secret,
                intervals_no=int(time.time()) // 30 - 1,
            )
            # totp 30 seconds in the past
            totp = get_totp(secret=secret, clock=(int(time.time()) - 30))
            self.assertEqual(hotp, totp)
Beispiel #25
0
 def test_epoch(self):
     with timecop.freeze(0):
         # test assumes knowledge of when epoch is but that's ok
         self.assertEqual(date(1969, 12, 31), date.today())
Beispiel #26
0
 def test_epoch(self):
     with timecop.freeze(0):
         # test assumes knowledge of when epoch is but that's ok
         self.assertEqual(date(1969, 12, 31), date.today())
Beispiel #27
0
 def test_can_freeze_to_a_timedelta_object(self):
     offset = timedelta(days=-1)
     now = time.time()
     with timecop.freeze(offset):
         self.assertAlmostEqual(now - self.SECONDS_IN_A_DAY, time.time(), delta=0.9)