def test_file_upload(self): # Construct form data as a dictionary, # Use Py StringIO Class to simulate a file object data = { "file": (StringIO("File contents"), "test.txt") } # Send dict to /api/files with content type of multipart/form-data response = self.client.post("/api/files", data=data, content_type="multipart/form-data", headers=[("Accept", "application/json")] ) # Response status 201 CREATED expected self.assertEqual(response.status_code, 201) # Response in JSON? self.assertEqual(response.mimetype, "application/json") # Decode JSON data = json.loads(response.data) # Validate file path self.assertEqual(urlparse(data["path"]).path, "/uploads/test.txt") # Create path from utils.py method path = upload_path("test.txt") # Verify file is in expected location self.assertTrue(os.path.isfile(path)) # Read the file contents with open(path) as f: contents = f.read() # Verify contents are as expected self.assertEqual(contents, "File contents")
def test_file_upload(self): data = { "file": (BytesIO(b"File contents"), "test.txt") } response = self.client.post("/api/files", data=data, content_type="multipart/form-data", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(urlparse(data["path"]).path, "/uploads/test.txt") path = upload_path("test.txt") self.assertTrue(os.path.isfile(path)) with open(path, "rb") as f: contents = f.read() self.assertEqual(contents, b"File contents")
def test_file_upload(self): # Construct form data as a dictionary, # Use Py StringIO Class to simulate a file object data = {"file": (StringIO("File contents"), "test.txt")} # Send dict to /api/files with content type of multipart/form-data response = self.client.post("/api/files", data=data, content_type="multipart/form-data", headers=[("Accept", "application/json")]) # Response status 201 CREATED expected self.assertEqual(response.status_code, 201) # Response in JSON? self.assertEqual(response.mimetype, "application/json") # Decode JSON data = json.loads(response.data) # Validate file path self.assertEqual(urlparse(data["path"]).path, "/uploads/test.txt") # Create path from utils.py method path = upload_path("test.txt") # Verify file is in expected location self.assertTrue(os.path.isfile(path)) # Read the file contents with open(path) as f: contents = f.read() # Verify contents are as expected self.assertEqual(contents, "File contents")
def tearDown(self): """ Test teardown """ session.close() # Remove the tables and their data from the database Base.metadata.drop_all(engine) # Delete test upload folder shutil.rmtree(upload_path())
def setUp(self): """ Test setup """ self.client = app.test_client() # Set up the tables in the database Base.metadata.create_all(engine) # Create folder for test uploads os.mkdir(upload_path())
def test_get_uploaded_files(self): path = upload_path('test.txt') with open(path, 'wb') as f: f.write(b'File contents') response = self.client.get('/uploads/test.txt') self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'text/plain') self.assertEqual(response.data, b'File contents')
def test_get_uploaded_file(self): path = upload_path('test.txt') with open(path, 'wb') as f: f.write(b'File contents') response = self.client.get('/uploads/test.txt') self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'text/plain') self.assertEqual(response.data, b'File contents')
def test_get_uploaded_file(self): path = upload_path("test.txt") with open(path, "w") as f: f.write("File contents") response = self.client.get("/uploads/test.txt") self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "text/plain") self.assertEqual(response.data, b"File contents")
def test_get_uploaded_file(self): path = upload_path("test.txt") with open(path, "wb") as f: f.write(b"File contents") response = self.client.get("/uploads/test.txt") self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "text/plain") self.assertEqual(response.data, b"File contents")
def setUp(self): """ Test setup """ # Configure our app to use the testing database app.config.from_object('tuneful.config.TestingConfig') self.client = app.test_client() # Set up the tables in the database Base.metadata.create_all(engine) # Create folder for test uploads try: os.mkdir(upload_path()) except OSError: print "Folder already created!"
def test_get_uploaded_file(self): # create the upload path with the filename # upload_path() is defined in utils.py path = upload_path("test.txt") # Fill the file with some foo with open(path, "w") as f: f.write("File contents") # Obtain the response from the app response = self.client.get("/uploads/test.txt") # Was the request successful? 200 OK expected. self.assertEqual(response.status_code, 200) # Is the response in plain text? self.assertEqual(response.mimetype, "text/plain") # Is the response data the contents of the file uploaded? self.assertEqual(response.data, "File contents")
def test_file_upload(self): data = {"file": (BytesIO(b"File contents"), "test.txt")} response = self.client.post("/api/files", data=data, content_type="multipart/form-data", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(urlparse(data["path"]).path, "/uploads/test.txt") path = upload_path("test.txt") self.assertTrue(os.path.isfile(path)) with open(path, "rb") as f: contents = f.read() self.assertEqual(contents, b"File contents")
def test_file_upload(self): data = {'file': (BytesIO(b'File contents'), 'test.txt')} response = self.client.post('/api/files', data=data, content_type='multipart/form-data', headers=[('Accept', 'application/json')]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(urlparse(data['path']).path, '/uploads/test.txt') path = upload_path('test.txt') self.assertTrue(os.path.isfile(path)) with open(path, 'rb') as f: contents = f.read() self.assertEqual(contents, b'File contents')
def test_file_upload(self): data = { 'file': (BytesIO(b'File contents'), 'test.txt') } response = self.client.post('/api/files', data = data, content_type = 'multipart/form-data', headers = [('Accept', 'application/json')] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(urlparse(data['path']).path, '/uploads/test.txt') path = upload_path('test.txt') self.assertTrue(os.path.isfile(path)) with open(path, 'rb') as f: contents = f.read() self.assertEqual(contents, b'File contents')