-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathtest_tasks_api.py
More file actions
332 lines (295 loc) · 14 KB
/
test_tasks_api.py
File metadata and controls
332 lines (295 loc) · 14 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# coding: utf-8
from __future__ import absolute_import
import os
import unittest
import asana
from asana.api.tasks_api import TasksApi # noqa: E501
from asana.rest import ApiException
from dotenv import load_dotenv
class TestTasksApi(unittest.TestCase):
"""TasksApi unit test stubs"""
@classmethod
def setUpClass(cls):
load_dotenv()
# Load environment variables
cls.TEXT_CUSTOM_FIELD_GID = os.environ["TEXT_CUSTOM_FIELD_GID"]
cls.USER_GID = os.environ["USER_GID"]
cls.WORKSPACE_GID = os.environ["WORKSPACE_GID"]
configuration = asana.Configuration()
configuration.access_token = os.environ["PERSONAL_ACCESS_TOKEN"]
api_client = asana.ApiClient(configuration)
cls.api = TasksApi(api_client) # noqa: E501
# Create a global task used for testing
try:
cls.task_name = "Task 1"
cls.task_notes = "Some description"
cls.task = cls.api.create_task(
{
"data": {
"assignee": cls.USER_GID,
"name": cls.task_name,
"notes": cls.task_notes,
"workspace": cls.WORKSPACE_GID,
}
},
{}
)
except ApiException as e:
raise Exception(e)
@classmethod
def tearDownClass(cls):
try:
# Delete global task
cls.api.delete_task(cls.task["gid"])
except ApiException as e:
raise Exception(e)
def test_create_task(self):
"""Test case for create_task
Create a task # noqa: E501
"""
try:
# Create a task
task_name = "Task 2"
task_notes = "Some description"
task = self.api.create_task(
{
"data": {
"name": task_name,
"notes": task_notes,
"workspace": self.WORKSPACE_GID,
}
},
{}
)
# Check that the task was created
self.assertIsNotNone(task)
self.assertEqual(task["name"], task_name)
self.assertEqual(task["notes"], task_notes)
# Delete task
self.api.delete_task(task["gid"])
except ApiException as e:
raise Exception(e)
def test_delete_task(self):
"""Test case for delete_task
Delete a task # noqa: E501
"""
try:
# Create a task to delete
task_name = "Task 2"
task_notes = "Some description"
task = self.api.create_task(
{
"data": {
"name": task_name,
"notes": task_notes,
"workspace": self.WORKSPACE_GID,
}
},
{}
)
# Delete task
deleted_task = self.api.delete_task(task["gid"])
# Check that the task got deleted
self.assertEqual(deleted_task, {})
except ApiException as e:
raise Exception(e)
def test_get_task(self):
"""Test case for get_task
Get a task # noqa: E501
"""
try:
# Get a task
task = self.api.get_task(self.task["gid"], {})
# Check task response
self.assertIsNotNone(task)
self.assertEqual(task["name"], self.task_name)
self.assertEqual(task["notes"], self.task_notes)
self.assertEqual(task["resource_subtype"], "default_task")
self.assertIsNone(task["actual_time_minutes"])
self.assertEqual(task["assignee"]["gid"], self.USER_GID)
self.assertIsNotNone(task["assignee_status"])
self.assertEqual(task["completed"], False)
self.assertIsNone(task["completed_at"])
self.assertTrue(task["custom_fields"].__class__ == list)
self.assertIsNone(task["due_at"])
self.assertIsNone(task["due_on"])
self.assertTrue(task["followers"].__class__ == list)
self.assertFalse(task["hearted"])
self.assertTrue(task["hearts"].__class__ == list)
self.assertFalse(task["liked"])
self.assertTrue(task["likes"].__class__ == list)
self.assertTrue(task["memberships"].__class__ == list)
self.assertIsNotNone(task["modified_at"])
self.assertEqual(task["num_hearts"], 0)
self.assertEqual(task["num_likes"], 0)
self.assertIsNone(task["parent"])
self.assertIsNotNone(task["permalink_url"])
self.assertTrue(task["projects"].__class__ == list)
self.assertEqual(task["resource_type"], "task")
self.assertIsNone(task["start_at"])
self.assertIsNone(task["start_on"])
self.assertTrue(task["tags"].__class__ == list)
self.assertTrue(task["workspace"].__class__ == dict)
except ApiException as e:
raise Exception(e)
def test_get_task_with_opt_fields(self):
"""Test case for get_task with opt_fields
Get a task with opt_fields # noqa: E501
"""
try:
# Get a task
task = self.api.get_task(self.task["gid"], {'opt_fields': "name,notes"})
# Check task response
self.assertIsNotNone(task)
self.assertEqual(task["name"], self.task_name)
self.assertEqual(task["notes"], self.task_notes)
except ApiException as e:
raise Exception(e)
def test_get_tasks(self):
"""Test case for get_tasks
Get multiple tasks # noqa: E501
"""
try:
# Get multiple tasks
tasks = self.api.get_tasks({
'assignee': self.USER_GID,
'workspace': self.WORKSPACE_GID,
})
for task in tasks:
self.assertIsNotNone(task["gid"])
self.assertIsNotNone(task["name"])
self.assertIsNotNone(task["resource_type"])
self.assertIsNotNone(task["resource_subtype"])
except ApiException as e:
raise Exception(e)
def test_get_tasks_with_opt_fields(self):
"""Test case for get_tasks with opt_fields
Get multiple tasks with opt_fields # noqa: E501
"""
try:
# Get multiple tasks
tasks = self.api.get_tasks({
'limit': 100,
'assignee': self.USER_GID,
'workspace': self.WORKSPACE_GID,
'opt_fields': "actual_time_minutes,approval_status,assignee,assignee.name,assignee_section,assignee_section.name,assignee_status,completed,completed_at,completed_by,completed_by.name,created_at,created_by,custom_fields,custom_fields.asana_created_field,custom_fields.created_by,custom_fields.created_by.name,custom_fields.currency_code,custom_fields.custom_label,custom_fields.custom_label_position,custom_fields.date_value,custom_fields.date_value.date,custom_fields.date_value.date_time,custom_fields.description,custom_fields.display_value,custom_fields.enabled,custom_fields.enum_options,custom_fields.enum_options.color,custom_fields.enum_options.enabled,custom_fields.enum_options.name,custom_fields.enum_value,custom_fields.enum_value.color,custom_fields.enum_value.enabled,custom_fields.enum_value.name,custom_fields.format,custom_fields.has_notifications_enabled,custom_fields.is_formula_field,custom_fields.is_global_to_workspace,custom_fields.is_value_read_only,custom_fields.multi_enum_values,custom_fields.multi_enum_values.color,custom_fields.multi_enum_values.enabled,custom_fields.multi_enum_values.name,custom_fields.name,custom_fields.number_value,custom_fields.people_value,custom_fields.people_value.name,custom_fields.precision,custom_fields.resource_subtype,custom_fields.text_value,custom_fields.type,dependencies,dependents,due_at,due_on,external,external.data,followers,followers.name,hearted,hearts,hearts.user,hearts.user.name,html_notes,is_rendered_as_separator,liked,likes,likes.user,likes.user.name,memberships,memberships.project,memberships.project.name,memberships.section,memberships.section.name,modified_at,name,notes,num_hearts,num_likes,num_subtasks,offset,parent,parent.created_by,parent.name,parent.resource_subtype,path,permalink_url,projects,projects.name,resource_subtype,start_at,start_on,tags,tags.name,uri,workspace,workspace.name,resource_type",
})
for task in tasks:
self.assertIsNotNone(task)
if self.task_name in task["name"]:
self.assertEqual(task["name"], self.task_name)
self.assertEqual(task["notes"], self.task_notes)
self.assertEqual(task["resource_subtype"], "default_task")
self.assertIsNone(task["actual_time_minutes"])
self.assertEqual(task["assignee"]["gid"], self.USER_GID)
self.assertIsNotNone(task["assignee_status"])
self.assertEqual(task["completed"], False)
self.assertIsNone(task["completed_at"])
self.assertTrue(task["custom_fields"].__class__ == list)
self.assertIsNone(task["due_at"])
self.assertIsNone(task["due_on"])
self.assertTrue(task["followers"].__class__ == list)
self.assertFalse(task["hearted"])
self.assertTrue(task["hearts"].__class__ == list)
self.assertFalse(task["liked"])
self.assertTrue(task["likes"].__class__ == list)
self.assertTrue(task["memberships"].__class__ == list)
self.assertIsNotNone(task["modified_at"])
self.assertEqual(task["num_hearts"], 0)
self.assertEqual(task["num_likes"], 0)
self.assertIsNone(task["parent"])
self.assertIsNotNone(task["permalink_url"])
self.assertTrue(task["projects"].__class__ == list)
self.assertEqual(task["resource_type"], "task")
self.assertIsNone(task["start_at"])
self.assertIsNone(task["start_on"])
self.assertTrue(task["tags"].__class__ == list)
self.assertTrue(task["workspace"].__class__ == dict)
except ApiException as e:
raise Exception(e)
def test_search_tasks_for_workspace(self):
"""Test case for search_tasks_for_workspace
Search tasks in a workspace # noqa: E501
"""
try:
search_text = "task" # Make sure this is lower case
opts = {
'text': search_text,
'completed': False,
'opt_fields': "name, notes"
}
tasks = self.api.search_tasks_for_workspace(self.WORKSPACE_GID, opts)
# Check that there are tasks returned
count = 0
for task in tasks:
count += 1
# Check that the results contain the search text
self.assertTrue(search_text in task["name"].lower() or search_text in task["notes"].lower())
# Check that there are results
self.assertIsNotNone(tasks)
# We know that there is at least one result for this search.
# Assert greater than or equal to because other tests or modifications might increase this search result.
self.assertGreaterEqual(count, 1)
# Delete task
self.api.delete_task(task["gid"])
except ApiException as e:
raise Exception(e)
def test_search_tasks_for_workspace_with_custom_field_parameter(self):
"""Test case for search_tasks_for_workspace with custom field parameter
Search tasks in a workspace with custom field parameter # noqa: E501
"""
try:
search_text = "custom_value" # Make sure this is lower case
opts = {
f'custom_fields.{self.TEXT_CUSTOM_FIELD_GID}.value': search_text,
'opt_fields': "custom_fields"
}
tasks = self.api.search_tasks_for_workspace(self.WORKSPACE_GID, opts)
# Check that the search result only returns tasks with the custom field value of search_text
for task in tasks:
for custom_field in task["custom_fields"]:
if custom_field["gid"] == self.TEXT_CUSTOM_FIELD_GID:
self.assertEqual(custom_field["text_value"].lower(), search_text)
except ApiException as e:
raise Exception(e)
def test_update_task(self):
"""Test case for update_task
Update a task # noqa: E501
"""
try:
# Create a task to update
task_name = "Task 2"
task_notes = "Some description"
task = self.api.create_task(
{
"data": {
"name": task_name,
"notes": task_notes,
"workspace": self.WORKSPACE_GID,
}
},
{}
)
# Update task
updated_task_name = "Task 2 - Updated"
updated_task_description = "Some description updated"
task = self.api.update_task(
{
"data": {
"name": updated_task_name,
"notes": updated_task_description,
}
},
task["gid"],
{}
)
# Check that the task has been updated
self.assertIsNotNone(task)
self.assertEqual(task["name"], updated_task_name)
self.assertEqual(task["notes"], updated_task_description)
# Delete task
self.api.delete_task(task["gid"])
except ApiException as e:
raise Exception(e)
if __name__ == '__main__':
unittest.main()