From 8916c05c5281bf0c97419df6e7ce6d641c56fb3e Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 24 Apr 2016 00:33:12 +0100 Subject: [PATCH] Added code and unit tests for editing calendars in the CalDAV object Added the following subroutines to the CalDAV object: EditCalendarProcess(*string, *string, *Colour, *string, *int); EditCalendar(*string, *string, *Colour, *string, *int); EditCalendar(*string, *Colour); EditCalendar(*string, *string); EditCalendar(*string, *int); EditCalendarDescription(*string, *string); Unit tests setup for editing calendars under CalDAV/EditCalendar testing the above functions. --- source/objects/CalDAV/CalDAV.cpp | 206 +++++++++++++++++++++++++++ source/objects/CalDAV/CalDAV.h | 18 +++ source/tests/xestiacalendar_caldav.h | 159 +++++++++++++++++++++ 3 files changed, 383 insertions(+) diff --git a/source/objects/CalDAV/CalDAV.cpp b/source/objects/CalDAV/CalDAV.cpp index 001743f..55ee65a 100644 --- a/source/objects/CalDAV/CalDAV.cpp +++ b/source/objects/CalDAV/CalDAV.cpp @@ -624,6 +624,212 @@ CalDAVServerResult CalDAV::AddCalendar(string CalendarName){ } +CalDAVServerResult CalDAV::EditCalendarProcess(string *CalendarHREF, + string *CalendarName, + Colour *CalendarColour, + string *CalendarDescription, + int *CalendarOrder){ + + CalDAVServerResult ServerResult; + CalDAVSendData CalendarEditSendData; + + // Build the server address. + + string UserPrincipalURI = ""; + UserPrincipalURI = GetUserPrincipal(); + + if (UserPrincipalURI.size() == 0){ + + return ServerResult; + + } + + string CalendarHomeURI = ""; + CalendarHomeURI = GetCalendarHome(UserPrincipalURI); + + // Generate the UUID. + + string UUIDValue = GenerateUUID(); + UUIDValue.erase(UUIDValue.end()-1); + + string CalendarHomeURL = CalendarHomeURI; + CalendarHomeURL.append(UUIDValue); + CalendarHomeURL.append("/"); + + // Build the calendar list address. + + string CalendarEditURLAddress = BuildServerAddress(&ConnectionData, (*CalendarHREF)); + + string CalendarEditRequest = "\n" + "\n" + " \n" + " \n"; + + // Update the calendar name. + + if (CalendarName != nullptr){ + + CalendarEditRequest += ""; + CalendarEditRequest += (*CalendarName); + CalendarEditRequest += "\n"; + + } + + // Update the calendar colour. + + if (CalendarColour != nullptr){ + + CalendarEditRequest += ""; + CalendarEditRequest += (*CalendarColour); + CalendarEditRequest += "\n"; + + } + + // Update the calendar description. + + if (CalendarDescription != nullptr){ + + CalendarEditRequest += ""; + CalendarEditRequest += (*CalendarDescription); + CalendarEditRequest += "\n"; + + } + + // Update the calendar order. + + if (CalendarOrder != nullptr){ + + CalendarEditRequest += ""; + CalendarEditRequest += to_string((*CalendarOrder)); + CalendarEditRequest += "\n"; + + } + + CalendarEditRequest += " \n" + " \n" + ""; + + CalendarEditSendData.readptr = &CalendarEditRequest; + CalendarEditSendData.sizeleft = CalendarEditRequest.size(); + + // Setup the header. + + struct curl_slist *CalendarRequestHeader = NULL; + + //curl_easy_setopt(ConnectionHandle, CURLOPT_HTTPHEADER, CalendarRequestHeader); + curl_easy_setopt(ConnectionHandle, CURLOPT_URL, CalendarEditURLAddress.c_str()); + curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "PROPPATCH"); + curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 1L); + curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, &CalendarEditSendData); + curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, CalDAVSend); + + // Process the data. + + ServerData.clear(); + ServerHeader.clear(); + + CURLcode ServerConnectionResult = curl_easy_perform(ConnectionHandle); + + if (ServerConnectionResult == CURLE_OK){ + ServerResult.Result = CALDAVQUERYRESULT_OK; + } else { + ServerResult.Result = CALDAVQUERYRESULT_SERVERERROR; + } + ServerResult.Code = ServerConnectionResult; + curl_easy_getinfo(ConnectionHandle, CURLINFO_RESPONSE_CODE, &ServerResult.HTTPCode); + + // Restore the original settings. + + string OriginalServerAddress = BuildServerAddress(&ConnectionData, "/principals/"); + curl_easy_setopt(ConnectionHandle, CURLOPT_URL, OriginalServerAddress.c_str()); + curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, NULL); + curl_easy_setopt(ConnectionHandle, CURLOPT_UPLOAD, 0L); + curl_easy_setopt(ConnectionHandle, CURLOPT_READDATA, NULL); + curl_easy_setopt(ConnectionHandle, CURLOPT_READFUNCTION, NULL); + + return ServerResult; + +} + +CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF, + string *CalendarName, + Colour *CalendarColour, + string *CalendarDescription, + int *CalendarOrder){ + + CalDAVServerResult ServerResult; + + ServerResult = EditCalendarProcess(CalendarHREF, + CalendarName, + CalendarColour, + CalendarDescription, + CalendarOrder); + + return ServerResult; + +} + +CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF, + Colour *CalendarColour){ + + + CalDAVServerResult ServerResult; + + ServerResult = EditCalendarProcess(CalendarHREF, + nullptr, + CalendarColour, + nullptr, + nullptr); + + return ServerResult; + +} + +CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF, + string *CalendarName){ + + CalDAVServerResult ServerResult; + + ServerResult = EditCalendarProcess(CalendarHREF, + CalendarName, + nullptr, + nullptr, + nullptr); + + return ServerResult; + +} + +CalDAVServerResult CalDAV::EditCalendar(string *CalendarHREF, + int *CalendarOrder){ + + CalDAVServerResult ServerResult; + + ServerResult = EditCalendarProcess(CalendarHREF, + nullptr, + nullptr, + nullptr, + CalendarOrder); + + return ServerResult; + +} + +CalDAVServerResult CalDAV::EditCalendarDescription(string *CalendarHREF, + string *CalendarDescription){ + + CalDAVServerResult ServerResult; + + ServerResult = EditCalendarProcess(CalendarHREF, + nullptr, + nullptr, + CalendarDescription, + nullptr); + + return ServerResult; + +} bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){ // Check if the passed CalDAV Connection Data is has diff --git a/source/objects/CalDAV/CalDAV.h b/source/objects/CalDAV/CalDAV.h index cbe065a..9e9d553 100644 --- a/source/objects/CalDAV/CalDAV.h +++ b/source/objects/CalDAV/CalDAV.h @@ -126,6 +126,11 @@ class CalDAV{ bool MatchXMLNameTransverse(xmlNodePtr *NodePtr, string NodeName); bool MatchXMLName(xmlNodePtr *NodePtrOriginal, string NodeName); string FetchXMLData(xmlNodePtr *NodePtr); + CalDAVServerResult EditCalendarProcess(string *CalendarHREF, + string *CalendarName, + Colour *CalendarColour, + string *CalendarDescription, + int *CalendarOrder); CalDAVConnectionData ConnectionData; CalDAVServerResult ConnectionServerResult; @@ -144,6 +149,19 @@ class CalDAV{ CalDAVCalendarList GetCalendars(); CalDAVServerResult AddCalendar(string CalendarName); + CalDAVServerResult EditCalendar(string *CalendarHREF, + string *CalendarName, + Colour *CalendarColour, + string *CalendarDescription, + int *CalendarOrder); + CalDAVServerResult EditCalendar(string *CalendarHREF, + Colour *CalendarColour); + CalDAVServerResult EditCalendar(string *CalendarHREF, + string *CalendarName); + CalDAVServerResult EditCalendar(string *CalendarHREF, + int *CalendarOrder); + CalDAVServerResult EditCalendarDescription(string *CalendarHREF, + string *CalendarDescription); string GetUserPrincipal(); string GetCalendarHome(string UserPrincipalURI); diff --git a/source/tests/xestiacalendar_caldav.h b/source/tests/xestiacalendar_caldav.h index 2e5d9cb..f156b2a 100644 --- a/source/tests/xestiacalendar_caldav.h +++ b/source/tests/xestiacalendar_caldav.h @@ -551,4 +551,163 @@ TEST(CalDAV, AddCalendar){ ASSERT_EQ(201, ConnResult.HTTPCode); ASSERT_EQ(CURLE_OK, ConnResult.Code); +} + +TEST(CalDAV, EditCalendar){ + + CalDAVConnectionData ConnNormal; + string CurrentUserPrincipal; + + bool ValidDataNormal = false; + + // Attempt to read the caldavtest.auth file. + + ProcessConnectionDataFileResult DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal); + if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){ + ValidDataNormal = true; + } + + ASSERT_EQ(true, ValidDataNormal); + + // Setup the connection. + + CalDAV ServerConnection; + + ServerConnection.SetupConnectionData(&ConnNormal); + + // Verify the connection settings. + + CalDAVStatus CalDAVStatus = ServerConnection.GetConnectionData(); + + ASSERT_EQ(CalDAVStatus.Hostname, ConnNormal.Hostname); + ASSERT_EQ(CalDAVStatus.Username, ConnNormal.Username); + ASSERT_EQ(CalDAVStatus.Port, ConnNormal.Port); + ASSERT_EQ(CalDAVStatus.Prefix, ConnNormal.Prefix); + ASSERT_EQ(CalDAVStatus.UseSSL, ConnNormal.UseSSL); + + // Connect to the server. + + CalDAVServerResult ConnResult = ServerConnection.Connect(); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(200, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Check that the server supports CalDAV. + + CalDAVServerSupport ConnSupport = ServerConnection.GetServerSupport(); + ConnResult = ServerConnection.GetServerResult(); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(200, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + ASSERT_EQ(true, ConnSupport.BasicSupport); + + // Add a calendar to the server. + + ConnResult = ServerConnection.AddCalendar("Calendar To Edit"); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(201, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Get the list of calendars. + + CalDAVCalendarList CalendarList = ServerConnection.GetCalendars(); + + // Check the response result from the server. + + ConnResult = ServerConnection.GetServerResult(); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(207, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Find the calendar containing the name "Calendar To Edit" + // created earlier. + + int ItemSeek = 0; + bool ItemFound = false; + + for (map::iterator CalendarNameIter = CalendarList.Name.begin(); + CalendarNameIter != CalendarList.Name.end(); CalendarNameIter++){ + + if (CalendarNameIter->second == "Calendar To Edit"){ + ItemFound = true; + break; + } + + ItemSeek++; + + } + + ASSERT_NE(false, ItemFound); + + // Edit the name of the calendar. + + string CalendarEditHREF = CalendarList.HREF[ItemSeek]; + string NewCalendarName = "Edited Calendar"; + + ConnResult = ServerConnection.EditCalendar(&CalendarEditHREF, &NewCalendarName); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(207, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Edit the colour of the calendar. + + Colour NewColour; + + NewColour.red = 255; + NewColour.green = 0; + NewColour.blue = 0; + NewColour.alpha = 0; + + ConnResult = ServerConnection.EditCalendar(&CalendarEditHREF, &NewColour); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(207, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Edit the description of the calendar. + + string NewCalendarDescription = "Update Calendar Description"; + + ConnResult = ServerConnection.EditCalendarDescription(&CalendarEditHREF, &NewCalendarDescription); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(207, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Edit the order of the calendar. + + int NewCalendarOrder = 30; + + ConnResult = ServerConnection.EditCalendar(&CalendarEditHREF, &NewCalendarOrder); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(207, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Edit all of the available properties of the calendar. + + NewCalendarName = "Calendar Edited Again"; + NewCalendarDescription = "Another updated calendar description"; + NewColour.red = 0; + NewColour.green = 255; + NewColour.blue = 0; + NewColour.alpha = 0; + NewCalendarOrder = 40; + + ConnResult = ServerConnection.EditCalendar(&CalendarEditHREF, + &NewCalendarName, + &NewColour, + &NewCalendarDescription, + &NewCalendarOrder); + + EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(207, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + +} } \ No newline at end of file -- 2.39.5