diff --git a/login_server/src/cpp/JSONInterface/JsonResetPassword.cpp b/login_server/src/cpp/JSONInterface/JsonResetPassword.cpp index 2b0208681..ac1606ab5 100644 --- a/login_server/src/cpp/JSONInterface/JsonResetPassword.cpp +++ b/login_server/src/cpp/JSONInterface/JsonResetPassword.cpp @@ -19,6 +19,7 @@ Poco::JSON::Object* JsonResetPassword::handle(Poco::Dynamic::Var params) if (password_obj.isEmpty()) { return stateError("password missing"); } + password_obj.convert(password); } catch (Poco::Exception& ex) { return stateError("error parsing json", ex.what()); @@ -37,7 +38,7 @@ Poco::JSON::Object* JsonResetPassword::handle(Poco::Dynamic::Var params) auto observer = SingletonTaskObserver::getInstance(); auto email_hash = observer->makeHash(user->getModel()->getEmail()); - if (observer->getTaskCount(email_hash, TASK_OBSERVER_PASSWORD_CREATION)) { + if (observer->getTaskCount(email_hash, TASK_OBSERVER_PASSWORD_CREATION) > 0) { return stateError("password encryption is already running"); } diff --git a/login_server/src/cpp/test/JSONInterface/TestJsonResetPassword.cpp b/login_server/src/cpp/test/JSONInterface/TestJsonResetPassword.cpp new file mode 100644 index 000000000..30218365c --- /dev/null +++ b/login_server/src/cpp/test/JSONInterface/TestJsonResetPassword.cpp @@ -0,0 +1,95 @@ +#include "gtest/gtest.h" + +#include "JSONInterface/JsonResetPassword.h" +#include "TestJsonResetPassword.h" +#include "lib/Profiler.h" + + +void TestJsonResetPassword::SetUp() +{ + auto sm = SessionManager::getInstance(); + //sm->init(); + mUserSession = sm->getNewSession(); + auto user = controller::User::create(); + user->load("Nikola_Tesla@email.de"); + mUserSession->setUser(user); +} + +void TestJsonResetPassword::TearDown() +{ + auto sm = SessionManager::getInstance(); + if (!mUserSession) { + sm->releaseSession(mUserSession); + } +} + + +TEST_F(TestJsonResetPassword, WithoutSession) +{ + JsonResetPassword jsonCall; + Poco::JSON::Object::Ptr params = new Poco::JSON::Object; + params->set("password", "ashze_Sja/63"); + auto result = jsonCall.handle(params); + + auto state = result->get("state"); + ASSERT_FALSE(state.isEmpty()); + ASSERT_TRUE(state.isString()); + ASSERT_EQ(state.toString(), "error"); + + auto msg = result->get("msg"); + ASSERT_FALSE(msg.isEmpty()); + ASSERT_TRUE(msg.isString()); + ASSERT_EQ(msg.toString(), "missing session_id"); + +} + +TEST_F(TestJsonResetPassword, WithoutPassword) +{ + JsonResetPassword jsonCall; + Poco::JSON::Object::Ptr params = new Poco::JSON::Object; + params->set("session_id", mUserSession->getHandle()); + auto result = jsonCall.handle(params); + + auto state = result->get("state"); + ASSERT_FALSE(state.isEmpty()); + ASSERT_TRUE(state.isString()); + ASSERT_EQ(state.toString(), "error"); + + auto msg = result->get("msg"); + ASSERT_FALSE(msg.isEmpty()); + ASSERT_TRUE(msg.isString()); + ASSERT_EQ(msg.toString(), "password missing"); +} + +TEST_F(TestJsonResetPassword, InvalidPassword) +{ + JsonResetPassword jsonCall; + Poco::JSON::Object::Ptr params = new Poco::JSON::Object; + params->set("session_id", mUserSession->getHandle()); + params->set("password", "ash"); + auto result = jsonCall.handle(params); + + auto state = result->get("state"); + ASSERT_FALSE(state.isEmpty()); + ASSERT_TRUE(state.isString()); + ASSERT_EQ(state.toString(), "error"); + + auto msg = result->get("msg"); + ASSERT_FALSE(msg.isEmpty()); + ASSERT_TRUE(msg.isString()); + ASSERT_EQ(msg.toString(), "password isn't valid"); +} + +TEST_F(TestJsonResetPassword, ValidPassword) +{ + JsonResetPassword jsonCall; + Poco::JSON::Object::Ptr params = new Poco::JSON::Object; + params->set("session_id", mUserSession->getHandle()); + params->set("password", "hath6/&Sja"); + auto result = jsonCall.handle(params); + + auto state = result->get("state"); + ASSERT_FALSE(state.isEmpty()); + ASSERT_TRUE(state.isString()); + ASSERT_EQ(state.toString(), "success"); +} diff --git a/login_server/src/cpp/test/JSONInterface/TestJsonResetPassword.h b/login_server/src/cpp/test/JSONInterface/TestJsonResetPassword.h new file mode 100644 index 000000000..b8375bd90 --- /dev/null +++ b/login_server/src/cpp/test/JSONInterface/TestJsonResetPassword.h @@ -0,0 +1,20 @@ +#ifndef __GRADIDO_LOGIN_SERVER_TEST_JSON_INTERFACE_TEST_JSON_RESET_PASSWORD_H +#define __GRADIDO_LOGIN_SERVER_TEST_JSON_INTERFACE_TEST_JSON_RESET_PASSWORD_H + +#include "gtest/gtest.h" +#include "SingletonManager/SessionManager.h" + +#include "Poco/JSON/Object.h" + +class TestJsonResetPassword : public ::testing::Test +{ + +protected: + void SetUp() override; + void TearDown() override; + + Session* mUserSession; + +}; + +#endif //__GRADIDO_LOGIN_SERVER_TEST_JSON_INTERFACE_TEST_JSON_RESET_PASSWORD_H \ No newline at end of file