def test_access_right_embargo(app): """Test access right embargo.""" assert AccessRight.get(AccessRight.OPEN) == "open" assert AccessRight.get(AccessRight.EMBARGOED) == "embargoed" # Embargo just lifted today. assert AccessRight.get(AccessRight.EMBARGOED, embargo_date=date.today()) == "open" # Future embargo date. assert AccessRight.get(AccessRight.EMBARGOED, embargo_date=date.today() + timedelta(days=1)) == "embargoed"
def test_access_right_embargo(): """Test access right embargo.""" assert AccessRight.get(AccessRight.OPEN) == 'open' assert AccessRight.get(AccessRight.EMBARGOED) == 'embargoed' # Embargo just lifted today. assert AccessRight.get(AccessRight.EMBARGOED, embargo_date=date.today()) == 'open' # Future embargo date. assert AccessRight.get( AccessRight.EMBARGOED, embargo_date=date.today()+timedelta(days=1)) \ == 'embargoed'
def test_access_right_embargo(): """Test access right embargo.""" assert AccessRight.get(AccessRight.OPEN) == 'open' assert AccessRight.get(AccessRight.EMBARGOED) == 'embargoed' # Embargo just lifted today. today = datetime.utcnow().date() assert AccessRight.get( AccessRight.EMBARGOED, embargo_date=today) == 'open' # Future embargo date. assert AccessRight.get( AccessRight.EMBARGOED, embargo_date=today+timedelta(days=1)) \ == 'embargoed'
def test_access_right(): """Test basic access right features.""" for val in ['open', 'embargoed', 'restricted', 'closed']: assert getattr(AccessRight, val.upper()) == val assert AccessRight.is_valid(val) assert not AccessRight.is_valid('invalid') assert AccessRight.as_title(AccessRight.OPEN) == 'Open Access' assert AccessRight.as_category(AccessRight.EMBARGOED) == 'warning' options = AccessRight.as_options() assert isinstance(options, tuple) assert options[0] == ('open', 'Open Access')
def test_access_right(app): """Test basic access right features.""" for val in ["open", "embargoed", "restricted", "closed"]: assert getattr(AccessRight, val.upper()) == val assert AccessRight.is_valid(val) assert not AccessRight.is_valid("invalid") assert AccessRight.as_title(AccessRight.OPEN) == "Open Access" assert AccessRight.as_category(AccessRight.EMBARGOED) == "warning" options = AccessRight.as_options() assert isinstance(options, tuple) assert options[0] == ("open", "Open Access")
def get_access_right(self, obj): """Get access right information.""" dt = obj.get('embargo_date') return AccessRight.get( obj['access_right'], embargo_date=parse(dt).date() if dt else None )
def has_access(user=None, record=None): """Check whether the user has access to the record. The rules followed are: 1. Open Access records can be viewed by everyone. 2. Embargoed, Restricted and Closed records can be viewed by the record owners. 3. Administrators can view every record. """ if AccessRight.get(record['access_right'], record.get('embargo_date')) \ == AccessRight.OPEN: return True user_id = int(user.get_id()) if user.is_authenticated else None if user_id in record.get('owners', []): return True if DynamicPermission(ActionNeed('admin-access')): return True try: token = session['accessrequests-secret-token'] recid = record['recid'] if SecretLink.validate_token(token, dict(recid=int(recid))): return True else: del session['accessrequests-secret-token'] except KeyError: pass return False
def update_expired_embargos(): """Release expired embargoes every midnight.""" record_ids = AccessRight.get_expired_embargos() for record in Record.get_records(record_ids): record['access_right'] = AccessRight.OPEN record.commit() db.session.commit() indexer = RecordIndexer() indexer.bulk_index(record_ids) indexer.process_bulk_queue()
def test_get_expired_embargos(app): """Test get expired records.""" c = MagicMock() id1 = str(uuid.uuid4()) id2 = str(uuid.uuid4()) with patch('zenodo.modules.records.models.current_search_client', c): c.search.return_value = dict( hits=dict(hits=[ {'_id': id1}, {'_id': id2}, ]) ) assert c.search.called_with(index='records') assert AccessRight.get_expired_embargos() == [id1, id2]
def has_read_permission(user, record): """Check if user has read access to the record.""" # Allow if record is open access if AccessRight.get(record['access_right'], record.get('embargo_date')) \ == AccessRight.OPEN: return True # Allow token bearers token = session.get('accessrequests-secret-token') if token and SecretLink.validate_token( token, dict(recid=int(record['recid']))): return True return has_update_permission(user, record)
def has_access(user=None, record=None): """Check whether the user has access to the record. The rules followed are: 1. Open Access records can be viewed by everyone. 2. Embargoed, Restricted and Closed records can be viewed by the record owners. 3. Administrators can view every record. """ if AccessRight.get(record['access_right'], record.get('embargo_date')) \ == AccessRight.OPEN: return True user_id = int(user.get_id()) if user.is_authenticated else None if user_id in record.get('owners', []): return True if DynamicPermission(ActionNeed('admin-access')): return True return False
def test_update_embargoed_records(app, db, es): """Test update embargoed records.""" records = [ Record.create({ 'title': 'yesterday', 'access_right': 'embargoed', 'embargo_date': _today_offset(-1) }), Record.create({ 'title': 'today', 'access_right': 'embargoed', 'embargo_date': _today_offset(0) }), Record.create({ 'title': 'tomorrow', 'access_right': 'embargoed', 'embargo_date': _today_offset(1) }), Record.create({ 'title': 'already open', 'access_right': 'open', 'embargo_date': _today_offset(-1) }) ] db.session.commit() for r in records: RecordIndexer().index(r) current_search.flush_and_refresh('records-record-v1.0.0') res = AccessRight.get_expired_embargos() assert len(res) == 2 assert str(records[0].id) in res assert str(records[1].id) in res update_expired_embargos() assert Record.get_record(records[0].id)['access_right'] == AccessRight.OPEN assert Record.get_record(records[1].id)['access_right'] == AccessRight.OPEN
def test_access_right_embargo(): """Test access right embargo.""" assert AccessRight.get(AccessRight.OPEN) == 'open' assert AccessRight.get(AccessRight.EMBARGOED) == 'embargoed' # Embargo just lifted today. today = datetime.utcnow().date() assert AccessRight.get( AccessRight.EMBARGOED, embargo_date=today) == 'open' # Future embargo date. assert AccessRight.get( AccessRight.EMBARGOED, embargo_date=today+timedelta(days=1)) \ == 'embargoed' # Should work with strings as well assert AccessRight.get( AccessRight.EMBARGOED, embargo_date='1253-01-01') == AccessRight.OPEN assert AccessRight.get( AccessRight.EMBARGOED, embargo_date=str(today+timedelta(days=1))) == AccessRight.EMBARGOED
def get_access_right_category(self, obj): """Get access right category.""" return AccessRight.as_category(self.get_access_right(obj))