def test_create_annotation_calls_nipsa(nipsa): """It should call has_nipsa() once with the user's id.""" user = mock.Mock() logic.create_annotation({}, user) nipsa.has_nipsa.assert_called_once_with(user.id)
def test_create_annotation_pops_protected_fields(Annotation): """It should remove any protected fields before calling Annotation.""" logic.create_annotation( fields={"foo": "bar", "created": "foo", "updated": "foo", "user": "******", "consumer": "foo", "id": "foo"}, user=mock.Mock(), ) for field in ("created", "updated", "user", "consumer", "id"): assert field not in Annotation.call_args[0][0]
def test_create_annotation_pops_protected_fields(Annotation): """It should remove any protected fields before calling Annotation.""" logic.create_annotation( fields={ 'foo': 'bar', 'created': 'foo', 'updated': 'foo', 'user': '******', 'id': 'foo' }, userid='acct:[email protected]') for field in ('created', 'updated', 'user', 'id'): assert field not in Annotation.call_args[0][0]
def test_create_annotation_pops_protected_fields(Annotation): """It should remove any protected fields before calling Annotation.""" logic.create_annotation( fields={ 'foo': 'bar', 'created': 'foo', 'updated': 'foo', 'user': '******', 'consumer': 'foo', 'id': 'foo' }, user=mock.Mock()) for field in ('created', 'updated', 'user', 'consumer', 'id'): assert field not in Annotation.call_args[0][0]
def test_create_annotation_sets_consumer(Annotation): """It should set the annotation's 'consumer' field to the consumer key.""" user = mock.Mock() Annotation.return_value = _mock_annotation() annotation = logic.create_annotation({}, user) assert annotation['consumer'] == user.consumer.key
def test_create_annotation_sets_user(Annotation): """It should set the annotation's 'user' field to the userid.""" userid = 'acct:[email protected]' Annotation.return_value = _mock_annotation() annotation = logic.create_annotation({}, userid) assert annotation['user'] == userid
def test_create_annotation_does_not_crash_if_annotations_parent_has_no_group( Annotation): """It shouldn't crash if the parent annotation has no group. It shouldn't crash if the annotation is a reply and its parent annotation has no 'group' field. """ # No group in the original annotation/reply itself. Annotation.return_value = _mock_annotation() assert 'group' not in Annotation.return_value fields = {} # No group here either. # And no group in the parent annotation either. Annotation.fetch.return_value = {} logic.create_annotation(fields, userid='acct:[email protected]')
def test_create_annotation_sets_user(Annotation): """It should set the annotation's 'user' field to the user's id.""" user = mock.Mock() Annotation.return_value = _mock_annotation() annotation = logic.create_annotation({}, user) assert annotation['user'] == user.id
def test_create_annotation_sets_nipsa_if_user_is_nipsad(Annotation, nipsa): Annotation.return_value = _mock_annotation() # The user is nipsa'd. nipsa.has_nipsa.return_value = True annotation = logic.create_annotation({}, mock.Mock()) assert annotation['nipsa'] is True
def test_create_annotation_does_not_set_nipsa_if_user_is_not_nipsad( Annotation, nipsa): Annotation.return_value = _mock_annotation() # The user is not nipsa'd. nipsa.has_nipsa.return_value = False annotation = logic.create_annotation({}, mock.Mock()) assert 'nipsa' not in annotation
def create(request): """Read the POSTed JSON-encoded annotation and persist it.""" schema = schemas.CreateAnnotationSchema(request) appstruct = schema.validate(_json_payload(request)) annotation = logic.create_annotation(appstruct) # Notify any subscribers _publish_annotation_event(request, annotation, 'create') # Return it so the client gets to know its ID and such return search_lib.render(annotation)
def create(request): """Read the POSTed JSON-encoded annotation and persist it.""" # Read the annotation from the request payload try: fields = request.json_body except ValueError: return _api_error(request, 'No JSON payload sent. Annotation not created.', status_code=400) # Client Error: Bad Request user = get_user(request) # Create the annotation annotation = logic.create_annotation(fields, user) # Notify any subscribers _publish_annotation_event(request, annotation, 'create') # Return it so the client gets to know its ID and such return search_lib.render(annotation)
def create(request): """Read the POSTed JSON-encoded annotation and persist it.""" # Read the annotation from the request payload try: fields = request.json_body except ValueError: return _api_error( request, "No JSON payload sent. Annotation not created.", status_code=400 ) # Client Error: Bad Request try: appstruct = schemas.AnnotationSchema().validate(fields) except schemas.ValidationError as err: return _api_error(request, err.message, status_code=400) annotation = logic.create_annotation(appstruct, userid=request.authenticated_userid) # Notify any subscribers _publish_annotation_event(request, annotation, "create") # Return it so the client gets to know its ID and such return search_lib.render(annotation)
def create(request): """Read the POSTed JSON-encoded annotation and persist it.""" # Read the annotation from the request payload try: fields = request.json_body except ValueError: return _api_error(request, 'No JSON payload sent. Annotation not created.', status_code=400) # Client Error: Bad Request try: appstruct = schemas.AnnotationSchema().validate(fields) except schemas.ValidationError as err: return _api_error(request, err.message, status_code=400) annotation = logic.create_annotation(appstruct, userid=request.authenticated_userid) # Notify any subscribers _publish_annotation_event(request, annotation, 'create') # Return it so the client gets to know its ID and such return search_lib.render(annotation)
def test_create_annotation_calls_save(Annotation): """It should call save() once.""" logic.create_annotation({}, userid='acct:[email protected]') Annotation.return_value.save.assert_called_once_with()
def test_create_annotation_returns_the_annotation(Annotation): result = logic.create_annotation({}, userid='acct:[email protected]') assert result == Annotation.return_value
def test_create_annotation_does_not_crash_if_annotation_has_no_group( Annotation): assert 'group' not in Annotation.return_value fields = {} # No group here either. logic.create_annotation(fields, userid='acct:[email protected]')
def test_create_annotation_calls_Annotation(Annotation): fields = mock.MagicMock() logic.create_annotation(fields, mock.Mock()) Annotation.assert_called_once_with(fields)
def test_create_annotation_does_not_crash_if_annotation_has_no_group( Annotation): assert 'group' not in Annotation.return_value fields = {} # No group here either. logic.create_annotation(fields, mock.Mock())
def test_create_annotation_returns_the_annotation(Annotation): assert logic.create_annotation({}, mock.Mock()) == Annotation.return_value
def test_create_annotation_calls_Annotation(Annotation): fields = mock.MagicMock() logic.create_annotation(fields) Annotation.assert_called_once_with(fields)
def test_create_annotation_calls_prepare(Annotation, search_lib): """It should call prepare() once with the annotation.""" logic.create_annotation({}, mock.Mock()) search_lib.prepare.assert_called_once_with(Annotation.return_value)
def test_create_annotation_returns_the_annotation(Annotation): result = logic.create_annotation({}) assert result == Annotation.return_value
def test_create_annotation_calls_prepare(Annotation, search_lib): """It should call prepare() once with the annotation.""" logic.create_annotation({}, userid='acct:[email protected]') search_lib.prepare.assert_called_once_with(Annotation.return_value)
def test_create_annotation_calls_save(Annotation): """It should call save() once.""" logic.create_annotation({}, mock.Mock()) Annotation.return_value.save.assert_called_once_with()
def test_create_annotation_calls_Annotation(Annotation): fields = mock.MagicMock() logic.create_annotation(fields, userid='acct:[email protected]') Annotation.assert_called_once_with(fields)
def test_create_annotation_calls_save(Annotation): """It should call save() once.""" logic.create_annotation({}) Annotation.return_value.save.assert_called_once_with()
def test_create_annotation_calls_prepare(Annotation, search_lib): """It should call prepare() once with the annotation.""" logic.create_annotation({}) search_lib.prepare.assert_called_once_with(Annotation.return_value)