~bzr-pqm/bzr/bzr.dev

3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
1
# Copyright (C) 2008 Canonical Ltd
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
4107.1.12 by Vincent Ladeuil
Easier patched than said :)
17
from cStringIO import StringIO
4137.2.1 by Jean-Francois Roy
Added new netrc crendential store plug-in test to verify its usage through get_credentials.
18
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
19
from bzrlib import (
20
    config,
21
    errors,
22
    osutils,
23
    tests,
24
    )
25
26
from bzrlib.plugins import netrc_credential_store
27
28
29
class TestNetrcCSNoNetrc(tests.TestCaseInTempDir):
30
31
    def test_home_netrc_does_not_exist(self):
32
        self.assertRaises(errors.NoSuchFile,
33
                          config.credential_store_registry.get_credential_store,
34
                          'netrc')
35
36
37
class TestNetrcCS(tests.TestCaseInTempDir):
38
39
    def setUp(self):
40
        super(TestNetrcCS, self).setUp()
41
        # Create a .netrc file
42
        netrc_content = """
43
machine host login joe password secret
44
default login anonymous password joe@home
45
"""
6587.1.1 by Vincent Ladeuil
Make .netrc 0600 in tests to fix python-2.7.5-8's netrc happy.
46
        netrc_path = osutils.pathjoin(self.test_home_dir, '.netrc')
47
        with open(netrc_path, 'wb') as f:
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
48
            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.
49
        # python's netrc will complain about access permissions starting with
50
        # 2.7.5-8 so we restrict the access unconditionally
6587.1.2 by Vincent Ladeuil
Use osutils.chmod_if_possible as raised during review.
51
        osutils.chmod_if_possible(netrc_path, 0600)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
52
4107.1.3 by Jean-Francois Roy
Updated the test_default_password_without_user to access the netrc credential store directly.
53
    def _get_netrc_cs(self):
54
        return  config.credential_store_registry.get_credential_store('netrc')
55
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
56
    def test_not_matching_user(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
57
        cs = self._get_netrc_cs()
58
        password = cs.decode_password(dict(host='host', user='jim'))
59
        self.assertIs(None, password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
60
61
    def test_matching_user(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
62
        cs = self._get_netrc_cs()
63
        password = cs.decode_password(dict(host='host', user='joe'))
64
        self.assertEquals('secret', password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
65
66
    def test_default_password(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
67
        cs = self._get_netrc_cs()
68
        password = cs.decode_password(dict(host='other', user='anonymous'))
69
        self.assertEquals('joe@home', password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
70
71
    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.
72
        cs = self._get_netrc_cs()
73
        password = cs.decode_password(dict(host='other'))
74
        self.assertIs(None, password)
4107.1.12 by Vincent Ladeuil
Easier patched than said :)
75
76
    def test_get_netrc_credentials_via_auth_config(self):
77
        # Create a test AuthenticationConfig object
78
        ac_content = """
79
[host1]
80
host = host
81
user = joe
82
password_encoding = netrc
83
"""
84
        conf = config.AuthenticationConfig(_file=StringIO(ac_content))
85
        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.
86
        self.assertIsNot(None, credentials)
4137.2.2 by Jean-Francois Roy
Use assertEquals, not assertIs in test_get_credentials.
87
        self.assertEquals('secret', credentials.get('password', None))