add: tests
This commit is contained in:
		
							parent
							
								
									760cec9a20
								
							
						
					
					
						commit
						865caa627e
					
				
					 15 changed files with 558 additions and 386 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,5 @@
 | 
			
		|||
#include <arpa/inet.h>
 | 
			
		||||
 | 
			
		||||
#include <cJSON.h>
 | 
			
		||||
#include <macros.h>
 | 
			
		||||
#include <command.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -74,35 +76,66 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
 | 
			
		|||
    {
 | 
			
		||||
        static const char content[] = "no valid json was supplied";
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        controller_free(controller);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
 | 
			
		||||
    if(cJSON_IsString(json_name) && json_name->valuestring)
 | 
			
		||||
    if(json_name)
 | 
			
		||||
    {
 | 
			
		||||
        strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
 | 
			
		||||
        controller->name[MAX_NAME_LENGTH] = '\0';
 | 
			
		||||
        if(cJSON_IsString(json_name) && json_name->valuestring)
 | 
			
		||||
        {
 | 
			
		||||
            strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
 | 
			
		||||
            controller->name[MAX_NAME_LENGTH] = '\0';
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            static const char content[] = "the given name is no valid string";
 | 
			
		||||
            endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
            cJSON_Delete(json);
 | 
			
		||||
            controller_free(controller);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON *json_ip = cJSON_GetObjectItemCaseSensitive(json, "ip");
 | 
			
		||||
    if(cJSON_IsString(json_ip) && json_ip->valuestring)
 | 
			
		||||
    if(json_ip)
 | 
			
		||||
    {
 | 
			
		||||
        strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
 | 
			
		||||
        controller->ip[IP_LENGTH] = '\0';
 | 
			
		||||
        if(cJSON_IsString(json_ip) && json_ip->valuestring)
 | 
			
		||||
        {
 | 
			
		||||
            unsigned char buf[sizeof(struct in_addr)];
 | 
			
		||||
            if(inet_pton(AF_INET, json_ip->valuestring, buf))
 | 
			
		||||
            {
 | 
			
		||||
                strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
 | 
			
		||||
                controller->ip[IP_LENGTH] = '\0';
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                static const char content[] = "the given ip address is no valid IPv4 address";
 | 
			
		||||
                endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
                cJSON_Delete(json);
 | 
			
		||||
                controller_free(controller);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            static const char content[] = "the given ip address is no valid string";
 | 
			
		||||
            endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
            cJSON_Delete(json);
 | 
			
		||||
            controller_free(controller);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(controller_save(controller))
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to save controller\n");
 | 
			
		||||
        free(controller);
 | 
			
		||||
        controller_free(controller);
 | 
			
		||||
        cJSON_Delete(json);
 | 
			
		||||
 | 
			
		||||
        static const char content[] = "failed to save controller to database";
 | 
			
		||||
        response->status_code = 500;
 | 
			
		||||
        response->content_type = "text/plain";
 | 
			
		||||
        response->content_length = STRLEN(content);;
 | 
			
		||||
        response->content = content;
 | 
			
		||||
        response->alloced_content = false;
 | 
			
		||||
        endpoint_response_text(response, 500, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -111,26 +144,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
 | 
			
		|||
 | 
			
		||||
    command_set_controller_name(controller);
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to print controller json\n");
 | 
			
		||||
 | 
			
		||||
        static const char content[] = "failed to print json for controller";
 | 
			
		||||
        response->status_code = 500;
 | 
			
		||||
        response->content_type = "text/plain";
 | 
			
		||||
        response->content_length = STRLEN(content);;
 | 
			
		||||
        response->content = content;
 | 
			
		||||
        response->alloced_content = false;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        response->status_code = 200;
 | 
			
		||||
        response->content_type = "application/json";
 | 
			
		||||
        response->content_length = strlen(json_str);
 | 
			
		||||
        response->content = json_str;
 | 
			
		||||
        response->alloced_content = true;
 | 
			
		||||
    }
 | 
			
		||||
    endpoint_response_json(response, 200, json);
 | 
			
		||||
    cJSON_Delete(json);
 | 
			
		||||
    controller_free(controller);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -149,11 +163,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
 | 
			
		|||
        LOG_DEBUG("failed to unparse uid\n");
 | 
			
		||||
 | 
			
		||||
        static const char content[] = "given id was invalid";
 | 
			
		||||
        response->status_code = 400;
 | 
			
		||||
        response->content_type = "text/plain";
 | 
			
		||||
        response->content_length = STRLEN(content);;
 | 
			
		||||
        response->content = content;
 | 
			
		||||
        response->alloced_content = false;
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -164,11 +174,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
 | 
			
		|||
        LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
			
		||||
 | 
			
		||||
        static const char content[] = "no controller for id found";
 | 
			
		||||
        response->status_code = 404;
 | 
			
		||||
        response->content_type = "text/plain";
 | 
			
		||||
        response->content_length = STRLEN(content);;
 | 
			
		||||
        response->content = content;
 | 
			
		||||
        response->alloced_content = false;
 | 
			
		||||
        endpoint_response_text(response, 404, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -177,19 +183,11 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
 | 
			
		|||
        LOG_ERROR("failed to remove controller from database\n");
 | 
			
		||||
 | 
			
		||||
        static const char content[] = "failed to remove controller from database";
 | 
			
		||||
        response->status_code = 500;
 | 
			
		||||
        response->content_type = "text/plain";
 | 
			
		||||
        response->content_length = STRLEN(content);;
 | 
			
		||||
        response->content = content;
 | 
			
		||||
        response->alloced_content = false;
 | 
			
		||||
        endpoint_response_text(response, 500, content, STRLEN(content));
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        response->status_code = 200;
 | 
			
		||||
        response->content_type = "application/json";
 | 
			
		||||
        response->content_length = 0;
 | 
			
		||||
        response->content = "";
 | 
			
		||||
        response->alloced_content = false;
 | 
			
		||||
        endpoint_response_text(response, 200, "", 0);
 | 
			
		||||
    }
 | 
			
		||||
    controller_free(controller);
 | 
			
		||||
    return;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -31,8 +31,31 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
 | 
			
		|||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    cJSON *json_period;
 | 
			
		||||
    cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
 | 
			
		||||
    if(!cJSON_IsArray(json_periods))
 | 
			
		||||
    {
 | 
			
		||||
        LOG_DEBUG("no periods for schedule provided\n");
 | 
			
		||||
        cJSON_Delete(json);
 | 
			
		||||
 | 
			
		||||
        static const char content[] = "no periods for schedule provided";
 | 
			
		||||
        endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON *json_tag;
 | 
			
		||||
    cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
 | 
			
		||||
    cJSON_ArrayForEach(json_tag, json_tags)
 | 
			
		||||
    {
 | 
			
		||||
        if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
 | 
			
		||||
        {
 | 
			
		||||
            LOG_DEBUG("invalid tag in tags\n");
 | 
			
		||||
            cJSON_Delete(json);
 | 
			
		||||
 | 
			
		||||
            static const char content[] = "invalid tag in tags";
 | 
			
		||||
            endpoint_response_text(response, 400, content, STRLEN(content));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    schedule_t *new_schedule = malloc(sizeof(schedule_t));
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -47,6 +70,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
 | 
			
		|||
 | 
			
		||||
    int periods_valid = 0;
 | 
			
		||||
 | 
			
		||||
    cJSON *json_period;
 | 
			
		||||
    cJSON_ArrayForEach(json_period, json_periods)
 | 
			
		||||
    {
 | 
			
		||||
        cJSON *json_period_start = cJSON_GetObjectItemCaseSensitive(json_period, "start");
 | 
			
		||||
| 
						 | 
				
			
			@ -106,23 +130,18 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
 | 
			
		|||
    schedule_save(new_schedule);
 | 
			
		||||
 | 
			
		||||
    junction_tag_remove_for_schedule(new_schedule->id);
 | 
			
		||||
    cJSON *json_tag;
 | 
			
		||||
    cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
 | 
			
		||||
 | 
			
		||||
    json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
 | 
			
		||||
    cJSON_ArrayForEach(json_tag, json_tags)
 | 
			
		||||
    {
 | 
			
		||||
            if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
 | 
			
		||||
            {
 | 
			
		||||
                LOG_DEBUG("invalid tag in tags\n");
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            const char *tag = json_tag->valuestring;
 | 
			
		||||
            int tag_id = tag_get_id(tag);
 | 
			
		||||
            if(tag_id == 0)
 | 
			
		||||
            {
 | 
			
		||||
                tag_save(tag_id, tag);
 | 
			
		||||
                tag_id = tag_get_id(tag);
 | 
			
		||||
            }
 | 
			
		||||
            junction_tag_insert(tag_id, 0, new_schedule->id);
 | 
			
		||||
        const char *tag = json_tag->valuestring;
 | 
			
		||||
        int tag_id = tag_get_id(tag);
 | 
			
		||||
        if(tag_id == 0)
 | 
			
		||||
        {
 | 
			
		||||
            tag_save(tag_id, tag);
 | 
			
		||||
            tag_id = tag_get_id(tag);
 | 
			
		||||
        }
 | 
			
		||||
        junction_tag_insert(tag_id, 0, new_schedule->id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cJSON_Delete(json);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue