def test_list_files(self):
        db.session.add(
            models.Container('Gallery', '/home/user/pictures/gallery'))
        db.session.commit()

        rv = self.app.get('/container/1')
        self.assertEqual(rv.status_code, 200)
        assert '[]' in rv.data
 def test_delete_container(self):
     db.session.add(
         models.Container('Gallery', '/home/user/pictures/gallery'))
     db.session.commit()
     rv = self.app.delete('/container/1')
     self.assertEqual(rv.status_code, 200)
     container = models.Container.query.filter_by(name='Gallery').first()
     self.assertIsNone(container)
    def test_add_file_wrong_input(self):
        db.session.add(
            models.Container('Gallery', '/home/user/pictures/gallery'))
        db.session.commit()

        rv = self.app.post('/container/1/files/', data='{}')
        self.assertEqual(rv.status_code, 400)
        file = models.File.query.filter_by(
            path='/home/user/pictures/gallery/picture4.png').first()
        self.assertIsNone(file)
 def test_edit_container(self):
     db.session.add(
         models.Container('Gallery', '/home/user/pictures/gallery'))
     db.session.commit()
     rv = self.app.put(
         '/container/1',
         data='{"name":"Gallery 1", "path":"/home/user/pictures/gallery"}')
     self.assertEqual(rv.status_code, 200)
     container = models.Container.query.filter_by(name='Gallery 1').first()
     self.assertIsNotNone(container)
def add_container():
    data = json.JSONDecoder().decode(request.data)
    if 'name' not in data or 'files' not in data:
        return make_response('', 400)  # error missing parameter

    if not data['files']:
        return make_response('', 400)  # error missing parameter

    if 'output_path' in data:
        output_path = os.path.join(data['output_path'], data['name'] + cont_ext)  # if output_path is specified use it
    else:
        output_path = os.path.join(basedir, 'data', str(current_user.id), data['name'] + cont_ext)  # otherwise generate it

    container = models.Container(data['name'], output_path, policy_mode, current_user.id)  # create container
    db.session.add(container)  # and store it persistently
    db.session.commit()

    for f in data['files']:  # iterate through submitted files array
        if 'path' not in f or 'type' not in f or 'policy' not in f:
            return make_response('', 400)  # error missing file parameter

        if not os.path.isfile(f['path']):
            return make_response('', 404)  # error file does not exist

        # TODO: delete created container in error case

        f['policy'] = f['policy'].replace(', ', ',').replace(': ', ':')  # parse submitted policy
        policy = models.Policy.generate(f['policy'], current_user)  # create file
        db.session.add(models.File(f['path'], f['type'], policy, f['policy'], container.id))  # store in DB
    db.session.commit()

    # serialize container for response
    out = container.dict()
    out['files'] = list()
    for f in container.files:
        out['files'].append(f.dict())

    # generate AA parameter
    aa_param = dict()
    aa_param['files'] = list()
    for f in out['files']:
        aa_param['files'].append({
            "path": f['path'],
            "type": f['type'],
            "policy": f['policy']
        })
    aa_param['outfile'] = container.path
    aa_param['owner'] = {'emails': [current_user.email]} # embed identity of user into container

    aa_response = AttrAuth.encrypt_container(container, aa_param) # start encryption
    if aa_response is None:
        return make_response('', 500) # error AA error

    return make_response(json.dumps(out), 201) # successful encryption
    def test_show_file(self):
        container = models.Container('Gallery', '/home/user/pictures/gallery')
        db.session.add(container)
        db.session.commit()
        file = models.File('/home/user/pictures/gallery/picture4.png',
                           'PABE14', 'all', container.id)
        db.session.add(file)
        db.session.commit()

        rv = self.app.get('/container/1/files/1')
        self.assertEqual(rv.status_code, 200)
    def test_delete_file(self):
        container = models.Container('Gallery', '/home/user/pictures/gallery')
        db.session.add(container)
        db.session.commit()
        file = models.File('/home/user/pictures/gallery/picture4.png',
                           'PABE14', 'all', container.id)
        db.session.add(file)
        db.session.commit()

        rv = self.app.delete('/container/1/files/1')
        self.assertEqual(rv.status_code, 200)
        file = models.File.query.filter_by(id=1).first()
        self.assertIsNone(file)
    def test_add_file(self):
        db.session.add(
            models.Container('Gallery', '/home/user/pictures/gallery'))
        db.session.commit()

        rv = self.app.post(
            '/container/1/files/',
            data=
            '{"path":"/home/user/pictures/gallery/picture4.png", "type":"PABE14", "policy":"all"}'
        )
        self.assertEqual(rv.status_code, 201)
        file = models.File.query.filter_by(
            path='/home/user/pictures/gallery/picture4.png').first()
        self.assertIsNotNone(file)
    def test_edit_file_wrong_input(self):
        container = models.Container('Gallery', '/home/user/pictures/gallery')
        db.session.add(container)
        db.session.commit()
        file = models.File('/home/user/pictures/gallery/picture4.png',
                           'PABE14', 'all', container.id)
        db.session.add(file)
        db.session.commit()

        rv = self.app.put('/container/1/files/1', data='{}')
        self.assertEqual(rv.status_code, 400)
        file = models.File.query.filter_by(
            path='/home/user/pictures/gallery/picture5.png').first()
        self.assertIsNone(file)
 def test_edit_container_wrong_input(self):
     db.session.add(
         models.Container('Gallery', '/home/user/pictures/gallery'))
     db.session.commit()
     rv = self.app.put('/container/1', data='{"name":"Gallery 1"}')
     self.assertEqual(rv.status_code, 400)
Exemple #11
0
from bootstrap import db
from cm import models

c1 = models.Container('Gallery', '/home/user/pictures/gallery')
c2 = models.Container('Music', '/home/user/music')
c3 = models.Container('Summer 96', '/home/user/pictures/vacation')
c4 = models.Container('Work', '/home/user/work')
c5 = models.Container('Notes', '/home/user/notes')

db.session.add(c1)
db.session.add(c2)
db.session.add(c3)
db.session.add(c4)
db.session.add(c5)
db.session.commit()

f1 = models.File('/home/user/pictures/gallery/picture1.png', 'PABE14', 'all',
                 c1.id)
f2 = models.File('/home/user/pictures/gallery/picture2.png', 'PABE14', 'all',
                 c1.id)
f3 = models.File('/home/user/pictures/gallery/picture3.png', 'PABE14', 'all',
                 c1.id)

f4 = models.File('/home/user/music/song1.mp3', 'PABE14', 'all', c2.id)
f5 = models.File('/home/user/music/song2.mp3', 'PABE14', 'all', c2.id)
f6 = models.File('/home/user/music/song3.mp3', 'PABE14', 'all', c2.id)

f7 = models.File('/home/user/pictures/vacation/summer96_1.jpg', 'PABE14',
                 'all', c3.id)
f8 = models.File('/home/user/pictures/vacation/summer96_2.jpg', 'PABE14',
                 'all', c3.id)