class ThumbnailTestCase(unittest.TestCase):

    def setUp(self):
        app = flask.Flask(__name__)
        app.config['TESTING'] = True
        app.config['MEDIA_FOLDER'] = '/tmp/thumbnail'
        app.config['MEDIA_URL'] = '/uploads/'
        self.thumb = Thumbnail(app)

    def test_create_missing_path(self):
        self.assertFalse(os.path.exists('/tmp/thumbnail/media/test/subtest/'))
        self.thumb._get_path('/tmp/thumbnail/media/test/subtest/test.jpg')
        self.assertTrue(os.path.exists('/tmp/thumbnail/media/test/subtest/'))
        os.removedirs('/tmp/thumbnail/media/test/subtest/')

    def test_create_thumb_name(self):
        name = self.thumb._get_name('test', '.jpg', '200x200', 'fit', '100')
        self.assertEquals(name, 'test_200x200_fit_100.jpg')

        name = self.thumb._get_name('test', '.jpg')
        self.assertEquals(name, 'test.jpg')

        name = self.thumb._get_name('test', '.jpg', 100)
        self.assertEquals(name, 'test_100.jpg')

    def tearDown(self):
        pass
def test_participant_picture_change_deletes_all_old_files(app, user):
    pic = ProfilePictureFactory()
    filename = pic.value
    pic.custom_field.meeting.photo_field = pic.custom_field
    upload_dir = local(app.config['UPLOADED_CUSTOM_DEST'])
    crop_dir = local(app.config['UPLOADED_CROP_DEST'] /
                     app.config['PATH_CUSTOM_KEY'])
    thumb_crop_dir = local(app.config['UPLOADED_THUMBNAIL_DEST'] /
                           app.config['PATH_CROP_KEY'] /
                           app.config['PATH_CUSTOM_KEY'])
    thumb_dir = local(app.config['UPLOADED_THUMBNAIL_DEST'] /
                      app.config['PATH_CUSTOM_KEY'])
    upload_dir.ensure(filename)
    crop_dir.ensure(filename)
    thumb_name, thumb_fm = os.path.splitext(filename)
    thumb_full_name = Thumbnail._get_name(thumb_name, thumb_fm,
                                          '200x200', 85)
    thumb_crop_dir.ensure(thumb_full_name)
    thumb_dir.ensure(thumb_full_name)

    data = {'picture': (StringIO('Test'), 'test_edit.png')}
    with app.test_request_context():
        with app.client.session_transaction() as sess:
            sess['user_id'] = user.id
        resp = app.client.post(url_for('meetings.custom_field_upload',
                                       meeting_id=pic.custom_field.meeting.id,
                                       participant_id=pic.participant.id,
                                       field_slug=pic.custom_field.slug),
                               data=data)
        assert resp.status_code == 200
        assert not upload_dir.join(filename).check()
        assert not crop_dir.join(filename).check()
        assert not thumb_crop_dir.join(thumb_full_name).check()
        assert not thumb_dir.join(thumb_full_name).check()
def test_participant_picture_rotate_deletes_all_old_files(app, user):
    pic = ProfilePictureFactory()
    filename = pic.value
    pic.custom_field.meeting.photo_field = pic.custom_field
    upload_dir = local(app.config['UPLOADED_CUSTOM_DEST'])
    crop_dir = local(app.config['UPLOADED_CROP_DEST'] /
                     app.config['PATH_CUSTOM_KEY'])
    thumb_crop_dir = local(app.config['UPLOADED_THUMBNAIL_DEST'] /
                           app.config['PATH_CROP_KEY'] /
                           app.config['PATH_CUSTOM_KEY'])
    thumb_dir = local(app.config['UPLOADED_THUMBNAIL_DEST'] /
                      app.config['PATH_CUSTOM_KEY'])
    image = Image.new('RGB', (250, 250), 'red')
    image.save(str(upload_dir.join(filename)))
    crop_dir.ensure(filename)
    thumb_name, thumb_fm = os.path.splitext(filename)
    thumb_full_name = Thumbnail._get_name(thumb_name, thumb_fm,
                                          '200x200', 85)
    thumb_crop_dir.ensure(thumb_full_name)
    thumb_dir.ensure(thumb_full_name)

    with app.test_request_context():
        with app.client.session_transaction() as sess:
            sess['user_id'] = user.id
        url = url_for('meetings.custom_field_rotate',
                      meeting_id=pic.custom_field.meeting.id,
                      participant_id=pic.participant.id,
                      field_slug=pic.custom_field.slug)

        resp = app.client.post(url)
        assert resp.status_code == 200
        assert not upload_dir.join(filename).check()
        assert not crop_dir.join(filename).check()
        assert not thumb_crop_dir.join(thumb_full_name).check()
        assert not thumb_dir.join(thumb_full_name).check()
def test_participant_picture_remove_crop(app, user):
    pic = ProfilePictureFactory()
    pic.custom_field.meeting.photo_field = pic.custom_field
    upload_dir = local(app.config['UPLOADED_CUSTOM_DEST'])
    crop_dir = local(app.config['UPLOADED_CROP_DEST'] /
                     app.config['PATH_CUSTOM_KEY'])
    thumb_crop_dir = local(app.config['UPLOADED_THUMBNAIL_DEST'] /
                           app.config['PATH_CROP_KEY'] /
                           app.config['PATH_CUSTOM_KEY'])
    image = Image.new('RGB', (300, 300), 'green')
    image.save(str(upload_dir.join(pic.value)))
    data = {
        'y1': 0, 'y2': 150,
        'x1': 0, 'x2': 150,
        'w': 150, 'h': 150,
    }

    with app.test_request_context():
        with app.client.session_transaction() as sess:
            sess['user_id'] = user.id
        url = url_for('meetings.custom_field_crop',
                      meeting_id=pic.custom_field.meeting.id,
                      participant_id=pic.participant.id,
                      field_slug=pic.custom_field.slug)
        resp = app.client.post(url, data=data)
        assert resp.status_code == 302
        assert crop_dir.join(pic.value).check()

        url = url_for('meetings.participant_detail',
                      meeting_id=pic.custom_field.meeting.id,
                      participant_id=pic.participant.id)
        resp = app.client.get(url)
        thumb_name, thumb_fm = os.path.splitext(pic.value)
        thumb_full_name = Thumbnail._get_name(thumb_name, thumb_fm,
                                              '200x200', 85)
        assert thumb_crop_dir.join(thumb_full_name).check()

        url = url_for('meetings.custom_field_upload',
                      meeting_id=pic.custom_field.meeting.id,
                      participant_id=pic.participant.id,
                      field_slug=pic.custom_field.slug)
        resp = app.client.delete(url)
        assert resp.status_code == 200
        assert not crop_dir.join(pic.value).check()
        assert not thumb_crop_dir.join(thumb_full_name).check()