Skip to content

Commit

Permalink
added helper functions for assigning and unassigning agents from tasks (
Browse files Browse the repository at this point in the history
#1044)

* added helper functions for assigning and unassigning agents from tasks

* pass delete=True for test object creation

* call tearDown for other test class manually to clean up
  • Loading branch information
s3inlc committed Feb 24, 2024
1 parent 1c09b99 commit be80815
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
15 changes: 15 additions & 0 deletions ci/apiv2/hashtopolis.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,18 @@ def import_cracked_hashes(self, hashlist, source_data, separator):
}
response = self._helper_request("importCrackedHashes", payload)
return response['data']

def unassign_agent(self, agent):
payload = {
'agentId': agent.id,
}
response = self._helper_request("unassignAgent", payload)
return response['data']

def assign_agent(self, agent, task):
payload = {
'agentId': agent.id,
'taskId': task.id,
}
response = self._helper_request("assignAgent", payload)
return response['data']
21 changes: 20 additions & 1 deletion ci/apiv2/test_agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from hashtopolis import Agent
from test_task import TaskTest
from hashtopolis import Agent, Helper
from hashtopolis import HashtopolisError

from utils import BaseTest
Expand Down Expand Up @@ -28,3 +29,21 @@ def test_expandables(self):
model_obj = self.create_test_object()
expandables = ['accessGroups', 'agentstats']
self._test_expandables(model_obj, expandables)

def test_assign_unassign_agent(self):
agent_obj = self.create_test_object()

task_test = TaskTest()
task_obj = task_test.create_test_object(delete=True)

helper = Helper()

result = helper.assign_agent(agent=agent_obj, task=task_obj)

self.assertEqual(result['assign'], 'success')

result = helper.unassign_agent(agent=agent_obj)

self.assertEqual(result['unassign'], 'success')

task_test.tearDown()
2 changes: 2 additions & 0 deletions src/api/v2/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public function process(Request $request, RequestHandler $handler): Response {
require __DIR__ . "/../../inc/apiv2/model/vouchers.routes.php";

require __DIR__ . "/../../inc/apiv2/helper/abortChunk.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/assignAgent.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/createSupertask.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/createSuperHashlist.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/exportCrackedHashes.routes.php";
Expand All @@ -277,5 +278,6 @@ public function process(Request $request, RequestHandler $handler): Response {
require __DIR__ . "/../../inc/apiv2/helper/purgeTask.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/resetChunk.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/setUserPassword.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/unassignAgent.routes.php";

$app->run();
36 changes: 36 additions & 0 deletions src/inc/apiv2/helper/assignAgent.routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use DBA\Agent;
use DBA\Task;

require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php");

class AssignAgentHelperAPI extends AbstractHelperAPI {
public static function getBaseUri(): string {
return "/api/v2/helper/assignAgent";
}

public static function getAvailableMethods(): array {
return ['POST'];
}

public function getRequiredPermissions(string $method): array {
return [Agent::PERM_UPDATE, Task::PERM_UPDATE];
}

public function getFormFields(): array {
return [
Agent::AGENT_ID => ["type" => "int"],
Task::TASK_ID => ["type" => "int"],
];
}

public function actionPost($data): array|null {
AgentUtils::assign($data[Agent::AGENT_ID], $data[Task::TASK_ID], $this->getCurrentUser());

# TODO: Check how to handle custom return messages that are not object, probably we want that to be in some kind of standardized form.
return ["assign" => "success"];
}
}

AssignAgentHelperAPI::register($app);
35 changes: 35 additions & 0 deletions src/inc/apiv2/helper/unassignAgent.routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use DBA\Agent;
use DBA\Task;

require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php");

class UnassignAgentHelperAPI extends AbstractHelperAPI {
public static function getBaseUri(): string {
return "/api/v2/helper/unassignAgent";
}

public static function getAvailableMethods(): array {
return ['POST'];
}

public function getRequiredPermissions(string $method): array {
return [Agent::PERM_UPDATE, Task::PERM_UPDATE];
}

public function getFormFields(): array {
return [
Agent::AGENT_ID => ["type" => "int"],
];
}

public function actionPost($data): array|null {
AgentUtils::assign($data[Agent::AGENT_ID], 0, $this->getCurrentUser());

# TODO: Check how to handle custom return messages that are not object, probably we want that to be in some kind of standardized form.
return ["unassign" => "success"];
}
}

UnassignAgentHelperAPI::register($app);

0 comments on commit be80815

Please sign in to comment.