Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added authentication method detection to gerrit #464

Merged
merged 2 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions bugwarrior/services/gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ def __init__(self, *args, **kw):
'Accept-Encoding': 'gzip',
})

self.session.auth = requests.auth.HTTPDigestAuth(
self.username, self.password)

if self.ssl_ca_path != None:
if self.ssl_ca_path:
self.session.verify = os.path.expanduser(self.ssl_ca_path)

# uses digest authentication if supported by the server, fallback to basic
# gerrithub.io supports only basic
response = self.session.head(self.url + '/a/')
if 'digest' in response.headers.get('www-authenticate', '').lower():
self.session.auth = requests.auth.HTTPDigestAuth(
self.username, self.password)
else:
self.session.auth = requests.auth.HTTPBasicAuth(
self.username, self.password)

@staticmethod
def get_keyring_service(service_config):
base_uri = service_config.get('base_uri')
Expand All @@ -110,6 +117,7 @@ def issues(self):
'?q=is:open+is:reviewer' + \
'&o=MESSAGES&o=DETAILED_ACCOUNTS'
response = self.session.get(url)
response.raise_for_status()
# The response has some ")]}'" garbage prefixed.
body = response.text[4:]
changes = json.loads(body)
Expand Down
4 changes: 4 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class ServiceTest(ConfigTest):
SERVICE_CONFIG = {
}

@classmethod
def setUpClass(cls):
cls.maxDiff = None

def get_mock_service(
self, service_class, section='unspecified',
config_overrides=None, general_overrides=None
Expand Down
13 changes: 9 additions & 4 deletions tests/test_gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TestGerritIssue(AbstractServiceTest, ServiceTest):
SERVICE_CONFIG = {
'gerrit.base_uri': 'https://one',
'gerrit.base_uri': 'https://one.com',
'gerrit.username': 'two',
'gerrit.password': 'three',
}
Expand All @@ -27,6 +27,11 @@ class TestGerritIssue(AbstractServiceTest, ServiceTest):

def setUp(self):
super(TestGerritIssue, self).setUp()

responses.add(
responses.HEAD,
self.SERVICE_CONFIG['gerrit.base_uri'] + '/a/',
adding_headers={'www-authenticate': 'digest'})
self.service = self.get_mock_service(GerritService)

def test_to_taskwarrior(self):
Expand Down Expand Up @@ -56,18 +61,18 @@ def test_to_taskwarrior(self):
@responses.activate
def test_issues(self):
self.add_response(
'https://one/a/changes/?q=is:open+is:reviewer&o=MESSAGES&o=DETAILED_ACCOUNTS',
'https://one.com/a/changes/?q=is:open+is:reviewer&o=MESSAGES&o=DETAILED_ACCOUNTS',
# The response has some ")]}'" garbage prefixed.
body=")]}'" + json.dumps([self.record]))

issue = next(self.service.issues())

expected = {
'annotations': [u'@Iam Author - is is a message'],
'description': u'(bw)PR#1 - this is a title .. https://one/#/c/1/',
'description': u'(bw)PR#1 - this is a title .. https://one.com/#/c/1/',
'gerritid': 1,
'gerritsummary': u'this is a title',
'gerriturl': 'https://one/#/c/1/',
'gerriturl': 'https://one.com/#/c/1/',
'gerritbranch': 'master',
'gerrittopic': 'test-topic',
'priority': 'M',
Expand Down