def set_permissions(orcid, permissions=None): if permissions is None: permissions = [] needs = set() with app.app_context(): db = database.get_db() cursor = db.cursor() cursor.execute("select id from user where id=?", [orcid]) user = cursor.fetchone() if user is None: raise ValueError('No user with orcid "{}" in database.'.format(orcid)) for permission_name in permissions: permission_attr = "{}_permission".format(permission_name) permission = getattr(auth, permission_attr, None) if permission is None: raise ValueError("No such permission: {}".format(permission_name)) for need in permission.needs: needs.add(tuple(need)) cursor.execute("UPDATE user SET permissions = ? WHERE id = ?", [json.dumps(list(needs)), orcid]) db.commit()
def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True commands.init_db() commands.load_data(filepath('test-data.json')) self.client = app.test_client() with open(filepath('test-patch-replace-values-1.json')) as f: self.patch = f.read() with app.app_context(): self.unauthorized_identity = auth.add_user_or_update_credentials({ 'name': 'Dangerous Dan', 'access_token': 'f7e00c02-6f97-4636-8499-037446d95446', 'expires_in': 631138518, 'orcid': '0000-0000-0000-000X', }) db = database.get_db() curs = db.cursor() curs.execute('UPDATE user SET permissions = ? WHERE name = ?', ('[]', 'Dangerous Dan')) self.user_identity = auth.add_user_or_update_credentials({ 'name': 'Regular Gal', 'access_token': '5005eb18-be6b-4ac0-b084-0443289b3378', 'expires_in': 631138518, 'orcid': '1234-5678-9101-112X', }) self.admin_identity = auth.add_user_or_update_credentials({ 'name': 'Super Admin', 'access_token': 'f7c64584-0750-4cb6-8c81-2932f5daabb8', 'expires_in': 3600, 'orcid': '1211-1098-7654-321X', }, (ActionNeed('accept-patch'),)) db.commit()
def test_add_admin_user(self): with app.app_context(): row = database.query_db( 'SELECT permissions FROM user WHERE id = ?', (self.admin_identity.id,), one=True) self.assertEqual( row['permissions'], '[["action", "submit-patch"], ["action", "accept-patch"]]')
def load_data(datafile): with app.app_context(): with open(datafile) as f: data = json.load(f) user_id = "initial-data-loader" patch = JsonPatch.from_diff({}, data) patch_request_id = patching.create_request(patch, user_id) patching.merge(patch_request_id, user_id) database.commit()
def test_add_user(self): self.assertEqual( self.identity.id, 'http://orcid.org/1234-5678-9101-112X') self.assertEqual( self.identity.auth_type, 'bearer') with app.app_context(): row = database.query_db( 'SELECT name, permissions, b64token FROM user WHERE id = ?', (self.identity.id,), one=True) self.assertEqual(row['name'], 'Testy Testerson') self.assertEqual( row['permissions'], '[["action", "submit-patch"]]') self.assertEqual( row['b64token'], b'NTAwNWViMTgtYmU2Yi00YWMwLWIwODQtMDQ0MzI4OWIzMzc4')
def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True self.client = app.test_client() commands.init_db() with app.app_context(): self.identity = auth.add_user_or_update_credentials( {'name': 'Testy Testerson', 'access_token': '5005eb18-be6b-4ac0-b084-0443289b3378', 'expires_in': 631138518, 'orcid': '1234-5678-9101-112X'}) self.expired_identity = auth.add_user_or_update_credentials({ 'name': 'Eric Expired', 'access_token': 'f7c64584-0750-4cb6-8c81-2932f5daabb8', 'expires_in': -3600, 'orcid': '1211-1098-7654-321X', }) database.commit()
def setUp(self): self.db_fd, app.config['DATABASE'] = tempfile.mkstemp() app.config['TESTING'] = True self.client = app.test_client() commands.init_db() commands.load_data(filepath('test-data.json')) with app.app_context(): self.user_identity = auth.add_user_or_update_credentials({ 'name': 'Regular Gal', 'access_token': '5005eb18-be6b-4ac0-b084-0443289b3378', 'expires_in': 631138518, 'orcid': '1234-5678-9101-112X', }) self.admin_identity = auth.add_user_or_update_credentials({ 'name': 'Super Admin', 'access_token': 'f7c64584-0750-4cb6-8c81-2932f5daabb8', 'expires_in': 3600, 'orcid': '1211-1098-7654-321X', }, (ActionNeed('accept-patch'),)) database.commit()
def init_db(): with app.app_context(): db = database.get_db() with app.open_resource("schema.sql", mode="r") as schema_file: db.cursor().executescript(schema_file.read()) db.commit()