def test_req_json_with_body():
    '''test req_json when there is a body'''
    baseResource = BaseResource()
    ctx = ShipyardRequestContext()

    json_body = json.dumps({
        'user': "******",
        'req_id': "test_req_id",
        'external_ctx': "test_ext_ctx",
        'name': "test_name"
    }).encode('utf-8')

    req = create_req(ctx, body=json_body)

    result = baseResource.req_json(req, validate_json_schema=ACTION)
    assert result == json.loads(json_body.decode('utf-8'))

    req = create_req(ctx, body=json_body)

    json_body = ('testing json').encode('utf-8')
    req = create_req(ctx, body=json_body)

    with pytest.raises(InvalidFormatError) as expected_exc:
        baseResource.req_json(req)
    assert 'JSON could not be decoded' in str(expected_exc)
    assert str(req.path) in str(expected_exc)
Пример #2
0
def test_add_roles():
    '''test add_roles'''
    ctx = ShipyardRequestContext()
    print(ctx.roles)
    test_roles = ['Waiter', 'Host', 'Chef']
    ctx.add_roles(test_roles)
    assert ['Chef', 'Host', 'Waiter', 'anyone'] == sorted(ctx.roles)
Пример #3
0
def test_to_policy_view():
    '''test to_policy_view'''
    ctx = ShipyardRequestContext()
    policy_dict = ctx.to_policy_view()
    assert policy_dict['user_id'] == ctx.user_id
    assert policy_dict['user_domain_id'] == ctx.user_domain_id
    assert policy_dict['project_domain_id'] == ctx.project_domain_id
    assert policy_dict['roles'] == ctx.roles
    assert policy_dict['is_admin_project'] == ctx.is_admin_project
def test_get_committed_design_version_missing(*args):
    with pytest.raises(ApiError) as apie:
        act_resource = ActionsResource()
        act_resource.configdocs_helper = ConfigdocsHelper(
            ShipyardRequestContext())
        act_resource.get_committed_design_version()
    assert apie.value.status == falcon.HTTP_404
    assert apie.value.title == ('Unable to locate any committed revision in '
                                'Deckhand')
def test_on_options():
    '''tests on_options'''
    baseResource = BaseResource()
    ctx = ShipyardRequestContext()

    req = create_req(ctx, body=None)
    resp = create_resp()

    baseResource.on_options(req, resp)
    assert 'OPTIONS' in resp.get_header('Allow')
    assert resp.status == '200 OK'
def test_req_json_no_body():
    '''test req_json when there is no body in the request'''
    baseResource = BaseResource()
    ctx = ShipyardRequestContext()

    req = create_req(ctx, body=None)

    with pytest.raises(InvalidFormatError) as expected_exc:
        baseResource.req_json(req, validate_json_schema=ACTION)
    assert 'Json body is required' in str(expected_exc)
    assert str(req.path) in str(expected_exc)

    result = baseResource.req_json(req, validate_json_schema=None)
    assert result is None
Пример #7
0
def test_get_committed_design_version_missing(*args):
    act_resource = ActionsResource()
    act_resource.configdocs_helper = ConfigdocsHelper(ShipyardRequestContext())
    assert act_resource.get_committed_design_version() is None
Пример #8
0
    Stub for inserting the invoke record
    """
    assert action_audit['command'] == 'invoke'


@pytest.fixture(scope='function')
def conf_fixture(request):
    def set_override(name, override, group):
        CONF = cfg.CONF
        CONF.set_override(name, override, group=group)
        request.addfinalizer(CONF.clear_override(name, group=group))

    return set_override


context = ShipyardRequestContext()


def test_actions_all_in_list():
    """Test that all actions are in alignment with supported list
    Compares the action mappings structure with the externalized list of
    supported actions, allowing for better alignment with the client
    """
    mappings = actions_api._action_mappings()
    actions = _get_actions_list()
    for action in actions:
        assert action in mappings
    for action in mappings.keys():
        assert action in actions

# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mock import patch

import pytest

from shipyard_airflow.control.base import ShipyardRequestContext
from shipyard_airflow.control.configdocs.rendered_configdocs_api import \
    RenderedConfigDocsResource
from shipyard_airflow.control.helpers.configdocs_helper import \
    ConfigdocsHelper
from shipyard_airflow.errors import ApiError

CTX = ShipyardRequestContext()


def test_validate_version_parameter():
    """
    test of the version parameter validation
    """
    rcdr = RenderedConfigDocsResource()
    with pytest.raises(ApiError):
        rcdr._validate_version_parameter('asdfjkl')

    try:
        rcdr._validate_version_parameter('buffer')
        rcdr._validate_version_parameter('committed')
    except:
        assert False
Пример #10
0
def test_get_action():
    """
    Tests the main response from get all actions
    """
    saved_control_dag_run = AIRFLOW_DB._control_dag_run
    try:
        action_resource = ActionsControlResource()
        # stubs for db
        action_resource.get_action_db = actions_db
        action_resource.audit_control_command_db = audit_control_command_db

        AIRFLOW_DB._control_dag_run = control_dag_run

        # bad action
        try:
            action_resource.handle_control(action_id='not found',
                                           control_verb='meep',
                                           context=ShipyardRequestContext())
            assert False, "shouldn't find the action"
        except ApiError as api_error:
            assert api_error.title == 'Action not found'
            assert api_error.status == '404 Not Found'

        # bad control
        try:
            action_resource.handle_control(
                action_id='59bb330a-9e64-49be-a586-d253bb67d443',
                control_verb='meep',
                context=ShipyardRequestContext())
            assert False, 'meep is not a valid action'
        except ApiError as api_error:
            assert api_error.title == 'Control not supported'
            assert api_error.status == '404 Not Found'

        # success on each action - pause, unpause, stop
        try:
            action_resource.handle_control(
                action_id='59bb330a-9e64-49be-a586-d253bb67d443',
                control_verb='pause',
                context=ShipyardRequestContext())
        except ApiError as api_error:
            assert False, 'Should not raise an ApiError'

        try:
            action_resource.handle_control(
                action_id='59bb330a-9e64-49be-a586-d253bb67d443',
                control_verb='unpause',
                context=ShipyardRequestContext())
        except ApiError as api_error:
            assert False, 'Should not raise an ApiError'

        try:
            action_resource.handle_control(
                action_id='59bb330a-9e64-49be-a586-d253bb67d443',
                control_verb='stop',
                context=ShipyardRequestContext())
        except ApiError as api_error:
            assert False, 'Should not raise an ApiError'

        # pause state conflict
        try:
            action_resource.handle_control(action_id='state error',
                                           control_verb='pause',
                                           context=ShipyardRequestContext())
            assert False, 'should raise a conflicting state'
        except ApiError as api_error:
            assert api_error.title == 'Unable to pause action'
            assert api_error.status == '409 Conflict'

        # Unpause state conflict
        try:
            action_resource.handle_control(action_id='state error',
                                           control_verb='unpause',
                                           context=ShipyardRequestContext())
            assert False, 'should raise a conflicting state'
        except ApiError as api_error:
            assert api_error.title == 'Unable to unpause action'
            assert api_error.status == '409 Conflict'

        # Stop state conflict
        try:
            action_resource.handle_control(action_id='state error',
                                           control_verb='stop',
                                           context=ShipyardRequestContext())
            assert False, 'should raise a conflicting state'
        except ApiError as api_error:
            assert api_error.title == 'Unable to stop action'
            assert api_error.status == '409 Conflict'
    finally:
        # modified class variable... replace it
        AIRFLOW_DB._control_dag_run = saved_control_dag_run
Пример #11
0
def test_set_policy_engine():
    '''test set_policy_engine'''
    ctx = ShipyardRequestContext()
    ctx.set_policy_engine('test_policy_engine')
    assert ctx.policy_engine == 'test_policy_engine'
Пример #12
0
def test_set_external_marker():
    '''test set_external_marker'''
    ctx = ShipyardRequestContext()
    ctx.set_external_marker('test_ext_marker')
    assert ctx.external_marker == 'test_ext_marker'
Пример #13
0
def test_remove_roles():
    '''test remove_roles'''
    ctx = ShipyardRequestContext()
    ctx.remove_role('anyone')
    assert ctx.roles == []
Пример #14
0
def test_add_role():
    '''test add_role'''
    ctx = ShipyardRequestContext()
    ctx.add_role('test_role')
    assert 'test_role' in ctx.roles
Пример #15
0
def test_set_project():
    '''test set_project'''
    ctx = ShipyardRequestContext()
    ctx.set_project('test_project')
    assert ctx.project == 'test_project'
Пример #16
0
def test_set_user():
    '''test set_user '''
    ctx = ShipyardRequestContext()
    ctx.set_user('test_user')
    assert ctx.user == 'test_user'