11# -*- coding: utf-8 -*-
22from __future__ import unicode_literals
33import pytest # noqa
4+ import io
45import requests # noqa
56import sys
67
78try :
89 # python 3.4+ should use builtin unittest.mock not mock package
9- from unittest .mock import patch
10+ from unittest .mock import patch , MagicMock
1011except ImportError :
11- from mock import patch
12+ from mock import patch , MagicMock
1213
1314from jira import Role , Issue , JIRA , JIRAError , Project # noqa
1415import jira .jirashell as jirashell
1516
1617
17- def test_unicode (requests_mock , capsys ):
18+ @pytest .fixture
19+ def testargs ():
20+ return ["jirashell" , "-s" , "http://localhost" ]
21+
22+
23+ def test_unicode (requests_mock , capsys , testargs ):
1824 """This functions tests that CLI tool does not throw an UnicodeDecodeError
1925 when it attempts to display some Unicode error message, which can happen
2026 when printing exceptions received from the remote HTTP server.
@@ -23,9 +29,77 @@ def test_unicode(requests_mock, capsys):
2329 Likely not needed for Py3 versions.
2430 """
2531 requests_mock .register_uri ('GET' , 'http://localhost/rest/api/2/serverInfo' , text = 'Δεν βρέθηκε' , status_code = 404 )
26- testargs = [ "jirashell" , "-s" , "http://localhost" ]
32+
2733 with patch .object (sys , 'argv' , testargs ):
2834 jirashell .main ()
2935 captured = capsys .readouterr ()
3036 assert captured .err .startswith ("JiraError HTTP 404" )
3137 assert captured .out == ""
38+
39+
40+ @pytest .fixture
41+ def mock_keyring ():
42+ _keyring = {}
43+
44+ def mock_set_password (server , username , password ):
45+ _keyring [(server , username )] = password
46+
47+ def mock_get_password (server , username ):
48+ return _keyring .get ((server , username ), '' )
49+
50+ mock_kr = MagicMock (
51+ set_password = MagicMock (side_effect = mock_set_password ),
52+ get_password = MagicMock (side_effect = mock_get_password ),
53+ _keyring = _keyring ,
54+ )
55+ mocked_module = patch .object (jirashell , 'keyring' , new = mock_kr )
56+ yield mocked_module .start ()
57+ mocked_module .stop ()
58+
59+
60+ @pytest .mark .timeout (4 )
61+ def test_no_password_try_keyring (requests_mock , capsys , testargs , mock_keyring , monkeypatch ):
62+ requests_mock .register_uri ('GET' , 'http://localhost/rest/api/2/serverInfo' , status_code = 200 )
63+
64+ # no password provided
65+ args = testargs + ['-u' , 'test@user' ]
66+ with patch .object (sys , 'argv' , args ):
67+ jirashell .main ()
68+
69+ assert len (requests_mock .request_history ) == 0
70+ captured = capsys .readouterr ()
71+ assert captured .err == "No password provided!\n assert ''\n "
72+ assert "Getting password from keyring..." == captured .out .strip ()
73+ assert mock_keyring ._keyring == {}
74+
75+ # password provided, don't save
76+ monkeypatch .setattr ('sys.stdin' , io .StringIO ('n' ))
77+ args = args + ['-p' , 'pass123' ]
78+ with patch .object (sys , 'argv' , args ):
79+ jirashell .main ()
80+
81+ assert len (requests_mock .request_history ) == 4
82+ captured = capsys .readouterr ()
83+ assert captured .out .strip () == "Would you like to remember password in OS keyring? (y/n)"
84+ assert mock_keyring ._keyring == {}
85+
86+ # password provided, save
87+ monkeypatch .setattr ('sys.stdin' , io .StringIO ('y' ))
88+ args = args + ['-p' , 'pass123' ]
89+ with patch .object (sys , 'argv' , args ):
90+ jirashell .main ()
91+
92+ assert len (requests_mock .request_history ) == 8
93+ captured = capsys .readouterr ()
94+ assert captured .out .strip () == "Would you like to remember password in OS keyring? (y/n)"
95+ assert mock_keyring ._keyring == {('http://localhost' , 'test@user' ): 'pass123' }
96+
97+ # user stored password
98+ args = testargs + ['-u' , 'test@user' ]
99+ with patch .object (sys , 'argv' , args ):
100+ jirashell .main ()
101+
102+ assert len (requests_mock .request_history ) == 12
103+ captured = capsys .readouterr ()
104+ assert "Getting password from keyring..." == captured .out .strip ()
105+ assert mock_keyring ._keyring == {('http://localhost' , 'test@user' ): 'pass123' }
0 commit comments