def test_api_comment_history(api_user, api_client): obj = CommentFactory.create() hist_obj = obj.history.last() add_permissions(api_user, Comment.history.model, 'view') res = api_client.get(f'/api/v1/comment-list/history/?_uid={obj.uid}') assert res.status_code == status.HTTP_200_OK _assert_api_object_list( res, [{ '_uid': str(obj.uid), '_type': 'historical' + 'comment', '_version': obj.version, 'contact': { '_uid': str(obj.contact.uid), '_type': 'contact', '_version': obj.contact.version, 'emails': obj.contact.emails, 'name': obj.contact.name, 'order_index': obj.contact.order_index, 'phones': obj.contact.phones, }, 'message': obj.message, 'user': obj.user.pk, 'history_change_reason': None, 'history_date': hist_obj.history_date.strftime('%Y-%m-%dT%H:%M:%S.%f'), 'history_id': hist_obj.history_id, 'history_type': '+', 'history_user_id': None, }])
def test_api_create_comment_simple(api_user, api_client): contact = ContactFactory.create(name=api_user.username) add_permissions(api_user, Comment, 'add', 'change') data = {'message': get_random_string(), 'contact': contact.uid} res = api_client.post('/api/v1/comment-list/', data=data) assert res.status_code == status.HTTP_201_CREATED assert res.data['user'] == api_user.pk
def test_api_retrieve(api_user, api_client, api_model): model, factory, options = api_model last_obj = _create_few_models(factory) url = _url(model, options, last_obj) _type = ContentType.objects.get_for_model(model).model add_permissions(api_user, model, 'view') res = api_client.get(url) assert res.status_code == status.HTTP_200_OK assert res.data['_uid'] == last_obj.uid assert res.data['_type'] == _type
def test_api_create_comment(api_user, api_client): contact = ContactFactory.create() add_permissions(api_user, Comment, 'add', 'change') payload = { '_uid': contact.uid, '_type': ContentType.objects.get_for_model(type(contact)).model, 'name': get_random_string(), } data = {'message': get_random_string(), 'contact': payload} res = api_client.post('/api/v1/comment-list/', data=data) assert res.status_code == status.HTTP_201_CREATED assert res.data['user'] == api_user.pk
def test_api_create_comment_otheruser(api_user, api_client): other_user = get_user_model().objects.create(username='******') contact = ContactFactory.create(name=api_user.username) add_permissions(api_user, Comment, 'add', 'change', 'change_user') data = { 'message': get_random_string(), 'contact': contact.uid, 'user': other_user.pk } res = api_client.post('/api/v1/comment-list/', data=data) assert res.status_code == status.HTTP_201_CREATED assert res.data['user'] == other_user.pk
def test_api_create_comment_without_contact( api_user, api_client): # noqa: pylint=invalid-name add_permissions(api_user, Comment, 'add') data = {'message': get_random_string()} res = api_client.post('/api/v1/comment-list/', data=data) assert res.status_code == status.HTTP_400_BAD_REQUEST assert res.data == { 'message': 'Invalid input.', 'code': 'invalid', 'detail': { 'contact': [REQUIRED_FIELD_ERROR], } }
def test_api_list_num_queries( api_user, api_client, api_model, assert_num_queries_lte ): model, factory, options = api_model _create_few_models(factory) url = _url(model, options) add_permissions(api_user, model, 'view') with assert_num_queries_lte(options["queries"]): res = api_client.get(url) assert res.status_code == status.HTTP_200_OK assert len(res.data['results']) >= BATCH_MODELS
def test_api_list_contact(api_user, api_client): # noqa: pylint=invalid-name add_permissions(api_user, Contact, 'view') obj = ContactFactory.create() res = api_client.get('/api/v1/contact-list/') assert res.status_code == status.HTTP_200_OK _assert_api_object_list(res, [{ '_uid': str(obj.uid), '_type': 'contact', '_version': obj.version, 'emails': obj.emails, 'name': obj.name, 'order_index': obj.order_index, 'phones': obj.phones, }])
def test_api_retrieve_contact(api_user, api_client): add_permissions(api_user, Contact, 'view') obj = ContactFactory.create() res = api_client.get(f'/api/v1/contact-list/{obj.uid}/') assert res.status_code == status.HTTP_200_OK _assert_api_object( res, { '_uid': str(obj.uid), '_type': 'contact', '_version': obj.version, 'emails': obj.emails, 'name': obj.name, 'order_index': obj.order_index, 'phones': obj.phones, })
def test_api_list(api_user, api_client, api_model): model, factory, options = api_model last_obj = _create_few_models(factory) url = _url(model, options) _type = ContentType.objects.get_for_model(model).model add_permissions(api_user, model, 'view') res = api_client.get(url) assert res.status_code == status.HTTP_200_OK assert res.data['count'] > BATCH_MODELS assert res.data['pages'] >= 1 assert res.data['page_size'] >= 20 assert res.data['page'] == 1 assert res.data['page_next'] is None or res.data['page_next'] == 2 assert res.data['page_previous'] is None assert res.data['results'][0]['_uid'] == last_obj.uid assert res.data['results'][0]['_type'] == _type assert len(res.data['results']) > BATCH_MODELS
def test_api_create_2_comments_for_one_contact( api_user, api_client): # noqa: pylint=invalid-name add_permissions(api_user, Comment, 'add', 'change') contact = ContactFactory.create() payload = { '_uid': contact.uid, '_type': ContentType.objects.get_for_model(type(contact)).model, } data = [ { 'message': get_random_string(), 'contact': payload }, { 'message': get_random_string(), 'contact': payload }, ] res = api_client.post('/api/v1/comment-list/', data=data) assert res.status_code == status.HTTP_201_CREATED assert len(res.data) == 2 assert contact.comments.all().count() == 2
def test_api_create_comment_otheruser_permitted(api_user, api_client): other_user = get_user_model().objects.create(username='******') contact = ContactFactory.create(name=api_user.username) add_permissions(api_user, Comment, 'add', 'change') data = { 'message': get_random_string(), 'contact': contact.uid, 'user': other_user.pk } res = api_client.post('/api/v1/comment-list/', data=data) assert res.status_code == status.HTTP_400_BAD_REQUEST assert res.json() == { 'code': 'invalid', 'detail': { 'user_id': [{ 'code': 'invalid', 'message': 'У вас нет прав для редактирования этого поля.' }] }, 'message': 'Invalid input.' }
def test_api_create_bulk_contact(api_user, api_client): add_permissions(api_user, Contact, 'add', 'change') data = [{'name': get_random_string()}, {'name': get_random_string()}] res = api_client.post('/api/v1/contact-list/', data=data) assert res.status_code == status.HTTP_201_CREATED assert len(res.data) == 2
def test_api_create_contact_with_extra_field( api_user, api_client): # noqa: pylint=invalid-name add_permissions(api_user, Contact, 'add', 'change') data = {'name': get_random_string(), 'fooo': 'no'} res = api_client.post('/api/v1/contact-list/', data=data) assert res.status_code == status.HTTP_201_CREATED