def test_id(self): from edwin.models.photo import Photo p = Photo(self.fname) self.assertEqual(p.id, None) p.id = '1234567890' p = Photo(self.fname) self.assertEqual(p.id, '1234567890')
def test_tags(self): from edwin.models.photo import Photo p = Photo(self.fname) self.assertEqual(len(p.tags), 0) p.tags = ['foo', 'bar'] p = Photo(self.fname) self.assertEqual(len(p.tags), 2) self.assertEqual(p.tags[0], 'foo') self.assertEqual(p.tags[1], 'bar')
def test_scale_by_height(self): from edwin.models.photo import Photo photo = Photo(self.photo_file) photo.id = '123456' from edwin.views.image import ImageApplication app = ImageApplication(self.tmpdir) version = app.version(photo, (3000, 300)) self.assertEqual(version['size'], (400, 300))
def test_rotate(self): from edwin.models.photo import Photo p = Photo(self.fname) self.assertEqual(p.size, (3072, 2304)) p.rotate(90) self.assertEqual(p.size, (2304, 3072)) self.assertEqual(p.image.size, (2304, 3072)) p.rotate(270) self.assertEqual(p._rotation, 0) self.assertEqual(p.size, (3072, 2304)) self.assertEqual(p.image.size, (3072, 2304))
def _make_photos(self, n, album=None): from edwin.models.photo import Photo import os import shutil import sys here = os.path.dirname(sys.modules[__name__].__file__) test_jpg = os.path.join(here, 'test.jpg') if album is None: album = self._make_one() for i in xrange(n): fpath = os.path.join(album.fspath, 'test%02d.jpg' % i) shutil.copy(test_jpg, fpath) photo = Photo(fpath) photo.title = 'Test %02d' % i
def test_timestamp(self): import datetime from edwin.models.photo import Photo p = Photo(self.fname) expected = datetime.datetime(2008, 12, 4, 22, 42, 57) self.assertEqual(p.timestamp, expected) expected = datetime.date(2008, 12, 4) self.assertEqual(p.date, expected) expected = datetime.date(1975, 7, 7) p.date = expected p = Photo(self.fname) self.assertEqual(p.date, expected)
def __getitem__(self, fname): fpath = os.path.join(self.fspath, fname) if os.path.isfile(fpath): photo = Photo(fpath) photo.__name__ = fname photo.__parent__ = self return photo if os.path.isdir(fpath): album = Album(fpath) album.__name__ = fname album.__parent__ = self return album raise KeyError(fname)
def test_unauthorized(self): from edwin.models.photo import Photo photo = Photo(self.photo_file) photo.id = '123456' from happy.acl import Allow from happy.acl import Everyone from edwin.views.image import ImageApplication app = ImageApplication(self.tmpdir) import os import webob request = webob.Request.blank('/image/123456.300x300.jpg') request.app_context = DummyAppContextCatalog(photo) response = app(request, '123456.300x300.jpg') self.assertEqual(response.status_int, 401)
def setUp(self): import os import shutil import sys import tempfile from edwin.models.album import Album from edwin.models.photo import Photo self.path = path = tempfile.mkdtemp('_test', 'edwin_') here = os.path.dirname(sys.modules[__name__].__file__) test_jpg = os.path.join(here, 'test.jpg') dst = os.path.join(path, 'test.jpg') os.symlink(test_jpg, dst) photo = Photo(dst) photo.title = 'Test Foo' self.album = Album(path)
def test_clear_cache(self): from edwin.models.photo import Photo photo = Photo(self.photo_file) photo.id = '123456' from edwin.views.image import ImageApplication app = ImageApplication(self.tmpdir) version = app.version(photo, (300, 300)) import os import webob cache_file = os.path.join(self.tmpdir, '123456.300x300.jpg') request = webob.Request.blank('/image/123456.300x300.jpg') request.app_context = DummyAppContextCatalog(photo) request.authenticated_principals = ['group.Administrators'] app(request, version['fname']) self.failUnless(os.path.exists(cache_file)) app.clear_cache() self.failIf(os.path.exists(cache_file))
def _make_repository(self, jpgs=['test.jpg', 'test2.jpg', 'test3.jpg'], date=(2007,2,15), visibility='new'): import datetime import os import shutil import sys import tempfile from edwin.models.album import Album from edwin.models.photo import Photo self.root_path = tempfile.mkdtemp('_test', 'edwin_') here = os.path.dirname(sys.modules[__name__].__file__) test_jpgs = [os.path.join(here, jpg) for jpg in jpgs] n = len(test_jpgs) from happy.acl import Allow from happy.acl import Everyone Album(self.root_path)._acl = [ (Allow, Everyone, 'view') ] if date is not None: year, month, day = 2007, 2, 15 for album_name in ['one', 'two']: if date is not None: day += 1 path = os.path.join(self.root_path, album_name) os.mkdir(path) album = Album(path) album.title = album_name.title() album.desc = 'Test %s' % album.title if date is not None: album.date_range = ( datetime.date(year, month, day), datetime.date(year, month, day), ) for i in xrange(5): fpath = os.path.join(album.fspath, 'test%02d.jpg' % i) shutil.copy(test_jpgs[i%n], fpath) photo = Photo(fpath) photo.title = 'Test %02d' % i photo.visibility = visibility
def test_it(self): from edwin.models.photo import Photo photo = Photo(self.photo_file) photo.id = '123456' from edwin.views.image import ImageApplication app = ImageApplication(self.tmpdir) version = app.version(photo, (300, 300)) self.assertEqual(version['fname'], '123456.300x300.jpg') self.assertEqual(version['size'], (300, 225)) import os import webob cache_file = os.path.join(self.tmpdir, '123456.300x300.jpg') request = webob.Request.blank('/image/123456.300x300.jpg') request.app_context = DummyAppContextCatalog(photo) request.authenticated_principals = ['group.Administrators'] response = app(request, version['fname']) self.assertEqual(response.content_type, 'image/jpeg') import Image image = Image.open(cache_file) self.assertEqual(image.size, (300, 225))
def test_metadata(self): from edwin.models.photo import Photo p = Photo(self.fname) p.photographer = 'Santa Clause' p.location = 'North Pole' p.title = 'Mrs. Clause and the elves' p.desc = 'Mrs. Clause and the elves blow off some steam.' self.assertEqual(p.version, 1) self.assertEqual(p.size, (3072, 2304)) del p p = Photo(self.fname) self.assertEqual(p.photographer, 'Santa Clause') self.assertEqual(p.location, 'North Pole') self.assertEqual(p.title, 'Mrs. Clause and the elves') self.assertEqual( p.desc, 'Mrs. Clause and the elves blow off some steam.' ) self.assertEqual(p.version, 1)
def setUp(self): # Create test fixtures import os import shutil import sys import tempfile tmpdir = tempfile.mkdtemp('edwin_', '_twill_test_fixture') here = os.path.dirname(sys.modules[__name__].__file__) test_jpg = os.path.join(here, 'test.jpg') photos_dir = os.path.join(tmpdir, 'var', 'photos') import datetime from edwin.models.album import Album from edwin.models.photo import Photo date = datetime.date(*self.start_date) from happy.acl import ALL_PERMISSIONS from happy.acl import Allow from happy.acl import Everyone os.makedirs(photos_dir) Album(photos_dir)._acl = [ (Allow, Everyone, ['view']), (Allow, 'group.Administrators', ALL_PERMISSIONS), ] for i in xrange(self.n_albums): parts = map(str, (date.year, date.month, date.day)) album_dir = os.path.join(photos_dir, *parts) os.makedirs(album_dir) album = Album(album_dir) album.title = str(date) for j in xrange(self.n_photos): photo_file = os.path.join(album_dir, 'photo_%02d.jpg' % j) os.symlink(test_jpg, photo_file) photo = Photo(photo_file) photo.title = 'Test %d' % j photo.visibility = self.initial_visibility album.update_acl() date += datetime.timedelta(days=self.delta_days) etc = os.path.join(tmpdir, 'etc') os.mkdir(etc) for fname in ('test.ini', 'htpasswd', 'principals'): src = os.path.join(here, fname) dst = os.path.join(etc, fname) os.symlink(src, dst) ini_file = os.path.join(etc, 'test.ini') from edwin.config import ApplicationContext app_context = ApplicationContext(ini_file) app_context.catalog.scan() # Twill is unable to parse the standard login form. I don't know why. # I don't want to f**k with it any more. Substitute this very easy # to parse one. def login_template(**kw): return u""" <html> <head> <title>WTF?</title> </head> <body> <form method="POST" name="login"> <input type="hidden" name="redirect_to" value="%(redirect_to)s"> <input name="login" value="%(login)s"> <input name="password" type="password"> <input type="submit" value="log in"> %(status_msg)s </form> </body> </html> """ % kw # Init wsgi app from edwin.application import make_app app = make_app(ini_file, login_template=login_template) def get_app(): return app # Register wsgi intercept and visit homepage to start off import twill twill.add_wsgi_intercept('localhost', 8080, get_app) twill.commands.go('http://localhost:8080/') twill.commands.code(200) self.tmpdir = tmpdir
def test_evolve1(self): from edwin.models.photo import Photo from edwin.models.metadata import OldMetadata import os metadata = OldMetadata(self.fname) p = Photo(self.fname) new_metadata = p._metadata._file p._metadata = metadata p._evolve() self.assertEqual(p.visibility, 'new') os.remove(new_metadata) metadata['published'] = True del metadata['version'] metadata.save() p = Photo(self.fname) p._metadata = metadata p._evolve() self.assertEqual(p.visibility, 'public') os.remove(new_metadata) metadata['published'] = False del metadata['version'] metadata.save() p = Photo(self.fname) p._metadata = metadata p._evolve() self.assertEqual(p.visibility, 'private')