From 29e7641e910901e813f5682da677f767a2aea5cc Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Tue, 20 Apr 2021 11:54:00 +0200 Subject: #915 Add /room/add POST method Adds POST method to add rooms (labs), required payload parameters are name, netmask and center; any additional attributes are optional. Required JSON: { "center": 0, "name": "classroom10", "netmask": "255.255.255.0" } Full JSON: { "center": 0, "name": "classroom11", "netmask": "255.255.255.0", "group": 0, "location": "First floor", "gateway": "192.168.56.1", "ntp": "hora.cica.es", "dns": "1.1.1.1", "remote": True } This commit also adds unit tests for /room/add POST method. --- src/dbi.h | 13 ++++ src/rest.c | 123 +++++++++++++++++++++++++++++++++ tests/units/test_0031_post_room_add.py | 47 +++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 tests/units/test_0031_post_room_add.py diff --git a/src/dbi.h b/src/dbi.h index 798fecd..33b7afc 100644 --- a/src/dbi.h +++ b/src/dbi.h @@ -24,6 +24,7 @@ void og_dbi_close(struct og_dbi *db); #define OG_DB_COMPUTER_NAME_MAXLEN 100 #define OG_DB_CENTER_NAME_MAXLEN 100 #define OG_DB_ROOM_NAME_MAXLEN 100 +#define OG_DB_ROOM_LOC_MAXLEN 255 #define OG_DB_SERIAL_NUMBER_MAXLEN 25 #define OG_DB_IMAGE_DESCRIPTION_MAXLEN 250 #define OG_DB_IMAGE_NAME_MAXLEN 50 @@ -88,6 +89,18 @@ struct og_computer { char ip[OG_DB_IP_MAXLEN + 1]; }; +struct og_room { + uint32_t center; + uint32_t group; + char location[OG_DB_ROOM_LOC_MAXLEN + 1]; + char name[OG_DB_ROOM_NAME_MAXLEN + 1]; + char gateway[OG_DB_IP_MAXLEN + 1]; + char netmask[OG_DB_IP_MAXLEN + 1]; + char ntp[OG_DB_IP_MAXLEN + 1]; + char dns[OG_DB_IP_MAXLEN + 1]; + bool remote; +}; + struct in_addr; int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, struct in_addr addr); diff --git a/src/rest.c b/src/rest.c index 423a6cd..00f40fb 100644 --- a/src/rest.c +++ b/src/rest.c @@ -73,6 +73,7 @@ struct ev_loop *og_loop; #define OG_REST_PARAM_NETMASK (1UL << 40) #define OG_REST_PARAM_SCOPE (1UL << 41) #define OG_REST_PARAM_MODE (1UL << 42) +#define OG_REST_PARAM_CENTER (1UL << 43) static LIST_HEAD(client_list); @@ -3921,6 +3922,115 @@ static int og_cmd_post_center_add(json_t *element, return 0; } +static int og_cmd_post_room_add(json_t *element, + struct og_msg_params *params) +{ + struct og_room room = {}; + const char *key, *msglog; + struct og_dbi *dbi; + dbi_result result; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string_copy(value, room.name, + sizeof(room.name)); + params->flags |= OG_REST_PARAM_NAME; + } else if (!strcmp(key, "location")) { + err = og_json_parse_string_copy(value, room.location, + sizeof(room.location)); + } else if (!strcmp(key, "gateway")) { + err = og_json_parse_string_copy(value, room.gateway, + sizeof(room.gateway)); + } else if (!strcmp(key, "netmask")) { + err = og_json_parse_string_copy(value, room.netmask, + sizeof(room.netmask)); + params->flags |= OG_REST_PARAM_NETMASK; + } else if (!strcmp(key, "ntp")) { + err = og_json_parse_string_copy(value, room.ntp, + sizeof(room.ntp)); + } else if (!strcmp(key, "dns")) { + err = og_json_parse_string_copy(value, room.dns, + sizeof(room.dns)); + } else if (!strcmp(key, "center")) { + err = og_json_parse_uint(value, &room.center); + params->flags |= OG_REST_PARAM_CENTER; + } else if (!strcmp(key, "group")) { + err = og_json_parse_uint(value, &room.group); + } else if (!strcmp(key, "remote")) { + err = og_json_parse_bool(value, &room.remote); + } + + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_NAME | + OG_REST_PARAM_NETMASK | + OG_REST_PARAM_CENTER)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "SELECT nombreaula FROM aulas " + "WHERE nombreaula='%s' AND idcentro=%d", + room.name, room.center); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + + if (dbi_result_get_numrows(result) > 0) { + syslog(LOG_ERR, "Room with name %s already exists in the " + "center with id %d\n", + room.name, room.center); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO aulas(" + " idcentro," + " nombreaula," + " netmask," + " grupoid," + " ubicacion," + " router," + " dns," + " ntp," + " inremotepc) VALUES (" + "%d, '%s', '%s', %d, '%s', " + "'%s', '%s', '%s', %d)", + room.center, room.name, room.netmask, + room.group, room.location, room.gateway, + room.dns, room.ntp, room.remote); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to add room to database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + + dbi_result_free(result); + og_dbi_close(dbi); + return 0; +} + static int og_client_method_not_found(struct og_client *cli) { /* To meet RFC 7231, this function MUST generate an Allow header field @@ -4410,6 +4520,19 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_post_center_add(root, ¶ms, buf_reply); + } else if (!strncmp(cmd, "room/add", + strlen("room/add"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, "command task with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_post_room_add(root, ¶ms); } else { syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); err = og_client_not_found(cli); diff --git a/tests/units/test_0031_post_room_add.py b/tests/units/test_0031_post_room_add.py new file mode 100644 index 0000000..3db8c6d --- /dev/null +++ b/tests/units/test_0031_post_room_add.py @@ -0,0 +1,47 @@ +import requests +import unittest + +class TestPostRoomAddMethods(unittest.TestCase): + + def setUp(self): + self.url = 'http://localhost:8888/room/add' + self.headers = {'Authorization' : '07b3bfe728954619b58f0107ad73acc1'} + self.full_json = { "center": 0, + "name": "classroom11", + "netmask": "255.255.255.0", + "group": 0, + "location": "First floor", + "gateway": "192.168.56.1", + "ntp": "hora.cica.es", + "dns": "1.1.1.1", + "remote": True } + self.minimal_json = { "center": 0, + "name": "classroom10", + "netmask": "255.255.255.0" } + self.duplicated_room_json = { "center": 0, + "name": "repeated_room", + "netmask": "255.255.255.0" } + + def test_post(self): + returned = requests.post(self.url, headers=self.headers, + json=self.full_json) + self.assertEqual(returned.status_code, 200) + + def test_post_only_required_fields(self): + returned = requests.post(self.url, headers=self.headers, + json=self.minimal_json) + self.assertEqual(returned.status_code, 200) + + def test_post_duplicated_room(self): + requests.post(self.url, headers=self.headers, + json=self.duplicated_room_json) + returned = requests.post(self.url, headers=self.headers, + json=self.duplicated_room_json) + self.assertEqual(returned.status_code, 400) + + def test_get(self): + returned = requests.get(self.url, headers=self.headers) + self.assertEqual(returned.status_code, 405) + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3-18-g5258