File: test_whoami.py

package info (click to toggle)
python-jenkins 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 556 kB
  • sloc: python: 6,099; makefile: 150
file content (48 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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?depth=0'))
        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?depth=0'))