Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / words / test / test_tap.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 from twisted.cred import credentials, error
5 from twisted.words import tap
6 from twisted.trial import unittest
7
8
9
10 class WordsTap(unittest.TestCase):
11     """
12     Ensures that the twisted.words.tap API works.
13     """
14
15     PASSWD_TEXT = "admin:admin\njoe:foo\n"
16     admin = credentials.UsernamePassword('admin', 'admin')
17     joeWrong = credentials.UsernamePassword('joe', 'bar')
18
19
20     def setUp(self):
21         """
22         Create a file with two users.
23         """
24         self.filename = self.mktemp()
25         self.file = open(self.filename, 'w')
26         self.file.write(self.PASSWD_TEXT)
27         self.file.flush()
28
29
30     def tearDown(self):
31         """
32         Close the dummy user database.
33         """
34         self.file.close()
35
36
37     def test_hostname(self):
38         """
39         Tests that the --hostname parameter gets passed to Options.
40         """
41         opt = tap.Options()
42         opt.parseOptions(['--hostname', 'myhost'])
43         self.assertEqual(opt['hostname'], 'myhost')
44
45
46     def test_passwd(self):
47         """
48         Tests the --passwd command for backwards-compatibility.
49         """
50         opt = tap.Options()
51         opt.parseOptions(['--passwd', self.file.name])
52         self._loginTest(opt)
53
54
55     def test_auth(self):
56         """
57         Tests that the --auth command generates a checker.
58         """
59         opt = tap.Options()
60         opt.parseOptions(['--auth', 'file:'+self.file.name])
61         self._loginTest(opt)
62
63
64     def _loginTest(self, opt):
65         """
66         This method executes both positive and negative authentication
67         tests against whatever credentials checker has been stored in
68         the Options class.
69
70         @param opt: An instance of L{tap.Options}.
71         """
72         self.assertEqual(len(opt['credCheckers']), 1)
73         checker = opt['credCheckers'][0]
74         self.assertFailure(checker.requestAvatarId(self.joeWrong),
75                            error.UnauthorizedLogin)
76         def _gotAvatar(username):
77             self.assertEqual(username, self.admin.username)
78         return checker.requestAvatarId(self.admin).addCallback(_gotAvatar)