def test_link_to_support_item_issues(self):
        auditor = Auditor(accounts=['test_account'])
        sub_item_id = 2
        issue1_text = 'This is test issue1'
        issue2_text = 'This is test issue2'
        issue1_score = 10
        issue2_score = 5

        item = ChangeItem(index='test_index',
                          account='test_account',
                          name='item_name')
        sub_item = Item(id=sub_item_id,
                        tech_id=1,
                        account_id=1,
                        name='sub_item_name')
        sub_item.issues.append(ItemAudit(score=issue1_score,
                                         issue=issue1_text))
        sub_item.issues.append(ItemAudit(score=issue2_score,
                                         issue=issue2_text))
        auditor.link_to_support_item_issues(item, sub_item, None, "TEST")
        self.assertTrue(len(item.audit_issues) == 1)
        new_issue = item.audit_issues[0]
        self.assertTrue(new_issue.score == issue1_score + issue2_score)
        self.assertTrue(new_issue.issue == "TEST")
        self.assertTrue(len(new_issue.sub_items) == 1)
        self.assertTrue(new_issue.sub_items[0] == sub_item)
def export_environment_summary(output_file):
    """ Save the Auditor.OBJECT_STORE as a JSON file. """
    from security_monkey.auditor import Auditor
    from collections import defaultdict

    # Build the Environment Summary
    Auditor._load_object_store()

    # Convert sets to lists so we can serialize with JSON
    json_safe_object = defaultdict(dict)
    for tech_name, tech_body in Auditor.OBJECT_STORE.items():
        for item_name, item_accounts in tech_body.items():
            json_safe_object[tech_name][item_name] = list(item_accounts)

    # Write the file to disk
    with open(output_file, 'w') as of:
        json.dump(json_safe_object, of, indent=2, sort_keys=True)
    def test_link_to_support_item_issue(self):
        sub_item_id = 2
        issue_text = 'This is a test issue'
        issue_score = 10

        auditor = Auditor(accounts=['test_account'])
        item = ChangeItem(index='test_index',
                          account='test_account', name='item_name')
        sub_item = Item(id=sub_item_id, tech_id=1,
                        account_id=1, name='sub_item_name')
        sub_item.issues.append(ItemAudit(score=issue_score, issue=issue_text))
        auditor.link_to_support_item_issues(item, sub_item, issue_text)
        self.assertTrue(len(item.audit_issues) == 1)
        new_issue = item.audit_issues[0]
        self.assertTrue(new_issue.score == issue_score)
        self.assertTrue(new_issue.issue == issue_text)
        self.assertTrue(len(new_issue.sub_items) == 1)
        self.assertTrue(new_issue.sub_items[0] == sub_item)
    def test_save_issues(self):
        item = Item(region="us-west-2", name="testitem", technology=self.technology, account=self.test_account)
        revision = ItemRevision(item=item, config={}, active=True)
        item_audit = ItemAudit(item=item, issue="test issue")
        db.session.add(item)
        db.session.add(revision)
        db.session.add(item_audit)
        db.session.commit()

        auditor = Auditor(accounts=[self.test_account.name])
        auditor.index = self.technology.name
        auditor.i_am_singular = self.technology.name
        auditor.items = auditor.read_previous_items()
        auditor.audit_objects()

        try:
            auditor.save_issues()
        except AttributeError as e:
            self.fail("Auditor.save_issues() raised AttributeError unexpectedly: {}".format(e.message))
    def test_save_issues(self):
        mixer.init_app(self.app)
        test_account = mixer.blend(Account, name='test_account')
        technology = mixer.blend(Technology, name='testtech')
        item = Item(region="us-west-2", name="testitem", technology=technology, account=test_account)
        revision = mixer.blend(ItemRevision, item=item, config={}, active=True)
        item.latest_revision_id = revision.id
        mixer.blend(ItemAudit, item=item, issue='test issue')

        auditor = Auditor(accounts=[test_account.name])
        auditor.index = technology.name
        auditor.i_am_singular = technology.name
        auditor.items = auditor.read_previous_items()
        auditor.audit_objects()

        try:
            auditor.save_issues()
        except AttributeError as e:
            self.fail("Auditor.save_issues() raised AttributeError unexpectedly: {}".format(e.message))
    def test_save_issues(self):
        item = Item(region="us-west-2",
                    name="testitem",
                    technology=self.technology,
                    account=self.test_account)
        revision = ItemRevision(item=item, config={}, active=True)
        item_audit = ItemAudit(item=item, issue="test issue")
        db.session.add(item)
        db.session.add(revision)
        db.session.add(item_audit)
        db.session.commit()

        auditor = Auditor(accounts=[self.test_account.name])
        auditor.index = self.technology.name
        auditor.i_am_singular = self.technology.name
        auditor.items = auditor.read_previous_items()
        auditor.audit_objects()

        try:
            auditor.save_issues()
        except AttributeError as e:
            self.fail(
                "Auditor.save_issues() raised AttributeError unexpectedly: {}".
                format(e.message))