python-jenkins/tests/test_whoami.py
Darragh Bailey 34cca0c4d9 Migration to using requests
Convert to the requests library to allow for more sophisticated response
handling to be added.

Want to allow for applications to override the response handling in
certain cases where the default is incorrect. Handling of urlopen
responses results in version specific handling to ensure correct
behaviour across multiple versions of python.

Changing to use the requests package, provides a higher level interface
and removes some of the version specific handling for exceptions.

Change-Id: I5369a0d35be4bf8b3b197a51e60aba21b5742cc7
Depends-On: Iabd70aa457ceb4dbc147d7cbaeec913148cb3b56
2018-02-12 11:16:54 +00:00

49 lines
1.6 KiB
Python

import json
from mock import patch
import jenkins
from tests.base import JenkinsTestBase
from tests.helper import build_response_mock
class JenkinsWhoamiTest(JenkinsTestBase):
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_simple(self, jenkins_mock):
user_to_return = \
{u'absoluteUrl': u'https://example.com/jenkins/user/jsmith',
u'description': None,
u'fullName': u'John Smith',
u'id': u'jsmith',
u'property': [{},
{},
{},
{u'address': u'jsmith@example.com'},
{},
{},
{u'insensitiveSearch': False},
{}]}
jenkins_mock.return_value = json.dumps(user_to_return)
user = self.j.get_whoami()
self.assertEqual(user, user_to_return)
self.assertEqual(
jenkins_mock.call_args[0][0].url,
self.make_url('me/api/json'))
self._check_requests(jenkins_mock.call_args_list)
@patch('jenkins.requests.Session.send', autospec=True)
def test_raise_HTTPError(self, session_send_mock):
session_send_mock.side_effect = iter([
build_response_mock(404, reason="Not Found"), # crumb
build_response_mock(401, reason="Basic Auth Failed"), # request
])
with self.assertRaises(jenkins.JenkinsException):
self.j.get_whoami()
self.assertEqual(
session_send_mock.call_args_list[1][0][1].url,
self.make_url('me/api/json'))