/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2008, 2009, 2013, 2016 Canonical Ltd
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
16
6625.2.1 by Martin
Fix import level for netrc_credential_store
17
from .... import (
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
18
    config,
19
    errors,
20
    osutils,
21
    tests,
22
    )
6625.2.1 by Martin
Fix import level for netrc_credential_store
23
from ....sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
24
    BytesIO,
25
    )
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
26
6625.2.1 by Martin
Fix import level for netrc_credential_store
27
from ... import netrc_credential_store
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
28
29
30
class TestNetrcCSNoNetrc(tests.TestCaseInTempDir):
31
32
    def test_home_netrc_does_not_exist(self):
33
        self.assertRaises(errors.NoSuchFile,
34
                          config.credential_store_registry.get_credential_store,
35
                          'netrc')
36
37
38
class TestNetrcCS(tests.TestCaseInTempDir):
39
40
    def setUp(self):
41
        super(TestNetrcCS, self).setUp()
42
        # Create a .netrc file
43
        netrc_content = """
44
machine host login joe password secret
45
default login anonymous password joe@home
46
"""
6587.1.1 by Vincent Ladeuil
Make .netrc 0600 in tests to fix python-2.7.5-8's netrc happy.
47
        netrc_path = osutils.pathjoin(self.test_home_dir, '.netrc')
48
        with open(netrc_path, 'wb') as f:
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
49
            f.write(netrc_content)
6587.1.1 by Vincent Ladeuil
Make .netrc 0600 in tests to fix python-2.7.5-8's netrc happy.
50
        # python's netrc will complain about access permissions starting with
51
        # 2.7.5-8 so we restrict the access unconditionally
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
52
        osutils.chmod_if_possible(netrc_path, 0o600)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
53
4107.1.3 by Jean-Francois Roy
Updated the test_default_password_without_user to access the netrc credential store directly.
54
    def _get_netrc_cs(self):
55
        return  config.credential_store_registry.get_credential_store('netrc')
56
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
57
    def test_not_matching_user(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
58
        cs = self._get_netrc_cs()
59
        password = cs.decode_password(dict(host='host', user='jim'))
60
        self.assertIs(None, password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
61
62
    def test_matching_user(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
63
        cs = self._get_netrc_cs()
64
        password = cs.decode_password(dict(host='host', user='joe'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
65
        self.assertEqual('secret', password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
66
67
    def test_default_password(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
68
        cs = self._get_netrc_cs()
69
        password = cs.decode_password(dict(host='other', user='anonymous'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
70
        self.assertEqual('joe@home', password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
71
72
    def test_default_password_without_user(self):
4107.1.3 by Jean-Francois Roy
Updated the test_default_password_without_user to access the netrc credential store directly.
73
        cs = self._get_netrc_cs()
74
        password = cs.decode_password(dict(host='other'))
75
        self.assertIs(None, password)
4107.1.12 by Vincent Ladeuil
Easier patched than said :)
76
77
    def test_get_netrc_credentials_via_auth_config(self):
78
        # Create a test AuthenticationConfig object
79
        ac_content = """
80
[host1]
81
host = host
82
user = joe
83
password_encoding = netrc
84
"""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
85
        conf = config.AuthenticationConfig(_file=BytesIO(ac_content))
4107.1.12 by Vincent Ladeuil
Easier patched than said :)
86
        credentials = conf.get_credentials('scheme', 'host', user='joe')
4137.2.1 by Jean-Francois Roy
Added new netrc crendential store plug-in test to verify its usage through get_credentials.
87
        self.assertIsNot(None, credentials)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
88
        self.assertEqual('secret', credentials.get('password', None))