-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
118 lines (99 loc) · 4.1 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import datetime
import pickle
from unittest.mock import MagicMock, Mock, mock_open, patch
import colorama
import google.auth
from google.oauth2.credentials import Credentials
import rooms
colorama.init()
def test_authenticate():
# Create a mock credentials object
mock_credentials = Mock(spec=Credentials)
mock_credentials.valid = True
# Patch google.auth to return the mock credentials object
with patch.object(google.auth, "default", return_value=(mock_credentials, None)):
# Patch InstalledAppFlow to return the mock credentials object
mock_flow = Mock()
mock_flow.run_local_server.return_value = mock_credentials
with patch(
"rooms.InstalledAppFlow.from_client_secrets_file",
return_value=mock_flow,
):
# Patch builtins to mock open() and pickle.load()
with patch("builtins.open", mock_open()):
with patch.object(pickle, "load", return_value=mock_credentials):
creds = rooms.authenticate()
assert isinstance(creds, Credentials)
assert creds.valid
def test_get_calendar_list():
# Create a mock for the Google Calendar API service
service = MagicMock()
# Generate a mock calendar list response
calendar_list_response = {
"items": [
{
"id": "[email protected]",
"summary": "Calendar 1",
"timeZone": "America/New_York",
},
{
"id": "[email protected]",
"summary": "Calendar 2",
"timeZone": "Europe/London",
},
],
"nextPageToken": None,
}
# Set the mock service's calendarList().list().execute() method to return the mock response
service.calendarList().list().execute.return_value = calendar_list_response
# Call the function
calendar_list = rooms.get_calendar_list(service)
# Check that the returned list contains two entries with the correct information
assert len(calendar_list) == 2
assert calendar_list[0]["id"] == "[email protected]"
assert calendar_list[0]["summary"] == "Calendar 1"
assert calendar_list[0]["timeZone"] == "America/New_York"
assert calendar_list[1]["id"] == "[email protected]"
assert calendar_list[1]["summary"] == "Calendar 2"
assert calendar_list[1]["timeZone"] == "Europe/London"
def test_check_availability_free(capsys):
# Create a mock for the Google Calendar API service
service = MagicMock()
# Create a mock calendar entry
calendar_entry = {"id": "123", "summary": "Ostrava Office-3-Room1"}
# Create a mock freebusy response indicating the calendar is free
freebusy_response = {"calendars": {"123": {"busy": []}}}
service.freebusy().query().execute.return_value = freebusy_response
# Call the function
rooms.check_availability(service, calendar_entry)
# Check the output
assert colorama.Fore.GREEN + "FREE" in capsys.readouterr().out
def test_check_availability_busy(capsys):
# Create a mock for the Google Calendar API service
service = MagicMock()
# Create a mock calendar entry
calendar_entry = {"id": "123", "summary": "Ostrava Office-3-Room1"}
# Create a mock freebusy response indicating the calendar is busy
busy_end_time = (
datetime.datetime.utcnow() + datetime.timedelta(minutes=15)
).strftime("%H:%M")
freebusy_response = {
"calendars": {
"123": {
"busy": [
{
"start": datetime.datetime.utcnow().isoformat() + "Z",
"end": (
datetime.datetime.utcnow() + datetime.timedelta(minutes=15)
).isoformat()
+ "Z",
}
]
}
}
}
service.freebusy().query().execute.return_value = freebusy_response
# Call the function
rooms.check_availability(service, calendar_entry)
# Check the output
assert colorama.Fore.RED + "BUSY until: " + busy_end_time in capsys.readouterr().out