From 580e0a5c4840efd62b8d9ebe2feb89129a19e21b Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sun, 10 Mar 2024 17:45:26 +0300 Subject: [PATCH] Adding response in Allure report --- .github/workflows/{comfig.yml => config.yml} | 2 +- base/base_api.py | 6 +++++ config/links.py | 22 +++++++++---------- .../comment_controller/comment_get_list.py | 13 +++++++++-- .../comment_controller/create_comment.py | 16 +++++++++----- .../comment_controller/delete_comment.py | 11 ++++++++-- .../list_comment_by_post.py | 11 ++++++++-- .../list_comment_by_user.py | 10 ++++++++- endpoints/post_controller/create_post.py | 15 ++++++++----- endpoints/post_controller/delete_post.py | 12 ++++++++-- endpoints/post_controller/post_by_id.py | 11 +++++++++- endpoints/post_controller/post_list.py | 12 ++++++++-- .../post_controller/post_list_by_id_user.py | 12 ++++++++-- endpoints/post_controller/post_list_by_tag.py | 12 ++++++++-- endpoints/post_controller/update_post.py | 12 +++++++--- endpoints/tag_controller/list_tags.py | 12 +++++++--- endpoints/user_controller/create_user.py | 12 +++++++--- endpoints/user_controller/delete_user.py | 9 +++++++- endpoints/user_controller/get_user.py | 11 ++++++++-- endpoints/user_controller/get_user_list.py | 9 +++++++- endpoints/user_controller/update_user.py | 17 +++++++++----- enums/user_enums.py | 1 + model/{data_generator.py => payload.py} | 15 +++++++++---- tests/test_post_controller.py | 8 ------- tests/test_user_controller.py | 6 ++--- 25 files changed, 205 insertions(+), 72 deletions(-) rename .github/workflows/{comfig.yml => config.yml} (98%) rename model/{data_generator.py => payload.py} (83%) diff --git a/.github/workflows/comfig.yml b/.github/workflows/config.yml similarity index 98% rename from .github/workflows/comfig.yml rename to .github/workflows/config.yml index ad9692e..363406b 100644 --- a/.github/workflows/comfig.yml +++ b/.github/workflows/config.yml @@ -3,7 +3,7 @@ name: dummyapi on: workflow_dispatch jobs: - api_tests: + api_tests_dummyapi: runs-on: ubuntu-latest steps: diff --git a/base/base_api.py b/base/base_api.py index 873c130..aedd836 100644 --- a/base/base_api.py +++ b/base/base_api.py @@ -1,4 +1,6 @@ import allure +import json +from allure_commons.types import AttachmentType class BaseAPI: @@ -27,3 +29,7 @@ def check_total_object(self, objectCount, total_object=1): @allure.step('Check response is 404') def check_response_is_403(self): assert self.response.status_code == 403, f'Error status code {self.response.status_code}' + + def attach_response(self): + response = json.dumps(self.response_json, indent=4) + allure.attach(body=response, name="API Response", attachment_type=AttachmentType.JSON) diff --git a/config/links.py b/config/links.py index e97399f..fe94852 100644 --- a/config/links.py +++ b/config/links.py @@ -3,28 +3,28 @@ class UserDataLink: GET_LIST = f'{BASE_URL}/user' - GET_USER_BY_ID = f'{BASE_URL}/user/' + GET_USER_BY_ID = lambda self, user_id: f'{BASE_URL}/user/{user_id}' CREATE_USER = f'{BASE_URL}/user/create' - UPDATE_USER = f'{BASE_URL}/user' - DELETE_USER = f'{BASE_URL}/user' + UPDATE_USER = lambda self, user_id: f'{BASE_URL}/user/{user_id}' + DELETE_USER = lambda self, user_id: f'{BASE_URL}/user/{user_id}' class PostDataLink: GET_LIST_POST = f'{BASE_URL}/post' - GET_LIST_BY_USER = f'{BASE_URL}/user' - GET_POST_BY_ID = f'{BASE_URL}/post' - GET_LIST_BY_TAG = f'{BASE_URL}/tag' + GET_LIST_BY_USER = lambda self, user_id: f'{BASE_URL}/user/{user_id}/post' + GET_POST_BY_ID = lambda self, post_id: f'{BASE_URL}/post/{post_id}' + GET_LIST_BY_TAG = lambda self, user_id: f'{BASE_URL}/tag/{user_id}/post' CREATE_POST = f'{BASE_URL}/post/create' - UPDATE_POST = f'{BASE_URL}/post' - DELETE_POST = f'{BASE_URL}/post' + UPDATE_POST = lambda self, post_id: f'{BASE_URL}/post/{post_id}' + DELETE_POST = lambda self, post_id: f'{BASE_URL}/post/{post_id}' class CommentDataLink: GET_LIST_COMMENT = f'{BASE_URL}/comment' - GET_LIST_COMMENT_BY_POST = f'{BASE_URL}/post' - GET_LIST_COMMENT_BY_USER = f'{BASE_URL}/user' + GET_LIST_COMMENT_BY_POST = lambda self, post_id: f'{BASE_URL}/post/{post_id}/comment' + GET_LIST_COMMENT_BY_USER = lambda self, user_id: f'{BASE_URL}/user/{user_id}/comment' CREATE_COMMENT = f'{BASE_URL}/comment/create' - DELETE_COMMENT = f'{BASE_URL}/comment' + DELETE_COMMENT = lambda self, comment_id: f'{BASE_URL}/comment/{comment_id}' class TagsDataLink: diff --git a/endpoints/comment_controller/comment_get_list.py b/endpoints/comment_controller/comment_get_list.py index 2da4159..3c2e332 100644 --- a/endpoints/comment_controller/comment_get_list.py +++ b/endpoints/comment_controller/comment_get_list.py @@ -8,7 +8,16 @@ class CommentList(BaseAPI): SCHEMA = DataListComment + def __init__(self): + super().__init__() + self.url = CommentDataLink() + @allure.step('Request comment list') def request_comment_list(self, headers): - self.response = requests.get(url=CommentDataLink.GET_LIST_COMMENT, headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.get( + url=self.url.GET_LIST_COMMENT, + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() + diff --git a/endpoints/comment_controller/create_comment.py b/endpoints/comment_controller/create_comment.py index 4c08253..797e246 100644 --- a/endpoints/comment_controller/create_comment.py +++ b/endpoints/comment_controller/create_comment.py @@ -3,25 +3,29 @@ from base.base_api import BaseAPI from config.links import CommentDataLink from schema.comment import Comment -from model.data_generator import CreateCommentDate +from model.payload import CreateCommentDate class CreateComment(BaseAPI): SCHEMA = Comment def __init__(self): - self.payload_comment = CreateCommentDate.data_comment() + super().__init__() + self.payload = CreateCommentDate() + self.url = CommentDataLink() def request_create_comment(self, headers, post, commentCount=1): with allure.step(f'Create {commentCount} comment(s)'): for i in range(commentCount): - body = self.payload_comment - body['owner'] = post.owner.id - body['post'] = post.id - self.response = requests.post(url=CommentDataLink.CREATE_COMMENT, headers=headers, data=body) + self.response = requests.post( + url=self.url.CREATE_COMMENT, + headers=headers, + json=self.payload.data_comment(post.owner.id, post.id) + ) self.response_json = self.response.json() self.check_response_is_200() self.check_validate() + self.attach_response() @allure.step('Check user id in comment') def check_id_user_in_comment(post, response): diff --git a/endpoints/comment_controller/delete_comment.py b/endpoints/comment_controller/delete_comment.py index 1e583b5..8c2bed0 100644 --- a/endpoints/comment_controller/delete_comment.py +++ b/endpoints/comment_controller/delete_comment.py @@ -5,8 +5,15 @@ class DeleteComment(BaseAPI): + def __init__(self): + super().__init__() + self.url = CommentDataLink() @allure.step('Delete comment') def request_delete_comment(self, headers, comment): - self.response = requests.delete(url=f'{CommentDataLink.DELETE_COMMENT}/{comment.id}', headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.delete( + url=self.url.DELETE_COMMENT(comment.id), + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() diff --git a/endpoints/comment_controller/list_comment_by_post.py b/endpoints/comment_controller/list_comment_by_post.py index 6ea129a..af30df5 100644 --- a/endpoints/comment_controller/list_comment_by_post.py +++ b/endpoints/comment_controller/list_comment_by_post.py @@ -8,11 +8,18 @@ class ListCommentByPost(BaseAPI): SCHEMA = DataListComment + def __init__(self): + super().__init__() + self.url = CommentDataLink() + @allure.step('Request comment list by post') def request_list_comment_by_post(self, headers, post_id): - self.response = requests.get(url=f'{CommentDataLink.GET_LIST_COMMENT_BY_POST}/{post_id}/comment', - headers=headers) + self.response = requests.get( + url=self.url.GET_LIST_COMMENT_BY_POST(post_id), + headers=headers + ) self.response_json = self.response.json() + self.attach_response() @allure.step('Check post id in comment list') def check_id_post_in_comment_list(self, response, comment): diff --git a/endpoints/comment_controller/list_comment_by_user.py b/endpoints/comment_controller/list_comment_by_user.py index c02a666..7be7b71 100644 --- a/endpoints/comment_controller/list_comment_by_user.py +++ b/endpoints/comment_controller/list_comment_by_user.py @@ -8,10 +8,18 @@ class ListCommentByUser(BaseAPI): SCHEMA = DataListComment + def __init__(self): + super().__init__() + self.url = CommentDataLink() + @allure.step('Request comment list by user') def request_list_comment_by_user(self, headers, user_id): - self.response = requests.get(url=f'{CommentDataLink.GET_LIST_COMMENT_BY_USER}/{user_id}/comment', headers=headers) + self.response = requests.get( + url=self.url.GET_LIST_COMMENT_BY_USER(user_id), + headers=headers + ) self.response_json = self.response.json() + self.attach_response() @allure.step('Check user id in comment list') def check_id_user_in_comment_list(self, response, comment): diff --git a/endpoints/post_controller/create_post.py b/endpoints/post_controller/create_post.py index dfb805c..1de1472 100644 --- a/endpoints/post_controller/create_post.py +++ b/endpoints/post_controller/create_post.py @@ -3,24 +3,29 @@ from base.base_api import BaseAPI from config.links import PostDataLink from schema.post import Post -from model.data_generator import RegisterPost +from model.payload import RegisterPost class CreatePost(BaseAPI): SCHEMA = Post def __init__(self): - self.payload_post = RegisterPost.post_data() + super().__init__() + self.url = PostDataLink() + self.payload = RegisterPost() def request_create_post(self, headers, owner, postCount=1): with allure.step(f'Create {postCount} post(s)'): for i in range(postCount): - body = self.payload_post - body['owner'] = owner.id - self.response = requests.post(url=PostDataLink.CREATE_POST, headers=headers, data=body) + self.response = requests.post( + url=self.url.CREATE_POST, + headers=headers, + json=self.payload.post_data(owner.id) + ) self.response_json = self.response.json() self.check_response_is_200() self.check_validate() + self.attach_response() diff --git a/endpoints/post_controller/delete_post.py b/endpoints/post_controller/delete_post.py index 8799357..6c32b91 100644 --- a/endpoints/post_controller/delete_post.py +++ b/endpoints/post_controller/delete_post.py @@ -5,7 +5,15 @@ class DeletePost(BaseAPI): + def __init__(self): + super().__init__() + self.url = PostDataLink() + @allure.step('Delete post') def request_delete_post(self, headers, post): - self.response = requests.delete(url=f'{PostDataLink.DELETE_POST}/{post.id}', headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.delete( + url=self.url.DELETE_POST(post.id), + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() \ No newline at end of file diff --git a/endpoints/post_controller/post_by_id.py b/endpoints/post_controller/post_by_id.py index 76afe04..22570f0 100644 --- a/endpoints/post_controller/post_by_id.py +++ b/endpoints/post_controller/post_by_id.py @@ -4,13 +4,22 @@ from config.links import PostDataLink from schema.post import Post + class PostByID(BaseAPI): SCHEMA = Post + def __init__(self): + super().__init__() + self.url = PostDataLink() + @allure.step('Get post by id user') def request_post_by_id(self, headers, post): - self.response = requests.get(url=f'{PostDataLink.GET_POST_BY_ID}/{post.id}', headers=headers) + self.response = requests.get( + url=self.url.GET_POST_BY_ID(post.id), + headers=headers + ) self.response_json = self.response.json() + self.attach_response() @allure.step('Checking the requested post by id') def check_post_by_id(self, post, response): diff --git a/endpoints/post_controller/post_list.py b/endpoints/post_controller/post_list.py index 1a3f820..390c8fa 100644 --- a/endpoints/post_controller/post_list.py +++ b/endpoints/post_controller/post_list.py @@ -8,7 +8,15 @@ class PostList(BaseAPI): SCHEMA = DataListPost + def __init__(self): + super().__init__() + self.url = PostDataLink() + @allure.step('Request post list') def request_post_list(self, headers): - self.response = requests.get(url=PostDataLink.GET_LIST_POST, headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.get( + url=self.url.GET_LIST_POST, + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() \ No newline at end of file diff --git a/endpoints/post_controller/post_list_by_id_user.py b/endpoints/post_controller/post_list_by_id_user.py index 204cd62..068f4f8 100644 --- a/endpoints/post_controller/post_list_by_id_user.py +++ b/endpoints/post_controller/post_list_by_id_user.py @@ -8,7 +8,15 @@ class PostListByIDUser(BaseAPI): SCHEMA = DataListPost + def __init__(self): + super().__init__() + self.url = PostDataLink() + @allure.step('User post list by id') def request_post_list_by_id(self, headers, user_id): - self.response = requests.get(url=f'{PostDataLink.GET_LIST_BY_USER}/{user_id}/post', headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.get( + url=self.url.GET_LIST_BY_USER(user_id), + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() \ No newline at end of file diff --git a/endpoints/post_controller/post_list_by_tag.py b/endpoints/post_controller/post_list_by_tag.py index 37f984b..2f31489 100644 --- a/endpoints/post_controller/post_list_by_tag.py +++ b/endpoints/post_controller/post_list_by_tag.py @@ -8,7 +8,15 @@ class PostListByTag(BaseAPI): SCHEMA = DataListPost + def __init__(self): + super().__init__() + self.url = PostDataLink() + @allure.step('User tag list request') def request_post_list_by_tag(self, headers, user): - self.response = requests.get(url=f'{PostDataLink.GET_LIST_BY_TAG}/{user.owner.id}/post', headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.get( + url=self.url.GET_LIST_BY_TAG(user.owner.id), + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() \ No newline at end of file diff --git a/endpoints/post_controller/update_post.py b/endpoints/post_controller/update_post.py index e6e960d..b83cc61 100644 --- a/endpoints/post_controller/update_post.py +++ b/endpoints/post_controller/update_post.py @@ -3,19 +3,25 @@ from base.base_api import BaseAPI from config.links import PostDataLink from schema.post import Post -from model.data_generator import UpdatePostDate +from model.payload import UpdatePostDate class UpdatePost(BaseAPI): SCHEMA = Post def __init__(self): - self.payload_post = UpdatePostDate.update_post() + super().__init__() + self.url = PostDataLink() + self.payload = UpdatePostDate() @allure.step('Update post') def request_update_post(self, headers, post): - self.response = requests.put(url=f'{PostDataLink.UPDATE_POST}/{post.id}', headers=headers, data=self.payload_post) + self.response = requests.put(url=self.url.UPDATE_POST(post.id), + headers=headers, + data=self.payload.update_post() + ) self.response_json = self.response.json() + self.attach_response() @allure.step('Checking for changes to the post data') def check_update_post(self, payload, response): diff --git a/endpoints/tag_controller/list_tags.py b/endpoints/tag_controller/list_tags.py index 6d92920..4d44961 100644 --- a/endpoints/tag_controller/list_tags.py +++ b/endpoints/tag_controller/list_tags.py @@ -5,11 +5,17 @@ from schema.tags import DataListTags - class TagsList(BaseAPI): SCHEMA = DataListTags + def __init__(self): + super().__init__() + self.url = TagsDataLink() + @allure.step('Get tag list') def request_tags_list(self, headers): - self.response = requests.get(url=TagsDataLink.GET_LIST_TAGS, headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.get(url=self.url.GET_LIST_TAGS, + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() diff --git a/endpoints/user_controller/create_user.py b/endpoints/user_controller/create_user.py index 4add1b6..d72aad1 100644 --- a/endpoints/user_controller/create_user.py +++ b/endpoints/user_controller/create_user.py @@ -3,16 +3,22 @@ from base.base_api import BaseAPI from config.links import UserDataLink from schema.user_full import User -from model.data_generator import RegisterUser +from model.payload import RegisterUser class CreateUser(BaseAPI): SCHEMA = User def __init__(self): - self.payload_reg = RegisterUser.user_data() + super().__init__() + self.url = UserDataLink() + self.payload = RegisterUser() @allure.step('Create user') def request_create_user(self, headers): - self.response = requests.post(url=UserDataLink.CREATE_USER, headers=headers, data=self.payload_reg) + self.response = requests.post(url=self.url.CREATE_USER, + headers=headers, + json=self.payload.user_data() + ) self.response_json = self.response.json() + self.attach_response() diff --git a/endpoints/user_controller/delete_user.py b/endpoints/user_controller/delete_user.py index c77b639..fcd563d 100644 --- a/endpoints/user_controller/delete_user.py +++ b/endpoints/user_controller/delete_user.py @@ -5,7 +5,14 @@ class DeleteUser(BaseAPI): + def __init__(self): + super().__init__() + self.url = UserDataLink() + @allure.step('Delete user') def request_delete_user(self, user, headers): - self.response = requests.delete(f'{UserDataLink.DELETE_USER}/{user.id}', headers=headers) + self.response = requests.delete(url=self.url.DELETE_USER(user.id), + headers=headers + ) self.response_json = self.response.json() + self.attach_response() diff --git a/endpoints/user_controller/get_user.py b/endpoints/user_controller/get_user.py index 87b7c07..1ff3370 100644 --- a/endpoints/user_controller/get_user.py +++ b/endpoints/user_controller/get_user.py @@ -8,7 +8,14 @@ class UserGet(BaseAPI): SCHEMA = User + def __init__(self): + super().__init__() + self.url = UserDataLink() + @allure.step('Request user data') def request_user(self, user, headers): - self.response = requests.get(f'{UserDataLink.GET_USER_BY_ID}/{user.id}', headers=headers) - self.response_json = self.response.json() \ No newline at end of file + self.response = requests.get(url=self.url.GET_USER_BY_ID(user.id), + headers=headers + ) + self.response_json = self.response.json() + self.attach_response() \ No newline at end of file diff --git a/endpoints/user_controller/get_user_list.py b/endpoints/user_controller/get_user_list.py index 00ab1b1..b29ab9d 100644 --- a/endpoints/user_controller/get_user_list.py +++ b/endpoints/user_controller/get_user_list.py @@ -8,7 +8,14 @@ class UserList(BaseAPI): SCHEMA = DataListUser + def __init__(self): + super().__init__() + self.url = UserDataLink() + @allure.step('Request user list') def request_user_list(self, headers={}): - self.response = requests.get(url=UserDataLink.GET_LIST, headers=headers) + self.response = requests.get(url=self.url.GET_LIST, + headers=headers + ) self.response_json = self.response.json() + self.attach_response() diff --git a/endpoints/user_controller/update_user.py b/endpoints/user_controller/update_user.py index ef433cf..de84e67 100644 --- a/endpoints/user_controller/update_user.py +++ b/endpoints/user_controller/update_user.py @@ -3,18 +3,25 @@ from base.base_api import BaseAPI from config.links import UserDataLink from schema.user_full import User -from model.data_generator import UpdateUserDate +from model.payload import UpdateUserDate class UpdateUser(BaseAPI): SCHEMA = User - payload_upd = UpdateUserDate.data_update() + + def __init__(self): + super().__init__() + self.url = UserDataLink() @allure.step('Update user data') - def request_update_user(self, user_before, headers, model=payload_upd): - self.response = requests.put(f'{UserDataLink.UPDATE_USER}/{user_before.id}', headers=headers, data=model) + def request_update_user(self, user, headers, payload): + self.response = requests.put(url=self.url.UPDATE_USER(user.id), + headers=headers, + json=payload + ) self.response_json = self.response.json() - self.payload = self.payload_upd + self.payload = payload + self.attach_response() @allure.step('Checking changes to user data') def check_update_user(self, user_before, user_after): diff --git a/enums/user_enums.py b/enums/user_enums.py index a90cc16..86856d8 100644 --- a/enums/user_enums.py +++ b/enums/user_enums.py @@ -8,6 +8,7 @@ class Title(Enum): dr = "dr" mr = "mr" + class Genders(Enum): male ="male" female = "female" diff --git a/model/data_generator.py b/model/payload.py similarity index 83% rename from model/data_generator.py rename to model/payload.py index 8a0b20b..fcbb281 100644 --- a/model/data_generator.py +++ b/model/payload.py @@ -39,15 +39,17 @@ def update_email(): class RegisterPost: @staticmethod - def post_data(): + def post_data(owner_id): text = fake.text(max_nb_chars=20) image = fake.image_url() likes = randint(0, 1000) tags = fake.word() + owner = owner_id return {"text": text, "image": image, "likes": likes, - "tags": tags + "tags": tags, + "owner": owner } @@ -67,6 +69,11 @@ def update_post(): class CreateCommentDate: @staticmethod - def data_comment(): + def data_comment(owner_id, post_id): message = fake.text(max_nb_chars=70) - return {"message": message} + owner = owner_id + post = post_id + return {"owner": owner, + "post": post, + "message": message + } diff --git a/tests/test_post_controller.py b/tests/test_post_controller.py index 39e2df1..388d916 100644 --- a/tests/test_post_controller.py +++ b/tests/test_post_controller.py @@ -6,14 +6,6 @@ @allure.feature('Post controller') class TestPost(BaseTest): - @pytest.fixture() - def post(self, authorization, user): - self.create_post.request_create_post(authorization, user) - self.create_post.check_response_is_200() - post = self.create_post.check_validate() - yield post - self.delete_post.request_delete_post(authorization, post) - @allure.title('Get list of posts') def test_get_post_list(self, authorization): self.post_list.request_post_list(authorization) diff --git a/tests/test_user_controller.py b/tests/test_user_controller.py index 3869dbe..9656af7 100644 --- a/tests/test_user_controller.py +++ b/tests/test_user_controller.py @@ -1,6 +1,6 @@ import allure from base.base_test import BaseTest -from model.data_generator import UpdateUserDate as UpUs +from model.payload import UpdateUserDate as UpUs @allure.feature('User controller') @@ -25,14 +25,14 @@ def test_create_user(self, authorization): @allure.title('Update user by id') def test_update_user(self, user, authorization): - self.update_user.request_update_user(user, authorization) + self.update_user.request_update_user(user, authorization, payload=UpUs.data_update()) self.update_user.check_response_is_200() user_after = self.update_user.check_validate() self.update_user.check_update_user(user, user_after) @allure.title('Update user email by id') def test_update_email_user(self, user, authorization): - self.update_user.request_update_user(user, authorization, UpUs.update_email()) + self.update_user.request_update_user(user, authorization, payload=UpUs.update_email()) self.update_user.check_response_is_403() @allure.title('Get user full data by user id')