Restoring user level function gcal_contact_phone() and
[libgcal.git] / utests / utest_userapi.c
blob8e3d4f5fd3cae8add3c91d112042aae67e8ff086
1 /*
2 * @file utest_userapi.h
3 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
4 * @date Started on June 24 2008
6 * @brief Implementation module for user api unit tests.
8 * This is a good place to look when learning to use libgcal. The following
9 * operations are covered:
10 * - authentication
11 * - getting all calendar events
12 * - accessing them
13 * - adding a new calendar event
14 * - editing and deleting an event
15 * - querying for updated calendar events
19 #include "utest_userapi.h"
20 #include "gcalendar.h"
21 #include "gcontact.h"
22 #include "utils.h"
23 #include <stdio.h>
24 #include <string.h>
26 /* I use this variable to exchange data between the contacts tests */
27 char *deleted_contact_id = NULL;
29 START_TEST (test_get_calendar)
31 gcal_t gcal;
32 struct gcal_event_array event_array;
33 int result;
35 gcal = gcal_new(GCALENDAR);
36 fail_if(gcal == NULL, "Failed constructing gcal object!");
38 result = gcal_get_authentication(gcal, "gcal4tester", "66libgcal");
39 fail_if(result == -1, "Cannot authenticate!");
41 result = gcal_get_events(gcal, &event_array);
42 fail_if(result == -1, "Failed downloading events!");
43 fail_if(event_array.length < 1, "gcal4tester must have at least"
44 "1 event!");
46 /* Cleanup */
47 gcal_cleanup_events(&event_array);
48 gcal_delete(gcal);
51 END_TEST
54 START_TEST (test_access_calendar)
56 gcal_t gcal;
57 struct gcal_event_array event_array;
58 gcal_event_t event;
59 size_t i;
60 int result;
61 char *ptr;
63 gcal = gcal_new(GCALENDAR);
64 result = gcal_get_authentication(gcal, "gcal4tester", "66libgcal");
65 result = gcal_get_events(gcal, &event_array);
67 /* Access events properties */
68 for (i = 0; i < event_array.length; ++i) {
70 /* Access i-nth calendar event */
71 event = gcal_event_element(&event_array, i);
73 /* Common fields between calendar and contacts are
74 * of type 'gcal_entry'
76 ptr = gcal_event_get_id(event);
77 ptr = gcal_event_get_updated(event);
78 ptr = gcal_event_get_title(event);
79 ptr = gcal_event_get_url(event);
81 fail_if(ptr == NULL, "Can't get edit url!");
83 /* This are the fields unique to calendar events */
84 ptr = gcal_event_get_content(event);
85 ptr = gcal_event_get_recurrent(event);
86 ptr = gcal_event_get_start(event);
87 ptr = gcal_event_get_end(event);
88 ptr = gcal_event_get_where(event);
89 ptr = gcal_event_get_status(event);
92 /* This code block is for testing overflow only! Please dont use
93 * gcal in this way.
95 ptr = gcal_event_get_id(gcal_event_element(&event_array,
96 event_array.length));
97 fail_if(ptr != NULL, "Getting field must fail!");
98 ptr = gcal_event_get_updated(gcal_event_element(&event_array,
99 event_array.length));
100 fail_if(ptr != NULL, "Getting field must fail!");
101 ptr = gcal_event_get_title(gcal_event_element(&event_array,
102 event_array.length));
103 fail_if(ptr != NULL, "Getting field must fail!");
104 ptr = gcal_event_get_url(gcal_event_element(&event_array,
105 event_array.length));
106 fail_if(ptr != NULL, "Getting field must fail!");
107 ptr = gcal_event_get_content(gcal_event_element(&event_array,
108 event_array.length));
109 fail_if(ptr != NULL, "Getting field must fail!");
110 ptr = gcal_event_get_recurrent(gcal_event_element(&event_array,
111 event_array.length));
112 fail_if(ptr != NULL, "Getting field must fail!");
113 ptr = gcal_event_get_start(gcal_event_element(&event_array,
114 event_array.length));
115 fail_if(ptr != NULL, "Getting field must fail!");
116 ptr = gcal_event_get_end(gcal_event_element(&event_array,
117 event_array.length));
118 fail_if(ptr != NULL, "Getting field must fail!");
119 ptr = gcal_event_get_where(gcal_event_element(&event_array,
120 event_array.length));
121 fail_if(ptr != NULL, "Getting field must fail!");
122 ptr = gcal_event_get_status(gcal_event_element(&event_array,
123 event_array.length));
124 fail_if(ptr != NULL, "Getting field must fail!");
127 /* Cleanup */
128 gcal_cleanup_events(&event_array);
129 gcal_delete(gcal);
131 END_TEST
134 START_TEST (test_oper_event_event)
136 gcal_t gcal;
137 gcal_event_t event;
138 int result;
140 /* Create a new event object */
141 event = gcal_event_new(NULL);
142 fail_if (!event, "Cannot construct event object!");
143 gcal_event_set_title(event, "A new event");
144 gcal_event_set_content(event, "Here goes the description");
145 gcal_event_set_start(event, "2008-06-24T16:00:00Z");
146 gcal_event_set_end(event, "2008-06-24T18:00:00Z");
147 gcal_event_set_where(event, "A nice place for a meeting");
149 /* Create a gcal object and authenticate */
150 gcal = gcal_new(GCALENDAR);
151 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
152 fail_if(result == -1, "Failed getting authentication");
154 /* Add a new event */
155 result = gcal_add_event(gcal, event);
156 fail_if(result == -1, "Failed adding a new event!");
159 /* Edit this event */
160 gcal_event_set_title(event, "Changing the title");
161 result = gcal_update_event(gcal, event);
162 fail_if(result == -1, "Failed editing event!");
164 /* Delete this event (note: google doesn't really deletes
165 * the event, but set its status to 'cancelled' and keeps
166 * then for nearly 4 weeks).
168 result = gcal_erase_event(gcal, event);
169 fail_if(result == -1, "Failed deleting event!");
171 /* Cleanup */
172 gcal_event_delete(event);
173 gcal_delete(gcal);
175 END_TEST
177 START_TEST (test_query_event_updated)
179 gcal_t gcal;
180 struct gcal_event_array event_array;
181 gcal_event_t event;
182 int result;
183 /* Previous test added/edited/deleted an event with this title */
184 char *title = "Changing the title";
186 gcal = gcal_new(GCALENDAR);
187 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
189 /* This will query for all updated events (fall in this category
190 * added/deleted/updated events) starting for 06:00Z UTC of today).
192 result = gcal_get_updated_events(gcal, &event_array, NULL);
193 fail_if(result == -1, "Failed downloading updated events!");
194 fail_if(event_array.length < 1, "If previous test was ok, it must"
195 " return at least one updated event!");
197 /* Google returns the last updated event first */
198 event = gcal_event_element(&event_array, 0);
199 if (gcal_event_is_deleted(event)) {
200 if (gcal_event_get_title(event))
201 result = strcmp(gcal_event_get_title(event), title);
202 } else
203 result = -1;
204 fail_if(result != 0, "Cannot locate event!");
206 /* Cleanup */
207 gcal_cleanup_events(&event_array);
208 gcal_delete(gcal);
211 END_TEST
213 START_TEST (test_get_contacts)
215 gcal_t gcal;
216 struct gcal_contact_array contact_array;
217 int result;
219 gcal = gcal_new(GCONTACT);
220 fail_if(gcal == NULL, "Failed constructing gcal object!");
222 result = gcal_get_authentication(gcal, "gcal4tester", "66libgcal");
223 fail_if(result == -1, "Cannot authenticate!");
225 result = gcal_get_contacts(gcal, &contact_array);
226 fail_if(result == -1, "Failed downloading contacts!");
227 fail_if(contact_array.length != 3, "gcal4tester must have only"
228 "3 contacts!");
230 /* Cleanup */
231 gcal_cleanup_contacts(&contact_array);
232 gcal_delete(gcal);
235 END_TEST
238 START_TEST (test_access_contacts)
240 gcal_t gcal;
241 struct gcal_contact_array contact_array;
242 gcal_contact_t contact;
243 size_t i;
244 int result;
245 char *ptr;
246 int j;
247 gcal_email_type get;
248 gcal_phone_type gpt;
250 gcal = gcal_new(GCONTACT);
251 result = gcal_get_authentication(gcal, "gcal4tester", "66libgcal");
252 result = gcal_get_contacts(gcal, &contact_array);
254 /* Access events properties */
255 for (i = 0; i < contact_array.length; ++i) {
257 /* Access i-nth calendar event */
258 contact = gcal_contact_element(&contact_array, i);
260 /* Common fields between calendar and contacts are
261 * of type 'gcal_entry'
263 ptr = gcal_contact_get_id(contact);
264 ptr = gcal_contact_get_updated(contact);
265 /* Tip: it *is* valid a contact have no name. */
266 ptr = gcal_contact_get_title(contact);
267 ptr = gcal_contact_get_url(contact);
269 fail_if(ptr == NULL, "Can't get edit url!");
271 /* This are the fields unique to calendar events */
272 j = gcal_contact_get_emails_count(contact);
273 j = gcal_contact_get_pref_email(contact);
274 ptr = gcal_contact_get_email_address(contact, 0);
275 get = gcal_contact_get_email_address_type(contact, 0);
276 ptr = gcal_contact_get_content(contact);
277 ptr = gcal_contact_get_organization(contact);
278 ptr = gcal_contact_get_profission(contact);
279 ptr = gcal_contact_get_im(contact);
280 j = gcal_contact_get_phone_numbers_count(contact);
281 ptr = gcal_contact_get_phone_number(contact, 0);
282 gpt = gcal_contact_get_phone_number_type(contact, 0);
283 ptr = gcal_contact_get_address(contact);
287 /* This code block is for testing overflow only! Please dont use
288 * gcal in this way.
290 ptr = gcal_contact_get_id(gcal_contact_element(&contact_array,
291 contact_array.length));
292 fail_if(ptr != NULL, "Getting field must fail!");
293 ptr = gcal_contact_get_updated(gcal_contact_element(&contact_array,
294 contact_array.length));
295 fail_if(ptr != NULL, "Getting field must fail!");
296 ptr = gcal_contact_get_title(gcal_contact_element(&contact_array,
297 contact_array.length));
298 fail_if(ptr != NULL, "Getting field must fail!");
299 ptr = gcal_contact_get_url(gcal_contact_element(&contact_array,
300 contact_array.length));
301 fail_if(ptr != NULL, "Getting field must fail!");
302 ptr = gcal_contact_get_email_address(gcal_contact_element(&contact_array,
303 contact_array.length), 0);
304 fail_if(ptr != NULL, "Getting field must fail!");
305 ptr = gcal_contact_get_content(gcal_contact_element(&contact_array,
306 contact_array.length));
307 fail_if(ptr != NULL, "Getting field must fail!");
308 ptr = gcal_contact_get_organization(gcal_contact_element(&contact_array,
309 contact_array.length));
310 fail_if(ptr != NULL, "Getting field must fail!");
311 ptr = gcal_contact_get_profission(gcal_contact_element(&contact_array,
312 contact_array.length));
313 fail_if(ptr != NULL, "Getting field must fail!");
314 ptr = gcal_contact_get_im(gcal_contact_element(&contact_array,
315 contact_array.length));
316 fail_if(ptr != NULL, "Getting field must fail!");
317 ptr = gcal_contact_get_phone_number(gcal_contact_element(&contact_array,
318 contact_array.length), 0);
319 fail_if(ptr != NULL, "Getting field must fail!");
320 ptr = gcal_contact_get_address(gcal_contact_element(&contact_array,
321 contact_array.length));
322 fail_if(ptr != NULL, "Getting field must fail!");
325 /* Cleanup */
326 gcal_cleanup_contacts(&contact_array);
327 gcal_delete(gcal);
330 END_TEST
332 START_TEST (test_oper_contact)
334 gcal_t gcal;
335 gcal_contact_t contact;
336 int result;
338 /* Create a new contact object */
339 contact = gcal_contact_new(NULL);
340 fail_if (!contact, "Cannot construct contact object!");
341 gcal_contact_set_title(contact, "John Doe");
342 gcal_contact_set_email(contact, "john.doe@foo.bar.com");
343 gcal_contact_add_email_address(contact, "jonny@theman.com", E_OTHER, 0);
344 gcal_contact_set_phone(contact, "111-2222-3333-888");
347 /* Create a gcal object and authenticate */
348 gcal = gcal_new(GCONTACT);
349 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
350 fail_if(result == -1, "Failed getting authentication");
352 /* Add a new contact */
353 result = gcal_add_contact(gcal, contact);
354 fail_if(result == -1, "Failed adding a new contact!");
357 /* Tests integraty */
358 result = strcmp(gcal_contact_get_phone(contact), "111-2222-3333-888");
359 fail_if(result != 0, "Failed to extract phone from gcal_contact_t!");
362 /* Edit this contact */
363 gcal_contact_set_title(contact, "John 'The Generic' Doe");
364 fail_if(result == -1, "Failed editing contact!");
365 gcal_contact_delete_email_addresses(contact);
366 gcal_contact_set_email(contact, "john.super.doe@foo.bar.com");
367 fail_if(result == -1, "Failed editing contact!");
368 result = gcal_update_contact(gcal, contact);
369 fail_if(result == -1, "Failed uploading edited contact!");
371 /* Save this contact's ID to use it in the next test, where we
372 * search for updated contacts.
374 deleted_contact_id = strdup(gcal_contact_get_id(contact));
376 /* Delete this contact (note: google still keeps a deleted contact
377 * for nearly 4 weeks. Its possible to retrieve it using
378 * 'gcal_deleted(gcal, SHOW)' before downloading contacts)
380 result = gcal_erase_contact(gcal, contact);
381 fail_if(result == -1, "Failed deleting contact!");
383 /* Cleanup */
384 gcal_contact_delete(contact);
385 gcal_delete(gcal);
387 END_TEST
389 START_TEST (test_query_contact_updated)
391 gcal_t gcal;
392 struct gcal_contact_array contact_array;
393 gcal_contact_t contact;
394 int result;
395 size_t tmp;
397 gcal = gcal_new(GCONTACT);
398 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
400 /* This will query for all updated contacts (fall in this category
401 * added/updated contacts) starting for 06:00Z UTC of today).
403 result = gcal_get_updated_contacts(gcal, &contact_array, NULL);
404 fail_if(result == -1, "Failed downloading updated contacts!");
405 fail_if(contact_array.length > 3, "This user should not have more"
406 " than 3 updated contacts!");
408 /* Now we query for deleted contacts (previous test
409 * added/updated/deleted one contact, remember?)
411 tmp = contact_array.length;
412 gcal_deleted(gcal, SHOW);
413 result = gcal_get_updated_contacts(gcal, &contact_array, NULL);
414 fail_if(result == -1, "Failed downloading updated contacts!");
415 fail_if(contact_array.length <= tmp , "If previous test was ok, it must"
416 " return one more contact!");
418 /* FIXME: Contacts doesn't return the last updated contact
419 * first when running with 'showdeleted'.
421 result = -1;
422 for (tmp = 0; tmp < contact_array.length; ++tmp) {
423 contact = gcal_contact_element(&contact_array, tmp);
424 /* only compare deleted contacts */
425 if (gcal_contact_is_deleted(contact))
426 result = strcmp(gcal_contact_get_id(contact),
427 deleted_contact_id);
428 if (!result)
429 break;
432 fail_if(result != 0, "Cannot locate contact!");
434 /* Cleanup */
435 gcal_cleanup_contacts(&contact_array);
436 gcal_delete(gcal);
439 END_TEST
442 START_TEST (test_contact_photo)
444 gcal_t gcal;
445 gcal_contact_t contact, tmp;
446 char *photo_data;
447 struct gcal_contact_array contact_array;
448 int result;
450 if (find_load_photo("/utests/images/gromit.jpg", &photo_data, &result))
451 fail_if(1, "Cannot load photo!");
453 /* Create a new contact object */
454 contact = gcal_contact_new(NULL);
455 fail_if (!contact, "Cannot construct contact object!");
456 gcal_contact_set_title(contact, "Gromit");
457 gcal_contact_add_email_address(contact, "gromit@wallace.com", E_OTHER, 1);
458 fail_if(gcal_contact_set_photo(contact, photo_data, result),
459 "Failed copying photo data");
461 /* Create a gcal object and authenticate */
462 gcal = gcal_new(GCONTACT);
463 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
464 fail_if(result == -1, "Failed getting authentication");
466 /* Create a new contact with photo */
467 result = gcal_add_contact(gcal, contact);
468 fail_if(result == -1, "Failed adding a new contact!");
470 /* Update the contact: new title, photo, name, etc */
471 free(photo_data);
472 photo_data = NULL;
473 if (find_load_photo("/utests/images/hutch.png", &photo_data, &result))
474 fail_if(1, "Cannot load photo!");
475 gcal_contact_set_title(contact, "hutch");
476 gcal_contact_delete_email_addresses(contact);
477 gcal_contact_add_email_address(contact, "hutch@wallace.com", E_OTHER, 1);
478 fail_if(gcal_contact_set_photo(contact, photo_data, result),
479 "Failed copying photo data");
480 result = gcal_update_contact(gcal, contact);
481 fail_if(result == -1, "Failed updating a contact!");
484 /* Retrieve updated contacts and test for contact photo */
485 result = gcal_get_updated_contacts(gcal, &contact_array, NULL);
486 fail_if(result == -1, "Failed downloading updated contacts!");
487 /* fail_if(contact_array.length > 3, "This user should not have more" */
488 /* " than 3 updated contacts!"); */
490 /* Last updated contact (i.e. last) should have photo */
491 tmp = gcal_contact_element(&contact_array, (contact_array.length - 1));
492 fail_if(tmp == NULL, "Last contact must not be NULL!");
493 fail_if(gcal_contact_get_photo(tmp) == NULL,
494 "Last updated contact must have photo: %s",
495 gcal_contact_get_title(tmp));
496 fail_if(gcal_contact_get_photolength(tmp) < 2,
497 "Last updated contact photo length must be bigger");
499 /* Delete */
500 result = gcal_erase_contact(gcal, contact);
501 fail_if(result == -1, "Failed deleting contact!");
503 /* Cleanup */
504 gcal_contact_delete(contact);
505 gcal_delete(gcal);
507 gcal_cleanup_contacts(&contact_array);
508 free(photo_data);
510 END_TEST
512 START_TEST (test_url_sanity_calendar)
514 gcal_t gcal;
515 gcal_event_t event;
516 struct gcal_event_array all_events;
517 int result;
518 event = gcal_event_new(NULL);
520 gcal = gcal_new(GCALENDAR);
521 gcal_set_store_xml(gcal, 1);
522 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
523 fail_if(result == -1, "Cannot authenticate!");
525 char start[] = "2009-03-26T11:00:00.000Z";
526 char end[] = "2009-03-26T12:00:00.000Z";
527 gcal_event_set_title(event, "Insanity in edit URL");
528 gcal_event_set_content(event, "I'm bored of gcalendar bugs");
529 gcal_event_set_where(event, "someplace");
530 gcal_event_set_start(event, start);
531 gcal_event_set_end(event, end);
533 fail_if((result = gcal_add_event(gcal, event)) != 0,
534 "Failed adding new event!");
535 fail_if((result = gcal_get_events(gcal, &all_events)) != 0,
536 "Failed retrieving all events!");
537 fail_if((strcmp(gcal_event_get_url(event),
538 gcal_event_get_url(gcal_event_element(&all_events, 0)))
539 != 0), "Edit url is different!");
541 /* fprintf(stderr, "add: %s\nretrieve: %s\n", gcal_event_get_url(event), */
542 /* gcal_event_get_url(gcal_event_element(&all_events, 0))); */
544 fail_if((result = gcal_erase_event(gcal, event)) != 0,
545 "Failed deleting test event!");
547 gcal_event_delete(event);
548 gcal_cleanup_events(&all_events);
549 gcal_delete(gcal);
552 END_TEST
554 START_TEST (test_url_sanity_contact)
556 gcal_t gcal;
557 gcal_contact_t contact;
558 struct gcal_contact_array all_contacts;
559 int result;
560 contact = gcal_contact_new(NULL);
562 gcal = gcal_new(GCONTACT);
563 gcal_set_store_xml(gcal, 1);
564 result = gcal_get_authentication(gcal, "gcalntester", "77libgcal");
565 fail_if(result == -1, "Cannot authenticate!");
567 gcal_contact_set_title(contact, "Insanity in edit URL");
568 gcal_contact_add_email_address(contact, "prooftest@add.get.com", E_OTHER, 1);
570 fail_if((result = gcal_add_contact(gcal, contact)) != 0,
571 "Failed adding new contact!");
572 fail_if((result = gcal_get_contacts(gcal, &all_contacts)) != 0,
573 "Failed retrieving all contacts!");
574 fail_if((strcmp(gcal_contact_get_url(contact),
575 gcal_contact_get_url(gcal_contact_element(&all_contacts, all_contacts.length - 1)))
576 != 0), "Edit url is different!");
578 /* fprintf(stderr, "add: %s\nretrieve: %s\n", */
579 /* gcal_contact_get_url(contact), */
580 /* gcal_contact_get_url(gcal_contact_element(&all_contacts, all_contacts.length - 1))); */
582 fail_if((result = gcal_erase_contact(gcal, contact)) != 0,
583 "Failed deleting test contact!");
585 gcal_contact_delete(contact);
586 gcal_cleanup_contacts(&all_contacts);
587 gcal_delete(gcal);
590 END_TEST
592 TCase *gcal_userapi(void)
594 TCase *tc = NULL;
595 int timeout_seconds = 60;
596 tc = tcase_create("gcaluserapi");
597 tcase_set_timeout (tc, timeout_seconds);
599 tcase_add_test(tc, test_get_calendar);
600 tcase_add_test(tc, test_access_calendar);
601 tcase_add_test(tc, test_oper_event_event);
602 tcase_add_test(tc, test_query_event_updated);
603 tcase_add_test(tc, test_get_contacts);
604 tcase_add_test(tc, test_access_contacts);
605 tcase_add_test(tc, test_oper_contact);
606 tcase_add_test(tc, test_query_contact_updated);
607 tcase_add_test(tc, test_contact_photo);
608 tcase_add_test(tc, test_url_sanity_calendar);
609 tcase_add_test(tc, test_url_sanity_contact);
611 return tc;