From 36bdb8634346308f6bf0cd6a2b6d021865cb284e Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 24 Apr 2023 14:54:26 +0200 Subject: [PATCH] feat: Add Test for Connect API --- IOT/lib/API/include/API.h | 9 ++++++--- IOT/lib/API/src/API.cpp | 8 ++++++-- IOT/test/main.cpp | 2 ++ IOT/test/test.h | 2 ++ IOT/test/test_api.cpp | 24 +++++++++++++++++++++++- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/IOT/lib/API/include/API.h b/IOT/lib/API/include/API.h index 27eda6b..463f6f6 100644 --- a/IOT/lib/API/include/API.h +++ b/IOT/lib/API/include/API.h @@ -40,9 +40,7 @@ public: * @return false il y a une erreur durran l'envoie */ bool sendValue(String val, String poubelleID, String unit, bool full); - - -private: + /** * @brief connect l'utilisateur a l'api et met a jour le token * @@ -51,6 +49,11 @@ private: */ bool connect(); + //TODO :: Check wifibegin avant + +private: + + /** * @brief ID de l'utilisateur * diff --git a/IOT/lib/API/src/API.cpp b/IOT/lib/API/src/API.cpp index 89427f5..5af9675 100644 --- a/IOT/lib/API/src/API.cpp +++ b/IOT/lib/API/src/API.cpp @@ -59,7 +59,8 @@ bool API::connect(){ this->client->println(); - while (!client->available()) {} + while (!client->available()) { + } String responce = ""; while (client->available()) { @@ -77,7 +78,10 @@ bool API::connect(){ this->client->stop(); sortie = true; - + if (JSONVar::stringify(JSONVar::parse(str)["code"]) == "400") { + Serial.println("Failed to authenticate"); + return false; + } return sortie; } diff --git a/IOT/test/main.cpp b/IOT/test/main.cpp index 6683380..8fe4dc6 100644 --- a/IOT/test/main.cpp +++ b/IOT/test/main.cpp @@ -8,6 +8,8 @@ void setup() { Serial.begin(115200); RUN_TEST(TestWifiBeginConnected); RUN_TEST(TestWifiBeginNotConnected); + RUN_TEST(TestConnectAPI); + // RUN_TEST(TestConnectAPIFailed); UNITY_END(); diff --git a/IOT/test/test.h b/IOT/test/test.h index 6795825..2ef0746 100644 --- a/IOT/test/test.h +++ b/IOT/test/test.h @@ -3,5 +3,7 @@ void TestWifiBeginConnected(); void TestWifiBeginNotConnected(); +void TestConnectAPI(); +// void TestConnectAPIFailed(); #endif \ No newline at end of file diff --git a/IOT/test/test_api.cpp b/IOT/test/test_api.cpp index 0f08f49..98fc292 100644 --- a/IOT/test/test_api.cpp +++ b/IOT/test/test_api.cpp @@ -2,6 +2,8 @@ #include #include "API.h" + +//Testing WifiBegin function void TestWifiBeginConnected() { API* api = new API(USER_NAME, USER_PASSWORD, API_HOST); @@ -16,4 +18,24 @@ void TestWifiBeginNotConnected() { Serial2.begin(MONITOR_SPEED); TEST_ASSERT_EQUAL_MESSAGE(false, api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial2), "Wifi connected"); -} \ No newline at end of file +} + +//Testing Connect function +void TestConnectAPI() { + API* api = new API(USER_NAME, USER_PASSWORD, API_HOST); + + Serial1.begin(MONITOR_SPEED); + api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); + TEST_ASSERT_EQUAL_MESSAGE(true, api->connect(), "Not Connected"); +} + +// void TestConnectAPIFailed() { +// API* api = new API("Carl", "toto", API_HOST); + +// Serial1.begin(MONITOR_SPEED); +// api->wifiBegin(WIFI_SSID, WIFI_PASSWORD, &Serial1); +// TEST_ASSERT_EQUAL_MESSAGE(false, api->connect(), "Connected"); +// } + +//Testing SendValue function +