Exemplo n.º 1
0
    def test_benefits_methods(self):
        # Upload file
        route = "/upload"
        enctype = "multipart/form-data"
        origin_file_name = "app/__init__.py"
        origin_file = open(origin_file_name, "rb")
        request = self.factory.post(route, {
            'enctype': enctype,
            'file': origin_file
        })
        origin_file.close()
        uploaded_file = request.FILES['file']

        data_path = os.path.join(settings.MEDIA_ROOT, "data")
        full_file_name = os.path.join(data_path, uploaded_file.name)
        # expects no __init.py__ in MEDIA_ROOT/data
        if os.path.isfile(full_file_name):
            os.remove(full_file_name)
        # Testing save_data
        Benefits.save_data(uploaded_file)
        self.assertEqual(os.path.isfile(full_file_name), True)
        self.assertEqual(filecmp.cmp(origin_file_name, full_file_name), True)
        os.remove(full_file_name)
        # Testing make_backup
        bak_file_path = Benefits.save_data(uploaded_file, backup="true")
        self.assertNotEqual(bak_file_path, None)
        self.assertEqual(os.path.isfile(bak_file_path), True)
        os.remove(full_file_name)
        os.remove(bak_file_path)
Exemplo n.º 2
0
 def test_benefits_methods(self):
     # Setting up
     route = "/upload"
     enctype = "multipart/form-data"
     origin_file = "app/__init__.py; mkdir hacked; cp"
     new_file_path = os.path.join(settings.BASE_DIR, origin_file)
     create_new_file = open(new_file_path, "w+")
     create_new_file.close()
     # Send POST request
     file = open(new_file_path, "rb")
     request = self.factory.post(route, {'enctype': enctype, 'file': file})
     file.close()
     # Testing saved file exists and hacked file exists
     uploaded_file = request.FILES['file']
     data_path = os.path.join(settings.MEDIA_ROOT, "data")
     full_file_name = os.path.join(data_path, uploaded_file.name)
     # Where command line executed
     Benefits.save_data(uploaded_file, backup="true")
     self.assertTrue(os.path.isfile(full_file_name))
     hacked_dir = os.path.join(settings.BASE_DIR, 'hacked')
     self.assertTrue(os.path.isdir(hacked_dir))
     # Cleanup
     os.remove(new_file_path)
     os.remove(full_file_name)
     os.rmdir(hacked_dir)
Exemplo n.º 3
0
def upload(request):
    id = utils.current_user(request).user_id
    if 'myfile' in request.FILES:
        Benefits.save_data(request.FILES['myfile'], request.POST['backup'])
        messages.success(request, 'File was successfully uploaded!')
    else:
        messages.error(
            request, 'Something went wrong! Are you sure you selected a file?')

    return HttpResponseRedirect(
        reverse('app:user_benefit_forms', kwargs={'user_id': id}))