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_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_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_other_object(self):
        # should work with an independent object having the same attributes
        data = UserData()
        data.max_num_joins = 1
        data.valid_until = time.time() + 10000
        mgr, testuser = self._get_mgr_with_one_user_and_data(data)

        result = mgr.may_join(UserIdentifier('foo', 'bar'))

        self.assert_state_allowed(result)
    def test_may_join_other_name_different_id_format(self):
        # also reject when the device IDs are formatted differently
        data = UserData()
        data.max_num_joins = 1
        data.valid_until = time.time() + 10000
        user = UserIdentifier('foo', 'AB-C0')
        mgr = self._get_mgr_with(user, data)

        result = mgr.may_join(UserIdentifier('other', 'ab:c0'))

        self.assert_state_blocked(result)
    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_may_join_other_name(self):
        # reject other users having the same device ID
        data = UserData()
        data.max_num_joins = 1
        data.valid_until = time.time() + 10000
        mgr, testuser = self._get_mgr_with_one_user_and_data(data)

        result = mgr.may_join(UserIdentifier('other', 'bar'))

        self.assert_state_blocked(result)

        # this should also apply if the stored user has no assigned data
        # (i.e. no state)
        mgr, testuser = self._get_mgr_with_one_user()

        result = mgr.may_join(UserIdentifier('other', 'bar'))

        self.assert_state_blocked(result)
    def test_user_id_equality(self):
        """
        Checks if the equality of two UserIdentifier objects ignores the
        password and data attributes. This is needed as UserIdentifier objects
        without all attributes set are used e.g. for querying.
        """
        identifier1 = UserIdentifier('foo', 'bar', 'pass')
        identifier2 = UserIdentifier('foo', 'bar', 'word')

        self.assertEqual(identifier1, identifier2)

        identifier3 = UserIdentifier('foo', 'bar', 'word')
        testdata = UserData()
        testdata.max_num_joins = 12
        identifier3.user_data = testdata

        self.assertEqual(identifier1, identifier3)
        self.assertEqual(identifier2, identifier3)

        self.assertNotEqual(identifier1, UserIdentifier('foo', 'baz', 'pass'))
        self.assertNotEqual(identifier1, UserIdentifier('fo0', 'bar', 'pass'))
        self.assertNotEqual(identifier1, 'some other type')
    def test_check_expired_time_and_num_valid(self):
        data = UserData()
        data.max_num_joins = 1
        data.valid_until = time.time() + 10000

        self._run_check_expired(data, False)
    def test_check_expired_num_exceeded(self):
        data = UserData()
        data.max_num_joins = 10
        data.num_joins = 11

        self._run_check_expired(data, True)
    def test_check_expired_num(self):
        data = UserData()
        data.max_num_joins = 10

        self._run_check_expired(data, False)