/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
7479.2.1 by Jelmer Vernooij
Drop python2 support.
17
from io import BytesIO
6625.2.1 by Martin
Fix import level for netrc_credential_store
18
from .... import (
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
19
    config,
20
    errors,
21
    osutils,
22
    tests,
23
    )
24
6625.2.1 by Martin
Fix import level for netrc_credential_store
25
from ... import netrc_credential_store
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
26
27
28
class TestNetrcCSNoNetrc(tests.TestCaseInTempDir):
29
30
    def test_home_netrc_does_not_exist(self):
31
        self.assertRaises(errors.NoSuchFile,
32
                          config.credential_store_registry.get_credential_store,
33
                          'netrc')
34
35
36
class TestNetrcCS(tests.TestCaseInTempDir):
37
38
    def setUp(self):
39
        super(TestNetrcCS, self).setUp()
40
        # Create a .netrc file
6973.13.2 by Jelmer Vernooij
Fix some more tests.
41
        netrc_content = b"""
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
42
machine host login joe password secret
43
default login anonymous password joe@home
44
"""
6587.1.1 by Vincent Ladeuil
Make .netrc 0600 in tests to fix python-2.7.5-8's netrc happy.
45
        netrc_path = osutils.pathjoin(self.test_home_dir, '.netrc')
46
        with open(netrc_path, 'wb') as f:
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
47
            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.
48
        # python's netrc will complain about access permissions starting with
49
        # 2.7.5-8 so we restrict the access unconditionally
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
50
        osutils.chmod_if_possible(netrc_path, 0o600)
3757.3.2 by Vincent Ladeuil
Add a credential store for '.netrc'.
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):
7143.15.2 by Jelmer Vernooij
Run autopep8.
53
        return config.credential_store_registry.get_credential_store('netrc')
4107.1.3 by Jean-Francois Roy
Updated the test_default_password_without_user to access the netrc credential store directly.
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'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
63
        self.assertEqual('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'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
68
        self.assertEqual('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
6973.13.2 by Jelmer Vernooij
Fix some more tests.
77
        ac_content = b"""
4107.1.12 by Vincent Ladeuil
Easier patched than said :)
78
[host1]
79
host = host
80
user = joe
81
password_encoding = netrc
82
"""
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
83
        conf = config.AuthenticationConfig(_file=BytesIO(ac_content))
4107.1.12 by Vincent Ladeuil
Easier patched than said :)
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)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
86
        self.assertEqual('secret', credentials.get('password', None))