def test_create_inits_form_with_schema(GroupSchema, Form): schema = mock.Mock() GroupSchema.return_value = mock.Mock(bind=mock.Mock(return_value=schema)) views.create(request=_mock_request()) Form.assert_called_once_with(schema)
def test_create_inits_form_with_schema(GroupSchema, Form): schema = mock.Mock() GroupSchema.return_value = mock.Mock(bind=mock.Mock(return_value=schema)) views.create(request=_mock_request()) assert Form.call_args[0][0] == schema
def test_create_validates_form(Form): Form.return_value = form = mock.Mock() form.validate.return_value = {"name": "new group"} request = _mock_request() views.create(request) form.validate.assert_called_once_with(request.params.items())
def test_create_uses_name_from_validated_data(Form, User, Group): """It uses the "name" from the validated data to create a new group.""" Form.return_value = mock.Mock(validate=lambda data: {"name": "test-group"}) User.get_by_userid.return_value = user = mock.Mock() views.create(_mock_request()) Group.assert_called_once_with(name="test-group", creator=user)
def test_create_gets_user_with_authenticated_id(Form): """It uses the "name" from the validated data to create a new group.""" Form.return_value = mock.Mock(validate=lambda data: {"name": "test-group"}) request = _mock_request() type(request).authenticated_user = user_property = mock.PropertyMock() views.create(request) user_property.assert_called_once_with()
def test_create_gets_user_with_authenticated_id(Form, User): """It uses the "name" from the validated data to create a new group.""" Form.return_value = mock.Mock(validate=lambda data: {"name": "test-group"}) request = _mock_request() views.create(request) User.get_by_userid.assert_called_once_with( request.domain, request.authenticated_userid)
def test_create_adds_group_to_db(Group): """It should add the new group to the database session.""" group = mock.Mock(id=6) Group.return_value = group request = _mock_request() views.create(request) request.db.add.assert_called_once_with(group)
def test_create_publishes_join_event(Group, session_model): group = mock.Mock(pubid=mock.sentinel.pubid) Group.return_value = group request = _mock_request() views.create(request) request.get_queue_writer().publish.assert_called_once_with('user', { 'type': 'group-join', 'userid': request.authenticated_userid, 'group': group.pubid, 'session_model': session_model(), })
def test_create_rerenders_form_on_validation_failure(Form): Form.return_value = form = mock.Mock() form.validate.side_effect = deform.ValidationFailure(None, None, None) result = views.create(_mock_request()) assert result['form'] == form.render.return_value
def test_create_publishes_join_event(Group, session_model): group = mock.Mock(pubid=mock.sentinel.pubid) Group.return_value = group request = _mock_request() views.create(request) request.get_queue_writer().publish.assert_called_once_with( "user", { "type": "group-join", "userid": request.authenticated_userid, "group": group.pubid, "session_model": session_model(), }, )
def test_create_rerenders_form_on_validation_failure(Form): Form.return_value = form = mock.Mock() form.validate.side_effect = deform.ValidationFailure(None, None, None) params = {"foo": "bar"} template_data = views.create(_mock_request()) assert template_data['form'] == form assert template_data['data'] == params
def test_create_redirects_to_group_read_page(Group): """After successfully creating a new group it should redirect.""" group = mock.Mock(id='test-id', slug='test-slug') Group.return_value = group request = _mock_request() result = views.create(request) assert isinstance(result, httpexceptions.HTTPRedirection)
def test_create_redirects_to_group_read_page(Group, logic): """After successfully creating a new group it should redirect.""" group = mock.Mock(id='test-id', slug='test-slug') Group.return_value = group request = _mock_request() logic.url_for_group.return_value = "test-read-url" redirect = views.create(request) logic.url_for_group.assert_called_once_with(request, group) assert redirect.status_int == 303 assert redirect.location == "test-read-url"
def test_create_redirects_to_group_read_page(Group, hashids): """After successfully creating a new group it should redirect.""" group = mock.Mock(id='test-id', slug='test-slug') Group.return_value = group request = _mock_request() hashids.encode.return_value = "testhashid" redirect = views.create(request) request.route_url.assert_called_once_with( "group_read", hashid="testhashid", slug="test-slug") assert redirect.status_int == 303 assert redirect.location == "test-read-url"
def test_create_404s_if_groups_feature_is_off(): with pytest.raises(httpexceptions.HTTPNotFound): views.create(_mock_request(feature=lambda feature: False))
def test_create_with_non_ascii_name(): views.create(_mock_request(params={"name": u"☆ ßüper Gröup ☆"}))