~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/netrc_credential_store/tests/test_netrc.py

  • Committer: Vincent Ladeuil
  • Date: 2008-10-06 10:06:53 UTC
  • mto: (3926.1.1 bzr.integration)
  • mto: This revision was merged to the branch mainline in revision 3928.
  • Revision ID: v.ladeuil+lp@free.fr-20081006100653-m74ulbg7rlfcuqox
Add a credential store for '.netrc'.

* plugins/netrc_credential_store/tests/test_netrc.py: 
Associated tests.

* plugins/netrc_credential_store/tests/__init__.py: 
Associated tests.

* plugins/netrc_credential_store/__init__.py: 
Provides a credential store for .netrc. This also documents how to
plug a credential store for authentication.conf.

* tests/__init__.py:
(TestCaseInTempDir.build_tree): Drive-by fix, use assertIsInstance
instead of assert_ to get a meaningful error message.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from bzrlib import (
 
18
    config,
 
19
    errors,
 
20
    osutils,
 
21
    tests,
 
22
    )
 
23
 
 
24
from bzrlib.plugins import netrc_credential_store
 
25
 
 
26
 
 
27
class TestNetrcCSNoNetrc(tests.TestCaseInTempDir):
 
28
 
 
29
    def test_home_netrc_does_not_exist(self):
 
30
        self.assertRaises(errors.NoSuchFile,
 
31
                          config.credential_store_registry.get_credential_store,
 
32
                          'netrc')
 
33
 
 
34
 
 
35
class TestNetrcCS(tests.TestCaseInTempDir):
 
36
 
 
37
    def setUp(self):
 
38
        super(TestNetrcCS, self).setUp()
 
39
        # Create a .netrc file
 
40
        netrc_content = """
 
41
machine host login joe password secret
 
42
default login anonymous password joe@home
 
43
"""
 
44
        f = open(osutils.pathjoin(self.test_home_dir, '.netrc'), 'wb')
 
45
        try:
 
46
            f.write(netrc_content)
 
47
        finally:
 
48
            f.close()
 
49
 
 
50
    def _get_netrc_cs(self):
 
51
        return  config.credential_store_registry.get_credential_store('netrc')
 
52
 
 
53
    def test_not_matching_user(self):
 
54
        cs = self._get_netrc_cs()
 
55
        password = cs.decode_password(dict(host='host', user='jim'))
 
56
        self.assertIs(None, password)
 
57
 
 
58
    def test_matching_user(self):
 
59
        cs = self._get_netrc_cs()
 
60
        password = cs.decode_password(dict(host='host', user='joe'))
 
61
        self.assertEquals('secret', password)
 
62
 
 
63
    def test_default_password(self):
 
64
        cs = self._get_netrc_cs()
 
65
        password = cs.decode_password(dict(host='other', user='anonymous'))
 
66
        self.assertEquals('joe@home', password)
 
67
 
 
68
    def test_default_password_without_user(self):
 
69
        cs = self._get_netrc_cs()
 
70
        password = cs.decode_password(dict(host='other'))
 
71
        self.assertIs(None, password)