Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VIRTS-2877] Objectives api v2 Pytests #2283

Merged
merged 22 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1a0df7e
add tests for objectives api
bleepbop Sep 24, 2021
f58926d
code cleanup
bleepbop Sep 24, 2021
f73a0a2
resolve conflicts
bleepbop Sep 27, 2021
f2ccc6d
objectives unit tests
bleepbop Sep 27, 2021
b59cc65
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Sep 28, 2021
5a91322
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Sep 28, 2021
b572fe9
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Sep 29, 2021
45f619a
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Sep 30, 2021
c781248
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Sep 30, 2021
b6ccf34
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 4, 2021
a02466e
update fixtures
bleepbop Oct 4, 2021
be2ff6d
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 4, 2021
b03c07e
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 4, 2021
3f65c94
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 5, 2021
f7b6d27
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 5, 2021
b7d0ff1
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 5, 2021
8baa460
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 5, 2021
ad2025e
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 6, 2021
70de666
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 6, 2021
9844b7b
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 6, 2021
774880d
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 6, 2021
55367b7
Merge branch 'master' into bleepbop/VIRTS-2877/objectives-api-v2-pytest
bleepbop Oct 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions tests/api/v2/handlers/test_objectives_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import pytest

from http import HTTPStatus

from app.objects.c_objective import Objective, ObjectiveSchema
from app.objects.secondclass.c_goal import Goal
from app.utility.base_service import BaseService


@pytest.fixture
def new_objective_payload():
test_goal = Goal(target='new goal', value='in_progress')
return {
'id': '456',
'name': 'new test objective',
'description': 'a new test objective',
'goals': [test_goal.schema.dump(test_goal)]
}


@pytest.fixture
def expected_new_objective_dump(new_objective_payload):
objective = ObjectiveSchema().load(new_objective_payload)
return objective.schema.dump(objective)


@pytest.fixture
def updated_objective_payload(test_objective, test_goal):
objective_data = test_objective.schema.dump(test_objective)
updated_goal = Goal(target='updated target', value='complete')
objective_data.update(dict(name='an updated test objective',
description='a test objective that has been updated',
goals=[updated_goal.schema.dump(updated_goal)]))
return objective_data


@pytest.fixture
def replaced_objective_payload(test_objective):
objective_data = test_objective.schema.dump(test_objective)
test_goal = Goal(target='replaced target', value='in_progress')
objective_data.update(dict(name='replaced test objective',
description='a test objective that has been replaced',
goals=[test_goal.schema.dump(test_goal)]))
return objective_data


@pytest.fixture
def test_goal():
return Goal(target='test target', value='in_progress')


@pytest.fixture
def test_objective(loop, test_goal):
objective = Objective(id='123', name='test objective', description='a test objective', goals=[test_goal])
loop.run_until_complete(BaseService.get_service('data_svc').store(objective))
return objective


@pytest.fixture
def expected_test_objective_dump(test_objective):
return test_objective.schema.dump(test_objective)


class TestObjectivesApi:
async def test_get_objectives(self, api_v2_client, api_cookies, test_objective, expected_test_objective_dump):
resp = await api_v2_client.get('/api/v2/objectives', cookies=api_cookies)
objectives_list = await resp.json()
assert len(objectives_list) == 1
objective_dict = objectives_list[0]
assert objective_dict == expected_test_objective_dump

async def test_unauthorized_get_objectives(self, api_v2_client, test_objective):
resp = await api_v2_client.get('/api/v2/objectives')
assert resp.status == HTTPStatus.UNAUTHORIZED

async def test_get_objective_by_id(self, api_v2_client, api_cookies, test_objective, expected_test_objective_dump):
resp = await api_v2_client.get('/api/v2/objectives/123', cookies=api_cookies)
objective_dict = await resp.json()
assert objective_dict == expected_test_objective_dump

async def test_unauthorized_get_objective_by_id(self, api_v2_client, test_objective):
resp = await api_v2_client.get('/api/v2/objectives/123')
assert resp.status == HTTPStatus.UNAUTHORIZED

async def test_get_nonexistent_objective_by_id(self, api_v2_client, api_cookies):
resp = await api_v2_client.get('/api/v2/objectives/999', cookies=api_cookies)
assert resp.status == HTTPStatus.NOT_FOUND

async def test_create_objective(self, api_v2_client, api_cookies, new_objective_payload,
expected_new_objective_dump):
resp = await api_v2_client.post('/api/v2/objectives', cookies=api_cookies, json=new_objective_payload)
assert resp.status == HTTPStatus.OK
objective_data = await resp.json()
assert objective_data == expected_new_objective_dump
stored_objective = (await BaseService.get_service('data_svc').locate('objectives', {'id': '456'}))[0]
assert stored_objective.schema.dump(stored_objective) == expected_new_objective_dump

async def test_unauthorized_create_objective(self, api_v2_client, new_objective_payload):
resp = await api_v2_client.post('/api/v2/objectives', json=new_objective_payload)
assert resp.status == HTTPStatus.UNAUTHORIZED

async def test_create_duplicate_objective(self, api_v2_client, api_cookies, test_objective):
payload = test_objective.schema.dump(test_objective)
resp = await api_v2_client.post('/api/v2/objectives', cookies=api_cookies, json=payload)
assert resp.status == HTTPStatus.BAD_REQUEST

async def test_update_objective(self, api_v2_client, api_cookies, test_objective, updated_objective_payload,
mocker):
with mocker.patch('app.api.v2.managers.base_api_manager.BaseApiManager.strip_yml') as mock_strip_yml:
mock_strip_yml.return_value = [test_objective.schema.dump(test_objective)]
resp = await api_v2_client.patch('/api/v2/objectives/123', cookies=api_cookies,
json=updated_objective_payload)
assert resp.status == HTTPStatus.OK
objective = await resp.json()
assert objective == updated_objective_payload
stored_objective = (await BaseService.get_service('data_svc').locate('objectives', {'id': '123'}))[0]
assert stored_objective.schema.dump(stored_objective) == updated_objective_payload

async def test_unauthorized_update_objective(self, api_v2_client, test_objective, updated_objective_payload):
resp = await api_v2_client.patch('/api/v2/objectives/123', json=updated_objective_payload)
assert resp.status == HTTPStatus.UNAUTHORIZED

async def test_update_nonexistent_objective(self, api_v2_client, api_cookies, updated_objective_payload):
resp = await api_v2_client.patch('/api/v2/objectives/999', cookies=api_cookies, json=updated_objective_payload)
assert resp.status == HTTPStatus.NOT_FOUND

async def test_replace_objective(self, api_v2_client, api_cookies, test_objective, replaced_objective_payload):
resp = await api_v2_client.put('/api/v2/objectives/123', cookies=api_cookies, json=replaced_objective_payload)
assert resp.status == HTTPStatus.OK
objective = await resp.json()
assert objective == replaced_objective_payload
stored_objective = (await BaseService.get_service('data_svc').locate('objectives', {'id': '123'}))[0]
assert stored_objective.schema.dump(stored_objective) == replaced_objective_payload

async def test_unauthorized_replace_objective(self, api_v2_client, test_objective, replaced_objective_payload):
resp = await api_v2_client.put('/api/v2/objectives/123', json=replaced_objective_payload)
assert resp.status == HTTPStatus.UNAUTHORIZED

async def test_replace_nonexistent_objective(self, api_v2_client, api_cookies, new_objective_payload,
expected_new_objective_dump):
resp = await api_v2_client.put('/api/v2/objectives/456', cookies=api_cookies, json=new_objective_payload)
assert resp.status == HTTPStatus.OK
objective = await resp.json()
assert objective == expected_new_objective_dump
stored_objective = (await BaseService.get_service('data_svc').locate('objectives', {'id': '456'}))[0]
assert stored_objective.schema.dump(stored_objective) == expected_new_objective_dump
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pathlib import Path

from app.api.v2.handlers.ability_api import AbilityApi
from app.api.v2.handlers.objective_api import ObjectiveApi
from app.api.v2.handlers.adversary_api import AdversaryApi
from app.api.v2.handlers.operation_api import OperationApi
from app.api.v2.handlers.contact_api import ContactApi
Expand Down Expand Up @@ -274,6 +275,7 @@ def make_app(svcs):
OperationApi(svcs).add_routes(app)
AdversaryApi(svcs).add_routes(app)
ContactApi(svcs).add_routes(app)
ObjectiveApi(svcs).add_routes(app)
ObfuscatorApi(svcs).add_routes(app)
return app

Expand Down