add: test

add: period filters
fix: invalid reads (lost invalid json debug output)
This commit is contained in:
Tobias Reisinger 2020-05-29 00:57:09 +02:00
parent f2a40ca330
commit 2d992cfe3c
6 changed files with 181 additions and 32 deletions

View file

@ -106,13 +106,6 @@ api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpo
if(json == NULL) if(json == NULL)
{ {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
LOG_DEBUG("error before: %s\n", error_ptr);
}
cJSON_Delete(json);
static const char content[] = "no valid json was supplied"; static const char content[] = "no valid json was supplied";
response->status_code = 400; response->status_code = 400;
response->content_type = "text/plain"; response->content_type = "text/plain";

View file

@ -147,13 +147,6 @@ api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *
if(json == NULL) if(json == NULL)
{ {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
LOG_ERROR("error before: %s\n", error_ptr);
}
cJSON_Delete(json);
static const char content[] = "no valid json was supplied"; static const char content[] = "no valid json was supplied";
response->status_code = 400; response->status_code = 400;
response->content_type = "text/plain"; response->content_type = "text/plain";

View file

@ -15,13 +15,6 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
if(json == NULL) if(json == NULL)
{ {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
LOG_ERROR("error before: %s\n", error_ptr);
}
cJSON_Delete(json);
static const char content[] = "no valid json was supplied"; static const char content[] = "no valid json was supplied";
response->status_code = 400; response->status_code = 400;
response->content_type = "text/plain"; response->content_type = "text/plain";
@ -69,12 +62,30 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL)) if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL))
{ {
LOG_DEBUG("period is missing start\n"); LOG_DEBUG("period is missing start\n");
continue; cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "one period is missing a start";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
} }
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL)) if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
{ {
LOG_DEBUG("period is missing end\n"); LOG_DEBUG("period is missing end\n");
continue; cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "one period is missing an end";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
} }
uint16_t start; uint16_t start;
@ -82,12 +93,30 @@ api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_r
if(period_helper_parse_hhmm(json_period_start->valuestring, &start)) if(period_helper_parse_hhmm(json_period_start->valuestring, &start))
{ {
LOG_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring); LOG_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring);
continue; cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "the start for one period is invalid";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
} }
if(period_helper_parse_hhmm(json_period_end->valuestring, &end)) if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
{ {
LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring); LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring);
continue; cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "the end for one period is invalid";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
} }
new_schedule->periods[periods_valid].start = start; new_schedule->periods[periods_valid].start = start;

View file

@ -108,13 +108,6 @@ api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoin
if(json == NULL) if(json == NULL)
{ {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
LOG_DEBUG("error before: %s\n", error_ptr);
}
cJSON_Delete(json);
static const char content[] = "no valid json was supplied"; static const char content[] = "no valid json was supplied";
response->status_code = 400; response->status_code = 400;
response->content_type = "text/plain"; response->content_type = "text/plain";

View file

@ -31,6 +31,15 @@ period_helper_parse_hhmm(const char *hhmm_str, uint16_t *hhmm)
tmp_h = (uint16_t)strtol(&hhmm_str[0], NULL, 10); tmp_h = (uint16_t)strtol(&hhmm_str[0], NULL, 10);
tmp_m = (uint16_t)strtol(&hhmm_str[3], NULL, 10); tmp_m = (uint16_t)strtol(&hhmm_str[3], NULL, 10);
if(tmp_h > 24 || tmp_m >= 60)
{
return 1;
}
if(tmp_h == 24 && tmp_m > 0)
{
return 1;
}
*hhmm = (tmp_h * 60) + tmp_m; *hhmm = (tmp_h * 60) + tmp_m;
return 0; return 0;

View file

@ -10,6 +10,24 @@ stages:
verify_response_with: verify_response_with:
function: validate_schedule:multiple function: validate_schedule:multiple
- name: "[schedules_basic] post schedule with no periods, expect it to be echoed back"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
name: "same as off"
periods: []
response:
status_code: 201
verify_response_with:
function: validate_schedule:single
function: validate_schedule:check_name
extra_kwargs:
name: "{tavern.request_vars.json.name}"
function: validate_schedule:check_periods
extra_kwargs:
periods: "{tavern.request_vars.json.periods}"
- name: "[schedules_basic] post schedule, expect it to be echoed back" - name: "[schedules_basic] post schedule, expect it to be echoed back"
request: request:
method: POST method: POST
@ -67,3 +85,117 @@ stages:
url: "http://localhost:5000/api/v1/schedules/{returned_id}" url: "http://localhost:5000/api/v1/schedules/{returned_id}"
response: response:
status_code: 404 status_code: 404
- name: "[schedules_basic] get schedule with bad id"
request:
method: GET
url: "http://localhost:5000/api/v1/schedules/this_id_is_invalid"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad body (no json)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
data: "<b>not json</b><i>but html</i>"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad body (no name)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
periods:
- start: "00:10"
end: "00:20"
- start: "00:30"
end: "00:40"
- start: "00:50"
end: "01:00"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad body (name as number)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
name: 42
periods:
- start: "00:10"
end: "00:20"
- start: "00:30"
end: "00:40"
- start: "00:50"
end: "01:00"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad period (no start)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
name: "i am invalid"
periods:
- end: "00:20"
- start: "00:30"
end: "00:40"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad period (no end)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
name: "i am invalid"
periods:
- start: "00:20"
- start: "00:30"
end: "00:40"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad period (invalid start)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
name: "i am invalid"
periods:
- start: "hello"
end: "00:20"
- start: "00:30"
end: "00:40"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad period (invalid end)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
name: "i am invalid"
periods:
- start: "12:10"
end: 1215
- start: "00:30"
end: "00:40"
response:
status_code: 400
- name: "[schedules_basic] post schedule with bad period (invalid end 2)"
request:
method: POST
url: "http://localhost:5000/api/v1/schedules/"
json:
name: "i am invalid"
periods:
- start: "12:10"
end: "25:90"
- start: "00:30"
end: "00:40"
response:
status_code: 400