2017-07-19 08:05:03 +00:00
|
|
|
import unittest
|
|
|
|
import icalevents.icaldownload
|
2021-01-16 17:48:11 +00:00
|
|
|
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")
|
2021-01-16 17:48:11 +00:00
|
|
|
|
|
|
|
def test_read_only_directory(self):
|
2021-01-16 18:26:01 +00:00
|
|
|
# Switch to new directory so we can perform os.chmod()
|
|
|
|
os.mkdir("tmp")
|
|
|
|
os.chdir("tmp")
|
|
|
|
|
2021-01-16 17:48:11 +00:00
|
|
|
# Save current directory permissions
|
2021-01-16 18:44:34 +00:00
|
|
|
oldPerms = os.stat(os.getcwd()).st_mode
|
2021-01-16 17:48:11 +00:00
|
|
|
# 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)
|
2021-01-16 18:26:01 +00:00
|
|
|
finally:
|
|
|
|
# Change directory back to old permissions
|
|
|
|
print(oldPerms)
|
2021-01-16 18:44:34 +00:00
|
|
|
os.chmod(os.getcwd(), oldPerms)
|
2021-01-16 17:48:11 +00:00
|
|
|
|
2021-01-16 18:26:01 +00:00
|
|
|
print(os.stat(os.getcwd()))
|
|
|
|
|
|
|
|
# Delete tmp dir
|
|
|
|
os.chdir("..")
|
|
|
|
os.remove("tmp")
|
|
|
|
|