Esempio n. 1
0
    def test_user_file_local_sites(self):
        """Testing user FileAttachment create with local site"""
        user = User.objects.get(username='******')
        local_site = LocalSite.objects.get(name='local-site-1')

        form = UploadUserFileForm(files={})
        self.assertTrue(form.is_valid())

        file_attachment = form.create(user, local_site)

        self.assertEqual(file_attachment.user, user)
        self.assertEqual(file_attachment.local_site, local_site)
Esempio n. 2
0
    def test_user_file_local_sites(self):
        """Testing user FileAttachment create with local site"""
        user = User.objects.get(username='******')
        local_site = LocalSite.objects.get(name='local-site-1')

        form = UploadUserFileForm(files={})
        self.assertTrue(form.is_valid())

        file_attachment = form.create(user, local_site)

        self.assertEqual(file_attachment.user, user)
        self.assertEqual(file_attachment.local_site, local_site)
Esempio n. 3
0
    def create(self, request, local_site_name=None, *args, **kwargs):
        """Creates a new file attachment that is owned by the user.

        This accepts any file type and associates it with the user. Optionally,
        the file may be omitted here and uploaded later by updating the file
        attachment.

        If file data is provided, then it is expected that the data will be
        encoded as :mimetype:`multipart/form-data`. The file's name and content
        should be stored in the ``path`` field. A typical request may look
        like::

            -- SoMe BoUnDaRy
            Content-Disposition: form-data; name=path; filename="foo.zip"

            <Content here>
            -- SoMe BoUnDaRy --
        """
        try:
            user = resources.user.get_object(request,
                                             local_site_name=local_site_name,
                                             *args,
                                             **kwargs)
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        local_site = self._get_local_site(local_site_name)

        if ((local_site and not local_site.is_accessible_by(request.user))
                or not self.has_list_access(request, user)):
            return self.get_no_access_error(request)

        form = UploadUserFileForm(request.POST, request.FILES)

        if not form.is_valid():
            return INVALID_FORM_DATA, {
                'fields': self._get_form_errors(form),
            }

        file_attachment = form.create(request.user, local_site)

        return 201, {
            self.item_result_key:
            self.serialize_object(file_attachment,
                                  request=request,
                                  *args,
                                  **kwargs),
        }
Esempio n. 4
0
    def test_user_file_with_upload_file(self):
        """Testing user FileAttachment create with initial file"""
        user = User.objects.get(username='******')
        uploaded_file = self.make_uploaded_file()

        form = UploadUserFileForm(files={
            'path': uploaded_file,
        })
        self.assertTrue(form.is_valid())

        file_attachment = form.create(user)

        self.assertEqual(file_attachment.user, user)
        self.assertTrue(os.path.basename(file_attachment.file.name).endswith(
            '__trophy.png'))
        self.assertEqual(file_attachment.mimetype, 'image/png')
Esempio n. 5
0
    def test_user_file_with_upload_file(self):
        """Testing user FileAttachment create with initial file"""
        user = User.objects.get(username='******')
        uploaded_file = self.make_uploaded_file()

        form = UploadUserFileForm(files={
            'path': uploaded_file,
        })
        self.assertTrue(form.is_valid())

        file_attachment = form.create(user)

        self.assertEqual(file_attachment.user, user)
        self.assertTrue(
            os.path.basename(file_attachment.file.name).endswith('__logo.png'))
        self.assertEqual(file_attachment.mimetype, 'image/png')
Esempio n. 6
0
    def test_user_file_is_mutably_by(self):
        """Testing user FileAttachment.is_mutable_by"""
        creating_user = User.objects.get(username='******')
        admin_user = User.objects.get(username='******')
        same_site_user = User.objects.get(username='******')
        different_site_user = User.objects.get(username='******')

        local_site = LocalSite.objects.get(name='local-site-1')
        local_site.users.add(same_site_user)

        form = UploadUserFileForm(files={})
        self.assertTrue(form.is_valid())
        file_attachment = form.create(creating_user, local_site)

        self.assertTrue(file_attachment.is_mutable_by(admin_user))
        self.assertTrue(file_attachment.is_mutable_by(creating_user))
        self.assertFalse(file_attachment.is_mutable_by(same_site_user))
        self.assertFalse(file_attachment.is_mutable_by(different_site_user))
Esempio n. 7
0
    def test_user_file_is_mutably_by(self):
        """Testing user FileAttachment.is_mutable_by"""
        creating_user = User.objects.get(username='******')
        admin_user = User.objects.get(username='******')
        same_site_user = User.objects.get(username='******')
        different_site_user = User.objects.get(username='******')

        local_site = LocalSite.objects.get(name='local-site-1')
        local_site.users.add(same_site_user)

        form = UploadUserFileForm(files={})
        self.assertTrue(form.is_valid())
        file_attachment = form.create(creating_user, local_site)

        self.assertTrue(file_attachment.is_mutable_by(admin_user))
        self.assertTrue(file_attachment.is_mutable_by(creating_user))
        self.assertFalse(file_attachment.is_mutable_by(same_site_user))
        self.assertFalse(file_attachment.is_mutable_by(different_site_user))
    def create(self, request, local_site_name=None, *args, **kwargs):
        """Creates a new file attachment that is owned by the user.

        This accepts any file type and associates it with the user. Optionally,
        the file may be omitted here and uploaded later by updating the file
        attachment.

        If file data is provided, then it is expected that the data will be
        encoded as :mimetype:`multipart/form-data`. The file's name and content
        should be stored in the ``path`` field. A typical request may look
        like::

            -- SoMe BoUnDaRy
            Content-Disposition: form-data; name=path; filename="foo.zip"

            <Content here>
            -- SoMe BoUnDaRy --
        """
        try:
            user = resources.user.get_object(
                request, local_site_name=local_site_name, *args, **kwargs)
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        local_site = self._get_local_site(local_site_name)

        if ((local_site and not local_site.is_accessible_by(request.user)) or
           not self.has_list_access(request, user)):
            return self.get_no_access_error(request)

        form = UploadUserFileForm(request.POST, request.FILES)

        if not form.is_valid():
            return INVALID_FORM_DATA, {
                'fields': self._get_form_errors(form),
            }

        file_attachment = form.create(request.user, local_site)

        return 201, {
            self.item_result_key: self.serialize_object(
                file_attachment, request=request, *args, **kwargs),
        }
Esempio n. 9
0
    def test_user_file_add_file_after_create(self):
        """Testing user FileAttachment create without initial file and
        adding file through update
        """
        user = User.objects.get(username='******')

        form = UploadUserFileForm(files={})
        self.assertTrue(form.is_valid())

        file_attachment = form.create(user)
        self.assertFalse(file_attachment.file)
        self.assertEqual(file_attachment.user, user)

        uploaded_file = self.make_uploaded_file()
        form = UploadUserFileForm(files={
            'path': uploaded_file,
        })
        self.assertTrue(form.is_valid())

        file_attachment = form.update(file_attachment)

        self.assertTrue(os.path.basename(file_attachment.file.name).endswith(
            '__logo.png'))
        self.assertEqual(file_attachment.mimetype, 'image/png')