add test, fix errors

This commit is contained in:
einhornimmond 2021-06-17 11:48:05 +02:00
parent fdf70505f6
commit cba03dba09
3 changed files with 117 additions and 1 deletions

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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