1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
4 from twisted.cred import credentials, error
5 from twisted.words import tap
6 from twisted.trial import unittest
10 class WordsTap(unittest.TestCase):
12 Ensures that the twisted.words.tap API works.
15 PASSWD_TEXT = "admin:admin\njoe:foo\n"
16 admin = credentials.UsernamePassword('admin', 'admin')
17 joeWrong = credentials.UsernamePassword('joe', 'bar')
22 Create a file with two users.
24 self.filename = self.mktemp()
25 self.file = open(self.filename, 'w')
26 self.file.write(self.PASSWD_TEXT)
32 Close the dummy user database.
37 def test_hostname(self):
39 Tests that the --hostname parameter gets passed to Options.
42 opt.parseOptions(['--hostname', 'myhost'])
43 self.assertEqual(opt['hostname'], 'myhost')
46 def test_passwd(self):
48 Tests the --passwd command for backwards-compatibility.
51 opt.parseOptions(['--passwd', self.file.name])
57 Tests that the --auth command generates a checker.
60 opt.parseOptions(['--auth', 'file:'+self.file.name])
64 def _loginTest(self, opt):
66 This method executes both positive and negative authentication
67 tests against whatever credentials checker has been stored in
70 @param opt: An instance of L{tap.Options}.
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)