test_ftp_options.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.tap.ftp}.
  5. """
  6. from twisted.trial.unittest import TestCase
  7. from twisted.cred import credentials, error
  8. from twisted.tap.ftp import Options
  9. from twisted.python import versions
  10. from twisted.python.filepath import FilePath
  11. class FTPOptionsTests(TestCase):
  12. """
  13. Tests for the command line option parser used for C{twistd ftp}.
  14. """
  15. usernamePassword = (b'iamuser', b'thisispassword')
  16. def setUp(self):
  17. """
  18. Create a file with two users.
  19. """
  20. self.filename = self.mktemp()
  21. f = FilePath(self.filename)
  22. f.setContent(b':'.join(self.usernamePassword))
  23. self.options = Options()
  24. def test_passwordfileDeprecation(self):
  25. """
  26. The C{--password-file} option will emit a warning stating that
  27. said option is deprecated.
  28. """
  29. self.callDeprecated(
  30. versions.Version("Twisted", 11, 1, 0),
  31. self.options.opt_password_file, self.filename)
  32. def test_authAdded(self):
  33. """
  34. The C{--auth} command-line option will add a checker to the list of
  35. checkers
  36. """
  37. numCheckers = len(self.options['credCheckers'])
  38. self.options.parseOptions(['--auth', 'file:' + self.filename])
  39. self.assertEqual(len(self.options['credCheckers']), numCheckers + 1)
  40. def test_authFailure(self):
  41. """
  42. The checker created by the C{--auth} command-line option returns a
  43. L{Deferred} that fails with L{UnauthorizedLogin} when
  44. presented with credentials that are unknown to that checker.
  45. """
  46. self.options.parseOptions(['--auth', 'file:' + self.filename])
  47. checker = self.options['credCheckers'][-1]
  48. invalid = credentials.UsernamePassword(self.usernamePassword[0], 'fake')
  49. return (checker.requestAvatarId(invalid)
  50. .addCallbacks(
  51. lambda ignore: self.fail("Wrong password should raise error"),
  52. lambda err: err.trap(error.UnauthorizedLogin)))
  53. def test_authSuccess(self):
  54. """
  55. The checker created by the C{--auth} command-line option returns a
  56. L{Deferred} that returns the avatar id when presented with credentials
  57. that are known to that checker.
  58. """
  59. self.options.parseOptions(['--auth', 'file:' + self.filename])
  60. checker = self.options['credCheckers'][-1]
  61. correct = credentials.UsernamePassword(*self.usernamePassword)
  62. return checker.requestAvatarId(correct).addCallback(
  63. lambda username: self.assertEqual(username, correct.username)
  64. )