aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/unit_python/api/test_policies.py
blob: cd50f4c77151f9cf3bc7b5054304154e6028cd57 (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
73
74
75
76
77
# Copyright 2018 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
from uuid import uuid4
import api.utilities as utilities
from helpers import model_helper


def get_policies(client):
    req = client.get("/policies")
    policies = utilities.get_json(req.data)
    return req, policies


def add_policies(client, name):
    req = model_helper.add_model(model_id="mls_model_id"+uuid4().hex)
    model_id = list(req.keys())[0]
    data = {
        "name": name,
        "description": "description of {}".format(name),
        "model_id": model_id,
        "genre": "genre"
    }
    req = client.post("/policies", data=json.dumps(data),
                      headers={'Content-Type': 'application/json'})
    policies = utilities.get_json(req.data)
    return req, policies


def delete_policies(client, name):
    request, policies = get_policies(client)
    for key, value in policies['policies'].items():
        req = client.delete("/policies/{}".format(key))
        break
    return req


def delete_policies_without_id(client):
    req = client.delete("/policies/{}".format(""))
    return req


def test_get_policies():
    client = utilities.register_client()
    req, policies = get_policies(client)
    assert req.status_code == 200
    assert isinstance(policies, dict)
    assert "policies" in policies


def test_add_policies():
    policy_name = "testuser" + uuid4().hex
    client = utilities.register_client()
    req, policies = add_policies(client, policy_name)
    assert req.status_code == 200
    assert isinstance(policies, dict)
    value = list(policies["policies"].values())[0]
    assert "policies" in policies
    assert value['name'] == policy_name
    assert value["description"] == "description of {}".format(policy_name)


def test_delete_policies():
    client = utilities.register_client()
    req = delete_policies(client, "testuser")
    assert req.status_code == 200


def test_delete_policies_without_id():
    client = utilities.register_client()
    req = delete_policies_without_id(client)
    assert req.status_code == 400
    assert json.loads(req.data)["message"] == '400: Policy Unknown'