1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for L{twisted.tap.ftp}.
8 from twisted.trial.unittest import TestCase
10 from twisted.cred import credentials, error
11 from twisted.tap.ftp import Options
12 from twisted.python import versions
13 from twisted.python.filepath import FilePath
17 class FTPOptionsTestCase(TestCase):
19 Tests for the command line option parser used for C{twistd ftp}.
22 usernamePassword = ('iamuser', 'thisispassword')
26 Create a file with two users.
28 self.filename = self.mktemp()
29 f = FilePath(self.filename)
30 f.setContent(':'.join(self.usernamePassword))
31 self.options = Options()
34 def test_passwordfileDeprecation(self):
36 The C{--password-file} option will emit a warning stating that
37 said option is deprecated.
40 versions.Version("Twisted", 11, 1, 0),
41 self.options.opt_password_file, self.filename)
44 def test_authAdded(self):
46 The C{--auth} command-line option will add a checker to the list of
49 numCheckers = len(self.options['credCheckers'])
50 self.options.parseOptions(['--auth', 'file:' + self.filename])
51 self.assertEqual(len(self.options['credCheckers']), numCheckers + 1)
54 def test_authFailure(self):
56 The checker created by the C{--auth} command-line option returns a
57 L{Deferred} that fails with L{UnauthorizedLogin} when
58 presented with credentials that are unknown to that checker.
60 self.options.parseOptions(['--auth', 'file:' + self.filename])
61 checker = self.options['credCheckers'][-1]
62 invalid = credentials.UsernamePassword(self.usernamePassword[0], 'fake')
63 return (checker.requestAvatarId(invalid)
65 lambda ignore: self.fail("Wrong password should raise error"),
66 lambda err: err.trap(error.UnauthorizedLogin)))
69 def test_authSuccess(self):
71 The checker created by the C{--auth} command-line option returns a
72 L{Deferred} that returns the avatar id when presented with credentials
73 that are known to that checker.
75 self.options.parseOptions(['--auth', 'file:' + self.filename])
76 checker = self.options['credCheckers'][-1]
77 correct = credentials.UsernamePassword(*self.usernamePassword)
78 return checker.requestAvatarId(correct).addCallback(
79 lambda username: self.assertEqual(username, correct.username)