def test_you_cannot_change_an_annotations_userid(self): schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), '', '') appstruct = schema.validate({'userid': 'new_userid'}) assert 'userid' not in appstruct assert 'userid' not in appstruct.get('extra', {})
def test_you_cannot_change_an_annotations_references(self): schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), '', '') appstruct = schema.validate({'references': ['new_parent']}) assert 'references' not in appstruct assert 'references' not in appstruct.get('extra', {})
def test_updateannotationschema_raises_if_structure_validator_raises(): request = testing.DummyRequest() schema = schemas.UpdateAnnotationSchema(request, {}) schema.structure = mock.Mock() schema.structure.validate.side_effect = schemas.ValidationError('asplode') with pytest.raises(schemas.ValidationError): schema.validate({'foo': 'bar'})
def test_it_returns_the_appstruct_from_AnnotationSchema( self, annotation, AnnotationSchema): schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), annotation) appstruct = schema.validate(mock.sentinel.input_data) assert appstruct == AnnotationSchema.return_value.validate.return_value
def test_it_passes_input_to_AnnotationSchema_validator( self, annotation, AnnotationSchema): schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), annotation) schema.validate(mock.sentinel.input_data) schema.structure.validate.assert_called_once_with( mock.sentinel.input_data)
def test_it_raises_if_AnnotationSchema_validate_raises( self, annotation, AnnotationSchema): AnnotationSchema.return_value.validate.side_effect = ( schemas.ValidationError('asplode')) schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), annotation) with pytest.raises(schemas.ValidationError): schema.validate({'foo': 'bar'})
def test_updateannotationschema_passes_input_to_structure_validator(): request = testing.DummyRequest() schema = schemas.UpdateAnnotationSchema(request, {}) schema.structure = mock.Mock() schema.structure.validate.return_value = {} schema.validate({'foo': 'bar'}) schema.structure.validate.assert_called_once_with({'foo': 'bar'})
def test_updateannotationschema_permits_all_other_changes(data): request = testing.DummyRequest() annotation = {'group': 'flibble'} schema = schemas.UpdateAnnotationSchema(request, annotation) result = schema.validate(data) for k in data: assert result[k] == data[k]
def test_it_raises_if_you_try_to_change_the_group(self, annotation, AnnotationSchema): annotation.groupid = 'original-group' AnnotationSchema.return_value.validate.return_value['groupid'] = ( 'new-group') schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), annotation) with pytest.raises(schemas.ValidationError): schema.validate(mock.sentinel.input_data)
def test_updateannotationschema_removes_protected_fields(field): request = testing.DummyRequest() annotation = {} schema = schemas.UpdateAnnotationSchema(request, annotation) data = {} data[field] = 'something forbidden' result = schema.validate(data) assert field not in result
def update(annotation, request): """Update the specified annotation with data from the PUT payload.""" schema = schemas.UpdateAnnotationSchema(request, annotation=annotation) appstruct = schema.validate(_json_payload(request)) annotation = storage.update_annotation(request, annotation.id, appstruct) _publish_annotation_event(request, annotation, 'update') presenter = AnnotationJSONPresenter(annotation) return presenter.asdict()
def test_updateannotationschema_denies_group_changes(): """An annotation may not be moved between groups.""" request = testing.DummyRequest() annotation = {'group': 'flibble'} schema = schemas.UpdateAnnotationSchema(request, annotation) data = {'group': '__world__'} with pytest.raises(schemas.ValidationError) as exc: schema.validate(data) assert exc.value.message.startswith('group:')
def test_it_replaces_private_permissions_with_shared_False(self): schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), '', '') appstruct = schema.validate( {'permissions': { 'read': ['acct:[email protected]'] }}) assert appstruct['shared'] is False assert 'permissions' not in appstruct assert 'permissions' not in appstruct.get('extras', {})
def test_updateannotationschema_denies_permissions_changes_if_not_admin( annotation, authn_policy): """If a user is not an admin on an annotation, they cannot change perms.""" authn_policy.authenticated_userid.return_value = 'acct:[email protected]' request = testing.DummyRequest() schema = schemas.UpdateAnnotationSchema(request, annotation) data = {'permissions': {'admin': ['acct:[email protected]']}} with pytest.raises(schemas.ValidationError) as exc: schema.validate(data) assert exc.value.message.startswith('permissions:')
def test_updateannotationschema_allows_permissions_changes_if_admin( authn_policy): """If a user is an admin on an annotation, they can change perms.""" authn_policy.authenticated_userid.return_value = 'acct:[email protected]' request = testing.DummyRequest() annotation = {'permissions': {'admin': ['acct:[email protected]']}} schema = schemas.UpdateAnnotationSchema(request, annotation) data = {'permissions': {'admin': ['acct:[email protected]']}} result = schema.validate(data) assert result == data
def test_it_replaces_shared_permissions_with_shared_True(self): schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), '', '__world__') appstruct = schema.validate( {'permissions': { 'read': ['group:__world__'] }}) assert appstruct['shared'] is True assert 'permissions' not in appstruct assert 'permissions' not in appstruct.get('extras', {})
def update(context, request): """Update the fields we received and store the updated version.""" annotation = context.model schema = schemas.UpdateAnnotationSchema(request, annotation) appstruct = schema.validate(_json_payload(request)) # Update and store the annotation logic.update_annotation(annotation, appstruct) # Notify any subscribers _publish_annotation_event(request, annotation, 'update') # Return the updated version that was just stored. return search_lib.render(annotation)
def test_it_passes_existing_target_uri_to_document_metas_from_data( self, parse_document_claims): """ If no 'uri' is given it should use the existing target_uri. If no 'uri' is given in the update request then document_metas_from_data() should be called with the existing target_uri of the annotation in the database. """ document_data = {'foo': 'bar'} schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), mock.sentinel.target_uri, '') schema.validate({'document': document_data}) parse_document_claims.document_metas_from_data.assert_called_once_with( document_data, claimant=mock.sentinel.target_uri)
def update_annotation_schema_validate(data, existing_target_uri='', groupid=''): schema = schemas.UpdateAnnotationSchema(testing.DummyRequest(), existing_target_uri, groupid) return schema.validate(data)