From b089d5c262766545554f116e396b4b72e2384303 Mon Sep 17 00:00:00 2001 From: Oleg Averkin Date: Thu, 16 May 2019 15:53:09 +0300 Subject: [PATCH] Phase 5 API testing with 'Requests' --- .circleci/config.yml | 2 +- src/jira_api.py | 62 ++++++++++++++++++++++++++++ src/json_fixtures.py | 25 ++++++++++++ tests/test_jira_api.py | 91 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 src/jira_api.py create mode 100644 src/json_fixtures.py create mode 100644 tests/test_jira_api.py diff --git a/.circleci/config.yml b/.circleci/config.yml index 5368a31..301a443 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -27,7 +27,7 @@ jobs: command: | . venv/bin/activate chmod -R 777 ./allure-2.11.0/bin/ - pytest --alluredir=./allure-results + pytest --alluredir=./allure-results tests/test_jira_api.py - run: name: run report when: always diff --git a/src/jira_api.py b/src/jira_api.py new file mode 100644 index 0000000..a767e1e --- /dev/null +++ b/src/jira_api.py @@ -0,0 +1,62 @@ +import requests +import json +import allure +from src.json_fixtures import JSONFixture + +username = "OlegAverkin" +password = "OlegAverkin" +base_url = "https://jira.hillel.it" +create_issue = base_url + "/rest/api/2/issue" +search_issue = base_url + "/rest/api/2/search" +modify_issue = base_url + "/rest/api/2/issue/{0}" + +headers = { + "Authorization": "Basic T2xlZ0F2ZXJraW46T2xlZ0F2ZXJraW4=", + "Content-Type": "application/json; charset=utf8" +} + +created_issues = [] +j = JSONFixture() + + +class JiraApi: + + @allure.step + def login(self, username, password): + result = requests.get(base_url, auth=(username, password)) + return [result.status_code, result.headers.get("X-Seraph-LoginReason")] + + @allure.step + def search_issue(self, issue_name, start=0, max_results=10): + result = requests.post(search_issue, data=json.dumps(j.search_issue(issue_name, start, max_results)), + headers=headers) + return result + + @allure.step + def post_issue(self, summary, assignee, priority): + result = requests.post(create_issue, data=json.dumps(j.create_issue(summary, assignee, priority)), + headers=headers) + if result.status_code == 400: + return result + else: + created_issues.append(result.json().get('id')) + return result + + @allure.step + def update_issue(self, issue_id, summary, assignee, priority): + result = requests.put(modify_issue.format(issue_id), data=json.dumps(j.create_issue(summary, assignee, priority)), + headers=headers) + return result + + @allure.step + def delete_issue(self, issue_id): + result = requests.delete(modify_issue.format(issue_id), headers=headers) + return result + + @allure.step + def delete_all_issues(self): + response_codes_list = [] + for issue_id in created_issues: + result = JiraApi.delete_issue(self, issue_id) + response_codes_list.append(result.status_code) + return response_codes_list diff --git a/src/json_fixtures.py b/src/json_fixtures.py new file mode 100644 index 0000000..5b6d3ed --- /dev/null +++ b/src/json_fixtures.py @@ -0,0 +1,25 @@ +class JSONFixture: + + def create_issue(self, summary, assignee, priority): + json = { + "fields": { + "project": + { + "key": "WEBINAR" + }, + "summary": summary, + "description": "Creating of an issue", + "assignee": {"name": assignee}, + "priority": {"name": priority}, + "issuetype": {"name": "Bug"} + } + } + return json + + def search_issue(self, issue_name, start, max_results): + json = { + "jql": "summary~" + issue_name, + "startAt": start, + "maxResults": max_results + } + return json diff --git a/tests/test_jira_api.py b/tests/test_jira_api.py new file mode 100644 index 0000000..60c0b41 --- /dev/null +++ b/tests/test_jira_api.py @@ -0,0 +1,91 @@ +import pytest +from src.jira_api import * + +api = JiraApi() + + +class TestSuite: + + @allure.tag('api') + @allure.title("Login via API") + @pytest.mark.api + @pytest.mark.parametrize("response,expected", [ + (api.login("OlegAverkin", "fake"), [401, "AUTHENTICATED_FAILED"]), + (api.login("fake", "OlegAverkin"), [401, "AUTHENTICATED_FAILED"]), + (api.login("OlegAverkin", "OlegAverkin"), [200, "OK"]), ]) + def test_login_to_jira(self, response, expected): + assert response == expected + + @allure.tag('api') + @allure.title("Post issue") + @pytest.mark.api + def test_post_issue(self): + response = api.post_issue("OA API Issue001", "OlegAverkin", "Low") + assert response.status_code == 201 + + @allure.tag('api') + @allure.title("Update issue") + @pytest.mark.api + def test_update_issue(self): + response = api.post_issue("OA API Issue002 ", "OlegAverkin", "Medium") + issue_id = response.json().get('id') + response_update = api.update_issue(issue_id, "OA API Issue002 Updated", "OlegAverkin", "Low") + assert response_update.status_code == 204 + + @allure.tag('api') + @allure.title("Post multiple issues") + @pytest.mark.api + @pytest.mark.parametrize("response,expected", [ + (api.post_issue("OA API Issue003", "OlegAverkin", "Low"), 201), + (api.post_issue("OA API Issue004", "OlegAverkin", "Low"), 201), + (api.post_issue("OA API Issue005", "OlegAverkin", "Low"), 201), + (api.post_issue("OA API Issue006", "OlegAverkin", "Low"), 201)]) + def test_post_issues(self, response, expected): + assert response.status_code == expected + + @allure.tag('api') + @allure.title("Missing required fields") + @pytest.mark.api + def test_post_issue_missing_fields(self): + response = api.post_issue("", "OlegAverkin", "Low") + assert response.status_code == 400 + assert response.json().get('errors').get('summary') == "You must specify a summary of the issue." + + @allure.tag('api') + @allure.title("Long summary") + @pytest.mark.api + def test_post_issue_long_summary(self): + response = api.post_issue("AO" * 256, "OlegAverkin", "Low") + assert response.status_code == 400 + assert response.json().get('errors').get('summary') == "Summary must be less than 255 characters." + + @allure.tag('api') + @allure.title("Test search issue") + @pytest.mark.api + def test_search_1_issue(self): + response = api.search_issue("Issue001") + assert response.status_code == 200 + assert response.json().get('total') == 1 + + @allure.tag('api') + @allure.title("Test five issue") + @pytest.mark.api + def test_search_5_issues(self): + response = api.search_issue("OA") + assert response.status_code == 200 + assert response.json().get('total') == 6 + + @allure.tag('api') + @allure.title("Test search unknown issue") + @pytest.mark.api + def test_search_none_issue(self): + response = api.search_issue("somethingwrong") + assert response.status_code == 200 + assert response.json().get('total') == 0 + + @allure.tag('api') + @allure.title("Delete all issues") + @pytest.mark.api + def test_delete_all_issues(self): + response = api.delete_all_issues() + assert len(response) == 6