test_01_basic.py 6.9 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 random
  17. import unittest
  18. import logging
  19. import os, sys
  20. from test.OLItest import OLITestLib
  21. # Things need to be setup first, usually setup.py initializes everything.
  22. # but if e.g. called from command line, we take care of default values here:
  23. if not OLITestLib.cred_file:
  24. OLITestLib(cred_file='./test/credentials.conf', cmd='./offlineimap.py')
  25. def setUpModule():
  26. logging.info("Set Up test module %s" % __name__)
  27. tdir = OLITestLib.create_test_dir(suffix=__name__)
  28. def tearDownModule():
  29. logging.info("Tear Down test module")
  30. OLITestLib.delete_test_dir()
  31. #Stuff that can be used
  32. #self.assertEqual(self.seq, range(10))
  33. # should raise an exception for an immutable sequence
  34. #self.assertRaises(TypeError, random.shuffle, (1,2,3))
  35. #self.assertTrue(element in self.seq)
  36. #self.assertFalse(element in self.seq)
  37. class TestBasicFunctions(unittest.TestCase):
  38. def setUp(self):
  39. OLITestLib.delete_remote_testfolders()
  40. def tearDown(self):
  41. OLITestLib.delete_remote_testfolders()
  42. def test_01_olistartup(self):
  43. """Tests if OLI can be invoked without exceptions
  44. Cleans existing remote test folders. Then syncs all "OLItest*
  45. (specified in the default config) to our local Maildir. The
  46. result should be 0 folders and 0 mails."""
  47. code, res = OLITestLib.run_OLI()
  48. self.assertEqual(res, "")
  49. boxes, mails = OLITestLib.count_maildir_mails('')
  50. self.assertTrue((boxes, mails)==(0,0), msg="Expected 0 folders and 0 "
  51. "mails, but sync led to {0} folders and {1} mails".format(
  52. boxes, mails))
  53. def test_02_createdir(self):
  54. """Create local 'OLItest 1', sync"""
  55. OLITestLib.delete_maildir('') #Delete all local maildir folders
  56. OLITestLib.create_maildir('INBOX.OLItest 1')
  57. code, res = OLITestLib.run_OLI()
  58. self.assertEqual(res, "")
  59. boxes, mails = OLITestLib.count_maildir_mails('')
  60. self.assertTrue((boxes, mails)==(1,0), msg="Expected 1 folders and 0 "
  61. "mails, but sync led to {0} folders and {1} mails".format(
  62. boxes, mails))
  63. def test_03_createdir_quote(self):
  64. """Create local 'OLItest "1"' maildir, sync
  65. Folder names with quotes used to fail and have been fixed, so
  66. one is included here as a small challenge."""
  67. OLITestLib.delete_maildir('') #Delete all local maildir folders
  68. OLITestLib.create_maildir('INBOX.OLItest "1"')
  69. code, res = OLITestLib.run_OLI()
  70. if 'unallowed folder' in res:
  71. raise unittest.SkipTest("remote server doesn't handle quote")
  72. self.assertEqual(res, "")
  73. boxes, mails = OLITestLib.count_maildir_mails('')
  74. self.assertTrue((boxes, mails)==(1,0), msg="Expected 1 folders and 0 "
  75. "mails, but sync led to {0} folders and {1} mails".format(
  76. boxes, mails))
  77. def test_04_nametransmismatch(self):
  78. """Create mismatching remote and local nametrans rules
  79. This should raise an error."""
  80. config = OLITestLib.get_default_config()
  81. config.set('Repository IMAP', 'nametrans',
  82. 'lambda f: f' )
  83. config.set('Repository Maildir', 'nametrans',
  84. 'lambda f: f + "moo"' )
  85. OLITestLib.write_config_file(config)
  86. code, res = OLITestLib.run_OLI()
  87. #logging.warn("%s %s "% (code, res))
  88. # We expect an INFINITE FOLDER CREATION WARNING HERE....
  89. mismatch = "ERROR: INFINITE FOLDER CREATION DETECTED!" in res
  90. self.assertEqual(mismatch, True, msg="Mismatching nametrans rules did "
  91. "NOT trigger an 'infinite folder generation' error. Output was:\n"
  92. "{0}".format(res))
  93. # Write out default config file again
  94. OLITestLib.write_config_file()
  95. def test_05_createmail(self):
  96. """Create mail in OLItest 1, sync, wipe folder sync
  97. Currently, this will mean the folder will be recreated
  98. locally. At some point when remote folder deletion is
  99. implemented, this behavior will change."""
  100. OLITestLib.delete_remote_testfolders()
  101. OLITestLib.delete_maildir('') #Delete all local maildir folders
  102. OLITestLib.create_maildir('INBOX.OLItest')
  103. OLITestLib.create_mail('INBOX.OLItest')
  104. code, res = OLITestLib.run_OLI()
  105. #logging.warn("%s %s "% (code, res))
  106. self.assertEqual(res, "")
  107. boxes, mails = OLITestLib.count_maildir_mails('')
  108. self.assertTrue((boxes, mails)==(1,1), msg="Expected 1 folders and 1 "
  109. "mails, but sync led to {0} folders and {1} mails".format(
  110. 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, msg = "All mails should have been "+ \
  114. "assigned the IMAP's UID number, but {0} messages had no valid ID "\
  115. .format(len([None for x in uids if x==None])))
  116. def test_06_createfolders(self):
  117. """Test if createfolders works as expected
  118. Create a local Maildir, then sync with remote "createfolders"
  119. disabled. Delete local Maildir and sync. We should have no new
  120. local maildir then. TODO: Rewrite this test to directly test
  121. and count the remote folders when the helper functions have
  122. been written"""
  123. config = OLITestLib.get_default_config()
  124. config.set('Repository IMAP', 'createfolders',
  125. 'False' )
  126. OLITestLib.write_config_file(config)
  127. # delete all remote and local testfolders
  128. OLITestLib.delete_remote_testfolders()
  129. OLITestLib.delete_maildir('')
  130. OLITestLib.create_maildir('INBOX.OLItest')
  131. code, res = OLITestLib.run_OLI()
  132. #logging.warn("%s %s "% (code, res))
  133. self.assertEqual(res, "")
  134. OLITestLib.delete_maildir('INBOX.OLItest')
  135. code, res = OLITestLib.run_OLI()
  136. self.assertEqual(res, "")
  137. boxes, mails = OLITestLib.count_maildir_mails('')
  138. self.assertTrue((boxes, mails)==(0,0), msg="Expected 0 folders and 0 "
  139. "mails, but sync led to {} folders and {} mails".format(
  140. boxes, mails))