def setUp(self):
     self.user = User.objects.create_user(username='******', password='******')
     self.group = Group.objects.create(name='permissionários')
     self.user.groups.add(self.group)
     self.user.save()
     self.client.login(username='******', password='******')
     self.apartment = Apartment.objects.create(block='RN', number='101', user=self.user)
     self.resident = Resident.objects.create(
         post='MJ', full_name='Bruno Luiz Santana de Araujo',
         war_name='Santana', cpf='12345678901',
         email='*****@*****.**', apartment=self.apartment
     )
     file = SimpleUploadedFile('file.jpg', b'file_content', content_type='image/jpg')
     file.size = 10500000
     self.data = dict(content='Mensagem de contato do usuário', identify='1', file=file)
Exemple #2
0
    def test_upload_document_form_size_limit(self):
        form_data = {
            'title':
            'GeoNode Map',
            'permissions':
            '{"anonymous":"document_readonly","authenticated":"resourcebase_readwrite","users":[]}',
        }
        test_file = SimpleUploadedFile('test_img_file.gif',
                                       self.imgfile.read(), 'image/gif')
        test_file.size = settings.DEFAULT_MAX_UPLOAD_SIZE * 5  # Set as a large file

        file_data = {'doc_file': test_file}
        form = DocumentCreateForm(form_data, file_data)

        self.assertFalse(form.is_valid())
        expected_error = (
            f"File size size exceeds {filesizeformat(settings.DEFAULT_MAX_UPLOAD_SIZE)}. "
            f"Please try again with a smaller file.")
        self.assertEqual(form.errors, {'doc_file': [expected_error]})
    def file_complete(self, file_size):
        """
        Signal that a file has completed.
        NOTE: When activated, file size DOES NOT corresponds to the actual size.
        NOTE: When activated, file size IS NOT accumulated by all the chunks.
        In this case, it represents the total content_length of the request.
        """
        if not self.activated:
            return

        # Create a concrete `UploadedFile` with empty content and `file_size` size
        uploaded_file = SimpleUploadedFile(
            name=self.file_name,
            content=b"",  # Empty Content
            content_type=self.content_type,
        )
        uploaded_file.field_name = self.field_name
        uploaded_file.size = file_size  # Set Size
        uploaded_file.charset = self.charset
        uploaded_file.content_type_extra = self.content_type_extra

        return uploaded_file
Exemple #4
0
    def test_create_user(self):
        """test for signup route"""
        # required fields must be present ['first_name', 'last_name' and 'email'] # noqa
        payload = {"user": {}}
        response = self.client.post(self.create_url,
                                    payload,
                                    content_type='application/json')
        self.assertEqual(len(response.data.get('user')), 4)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        # profile photo should upload an actual file
        photo = SimpleUploadedFile("photo.jpeg",
                                   "nice photo".encode(),
                                   content_type="image/jpeg")
        photo.size = (1048576 * settings.MAX_PHOTO_UPLOAD_SIZE_MB) - 10
        payload = {"user": {}, "photo": photo}
        response = self.client.post(self.create_url, payload)
        photo_reponse = response.data.get('photo')[0]
        self.assertEqual(
            photo_reponse,
            "Upload a valid image. The file you uploaded was either not an image or a corrupted image."
        )  # noqa
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        # profile photo size should at most th value MAX_PHOTO_UPLOAD_SIZE_MB
        with self.settings(MAX_PHOTO_UPLOAD_SIZE_MB=0.1):
            mock_file_path = os.path.join(os.getcwd(),
                                          './api/tests/sample_photo.png')
            with open(mock_file_path, 'rb') as file:
                payload["photo"] = file
                response = self.client.post(self.create_url, payload)
                photo_reponse = response.data.get('photo')[0]
                expect_response = "The max file size that can be uploaded is \
{}MB. Set MAX_PHOTO_UPLOAD_SIZE_MB env var to modify limit".format(
                    settings.MAX_PHOTO_UPLOAD_SIZE_MB)  # noqa
                self.assertEqual(photo_reponse, expect_response)
                self.assertEqual(response.status_code,
                                 status.HTTP_400_BAD_REQUEST)

        # should signup when we provide the right data
        mock_file_path = os.path.join(os.getcwd(),
                                      './api/tests/sample_photo.png')  # noqa
        with open(mock_file_path, 'rb') as file:
            name = fake.name().split()
            email = fake.email()
            payload = {
                'user.email': [email],
                'user.first_name': [name[0]],
                'user.last_name': [name[1]],
                'user.password': [name[1]],
                'photo': [file],
                # the file is now empty so we need to open it again
                'international_passport': [open(mock_file_path, 'rb')]
            }
            response = self.client.post(self.create_url, payload)
            data = response.data
            user_data = response.data.get('user')
            self.assertEqual(user_data.get('email'), email)
            self.assertEqual(user_data.get('first_name'), name[0])
            self.assertIn('token', data)
            self.assertEqual(response.status_code, status.HTTP_201_CREATED)