def test_get_set_attribute(self): assert UserAttribute.get_user_attribute(self.user, self.name) is None UserAttribute.set_user_attribute(self.user, self.name, self.value) assert UserAttribute.get_user_attribute(self.user, self.name) == self.value new_value = 'new_value' UserAttribute.set_user_attribute(self.user, self.name, new_value) assert UserAttribute.get_user_attribute(self.user, self.name) == new_value
def test_command_by_activation_keys(self): """ Test population of created_on_site attribute by activation keys. """ call_command( "populate_created_on_site_user_attribute", "--activation-keys", self.activation_keys, "--site-domain", self.site.domain ) for register_user in self.registered_users: self.assertEqual(UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE), self.site.domain) # Populate 'created_on_site' attribute with different site domain call_command( "populate_created_on_site_user_attribute", "--activation-keys", self.activation_keys, "--site-domain", self.site_other.domain ) for register_user in self.registered_users: # 'created_on_site' attribute already exists. Attribute's value will not change self.assertNotEqual( UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE), self.site_other.domain )
def test_get_set_attribute(self): self.assertIsNone(UserAttribute.get_user_attribute(self.user, self.name)) UserAttribute.set_user_attribute(self.user, self.name, self.value) self.assertEqual(UserAttribute.get_user_attribute(self.user, self.name), self.value) new_value = 'new_value' UserAttribute.set_user_attribute(self.user, self.name, new_value) self.assertEqual(UserAttribute.get_user_attribute(self.user, self.name), new_value)
def _get_batched_users(self, site_domain, users_queryset, offset, users_query_batch_size): """ Args: site_domain: site where we need unsynced users users_queryset: users_queryset to slice users_query_batch_size: slice size Returns: site users """ self.stdout.write( u'Fetching Users for site {site} from {start} to {end}'.format( site=site_domain, start=offset, end=offset + users_query_batch_size)) users = users_queryset.select_related( 'profile')[offset:offset + users_query_batch_size] site_users = [ user for user in users if UserAttribute.get_user_attribute( user, 'created_on_site') == site_domain ] self.stdout.write( u'\tSite Users={count}'.format(count=len(site_users))) return site_users
def test_with_invalid_site_domain(self, populate): """ Test management command with invalid site domain. """ fake_site_domain = 'fake-site-domain' with mock.patch('six.moves.input', return_value=populate): call_command("populate_created_on_site_user_attribute", "--users", self.user_ids, "--site-domain", fake_site_domain) for user in self.users: if populate == 'y': assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) == fake_site_domain else: assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) is None
def handle(self, *args, **options): site_domain = options['site_domain'] user_ids = options['users'].split(',') if options['users'] else [] activation_keys = options['activation_keys'].split( ',') if options['activation_keys'] else [] if not user_ids and not activation_keys: raise CommandError('You must provide user ids or activation keys.') try: Site.objects.get(domain__exact=site_domain) except Site.DoesNotExist: question = "The site you specified is not configured as a Site in the system. " \ "Are you sure you want to continue? (y/n):" if str(six.moves.input(question)).lower().strip()[0] != 'y': return for user_id in user_ids: try: user = User.objects.get(id=user_id) if UserAttribute.get_user_attribute(user, CREATED_ON_SITE): self.stdout.write( "created_on_site attribute already exists for user id: {id}" .format(id=user_id)) else: UserAttribute.set_user_attribute(user, CREATED_ON_SITE, site_domain) except User.DoesNotExist: self.stdout.write( "This user id [{id}] does not exist in the system.".format( id=user_id)) for key in activation_keys: try: user = Registration.objects.get(activation_key=key).user if UserAttribute.get_user_attribute(user, CREATED_ON_SITE): self.stdout.write( "created_on_site attribute already exists for user id: {id}" .format(id=user.id)) else: UserAttribute.set_user_attribute(user, CREATED_ON_SITE, site_domain) except Registration.DoesNotExist: self.stdout.write( "This activation key [{key}] does not exist in the system." .format(key=key))
def test_command_by_user_ids(self): """ Test population of created_on_site attribute by user ids. """ call_command("populate_created_on_site_user_attribute", "--users", self.user_ids, "--site-domain", self.site.domain) for user in self.users: assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) == self.site.domain # Populate 'created_on_site' attribute with different site domain call_command("populate_created_on_site_user_attribute", "--users", self.user_ids, "--site-domain", self.site_other.domain) for user in self.users: # 'created_on_site' attribute already exists. Attribute's value will not change assert UserAttribute.get_user_attribute( user, CREATED_ON_SITE) != self.site_other.domain
def test_command_with_invalid_arguments(self): """ Test management command with invalid user ids and activation keys. """ user = self.users[0] call_command( "populate_created_on_site_user_attribute", "--users", '9{id}'.format(id=user.id), # invalid id "--site-domain", self.site.domain ) self.assertIsNone(UserAttribute.get_user_attribute(user, CREATED_ON_SITE)) register_user = self.registered_users[0] call_command( "populate_created_on_site_user_attribute", "--activation-keys", "invalid-{key}".format(key=register_user.activation_key), # invalid key "--site-domain", self.site.domain ) self.assertIsNone(UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE))
def test_command_with_invalid_arguments(self): """ Test management command with invalid user ids and activation keys. """ user = self.users[0] call_command( "populate_created_on_site_user_attribute", "--users", f'9{user.id}', # invalid id "--site-domain", self.site.domain) assert UserAttribute.get_user_attribute(user, CREATED_ON_SITE) is None register_user = self.registered_users[0] call_command( "populate_created_on_site_user_attribute", "--activation-keys", f"invalid-{register_user.activation_key}", # invalid key "--site-domain", self.site.domain) assert UserAttribute.get_user_attribute(register_user.user, CREATED_ON_SITE) is None
def fetch_from_created_on_site_prop(user, domain): """ Fetch option. """ if not domain: return False return UserAttribute.get_user_attribute(user, 'created_on_site') == domain