aboutsummaryrefslogtreecommitdiffstats
path: root/moon_wrapper/tests/unit_python/api/test_wrapper.py
blob: bd6baf3234619ace456f0f4eaa2d8837956104b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import json


def get_json(data):
    return json.loads(data.decode("utf-8"))


def test_authz_true(context):
    import moon_wrapper.server
    server = moon_wrapper.server.main()
    client = server.app.test_client()
    _target = {
        'target': {
            "name": context.get('object_name'),
        },
        "project_id": context.get('project_id'),
        "user_id": context.get('subject_name')
    }
    authz_data = {
        'rule': context.get('action_name'),
        'target': json.dumps(_target),
        'credentials': 'null'}
    req = client.post("/authz/oslo", data=json.dumps(authz_data))
    assert req.status_code is 200
    assert req.data
    assert isinstance(req.data, bytes)
    assert req.data == b"True"

def test_authz_error_response_code(context):
    import moon_wrapper.server
    server = moon_wrapper.server.main()
    client = server.app.test_client()
    _target = {
        'target': {
            "name": context.get('object_name'),
        },
        "project_id": context.get('invalid_project_id'),
        "user_id": context.get('subject_name')
    }
    authz_data = {
        'rule': context.get('action_name'),
        'target': json.dumps(_target),
        'credentials': 'null'}
    req = client.post("/authz/oslo", data=json.dumps(authz_data))
    assert req.status_code is 200
    assert req.data
    assert isinstance(req.data, bytes)
    assert req.data == b"False"

def test_authz_error_no_interface_key(context):
    import moon_wrapper.server
    server = moon_wrapper.server.main()
    client = server.app.test_client()
    _target = {
        'target': {
            "name": context.get('object_name'),
        },
        "project_id": context.get('project_with_no_interface_key'),
        "user_id": context.get('subject_name')
    }
    authz_data = {
        'rule': context.get('action_name'),
        'target': json.dumps(_target),
        'credentials': 'null'}
    req = client.post("/authz/oslo", data=json.dumps(authz_data))

    assert req.data == b"False"