Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Fixed delete_project and added tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
lechat committed Jul 9, 2016
1 parent 73bb2c7 commit 0ce4195
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 27 deletions.
54 changes: 28 additions & 26 deletions jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2503,48 +2503,50 @@ def current_user(self):

def delete_project(self, pid):
"""
Project can be id, project key or project name. It will return False if it fails.
Deletes project from Jira
:param str pid: JIRA projectID or Project or slug
:returns bool: True if project was deleted
:raises JIRAError: If project not found or not enough permissions
:raises ValueError: If pid parameter is not Project, slug or ProjectID
"""

# allows us to call it with Project objects
if hasattr(pid, 'id'):
pid = pid.id

found = False
# Check if pid is a number - then we assume that it is
# projectID
try:
if not str(int(pid)) == pid:
found = True
str(int(pid)) == pid
except Exception as e:
# pid looks like a slug, lets verify that
r_json = self._get_json('project')
for e in r_json:
if e['key'] == pid or e['name'] == pid:
pid = e['id']
found = True
break
if not found:
logging.error("Unable to recognize project `%s`" % pid)
return False
else:
# pid is not a Project
# not a projectID and not a slug - we raise error here
raise ValueError('Parameter pid="%s" is not a Project, '
'projectID or slug' % pid)

uri = '/secure/project/DeleteProject.jspa'
uri = '/rest/api/2/project/%s' % pid
url = self._options['server'] + uri
payload = {'pid': pid, 'Delete': 'Delete', 'confirm': 'true'}
# try:
# r = self._gain_sudo_session(payload, uri)
# if r.status_code != 200 or not self._check_for_html_error(r.text):
# return False
# except JIRAError as e:
# raise JIRAError(0, "You must have global administrator rights to delete projects.")
# return False

r = self._session.post(
url, headers=CaseInsensitiveDict({'content-type': 'application/x-www-form-urlencoded'}), data=payload)
try:
r = self._session.delete(
url, headers={'Content-Type': 'application/json'}
)
except JIRAError as je:
if '403' in str(je):
raise JIRAError('Not enough permissions to delete project')
if '404' in str(je):
raise JIRAError('Project not found in Jira')
raise je

if r.status_code == 200:
return self._check_for_html_error(r.text)
else:
logging.warning(
'Got %s response from calling delete_project.' % r.status_code)
return r.status_code
if r.status_code == 204:
return True

def _gain_sudo_session(self, options, destination):
url = self._options['server'] + '/secure/admin/WebSudoAuthenticate.jspa'
Expand Down
115 changes: 114 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,123 @@
import re
import sys
import json
import pytest
import getpass
from tests import JiraTestManager
from jira import Role, Issue, JIRA, JIRAError, Project # noqa

import jira.client


@pytest.fixture(scope='module')
def cl_admin():
return JiraTestManager(make_defaults=False).jira_admin


@pytest.fixture(scope='module')
def cl_normal():
return JiraTestManager(make_defaults=False).jira_normal


@pytest.fixture(scope='function')
def slug(request, cl_admin):
def remove_by_slug():
try:
cl_admin.delete_project(slug)
except ValueError:
# Some tests have project already removed, so we stay silent
pass

prefix = (
'T' + (re.sub("[^A-Z]", "", getpass.getuser().upper()))[0:6] +
str(sys.version_info[0]) + str(sys.version_info[1])
)

slug = prefix + 'T'
project_name = (
"Test user=%s key=%s A" % (getpass.getuser(), slug)
)

already_exists = True
try:
proj = cl_admin.project(slug)
except JIRAError:
already_exists = False

if not already_exists:
proj = cl_admin.create_project(slug, project_name)
assert proj

request.addfinalizer(remove_by_slug)

return slug


def test_delete_project(cl_admin, slug):
assert cl_admin.delete_project(slug)


def test_delete_inexistant_project(cl_admin):
slug = 'abogus123'
with pytest.raises(ValueError) as ex:
assert cl_admin.delete_project(slug)

assert (
'Parameter pid="%s" is not a Project, projectID or slug' % slug in
str(ex.value)
)


def test_no_rights_to_delete_project(cl_normal, slug):
with pytest.raises(JIRAError) as ex:
assert cl_normal.delete_project(slug)

assert 'Not enough permissions to delete project' in str(ex.value)


def test_template_list():
text = r'{"projectTemplatesGroupedByType": [ { "projectTemplates": [ { "projectTemplateModuleCompleteKey": "com.pyxis.greenhopper.jira:gh-scrum-template", "name": "Scrum software development"}, { "projectTemplateModuleCompleteKey": "com.pyxis.greenhopper.jira:gh-kanban-template", "name": "Kanban software development"}, { "projectTemplateModuleCompleteKey": "com.pyxis.greenhopper.jira:basic-software-development-template", "name": "Basic software development"} ], "applicationInfo": { "applicationName": "JIRA Software"} }, { "projectTypeBean": { "projectTypeKey": "service_desk", "projectTypeDisplayKey": "Service Desk"}, "projectTemplates": [ { "projectTemplateModuleCompleteKey": "com.atlassian.servicedesk:classic-service-desk-project", "name": "Basic Service Desk"}, { "projectTemplateModuleCompleteKey": "com.atlassian.servicedesk:itil-service-desk-project", "name": "IT Service Desk"} ], "applicationInfo": { "applicationName": "JIRA Service Desk"} }, { "projectTypeBean": { "projectTypeKey": "business", "projectTypeDisplayKey": "Business"}, "projectTemplates": [ { "projectTemplateModuleCompleteKey": "com.atlassian.jira-core-project-templates:jira-core-task-management", "name": "Task management"}, { "projectTemplateModuleCompleteKey": "com.atlassian.jira-core-project-templates:jira-core-project-management", "name": "Project management"}, { "projectTemplateModuleCompleteKey": "com.atlassian.jira-core-project-templates:jira-core-process-management", "name": "Process management"} ], "applicationInfo": { "applicationName": "JIRA Core"} }], "maxNameLength": 80, "minNameLength": 2, "maxKeyLength": 10 }' # noqa
text = (
r'{"projectTemplatesGroupedByType": ['
' { "projectTemplates": [ { "projectTemplateModuleCompleteKey": '
'"com.pyxis.greenhopper.jira:gh-scrum-template", '
'"name": "Scrum software development"}, '
'{ "projectTemplateModuleCompleteKey": '
'"com.pyxis.greenhopper.jira:gh-kanban-template", '
'"name": "Kanban software development"}, '
'{ "projectTemplateModuleCompleteKey": '
'"com.pyxis.greenhopper.jira:'
'basic-software-development-template",'
' "name": "Basic software development"} ],'
' "applicationInfo": { '
'"applicationName": "JIRA Software"} }, '
'{ "projectTypeBean": { '
'"projectTypeKey": "service_desk", '
'"projectTypeDisplayKey": "Service Desk"}, '
'"projectTemplates": [ { '
'"projectTemplateModuleCompleteKey": '
'"com.atlassian.servicedesk:classic-service-desk-project", '
'"name": "Basic Service Desk"},'
' { "projectTemplateModuleCompleteKey": '
'"com.atlassian.servicedesk:itil-service-desk-project",'
' "name": "IT Service Desk"} ], '
'"applicationInfo": { '
'"applicationName": "JIRA Service Desk"} }, '
'{ "projectTypeBean": { '
'"projectTypeKey": "business", '
'"projectTypeDisplayKey": "Business"}, '
'"projectTemplates": [ { '
'"projectTemplateModuleCompleteKey": '
'"com.atlassian.jira-core-project-templates:jira-core-task-management", '
'"name": "Task management"}, {'
' "projectTemplateModuleCompleteKey": '
'"com.atlassian.jira-core-project-templates:jira-core-project-management", '
'"name": "Project management"}, { '
'"projectTemplateModuleCompleteKey": '
'"com.atlassian.jira-core-project-templates:jira-core-process-management", '
'"name": "Process management"} ], '
'"applicationInfo": { "applicationName": "JIRA Core"} }],'
' "maxNameLength": 80, "minNameLength": 2, "maxKeyLength": 10 }'
) # noqa
j = json.loads(text)
template_list = jira.client._get_template_list(j)
assert [t['name'] for t in template_list] == ["Scrum software development", "Kanban software development", "Basic software development",
Expand Down

0 comments on commit 0ce4195

Please sign in to comment.