test_01_basic.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # Copyright (C) 2012- Sebastian Spaeth & contributors
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. import unittest
  17. import logging
  18. from test.OLItest import OLITestLib
  19. # Things need to be setup first, usually setup.py initializes everything.
  20. # but if e.g. called from command line, we take care of default values here:
  21. if not OLITestLib.cred_file:
  22. OLITestLib(cred_file='./test/credentials.conf', cmd='./offlineimap.py')
  23. def setUpModule():
  24. logging.info("Set Up test module %s" % __name__)
  25. OLITestLib.create_test_dir(suffix=__name__)
  26. def tearDownModule():
  27. logging.info("Tear Down test module")
  28. OLITestLib.delete_test_dir()
  29. # Stuff that can be used
  30. # self.assertEqual(self.seq, range(10))
  31. # should raise an exception for an immutable sequence
  32. # self.assertRaises(TypeError, random.shuffle, (1,2,3))
  33. # self.assertTrue(element in self.seq)
  34. # self.assertFalse(element in self.seq)
  35. class TestBasicFunctions(unittest.TestCase):
  36. def setUp(self):
  37. OLITestLib.delete_remote_testfolders()
  38. def tearDown(self):
  39. OLITestLib.delete_remote_testfolders()
  40. def test_01_olistartup(self):
  41. """Tests if OLI can be invoked without exceptions
  42. Cleans existing remote test folders. Then syncs all "OLItest*
  43. (specified in the default config) to our local Maildir. The
  44. result should be 0 folders and 0 mails."""
  45. OLITestLib.run_OLI()
  46. boxes, mails = OLITestLib.count_maildir_mails('')
  47. self.assertTrue((boxes, mails) == (0, 0),
  48. msg="Expected 0 folders and 0 "
  49. "mails, but sync led to {0} folders and {1} mails"
  50. .format(boxes, mails))
  51. def test_02_createdir(self):
  52. """Create local 'OLItest 1', sync"""
  53. OLITestLib.delete_maildir('') # Delete all local maildir folders
  54. OLITestLib.create_maildir('INBOX.OLItest 1')
  55. OLITestLib.run_OLI()
  56. boxes, mails = OLITestLib.count_maildir_mails('')
  57. self.assertTrue((boxes, mails) == (1, 0),
  58. msg="Expected 1 folders and 0 "
  59. "mails, but sync led to {0} folders and {1} mails"
  60. .format(boxes, mails))
  61. def test_03_createdir_quote(self):
  62. """Create local 'OLItest "1"' maildir, sync
  63. Folder names with quotes used to fail and have been fixed, so
  64. one is included here as a small challenge."""
  65. OLITestLib.delete_maildir('') # Delete all local maildir folders
  66. OLITestLib.create_maildir('INBOX.OLItest "1"')
  67. code, res = OLITestLib.run_OLI()
  68. if 'unallowed folder' in res:
  69. raise unittest.SkipTest("remote server doesn't handle quote")
  70. boxes, mails = OLITestLib.count_maildir_mails('')
  71. self.assertTrue((boxes, mails) == (1, 0),
  72. msg="Expected 1 folders and 0 "
  73. "mails, but sync led to {0} folders and {1} mails"
  74. .format(boxes, mails))
  75. def test_04_nametransmismatch(self):
  76. """Create mismatching remote and local nametrans rules
  77. This should raise an error."""
  78. config = OLITestLib.get_default_config()
  79. config.set('Repository IMAP', 'nametrans',
  80. 'lambda f: f')
  81. config.set('Repository Maildir', 'nametrans',
  82. 'lambda f: f + "moo"')
  83. OLITestLib.write_config_file(config)
  84. code, res = OLITestLib.run_OLI()
  85. # logging.warn("%s %s "% (code, res))
  86. # We expect an INFINITE FOLDER CREATION WARNING HERE....
  87. mismatch = "ERROR: INFINITE FOLDER CREATION DETECTED!" in res
  88. self.assertEqual(mismatch, True,
  89. msg="Mismatching nametrans rules did "
  90. "NOT trigger an 'infinite folder generation' error. Output was:\n"
  91. "{0}".format(res))
  92. # Write out default config file again
  93. OLITestLib.write_config_file()
  94. def test_05_createmail(self):
  95. """Create mail in OLItest 1, sync, wipe folder sync
  96. Currently, this will mean the folder will be recreated
  97. locally. At some point when remote folder deletion is
  98. implemented, this behavior will change."""
  99. OLITestLib.delete_remote_testfolders()
  100. OLITestLib.delete_maildir('') # Delete all local maildir folders
  101. OLITestLib.create_maildir('INBOX.OLItest')
  102. OLITestLib.create_mail('INBOX.OLItest')
  103. code, res = OLITestLib.run_OLI()
  104. # logging.warn("%s %s "% (code, res))
  105. self.assertEqual(res, "")
  106. boxes, mails = OLITestLib.count_maildir_mails('')
  107. self.assertTrue((boxes, mails) == (1, 1),
  108. msg="Expected 1 folders and 1 "
  109. "mails, but sync led to {0} folders and {1} mails"
  110. .format(boxes, mails))
  111. # The local Mail should have been assigned a proper UID now, check!
  112. uids = OLITestLib.get_maildir_uids('INBOX.OLItest')
  113. self.assertFalse(None in uids,
  114. msg="All mails should have been "
  115. "assigned the IMAP's UID number, but {0} messages had no valid ID "
  116. .format(len([None for x in uids if x is None])))
  117. def test_06_createfolders(self):
  118. """Test if createfolders works as expected
  119. Create a local Maildir, then sync with remote "createfolders"
  120. disabled. Delete local Maildir and sync. We should have no new
  121. local maildir then. TODO: Rewrite this test to directly test
  122. and count the remote folders when the helper functions have
  123. been written"""
  124. config = OLITestLib.get_default_config()
  125. config.set('Repository IMAP', 'createfolders',
  126. 'False')
  127. OLITestLib.write_config_file(config)
  128. # delete all remote and local testfolders
  129. OLITestLib.delete_remote_testfolders()
  130. OLITestLib.delete_maildir('')
  131. OLITestLib.create_maildir('INBOX.OLItest')
  132. OLITestLib.run_OLI()
  133. OLITestLib.delete_maildir('INBOX.OLItest')
  134. OLITestLib.run_OLI()
  135. boxes, mails = OLITestLib.count_maildir_mails('')
  136. self.assertTrue((boxes, mails) == (0, 0),
  137. msg="Expected 0 folders and 0 "
  138. "mails, but sync led to {} folders and {} mails"
  139. .format(boxes, mails))