From dd98af4edcfab1c6782c1ded4e5b084166fcf968 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Thu, 7 May 2020 02:38:58 +0200
Subject: [PATCH] fix: tests yaml formatting fix: command memory leak

---
 command.c                                  |   7 +-
 tests/test_get_all.tavern.yaml             |  38 +++---
 tests/test_schedules_basic.tavern.yaml     |  96 ++++++++--------
 tests/test_schedules_protected.tavern.yaml | 127 ++++++++++-----------
 tests/test_tags.tavern.yaml                |  40 +++++++
 5 files changed, 175 insertions(+), 133 deletions(-)
 create mode 100644 tests/test_tags.tavern.yaml

diff --git a/command.c b/command.c
index 73cbaa5..7c8075c 100644
--- a/command.c
+++ b/command.c
@@ -74,6 +74,7 @@ command_set_relay_schedule(relay_t *relay)
     int result = command_send(controller, COMMAND_CODE_SET_SCHEDULE, payload, payload_size);
 
     controller_free(controller);
+    free(payload);
     return result;
 }
 
@@ -102,7 +103,11 @@ command_set_controller_name(controller_t *controller)
         LOG_ERROR("an error occurred encoding the data");
         return 1;
     }
-    return command_send(controller, COMMAND_CODE_SET_NAME, payload, payload_size);
+
+    int result = command_send(controller, COMMAND_CODE_SET_NAME, payload, payload_size);
+
+    free(payload);
+    return result;
 }
     
 int
diff --git a/tests/test_get_all.tavern.yaml b/tests/test_get_all.tavern.yaml
index 5cf3a9d..e20e014 100644
--- a/tests/test_get_all.tavern.yaml
+++ b/tests/test_get_all.tavern.yaml
@@ -1,25 +1,23 @@
 test_name: Test basic get all requests
 
 stages:
-    - name: "[test_get_all] get all schedules"
-      request:
-          url: "http://localhost:5000/api/v1/schedules/"
-          method: GET
-      response:
-          status_code: 200
+- name: "[test_get_all] get all schedules"
+  request:
+    url: "http://localhost:5000/api/v1/schedules/"
+    method: GET
+  response:
+    status_code: 200
 
-    - name: "[test_get_all] get all relays"
-      skip: True
-      request:
-          url: "http://localhost:5000/api/v1/relays/"
-          method: GET
-      response:
-          status_code: 200
+- name: "[test_get_all] get all relays"
+  request:
+    url: "http://localhost:5000/api/v1/relays/"
+    method: GET
+  response:
+    status_code: 200
 
-    - name: "[test_get_all] get all controllers"
-      skip: True
-      request:
-          url: "http://localhost:5000/api/v1/controllers/"
-          method: GET
-      response:
-          status_code: 200
+- name: "[test_get_all] get all controllers"
+  request:
+    url: "http://localhost:5000/api/v1/controllers/"
+    method: GET
+  response:
+    status_code: 200
diff --git a/tests/test_schedules_basic.tavern.yaml b/tests/test_schedules_basic.tavern.yaml
index 26234d9..c877cae 100644
--- a/tests/test_schedules_basic.tavern.yaml
+++ b/tests/test_schedules_basic.tavern.yaml
@@ -1,50 +1,50 @@
-test_name: Test basic requests
+test_name: Test basic schedule requests
 
 stages:
-    - name: "[test_schedules_basic] Make sure we get any response"
-      request:
-          url: "http://localhost:5000/api/v1/schedules/"
-          method: GET
-      response:
-          status_code: 200
-    - name: "[test_schedules_basic] post schedule, expect it to be echoed back"
-      request:
-          method: POST
-          url: "http://localhost:5000/api/v1/schedules/"
-          json:
-              name: "hello"
-              periods:
-                - start: '00:10'
-                  end: '00:20'
-                - start: '00:30'
-                  end: '00:40'
-                - start: '00:50'
-                  end: '01:00'
-      response:
-          status_code: 201
-          body:
-              name: "{tavern.request_vars.json.name}"
-          save:
-              body:
-                  returned_name: name
-                  returned_id: id
-    - name: "[test_schedules_basic] get schedule, check name and some periods"
-      request:
-          method: GET
-          url: "http://localhost:5000/api/v1/schedules/{returned_id}"
-      response:
-          status_code: 200
-          body:
-              name: "{returned_name}"
-    - name: "[test_schedules_basic] delete schedule"
-      request:
-          method: DELETE
-          url: "http://localhost:5000/api/v1/schedules/{returned_id}"
-      response:
-          status_code: 200
-    - name: "[test_schedules_basic] get deleted schedule, expect 404"
-      request:
-          method: GET
-          url: "http://localhost:5000/api/v1/schedules/{returned_id}"
-      response:
-          status_code: 404
+- name: "[test_schedules_basic] Make sure we get any response"
+  request:
+    url: "http://localhost:5000/api/v1/schedules/"
+    method: GET
+  response:
+    status_code: 200
+- name: "[test_schedules_basic] post schedule, expect it to be echoed back"
+  request:
+    method: POST
+    url: "http://localhost:5000/api/v1/schedules/"
+    json:
+      name: "hello"
+      periods:
+      - start: "00:10"
+        end: "00:20"
+      - start: "00:30"
+        end: "00:40"
+      - start: "00:50"
+        end: "01:00"
+  response:
+    status_code: 201
+    body:
+      name: "{tavern.request_vars.json.name}"
+    save:
+      body:
+        returned_name: name
+        returned_id: id
+- name: "[test_schedules_basic] get schedule, check name and some periods"
+  request:
+    method: GET
+    url: "http://localhost:5000/api/v1/schedules/{returned_id}"
+  response:
+    status_code: 200
+    body:
+      name: "{returned_name}"
+- name: "[test_schedules_basic] delete schedule"
+  request:
+    method: DELETE
+    url: "http://localhost:5000/api/v1/schedules/{returned_id}"
+  response:
+    status_code: 200
+- name: "[test_schedules_basic] get deleted schedule, expect 404"
+  request:
+    method: GET
+    url: "http://localhost:5000/api/v1/schedules/{returned_id}"
+  response:
+    status_code: 404
diff --git a/tests/test_schedules_protected.tavern.yaml b/tests/test_schedules_protected.tavern.yaml
index 0a1a88b..f484408 100644
--- a/tests/test_schedules_protected.tavern.yaml
+++ b/tests/test_schedules_protected.tavern.yaml
@@ -1,66 +1,65 @@
-test_name: Test basic requests
+test_name: Test protected schedules requests
 
 stages:
-    - name: "[test_schedules_protected] delete protected off schedule; expect forbidden/fail"
-      request:
-          method: DELETE
-          url: "http://localhost:5000/api/v1/schedules/off"
-      response:
-          status_code: 403
-    - name: "[test_schedules_protected] get protected off schedule"
-      request:
-          method: GET
-          url: "http://localhost:5000/api/v1/schedules/off"
-      response:
-          status_code: 200
-          body:
-              name: "off"
-              periods: []
-    - name: "[test_schedules_protected] overwrite protected off schedule"
-      request:
-          method: PUT
-          url: "http://localhost:5000/api/v1/schedules/off"
-          json:
-              name: "turned_off"
-              periods:
-                - start: "00:10"
-                  end: "00:20"
-      response:
-          status_code: 200
-          body:
-              name: "{tavern.request_vars.json.name}"
-              periods: []
-
-    - name: "[test_schedules_protected] delete protected on schedule; expect forbidden/fail"
-      request:
-          method: DELETE
-          url: "http://localhost:5000/api/v1/schedules/on"
-      response:
-          status_code: 403
-    - name: get protected on schedule
-      request:
-          method: GET
-          url: "http://localhost:5000/api/v1/schedules/on"
-      response:
-          status_code: 200
-          body:
-              name: "on"
-              periods:
-                  - start: "00:00"
-                    end: "23:59"
-    - name: "[test_schedules_protected] overwrite protected on schedule"
-      request:
-          method: PUT
-          url: "http://localhost:5000/api/v1/schedules/on"
-          json:
-              name: "turned_on"
-              periods:
-                - start: "16:10"
-                  end: "17:20"
-      response:
-          status_code: 200
-          body:
-              name: "{tavern.request_vars.json.name}"
-              periods:
-                  - start: "00:00"
-                    end: "23:59"
+- name: "[test_schedules_protected] delete protected off schedule; expect forbidden/fail"
+  request:
+    method: DELETE
+    url: "http://localhost:5000/api/v1/schedules/off"
+  response:
+    status_code: 403
+- name: "[test_schedules_protected] get protected off schedule"
+  request:
+    method: GET
+    url: "http://localhost:5000/api/v1/schedules/off"
+  response:
+    status_code: 200
+    body:
+      name: "off"
+      periods: []
+- name: "[test_schedules_protected] overwrite protected off schedule"
+  request:
+    method: PUT
+    url: "http://localhost:5000/api/v1/schedules/off"
+    json:
+      name: "turned_off"
+      periods:
+        - start: "00:10"
+          end: "00:20"
+  response:
+    status_code: 200
+    body:
+      name: "{tavern.request_vars.json.name}"
+      periods: []
+- name: "[test_schedules_protected] delete protected on schedule; expect forbidden/fail"
+  request:
+    method: DELETE
+    url: "http://localhost:5000/api/v1/schedules/on"
+  response:
+    status_code: 403
+- name: get protected on schedule
+  request:
+    method: GET
+    url: "http://localhost:5000/api/v1/schedules/on"
+  response:
+    status_code: 200
+    body:
+      name: "on"
+      periods:
+      - start: "00:00"
+        end: "23:59"
+- name: "[test_schedules_protected] overwrite protected on schedule"
+  request:
+    method: PUT
+    url: "http://localhost:5000/api/v1/schedules/on"
+    json:
+      name: "turned_on"
+      periods:
+      - start: "16:10"
+        end: "17:20"
+  response:
+    status_code: 200
+    body:
+      name: "{tavern.request_vars.json.name}"
+      periods:
+      - start: "00:00"
+        end: "23:59"
diff --git a/tests/test_tags.tavern.yaml b/tests/test_tags.tavern.yaml
new file mode 100644
index 0000000..4090f53
--- /dev/null
+++ b/tests/test_tags.tavern.yaml
@@ -0,0 +1,40 @@
+test_name: Test tagging of schedules and relays
+
+stages:
+- name: "[test_tags] post schedule, expect it to be echoed back by tag"
+  request:
+    method: POST
+    url: "http://localhost:5000/api/v1/schedules/"
+    json:
+      name: "test tagging schedule"
+      periods:
+      - start: "00:50"
+        end: "01:00"
+      tags:
+      - "test_tag_1"
+  response:
+    status_code: 201
+    body:
+      id: !anystr
+      name: "{tavern.request_vars.json.name}"
+      periods:
+      - start: "00:50"
+        end: "01:00"
+      tags:
+        - "{tavern.request_vars.json.tags[0]}"
+    save:
+      body:
+        returned_name: name
+        returned_id: id
+- name: "[test_tags] get schedule, check name and some periods"
+  request:
+    method: GET
+    url: "http://localhost:5000/api/v1/schedules/tag/test_tag_1"
+  response:
+    status_code: 200
+    body:
+    - name: "{returned_name}"
+      id: "{returned_id}"
+      periods:
+      - start: "00:50"
+        end: "01:00"