Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Fix bugs in CreateTemplateCategory and DeleteTemplateCategory
Browse files Browse the repository at this point in the history
Resolves #15 and #16
  • Loading branch information
Jericho committed Apr 23, 2015
1 parent b2fc067 commit 00ff66e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
70 changes: 39 additions & 31 deletions CakeMail.RestClient.UnitTests/Templates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public void CreateTemplateCategory_with_minimal_parameters()
r.Resource == "/TemplateV2/CreateCategory/" &&
r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 5 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 7 &&
r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
{
StatusCode = HttpStatusCode.OK,
ContentType = "json",
Expand Down Expand Up @@ -71,12 +73,12 @@ public void CreateTemplateCategory_with_no_labels()
r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
{
StatusCode = HttpStatusCode.OK,
ContentType = "json",
Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId)
});
))).Returns(new RestResponse()
{
StatusCode = HttpStatusCode.OK,
ContentType = "json",
Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId)
});

// Act
var apiClient = new CakeMailRestClient(API_KEY, mockRestClient.Object);
Expand Down Expand Up @@ -104,18 +106,20 @@ public void CreateTemplateCategory_with_isvisiblebydefault_false()
r.Resource == "/TemplateV2/CreateCategory/" &&
r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 5 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 7 &&
r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "0" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
{
StatusCode = HttpStatusCode.OK,
ContentType = "json",
Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId)
});
r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
{
StatusCode = HttpStatusCode.OK,
ContentType = "json",
Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId)
});

// Act
var apiClient = new CakeMailRestClient(API_KEY, mockRestClient.Object);
Expand Down Expand Up @@ -143,18 +147,20 @@ public void CreateTemplateCategory_with_templatescanbecopied_false()
r.Resource == "/TemplateV2/CreateCategory/" &&
r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 5 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 7 &&
r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "0" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
{
StatusCode = HttpStatusCode.OK,
ContentType = "json",
Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId)
});
r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
{
StatusCode = HttpStatusCode.OK,
ContentType = "json",
Content = string.Format("{{\"status\":\"success\",\"data\":\"{0}\"}}", categoryId)
});

// Act
var apiClient = new CakeMailRestClient(API_KEY, mockRestClient.Object);
Expand Down Expand Up @@ -182,12 +188,14 @@ public void CreateTemplateCategory_with_clientid()
r.Resource == "/TemplateV2/CreateCategory/" &&
r.Parameters.Count(p => p.Name == "apikey" && (string)p.Value == API_KEY && p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.HttpHeader) == 1 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 6 &&
r.Parameters.Count(p => p.Type == ParameterType.GetOrPost) == 8 &&
r.Parameters.Count(p => p.Name == "user_key" && (string)p.Value == USER_KEY && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "default" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "templates_copyable" && (string)p.Value == "1" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[en_US]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[fr_FR]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[0][language]" && (string)p.Value == "en_US" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[0][name]" && (string)p.Value == "My Category" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][language]" && (string)p.Value == "fr_FR" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "label[1][name]" && (string)p.Value == "Ma Catégorie" && p.Type == ParameterType.GetOrPost) == 1 &&
r.Parameters.Count(p => p.Name == "client_id" && (long)p.Value == CLIENT_ID && p.Type == ParameterType.GetOrPost) == 1
))).Returns(new RestResponse()
{
Expand Down
16 changes: 13 additions & 3 deletions CakeMail.RestClient/CakeMailRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2961,14 +2961,20 @@ public long CreateTemplateCategory(string userKey, IDictionary<string, string> l
};
if (labels != null)
{
var labelsCount = 0;
foreach (var label in labels)
{
parameters.Add(new KeyValuePair<string, object>(string.Format("label[{0}]", label.Key), label.Value));
parameters.Add(new KeyValuePair<string, object>(string.Format("label[{0}][language]", labelsCount), label.Key));
parameters.Add(new KeyValuePair<string, object>(string.Format("label[{0}][name]", labelsCount), label.Value));
labelsCount++;
}
}
if (clientId.HasValue) parameters.Add(new KeyValuePair<string, object>("client_id", clientId.Value));

return ExecuteRequest<int>(path, parameters);
// The data returned when creating a new category is a little bit unusual
// Instead of simply returning the unique identifier of the new record like all the other 'Create' methods, for example: {"status":"success","data":"4593766"}
// this method return an object with a single property called 'id' containing the unique dietifyer of the new record, like this: {"status":"success","data":{"id":"14052"}}
return ExecuteRequest<long>(path, parameters, "id");
}

/// <summary>
Expand All @@ -2989,7 +2995,11 @@ public bool DeleteTemplateCategory(string userKey, long categoryId, long? client
};
if (clientId.HasValue) parameters.Add(new KeyValuePair<string, object>("client_id", clientId.Value));

return ExecuteRequest<bool>(path, parameters);
// The data returned when creating a new category is a little bit unusual
// Instead of returning a boolean value to indicate success, it returns an empty array!!!
// For example: {"status":"success","data":[]}
ExecuteRequest(path, parameters);
return true;
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ CakeMailRestAPI is available as a Nuget package.
+ **2.0.0**
- Unique identifiers changed to 'long' instead of 'int'.
- "Magic strings" replaced with enums. For example, instead of specifying sort direction with 'asc' and 'desc', you can now use SortDirection.Ascending and SortDirection.Descending.

- Fix bug in CreateTemplateCategory which prevents creating new categories
- Fix bug in DeleteTemplateCategory which causes an exception to be thrown despite the fact the category was successfuly deleted

+ **1.0.0**
- Initial release
Expand Down

0 comments on commit 00ff66e

Please sign in to comment.