import unittest import icalevents.icaldownload import os import logging class ICalDownloadTests(unittest.TestCase): def test_apple_data_fix(self): data = """ DTSTART:18831118T120702 RDATE;VALUE=DATE-TIME:18831118T120702 TZNAME:PST TZOFFSETFROM:+5328 TZOFFSETTO:-0800 END:STANDARD BEGIN:DAYLIGHT DTSTART:19180331T020000 """ expected = """ DTSTART:18831118T120702 RDATE;VALUE=DATE-TIME:18831118T120702 TZNAME:PST TZOFFSETFROM:+0053 TZOFFSETTO:-0800 END:STANDARD BEGIN:DAYLIGHT DTSTART:19180331T020000 """ res = icalevents.icaldownload.apple_data_fix(data) self.assertEqual(res, expected, "fix invalid TZOFFSETFROM") def test_apple_url_fix(self): data = "webcal://blah.blub/webcal/" expected = "http://blah.blub/webcal/" res = icalevents.icaldownload.apple_url_fix(data) self.assertEqual(res, expected, "fix url protocol") def test_apple_url_fix_right(self): data = "https://blah.blub/webcal/" res = icalevents.icaldownload.apple_url_fix(data) self.assertEqual(res, data, "no change") def test_data_from_file_google(self): file = "test/test_data/basic.ics" result = "test/test_data/basic_content.txt" expected = None with open(result, mode='r', encoding='utf-8') as f: expected = f.read() content = icalevents.icaldownload.ICalDownload().data_from_file(file) self.assertEqual(expected, content, "content form iCal file, google format") def test_data_from_file_apple(self): file = "test/test_data/icloud.ics" result = "test/test_data/icloud_content.txt" expected = None with open(result, mode='r', encoding='utf-8') as f: expected = f.read() content = icalevents.icaldownload.ICalDownload().data_from_file(file, apple_fix=True) self.assertEqual(expected, content, "content form iCal file, google format") def test_read_only_directory(self): # Switch to new directory so we can perform os.chmod() os.mkdir("tmp") os.chdir("tmp") # Save current directory permissions oldPerms = os.stat(os.getcwd()).st_mode # Set working directory as read-only os.chmod(os.getcwd(), 0o500) # Assert log message is being thrown logger = logging.getLogger() try: with self.assertLogs(level="WARNING") as cm: # Create new ICalDownload instance which will try to create the .cache directory ical_download = icalevents.icaldownload.ICalDownload(http=None) finally: # Change directory back to old permissions print(oldPerms) os.chmod(os.getcwd(), oldPerms) print(os.stat(os.getcwd())) # Delete tmp dir os.chdir("..") os.remove("tmp")