Skip to content

Commit

Permalink
Merge pull request #2 from montanizstills/feat/build-tools
Browse files Browse the repository at this point in the history
Issue CRUD
  • Loading branch information
montanizstills authored Sep 18, 2023
2 parents 59b98fe + cea2902 commit 8ff64a9
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 139 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/deploy-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

jobs:
unit_test:
environment: DEVELOPMENT
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -18,10 +19,15 @@ jobs:

- name: Install Pip
run: |
pip install --upgrade pip
if [ -f requirements.txt ]; then
pip install -r requirements.txt
fi
- name: Execute Tests
env:
JIRA_URL: ${{ vars.JIRA_URL }}
JIRA_AUTH_EMAIL: ${{ vars.JIRA_AUTH_EMAIL }}
JIRA_API_KEY: ${{ secrets.JIRA_API_KEY }}
run: |
python -m pytest -s src/tests
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/.benchmarks/
**/.pytest_cache/
resources/
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==7.4.2
requests==2.31.0
29 changes: 29 additions & 0 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import json
import os

import pytest
from requests.auth import HTTPBasicAuth


@pytest.fixture(scope='session')
def headers():
return {
"Content-Type": "application/json",
}


@pytest.fixture(scope='session')
def url():
return f'{os.environ.get("JIRA_URL")}/rest/api/2/issue/' if os.environ.get(
"JIRA_URL") else f"{json.load(open('../../resources/jira-credentials.json'))['jira_url']}/rest/api/2/issue/"


@pytest.fixture(scope='session')
def basic_auth():
auth_email = os.environ.get("JIRA_AUTH_EMAIL") if os.environ.get("JIRA_AUTH_EMAIL") else \
json.load(open('../../resources/jira-credentials.json'))['jira_auth_email']

token = os.environ.get("JIRA_API_KEY") if os.environ.get("JIRA_API_KEY") else \
json.load(open('../../resources/jira-credentials.json'))['jira_api_key']

return HTTPBasicAuth(auth_email, token)
253 changes: 114 additions & 139 deletions src/tests/test_issue.py
Original file line number Diff line number Diff line change
@@ -1,154 +1,129 @@
import json
import os
import unittest

from requests.auth import HTTPBasicAuth

from src.main import issue


class IssueTest(unittest.TestCase):
def setUp(self):
self.jira_auth_email = os.environ.get("JIRA_AUTH_EMAIL") if os.environ.get("JIRA_AUTH_EMAIL") else \
json.load(open('../../resources/jira-credentials.json'))['jira_auth_email']
self.token = os.environ.get("JIRA_API_KEY") if os.environ.get("JIRA_API_KEY") else \
json.load(open('../../resources/jira-credentials.json'))['jira_api_key']
self.url = os.environ.get("JIRA_URL") if os.environ.get(
"JIRA_URL") else f"{json.load(open('../../resources/jira-credentials.json'))['jira_url']}/rest/api/2/issue/"
self.basic_auth = HTTPBasicAuth(self.jira_auth_email, self.token)
self.headers = {
"Content-Type": "application/json",
}

def tearDown(self) -> None:
# clean up board after all tests
# clean up after create
# clean up after read
# clean up after update
pass

def test_create_issue(self):
# create story
# create bug
# create epic
# create subtask

# given
data = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - Create_for_Read_Test",
"description": "This issue is created to be read, then deleted.",
"issuetype": {
"name": "Story" # case-sensitive
}
def test_create_issue(url, headers, basic_auth):
# create story
# create bug
# create epic
# create subtask

# given
data = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - Create",
"description": "This issue is created to unit test issue.create_issue.",
"issuetype": {
"name": "Story" # case-sensitive
}
}

# when
response = issue.create_issue(self.url, self.headers, self.basic_auth, data).json()

# then
self.assertTrue(response.get('id'))
self.assertTrue(response.get('key'))
self.assertTrue(response.get('self'))

def test_read_issue(self):
# positive test for when issue exists
# given
data = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Issue summary2",
"description": "Issue description",
"issuetype": {
"name": "Story"
}
}

# when
response = issue.create_issue(url, headers, basic_auth, data).json()

# then
assert response.get('id')
assert response.get('key')


def test_read_issue(url, headers, basic_auth):
# positive test for when issue exists
# given
data = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - Create_for_Read_Test",
"description": "This issue is created to be read, then deleted.",
"issuetype": {
"name": "Story" # case-sensitive
}
}

# when
response = issue.create_issue(self.url, self.headers, self.basic_auth, data).json()
issue_key = response.get('key')
response = issue.read_issue(self.url, self.headers, self.basic_auth, issue_key).json()

# then
self.assertEqual('Story', response.get('fields').get('issuetype').get('name'))
self.assertIn(data.get('fields').get('project').get('key'), response.get('key'))

# negative test for when issue does not exist
# TODO

def test_update_issue(self):
# positive test for when issue exists
# given
start = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - Updated - Start",
"description": "This is the description when the issue is first created",
"issuetype": {
"name": "Story"
}
}
expected = data.get('fields').get('project').get('key')

# when
response = issue.create_issue(url, headers, basic_auth, data).json()
issue_key = response.get('key')
response = issue.read_issue(url, headers, basic_auth, issue_key).json()

# then
actual = response.get('key')
assert 'Story' == response.get('fields').get('issuetype').get('name')
assert expected in actual, f"Expected {expected}, but got {actual}"

# negative test for when issue does not exist
# TODO


def test_update_issue(url, headers, basic_auth):
# positive test for when issue exists
# given
start = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - Updated - Start",
"description": "This is the description when the issue is first created",
"issuetype": {
"name": "Story"
}
}
end = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - Updated - End",
"description": "This is the description after the issue has been updated.",
"issuetype": {
"name": "Bug"
}
}
end = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - Updated - End",
"description": "This is the description after the issue has been updated.",
"issuetype": {
"name": "Bug"
}
}

# when
issue_key = issue.create_issue(self.url, self.headers, self.basic_auth, start).json().get('key')
issue.update_issue(self.url, self.headers, self.basic_auth, issue_key, end)
updated_issue_json = issue.read_issue(self.url, self.headers, self.basic_auth, issue_key).json()

# then
self.assertEqual(end.get('description'), updated_issue_json.get('description'),
f"Expected {end.get('description')}, but got {updated_issue_json.get('description')}")

# negative test for when issue does not exist
# TODO

def test_delete_issue(self):
# positive test for when issue exists
# given
data = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - to be deleted",
"description": "This is the description of an issue you will never see.",
"issuetype": {
"name": "Story"
}
}
expected = end.get('description')

# when
issue_key = issue.create_issue(url, headers, basic_auth, start).json().get('key')
issue.update_issue(url, headers, basic_auth, issue_key, end)
updated_issue_json = issue.read_issue(url, headers, basic_auth, issue_key).json()
actual = updated_issue_json.get('description')

# then
assert expected == actual, f"Expected {expected}, but got {actual}"

# negative test for when issue does not exist
# TODO


def test_delete_issue(url, headers, basic_auth):
# positive test for when issue exists
# given
data = {
"fields": {
"project": {
"key": "SAGC"
},
"summary": "Test Issue - to be deleted",
"description": "This is the description of an issue you will never see.",
"issuetype": {
"name": "Story"
}
}
}

# when
response = issue.create_issue(self.url, self.headers, self.basic_auth, data).json()
issue_key = response.get('key')
delete_response = issue.delete_issue(self.url, self.headers, self.basic_auth, issue_key)
error_json = issue.read_issue(self.url, self.headers, self.basic_auth, issue_key).json()

# then
assert delete_response.status_code == 204
assert 'Issue does not exist or you do not have permission to see it.' in error_json.get('errorMessages')

# when
response = issue.create_issue(url, headers, basic_auth, data).json()
issue_key = response.get('key')
delete_response = issue.delete_issue(url, headers, basic_auth, issue_key)
error_json = issue.read_issue(url, headers, basic_auth, issue_key).json()

if __name__ == "__main__":
unittest.main()
# then
assert delete_response.status_code == 204
assert 'Issue does not exist or you do not have permission to see it.' in error_json.get('errorMessages')

0 comments on commit 8ff64a9

Please sign in to comment.