blob: 9a2847ce47144ac4ca504b12fe003fe1287cd99a [file] [log] [blame]
# Copyright 2014 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for file utility functions."""
import cloudstorage
import unittest
from google.appengine.ext import blobstore
from melange.utils import files as files_utils
from tests import test_utils
TEST_FILE_PATH = 'tests/images/melange_logo.png'
TEST_OBJECT_NAME = 'test-object'
TEST_BUCKET_NAME = 'test-bucket'
def _getTestObjectPath():
"""Returns a test path for an object in Cloud Storage."""
return '/' + TEST_BUCKET_NAME + '/' + TEST_OBJECT_NAME
class SaveFileTest(test_utils.SoCTestCase):
"""Unit tests for saveFile function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testFileSaved(self):
"""Tests that the file is saved correctly."""
with open(TEST_FILE_PATH, 'r') as test_file:
new_path = files_utils.buildObjectPath(
TEST_OBJECT_NAME, bucket_name=TEST_BUCKET_NAME)
object_path = files_utils.saveFile(
test_file, new_path)
test_file.seek(0)
expected_size = len(test_file.read())
# check that the returned path is correct
self.assertEqual(_getTestObjectPath(), object_path)
# check that the file is saved correctly
with cloudstorage.open(object_path) as file_object:
self.assertEqual(expected_size, len(file_object.read()))
class DeleteFileTest(test_utils.SoCTestCase):
"""Unit tests for deleteFile function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testFileDeleted(self):
"""Tests that a file is deleted correctly."""
with cloudstorage.open(_getTestObjectPath(), 'w') as gcs_file:
gcs_file.write('unused-file-content')
files_utils.deleteFile(_getTestObjectPath())
# check that the field is not found, because it is deleted
with self.assertRaises(cloudstorage.NotFoundError):
cloudstorage.open(_getTestObjectPath(), 'r')
class StatFileTest(test_utils.SoCTestCase):
"""Unit tests for statFile function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testFileExists(self):
"""Tests that a file exists."""
with cloudstorage.open(_getTestObjectPath(), 'w') as gcs_file:
gcs_file.write('unused-file-content')
self.assertTrue(files_utils.statFile(_getTestObjectPath()))
def testFileMissing(self):
"""Tests that a file does not exist."""
self.assertFalse(
files_utils.statFile(_getTestObjectPath() + "garbage"))
class GetBlobKeyTest(test_utils.SoCTestCase):
"""Unit tests for getBlobKey function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.init()
def testBlobKeyReturned(self):
"""Tests that a blob key is returned by the function."""
blob_key = files_utils.getBlobKey('path/to/object')
self.assertIsInstance(blob_key, blobstore.BlobKey)
class CleanFilenameTest(unittest.TestCase):
"""Unit tests for cleanFilename function."""
def testCleanFilenames(self):
"""Tests that ok filenames don't change."""
inputs = [
'hello.py',
'MixEdCase.ZiP',
'I has a space and a comma, dude',
'Dash-Dash-Dash',
'Underscore_This',
None
]
for filename in inputs:
self.assertEqual(filename, files_utils.cleanFilename(filename))
def testDirtyFilenames(self):
"""Tests that improper filenames are rewritten."""
inputs = [
('-bad start', '_bad start'),
('.bad start', '_bad start'),
(',bad start,', '_bad start,'),
('$bad start,', '_bad start,'),
('invalid character$%@!\\', 'invalid character_____')
]
for (input, want) in inputs:
self.assertEqual(want, files_utils.cleanFilename(input))