Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / test / test_ftp_options.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.tap.ftp}.
6 """
7
8 from twisted.trial.unittest import TestCase
9
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
14
15
16
17 class FTPOptionsTestCase(TestCase):
18     """
19     Tests for the command line option parser used for C{twistd ftp}.
20     """
21
22     usernamePassword = ('iamuser', 'thisispassword')
23
24     def setUp(self):
25         """
26         Create a file with two users.
27         """
28         self.filename = self.mktemp()
29         f = FilePath(self.filename)
30         f.setContent(':'.join(self.usernamePassword))
31         self.options = Options()
32
33
34     def test_passwordfileDeprecation(self):
35         """
36         The C{--password-file} option will emit a warning stating that
37         said option is deprecated.
38         """
39         self.callDeprecated(
40             versions.Version("Twisted", 11, 1, 0),
41             self.options.opt_password_file, self.filename)
42
43
44     def test_authAdded(self):
45         """
46         The C{--auth} command-line option will add a checker to the list of
47         checkers
48         """
49         numCheckers = len(self.options['credCheckers'])
50         self.options.parseOptions(['--auth', 'file:' + self.filename])
51         self.assertEqual(len(self.options['credCheckers']), numCheckers + 1)
52
53
54     def test_authFailure(self):
55         """
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.
59         """
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)
64             .addCallbacks(
65                 lambda ignore: self.fail("Wrong password should raise error"),
66                 lambda err: err.trap(error.UnauthorizedLogin)))
67
68
69     def test_authSuccess(self):
70         """
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.
74         """
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)
80         )