Ejemplo n.º 1
0
def testAssignJob(client, sample_Job, sample_Library, sample_Agent, sample_valid_JWT):
    # prepare mongomock with relevant sample documents
    agent = sample_Agent
    agent.save()
    library = sample_Library
    library["jobs"].append(sample_Job)
    library.save()
    # send job to api
    response = client.post("/agent/jobs",
                           headers={"Content-Type": "application/json",
                                    "Authorization": "Bearer " + sample_valid_JWT},
                           data=json.dumps({"agentID": sample_Agent["agentID"],
                                 "filename": sample_Job["filename"],
                                 "argv": []}))
    assert response.status_code == 200
    assert response.json["success"] == "job successfully submitted -- waiting for agent to check in"
    # check in and make sure job is in agent's queue now
    agentCheckin.__wrapped__(MockServer(agent["agentID"]))
    # make sure all job fields were included from the sample job
    agent.reload()
    job = agent.jobsRunning[0]
    assert job["executor"] == sample_Job["executor"]
    assert job["filename"] == sample_Job["filename"]
    assert job["description"] == sample_Job["description"]
    assert job["os"] == sample_Job["os"]
    assert job["user"] == sample_Job["user"]
    # make sure timeDispatched was created
    createdTimestamp = job["timeCreated"]
    createdTime = timestampToDatetime(createdTimestamp)
    dispatchTimestamp = job["timeDispatched"]
    dispatchTime = timestampToDatetime(dispatchTimestamp)
    assert dispatchTime >= createdTime
Ejemplo n.º 2
0
def testUnknownAgentCheckin(sample_Agent):
    # prepare mongomock with relevant sample documents
    agent = sample_Agent
    agent.save()
    # check in with api server
    error = agentCheckin.__wrapped__(MockServer("not-a-real-agent"))
    assert error["error"] == "agent ID not found, please check ID or register"
Ejemplo n.º 3
0
def testBadRequestCheckin(sample_Agent):
    # prepare mongomock with relevant sample documents
    agent = sample_Agent
    agent.save()
    # check in with api server
    error = agentCheckin.__wrapped__(MockServer("bad-request"))
    assert error["error"] == "message was not a valid json object"
Ejemplo n.º 4
0
def testMissingFieldsCheckin(sample_Agent):
    # prepare mongomock with relevant sample documents
    agent = sample_Agent
    agent.save()
    # check in with api server
    error = agentCheckin.__wrapped__(MockServer("missing-fields"))
    assert error[
        "error"] == "request is missing the following parameters: headers=['Agent-ID']"
Ejemplo n.º 5
0
def testAvailableJobCheckin(sample_Agent, sample_Job):
    # prepare mongomock with relevant sample documents
    agent = sample_Agent
    agent["jobsQueue"].append(sample_Job)
    agent.save()
    jobsCache.refresh()  # because we added the job to the DB manually
    # check in with api server
    agentCheckin.__wrapped__(MockServer(agent["agentID"]))
    # make sure all job fields were included from the sample job
    agent.reload()
    job = agent.jobsRunning[0]
    assert job["executor"] == sample_Job["executor"]
    assert job["filename"] == sample_Job["filename"]
    assert job["description"] == sample_Job["description"]
    assert job["os"] == sample_Job["os"]
    assert job["user"] == sample_Job["user"]
    createdTimestamp = job["timeCreated"]
    createdTime = timestampToDatetime(createdTimestamp)
    assert createdTime == timestampToDatetime(sample_Job["timeCreated"])
    # make sure timeDispatched was created
    dispatchTimestamp = job["timeDispatched"]
    dispatchTime = timestampToDatetime(dispatchTimestamp)
    assert dispatchTime >= createdTime