From 4130c2e87460f1776cd2476c5b44db998ab4ed03 Mon Sep 17 00:00:00 2001
From: Diego MENDEZ <Diego.MENDEZ@ext.ec.europa.eu>
Date: Mon, 4 Nov 2024 11:29:33 +0200
Subject: [PATCH] Repair robot bug with authentification.

---
 tests/README.md                | 25 +++++++++++++++++--------
 tests/requirements.robot.txt   |  3 +++
 tests/robot/metrics_test.robot | 19 ++++++++-----------
 tests/robot/plugins_test.robot | 21 ++++++++++-----------
 tests/robot/variables.yaml     |  5 +----
 5 files changed, 39 insertions(+), 34 deletions(-)
 create mode 100644 tests/requirements.robot.txt

diff --git a/tests/README.md b/tests/README.md
index 188f0a7..b508fa8 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -1,15 +1,23 @@
 # Testing the plugin
 
-## End to end
-
-Prepare a python environment to execute the E2E tests suite
+## Prepare a Python environment for E2E and Robot tests
 
 ```shell
 python3 -m venv venv
 source venv/bin/activate
 pip install -r tests/requirements.e2e.txt
+pip install -r tests/requirements.robot.txt
+```
+
+## E2E, End to end
+
+```shell
 python -m unittest discover tests/e2e
 ```
+or
+```shell
+env HOST=<host_to_connect> PORT=8080 API_TOKEN=<netbox_token> python -m unittest discover tests/e2e
+```
 
 
 ## Run Robot Framework tests
@@ -21,12 +29,13 @@ python -m unittest discover tests/e2e
 ###
 3. Go to the test folder and change the value of the variable `PORT` in the varibales.yaml file to the correct port number used by netbox
 ###
-4. Prepare a python environment, install aditional settings and run the tests:
+4. Run the tests:
 ```
-python3 -m venv venv
-source ./venv/bin/activate
-pip install robotframework robotframework-requests pyyaml
 robot ./tests/robot/
 ```
+or
+```
+robot -v HOST:<host_to_connect> -v PORT:8080 -v API_TOKEN:<netbox_token> ./tests/robot
+```
 ###
-5. Open the file `report.html` in the browser to check the test results.
\ No newline at end of file
+5. Open the file `report.html` in the browser to check the test results.
diff --git a/tests/requirements.robot.txt b/tests/requirements.robot.txt
new file mode 100644
index 0000000..d6c4c1b
--- /dev/null
+++ b/tests/requirements.robot.txt
@@ -0,0 +1,3 @@
+robotframework
+robotframework-requests
+pyyaml
diff --git a/tests/robot/metrics_test.robot b/tests/robot/metrics_test.robot
index a655050..6326acb 100644
--- a/tests/robot/metrics_test.robot
+++ b/tests/robot/metrics_test.robot
@@ -8,19 +8,16 @@ ${TOKEN}    Token ${API_TOKEN}
 
 *** Test Cases ***
 
-TC03 Check Metrics Page
-    ${auth}=  Evaluate  ("${USER_API}", "${PASSWORD_API}")
-    &{HEADERS}    Create Dictionary    Authorization=${TOKEN} Content-Type=${CONTENT_TYPE}
-    Create Session    robot_test_session    ${BASE_URL}    headers=${HEADERS} 
-    ${response}=    GET On session    robot_test_session    /${PAGE}    headers=${HEADERS}    auth=${auth}    expected_status=200
+Check Metrics Page
+    ${response}=    GET    ${BASE_URL}/metrics    expected_status=200
     Log to console    ${\n}
-    Check metrics response status "${response.ok}" "${PAGE}"
+    Check metrics response status "${response.ok}" "metrics"
     Log to console    ${\n}
 
-TC03.1 Check Nonexistent Page
-    ${auth}=  Evaluate  ("${USER_API}", "${PASSWORD_API}")
-    &{HEADERS}    Create Dictionary    Authorization=${TOKEN} Content-Type=${CONTENT_TYPE}
-    ${response}=    GET On session    robot_test_session    /api/plugins/nonexistent_plugin    auth=${auth}    headers=${HEADERS}    expected_status=404
+Check Nonexistent Page
+    ${HEADERS}       Create Dictionary    Content-Type    application/json    Authorization    ${TOKEN} 
+    Log to console    ${\n}
+    ${response}=    GET    ${BASE_URL}/nonexisting_page    headers=${HEADERS}  expected_status=404
     Log to console    ${\n}
     Check metrics response status "${response.ok}" "nonexistent"
     Log to console    ${\n}
@@ -34,4 +31,4 @@ Check metrics response status "${response.ok}" "${PAGE}"
     ELSE
         Log to console    ${\n}${PAGE} Not Ready
         Log    ${\n}${PAGE} Not Ready
-    END
\ No newline at end of file
+    END
diff --git a/tests/robot/plugins_test.robot b/tests/robot/plugins_test.robot
index ecdc4f3..a30cc99 100644
--- a/tests/robot/plugins_test.robot
+++ b/tests/robot/plugins_test.robot
@@ -8,21 +8,20 @@ ${TOKEN}    Token ${API_TOKEN}
 
 *** Test Cases ***
 
-TC02 Check installed plugins
-    ${auth}=  Evaluate  ("${USER_API}", "${PASSWORD_API}")
-    &{HEADERS}    Create Dictionary    Authorization=${TOKEN} Content-Type=${CONTENT_TYPE}
-    Create Session    robot_test_session    ${BASE_URL}    headers=${HEADERS}     
+Check installed plugins
+    ${HEADERS}       Create Dictionary    Content-Type    application/json    Authorization    ${TOKEN} 
     Log to console    ${\n}
+
     FOR    ${item}    IN    @{PLUGINS}
-    ${response}=    GET On session    robot_test_session    /api/plugins/${item}/    auth=${auth}   headers=${HEADERS}    expected_status=200  
-    Check plugins response status "${response.ok}" "${item}"
+      ${response}=  GET  ${BASE_URL}/api/plugins/${item}/  headers=${HEADERS}  expected_status=200
+      Check plugins response status "${response.ok}" "${item}"
     END
     Log to console    ${\n}
     
-TC02.1 Check Nonexistent Plugin
-    ${auth}=  Evaluate  ("${USER_API}", "${PASSWORD_API}")
-    &{HEADERS}    Create Dictionary    Authorization=${TOKEN} Content-Type=${CONTENT_TYPE}
-    ${response}=    GET On session    robot_test_session    /api/plugins/nonexistent_plugin   headers=${HEADERS}    expected_status=404
+Check Nonexistent Plugin
+    ${HEADERS}       Create Dictionary    Content-Type    application/json    Authorization    ${TOKEN} 
+    Log to console    ${\n}
+    ${response}=  GET  ${BASE_URL}/api/plugins/nonexistent_plugin/  headers=${HEADERS}  expected_status=404
     Check plugins response status "${response.ok}" "nonexistent"
     
 
@@ -35,4 +34,4 @@ Check plugins response status "${response.ok}" "${item}"
     ELSE
         Log to console    ${\n}Plugin ${item} Not Installed
         Log    ${\n}Plugin ${item} Not Installed
-    END
\ No newline at end of file
+    END
diff --git a/tests/robot/variables.yaml b/tests/robot/variables.yaml
index c783862..92fd081 100644
--- a/tests/robot/variables.yaml
+++ b/tests/robot/variables.yaml
@@ -1,8 +1,5 @@
 HOST: "localhost"
 PORT: 8080
-USER_API: "admin"
-PASSWORD_API: "thisisagoodpassword"
 API_TOKEN: "only4testingpurpose"
 CONTENT_TYPE: "application/json"
-PAGE: "metrics"
-PLUGINS: ["rps", "netbox-dns", "cert", "prometheus-sd", "docker", "mac-address"]
\ No newline at end of file
+PLUGINS: ["rps", "netbox-dns", "cert", "prometheus-sd", "docker", "mac-address"]
-- 
GitLab