def test_create_from_pathname_permission_denied_integrity(self): BundleStream.objects.all().delete() BundleStream.create_from_pathname("/anonymous/name/", user=None) self.assertRaises( IntegrityError, BundleStream.create_from_pathname, "/anonymous/name/", user=None)
def test_create_from_pathname(self): user = User.objects.get_or_create(username="******")[0] bundle_stream = BundleStream.create_from_pathname( "/private/personal/owner/name/", user) self.assertEqual(bundle_stream.pathname, "/private/personal/owner/name/") group = Group.objects.create(name='group') group.user_set.add(user) bundle_stream = BundleStream.create_from_pathname( "/private/team/group/name/", user) self.assertEqual(bundle_stream.pathname, "/private/team/group/name/") bundle_stream = BundleStream.create_from_pathname("/anonymous/name/", user=None) self.assertEqual(bundle_stream.pathname, "/anonymous/name/")
def test_create_from_pathname(self): user = User.objects.get_or_create(username="******")[0] bundle_stream = BundleStream.create_from_pathname( "/private/personal/owner/name/", user) self.assertEqual(bundle_stream.pathname, "/private/personal/owner/name/") group = Group.objects.create(name='group') group.user_set.add(user) bundle_stream = BundleStream.create_from_pathname( "/private/team/group/name/", user) self.assertEqual(bundle_stream.pathname, "/private/team/group/name/") bundle_stream = BundleStream.create_from_pathname( "/anonymous/name/", user=None) self.assertEqual(bundle_stream.pathname, "/anonymous/name/")
def create_bundle_stream(pathname): """ Create, or get an existing bundle stream designated by the provided pathname. The pathname is parsed and decomposed to determine the user/group and slug. Users and groups are created if necessary. """ try: return BundleStream.objects.get(pathname=pathname) except BundleStream.DoesNotExist: user_username, group_name, slug, is_public, is_anonymous = BundleStream.parse_pathname(pathname) if user_username is None and group_name is None: # Here we trick a little - since the introduction of the # django-restricted-resource each object has a principal # owner - either a user or a group. This information can be # conveyed from the pathname _except_ for anonymous streams # that are a remnant of the past. For those streams we just # create a dummy user. user_username = "******" if user_username is not None: user = User.objects.get_or_create(username=user_username)[0] else: user = None if group_name is not None: group = Group.objects.get_or_create(name=group_name)[0] else: group = None bundle_stream = BundleStream.objects.create( user=user, group=group, slug=slug, is_public=is_public, is_anonymous=is_anonymous) bundle_stream.save() return bundle_stream
def create_bundle_stream(pathname): """ Create, or get an existing bundle stream designated by the provided pathname. The pathname is parsed and decomposed to determine the user/group and slug. Users and groups are created if necessary. """ try: return BundleStream.objects.get(pathname=pathname) except BundleStream.DoesNotExist: user_username, group_name, slug, is_public, is_anonymous = BundleStream.parse_pathname( pathname) if user_username is None and group_name is None: # Here we trick a little - since the introduction of the # django-restricted-resource each object has a principal # owner - either a user or a group. This information can be # conveyed from the pathname _except_ for anonymous streams # that are a remnant of the past. For those streams we just # create a dummy user. user_username = "******" if user_username is not None: user = User.objects.get_or_create(username=user_username)[0] else: user = None if group_name is not None: group = Group.objects.get_or_create(name=group_name)[0] else: group = None bundle_stream = BundleStream.objects.create(user=user, group=group, slug=slug, is_public=is_public, is_anonymous=is_anonymous) bundle_stream.save() return bundle_stream
def test_unicode(self): obj = BundleStream(pathname=self.pathname) self.assertEqual(unicode(obj), self.pathname)
def make_stream(self, pathname, name): """ Name ---- `make_stream` (`pathname`, `name`) Description ----------- Create a bundle stream with the specified pathname Arguments --------- `pathname`: string The pathname must refer to an anonymous stream `name`: string The name of the stream (free form description text) Return value ------------ pathname is returned Exceptions raised ----------------- 403 Pathname does not designate an anonymous stream 409 Bundle stream with the specified pathname already exists Available Since --------------- 0.3 """ # Work around bug https://bugs.launchpad.net/lava-dashboard/+bug/771182 # Older clients would send None as the name and this would trigger an # IntegrityError to be raised by BundleStream.objects.create() below # which in turn would be captured by the fault handler and reported as # an unrelated issue to the user. Let's work around that by using an # empty string instead. if name is None: name = "" try: user_name, group_name, slug, is_public, is_anonymous = BundleStream.parse_pathname( pathname) except ValueError as ex: raise xmlrpclib.Fault(errors.FORBIDDEN, str(ex)) # Start with those to simplify the logic below user = None group = None if is_anonymous is False: if self.user is not None: assert is_anonymous is False assert self.user is not None if user_name is not None: if not self.user.is_superuser: if user_name != self.user.username: raise xmlrpclib.Fault( errors.FORBIDDEN, "Only user {user!r} could create this stream". format(user=user_name)) user = self.user # map to real user object elif group_name is not None: try: if self.user.is_superuser: group = Group.objects.get(name=group_name) else: group = self.user.groups.get(name=group_name) except Group.DoesNotExist: raise xmlrpclib.Fault( errors.FORBIDDEN, "Only a member of group {group!r} could create this stream" .format(group=group_name)) else: assert is_anonymous is False assert self.user is None raise xmlrpclib.Fault( errors.FORBIDDEN, "Only anonymous streams can be constructed (you are not signed in)" ) else: assert is_anonymous is True assert user_name is None assert group_name is None if self.user is not None: user = self.user else: # Hacky but will suffice for now user = User.objects.get_or_create( username="******")[0] try: bundle_stream = BundleStream.objects.create( user=user, group=group, slug=slug, is_public=is_public, is_anonymous=is_anonymous, name=name) except IntegrityError: raise xmlrpclib.Fault( errors.CONFLICT, "Stream with the specified pathname already exists") else: return bundle_stream.pathname
def make_stream(self, pathname, name): """ Name ---- `make_stream` (`pathname`, `name`) Description ----------- Create a bundle stream with the specified pathname Arguments --------- `pathname`: string The pathname must refer to an anonymous stream `name`: string The name of the stream (free form description text) Return value ------------ pathname is returned Exceptions raised ----------------- 403 Pathname does not designate an anonymous stream 409 Bundle stream with the specified pathname already exists """ # Work around bug https://bugs.launchpad.net/lava-dashboard/+bug/771182 # Older clients would send None as the name and this would trigger an # IntegrityError to be raised by BundleStream.objects.create() below # which in turn would be captured by the fault handler and reported as # an unrelated issue to the user. Let's work around that by using an # empty string instead. if name is None: name = "" try: user_name, group_name, slug, is_public, is_anonymous = BundleStream.parse_pathname(pathname) except ValueError as ex: raise xmlrpclib.Fault(errors.FORBIDDEN, str(ex)) # Start with those to simplify the logic below user = None group = None if is_anonymous is False: if self.user is not None: assert is_anonymous is False assert self.user is not None if user_name is not None: if not self.user.is_superuser: if user_name != self.user.username: raise xmlrpclib.Fault( errors.FORBIDDEN, "Only user {user!r} could create this stream".format(user=user_name)) user = self.user # map to real user object elif group_name is not None: try: if self.user.is_superuser: group = Group.objects.get(name=group_name) else: group = self.user.groups.get(name=group_name) except Group.DoesNotExist: raise xmlrpclib.Fault( errors.FORBIDDEN, "Only a member of group {group!r} could create this stream".format(group=group_name)) else: assert is_anonymous is False assert self.user is None raise xmlrpclib.Fault( errors.FORBIDDEN, "Only anonymous streams can be constructed (you are not signed in)") else: assert is_anonymous is True assert user_name is None assert group_name is None if self.user is not None: user = self.user else: # Hacky but will suffice for now user = User.objects.get_or_create(username="******")[0] try: bundle_stream = BundleStream.objects.create( user=user, group=group, slug=slug, is_public=is_public, is_anonymous=is_anonymous, name=name) except IntegrityError: raise xmlrpclib.Fault( errors.CONFLICT, "Stream with the specified pathname already exists") else: return bundle_stream.pathname