/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
"""
46
        f = open(osutils.pathjoin(self.test_home_dir, '.netrc'), 'wb')
47
        try:
48
            f.write(netrc_content)
49
        finally:
50
            f.close()
51
4107.1.3 by Jean-Francois Roy
Updated the test_default_password_without_user to access the netrc credential store directly.
52
    def _get_netrc_cs(self):
53
        return  config.credential_store_registry.get_credential_store('netrc')
54
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
55
    def test_not_matching_user(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
56
        cs = self._get_netrc_cs()
57
        password = cs.decode_password(dict(host='host', user='jim'))
58
        self.assertIs(None, password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
59
60
    def test_matching_user(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
61
        cs = self._get_netrc_cs()
62
        password = cs.decode_password(dict(host='host', user='joe'))
63
        self.assertEquals('secret', password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
64
65
    def test_default_password(self):
4107.1.9 by Jean-Francois Roy
Reverted the chages made to the netrc tests for this patch.
66
        cs = self._get_netrc_cs()
67
        password = cs.decode_password(dict(host='other', user='anonymous'))
68
        self.assertEquals('joe@home', password)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
69
70
    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.
71
        cs = self._get_netrc_cs()
72
        password = cs.decode_password(dict(host='other'))
73
        self.assertIs(None, password)
4107.1.12 by Vincent Ladeuil
Easier patched than said :)
74
75
    def test_get_netrc_credentials_via_auth_config(self):
76
        # Create a test AuthenticationConfig object
77
        ac_content = """
78
[host1]
79
host = host
80
user = joe
81
password_encoding = netrc
82
"""
83
        conf = config.AuthenticationConfig(_file=StringIO(ac_content))
84
        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.
85
        self.assertIsNot(None, credentials)
4137.2.2 by Jean-Francois Roy
Use assertEquals, not assertIs in test_get_credentials.
86
        self.assertEquals('secret', credentials.get('password', None))