ソースを参照

Resolve "US21.1: User deletion (Server)"

Pflanzer, Jonas 5 年 前
コミット
0015e7cd9f

+ 1 - 1
daemon/src/JsonCommander.cpp

@@ -392,7 +392,7 @@ JsonCommander::Response JsonCommander::executeDeleteMe(const Json::Value &messag
 	response.json["command"] = "deleteme";
 
 	if (!message["pass"].isString()) {
-		response.action = send;
+		response.action = closeAndSend;
 		response.json["accept"] = false;
 		response.json["error"] = "incorrect deleteme command request";
 	} else if (UserManager::deleteUser(currentUser, message["pass"].asString())) {

+ 1 - 1
daemon/test/CMakeLists.txt

@@ -12,7 +12,7 @@ find_package(GMock REQUIRED)
 include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})
 
 # Add test cpp file
-add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp src/UserManager.cpp test/ConfigMock.cpp)
+add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp test/ConfigMock.cpp test/UserManagerMock.cpp)
 target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GTEST_BOTH_LIBRARIES} ${GMOCK_BOTH_LIBRARIES})
 
 add_test(

+ 93 - 0
daemon/test/JsonCommanderTest.cpp

@@ -969,4 +969,97 @@ TEST(Head, FileTooSmall) {
 	EXPECT_NE(response.json["error"].asString(), "");
 }
 
+TEST(Deleteme, Positive) {
+	FileManagerMock fileManager;
+
+	JsonCommander jsonCommander(fileManager);
+
+	// need to set currentUser in jsonCommander via calling checkLogin
+	Json::Value login;
+	login["login"] = true;
+	login["user"] = "positive";
+	login["pass"] = "positive";
+	login["cancel"] = false;
+
+	JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
+	EXPECT_TRUE(loginRes.json["accept"].asBool());
+	EXPECT_EQ(loginRes.json["error"].asString(), "");
+
+	// now the actual test
+	const std::string command = "deleteme";
+
+	Json::Value message;
+	message["command"] = command;
+	message["pass"] = "positive";
+
+	JsonCommander::Response response = jsonCommander.execute(message);
+	EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
+	EXPECT_EQ(response.json["command"].asString(), command);
+	EXPECT_TRUE(response.json["accept"].asBool());
+	EXPECT_EQ(response.json["error"].asString(), "");
+}
+
+TEST(Deleteme, Negative) {
+	FileManagerMock fileManager;
+
+	JsonCommander jsonCommander(fileManager);
+
+	// need to set currentUser in jsonCommander via calling checkLogin
+	Json::Value login;
+	login["login"] = true;
+	login["user"] = "positive";
+	login["pass"] = "positive";
+	login["cancel"] = false;
+
+	JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
+	EXPECT_TRUE(loginRes.json["accept"].asBool());
+	EXPECT_EQ(loginRes.json["error"].asString(), "");
+
+	// now the actual test
+	const std::string command = "deleteme";
+
+	Json::Value message;
+	message["command"] = command;
+	message["pass"] = "negative";
+
+	JsonCommander::Response response = jsonCommander.execute(message);
+
+	std::cout << response.json << std::endl;
+
+	EXPECT_EQ(response.action, JsonCommander::Action::send);
+	EXPECT_EQ(response.json["command"].asString(), command);
+	EXPECT_FALSE(response.json["accept"].asBool());
+	EXPECT_NE(response.json["error"].asString(), "");
+}
+
+TEST(Deleteme, InvalidRequest) {
+	FileManagerMock fileManager;
+
+	JsonCommander jsonCommander(fileManager);
+
+	// need to set currentUser in jsonCommander via calling checkLogin
+	Json::Value login;
+	login["login"] = true;
+	login["user"] = "positive";
+	login["pass"] = "positive";
+	login["cancel"] = false;
+
+	JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
+	EXPECT_TRUE(loginRes.json["accept"].asBool());
+	EXPECT_EQ(loginRes.json["error"].asString(), "");
+
+	// now the actual test
+	const std::string command = "deleteme";
+
+	Json::Value message;
+	message["command"] = command;
+	message["pass"] = 123;
+
+	JsonCommander::Response response = jsonCommander.execute(message);
+	EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
+	EXPECT_EQ(response.json["command"].asString(), command);
+	EXPECT_FALSE(response.json["accept"].asBool());
+	EXPECT_NE(response.json["error"].asString(), "");
+}
+
 } // namespace

+ 13 - 0
daemon/test/UserManagerMock.cpp

@@ -0,0 +1,13 @@
+#include "../include/UserManager.h"
+
+void UserManager::init(const std::string &file) {}
+
+bool UserManager::isAllowed(const std::string &name, const std::string &pw) { return name == "positive" && pw == "positive"; }
+
+bool UserManager::addUser(const std::string &name, const std::string &pw) { return name == "positive" && pw == "positive"; }
+
+bool UserManager::deleteUser(const std::string &name, const std::string &pw) { return name == "positive" && pw == "positive"; }
+
+void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {}
+
+void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {}