def test_constructor(): from security_monkey.common.PolicyDiff import PolicyDiff try: PolicyDiff("{badjson}", None) assert False except ValueError: pass try: PolicyDiff(None, "{badjson}") assert False except ValueError: pass try: PolicyDiff({}, []) assert False except ValueError: pass import collections PolicyDiff(collections.defaultdict(), collections.defaultdict())
def get(self, revision_id): """ .. http:get:: /api/1/revision/1234 Get a specific revision. **Example Request**: .. sourcecode:: http GET /api/1/revision/123 HTTP/1.1 Host: example.com Accept: application/json, text/javascript **Example Response**: .. sourcecode:: http HTTP/1.1 200 OK Vary: Accept Content-Type: application/json { "auth": { "authenticated": true, "user": "******" }, "item_id": 114, "comments": [], "active": false, "date_created": "2013-10-04 22:01:47", "config": {}, "id":123 } :statuscode 200: no error :statuscode 401: Authentication failure. Please login. """ query = ItemRevision.query.filter(ItemRevision.id == revision_id) result = query.first() comments = [] for comment in result.comments: comment_marshaled = marshal(comment, REVISION_COMMENT_FIELDS) comments.append( dict(comment_marshaled.items() + {'user': comment.user.email}.items())) cloudtrail_entries = [] for entry in result.cloudtrail_entries: cloudtrail_entries.append(entry.full_entry) revision_marshaled = marshal(result, REVISION_FIELDS) revision_marshaled = dict( revision_marshaled.items() + {'config': OrderedDict(sorted(sub_dict(result.config).items())) }.items() + {'auth': self.auth_dict}.items() + {'comments': comments}.items() + {'cloudtrail': cloudtrail_entries}.items()) self.reqparse.add_argument('compare', type=int, default=None, location='args') args = self.reqparse.parse_args() compare_id = args.pop('compare', None) if compare_id: query = ItemRevision.query.filter(ItemRevision.id == compare_id) compare_result = query.first() pdiff = PolicyDiff( OrderedDict(sorted(sub_dict(result.config).items())), OrderedDict(sorted(sub_dict(compare_result.config).items()))) revision_marshaled = dict( revision_marshaled.items() + {'diff_html': pdiff.produceDiffHTML()}.items()) return revision_marshaled, 200
def get_pdiff_html(self): pdiff = PolicyDiff(self.new_config, self.old_config) return pdiff.produceDiffHTML()
def test_produce(): for case in TEST_CASES: differ = PolicyDiff(case['new'], case['old']) html = differ.produceDiffHTML() if html != case['expected_result']: print(html) assert html == case['expected_result'] differ = PolicyDiff({}, {}) result = differ.produceDiffHTML() assert result == 'No Policy.<br/>' differ._old_policy = None differ._new_policy = None try: differ.produceDiffHTML() assert False except ValueError: pass differ._old_policy = [] differ._new_policy = {} try: differ.produceDiffHTML() assert False except ValueError: pass differ._old_policy = "old_policy" differ._new_policy = "new_policy" result = differ.produceDiffHTML() assert result == """<font color='red'>old_policy</font><br/> <br/><font color='green'>new_policy</font><br/> """ differ._old_policy = [1, 2, 3] differ._new_policy = [1, 2, 3] differ.produceDiffHTML() differ._old_policy = set([1, 2, 3]) differ._new_policy = set([1, 2, 3]) try: differ.produceDiffHTML() assert False except ValueError: pass
def get(self, revision_id): """ .. http:get:: /api/1/revision/1234 Get a specific revision. **Example Request**: .. sourcecode:: http GET /api/1/revision/123 HTTP/1.1 Host: example.com Accept: application/json, text/javascript **Example Response**: .. sourcecode:: http HTTP/1.1 200 OK Vary: Accept Content-Type: application/json { "auth": { "authenticated": true, "user": "******" }, "item_id": 114, "comments": [], "active": false, "date_created": "2013-10-04 22:01:47", "config": {}, "id":123 } :statuscode 200: no error :statuscode 401: Authentication failure. Please login. """ auth, retval = __check_auth__(self.auth_dict) if auth: return retval query = ItemRevision.query.filter(ItemRevision.id == revision_id) result = query.first() comments = [] for comment in result.comments: comment_marshaled = marshal(comment, REVISION_COMMENT_FIELDS) comments.append(dict( comment_marshaled.items() + {'user': comment.user.email}.items() )) revision_marshaled = marshal(result, REVISION_FIELDS) revision_marshaled = dict( revision_marshaled.items() + {'config': result.config}.items() + {'auth': self.auth_dict}.items() + {'comments': comments}.items() ) self.reqparse.add_argument('compare', type=int, default=None, location='args') args = self.reqparse.parse_args() compare_id = args.pop('compare', None) if compare_id: query = ItemRevision.query.filter(ItemRevision.id == compare_id) compare_result = query.first() pdiff = PolicyDiff(result.config, compare_result.config) revision_marshaled = dict( revision_marshaled.items() + {'diff_html': pdiff.produceDiffHTML()}.items() ) return revision_marshaled, 200