1import json
2
3import pytest
4
5import stix2
6import stix2.exceptions
7import stix2.utils
8
9COA_WITH_BIN_JSON = """{
10    "type": "course-of-action",
11    "spec_version": "2.1",
12    "id": "course-of-action--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
13    "created_by_ref": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
14    "created": "2016-04-06T20:03:48.000Z",
15    "modified": "2016-04-06T20:03:48.000Z",
16    "name": "Add TCP port 80 Filter Rule to the existing Block UDP 1434 Filter",
17    "description": "This is how to add a filter rule to block inbound access to TCP port 80 to the existing UDP 1434 filter ..."
18}"""
19
20
21COA_WITH_REF_JSON = """{
22    "type": "course-of-action",
23    "spec_version": "2.1",
24    "id": "course-of-action--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
25    "created_by_ref": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
26    "created": "2016-04-06T20:03:48.000Z",
27    "modified": "2016-04-06T20:03:48.000Z",
28    "name": "Add TCP port 80 Filter Rule to the existing Block UDP 1434 Filter",
29    "description": "This is how to add a filter rule to block inbound access to TCP port 80 to the existing UDP 1434 filter ..."
30}"""
31
32
33COA_WITH_BIN_DICT = json.loads(COA_WITH_BIN_JSON)
34COA_WITH_REF_DICT = json.loads(COA_WITH_REF_JSON)
35
36
37@pytest.mark.parametrize(
38    "sdo_json,sdo_dict", [
39        (COA_WITH_BIN_JSON, COA_WITH_BIN_DICT),
40        (COA_WITH_REF_JSON, COA_WITH_REF_DICT),
41    ],
42)
43def test_course_of_action_example(sdo_json, sdo_dict):
44    coa = stix2.v21.CourseOfAction(**sdo_dict)
45    assert str(coa) == sdo_json
46
47
48@pytest.mark.parametrize(
49    "sdo_json,sdo_dict", [
50        (COA_WITH_BIN_JSON, COA_WITH_BIN_DICT),
51        (COA_WITH_REF_JSON, COA_WITH_REF_DICT),
52    ],
53)
54def test_parse_course_of_action(sdo_json, sdo_dict):
55
56    # Names of timestamp-valued attributes
57    ts_attrs = {"created", "modified"}
58
59    for data in (sdo_json, sdo_dict):
60        coa = stix2.parse(data, version="2.1")
61
62        # sdo_dict is handy as a source of attribute names/values to check
63        for attr_name, attr_value in sdo_dict.items():
64            cmp_value = stix2.utils.parse_into_datetime(attr_value) \
65                if attr_name in ts_attrs else attr_value
66
67            assert getattr(coa, attr_name) == cmp_value
68
69
70# TODO: Add other examples
71