def test_str_representation(self):
        # incrementally adds attributes and checks if they appear in str(item).
        # The str representation is important for several chat commands.
        identifier = UserIdentifier('foo', 'bar')
        # str needs to work without password and data
        self.assertIn('foo', str(identifier))
        self.assertIn('bar', str(identifier))

        identifier.password = '******'
        self.assertIn('passw0rd!', str(identifier))

        data = UserData()
        state_str = data.state_string()
        self.assertIn(state_str, str(data))

        # this is 1970-01-01 00:01:40
        data.valid_until = 100
        data.num_joins = 42
        data.max_num_joins = 1234
        # keep some freedoms on the actual output
        self.assertIn('1970', str(data))
        self.assertIn('00:01:40', str(data))
        self.assertIn('42', str(data))
        self.assertIn('1234', str(data))

        data_str = str(data)
        identifier.user_data = data
        self.assertIn(data_str, str(identifier))
    def test_check_expired_time_and_num_both_exceeded(self):
        data = UserData()
        data.max_num_joins = 1
        data.num_joins = 2
        data.valid_until = time.time() - 10000

        self._run_check_expired(data, True)
    def test_check_expired_time_and_num_one_exceeded(self):
        # num exceeded
        data = UserData()
        data.max_num_joins = 1
        data.num_joins = 2
        data.valid_until = time.time() + 10000

        self._run_check_expired(data, True)

        # time exceeded
        data = UserData()
        data.max_num_joins = 1
        data.num_joins = 0
        data.valid_until = time.time() - 10000

        self._run_check_expired(data, True)
    def _increment_num_join_helper(self, max_num_joins, num_joins, valid_until,
                                   expected_num_joins):
        data = UserData()
        data.max_num_joins = max_num_joins
        data.num_joins = num_joins
        data.valid_until = valid_until

        data.check_expired(True)

        self.assertEqual(data.num_joins, expected_num_joins)
    def test_may_join_blocked_user(self):
        data = UserData()
        data.max_num_joins = 1
        data.num_joins = 2
        valid_time = time.time() - 10000
        data.valid_until = valid_time
        data.join_state = UserData.JOIN_STATE_BLOCKED
        mgr, testuser = self._get_mgr_with_one_user_and_data(data)

        result = mgr.may_join(testuser)

        self.assert_state_blocked(result)
        # attributes should be unchanged
        self.assertEqual(data.max_num_joins, 1)
        self.assertEqual(data.num_joins, 2)
        self.assertEqual(data.valid_until, valid_time)
    def test_check_expired_num_exceeded(self):
        data = UserData()
        data.max_num_joins = 10
        data.num_joins = 11

        self._run_check_expired(data, True)