icalevents/test/test_icaldownload.py

91 wiersze
2.8 KiB
Python
Czysty Zwykły widok Historia

2017-07-19 08:05:03 +00:00
import unittest
import icalevents.icaldownload
import os
import logging
2017-07-19 08:05:03 +00:00
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")
2017-07-20 16:11:50 +00:00
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")
2017-07-19 08:05:03 +00:00
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):
# Save current directory permissions
2021-01-16 18:44:34 +00:00
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()
2021-01-16 18:44:34 +00:00
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)
except:
# If the test failed, we have to recover the old directory permissions and let the test fail ultimately
os.chmod(os.getcwd(), oldPerms)
self.assertTrue(False, "Test run failed. Changed directory permissions back to old permissions.")
# Change directory back to old permissions
os.chmod(os.getcwd(), oldPerms)