From e7dd146c8f109b76fb5815abc7f627d5cacd606e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Owen=20Rumney?= Date: Wed, 24 Feb 2021 10:51:12 +0000 Subject: [PATCH] Add GetRuleById --- models/run.go | 16 ++++++++++++++++ test/run_stage_test.go | 8 ++++++++ test/run_test.go | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/models/run.go b/models/run.go index 5dc7975..2fe7451 100644 --- a/models/run.go +++ b/models/run.go @@ -1,5 +1,9 @@ package models +import ( + "fmt" +) + // Run type represents a run of a tool type Run struct { Tool *tool `json:"tool"` @@ -81,3 +85,15 @@ func updateResultLocationIndex(result *Result, location string, index int) { } } } + +func (run *Run) GetRuleById(ruleId string) (*Rule, error) { + if run.Tool != nil || run.Tool.Driver != nil { + for _, rule := range run.Tool.Driver.Rules { + if rule.ID == ruleId { + return rule, nil + } + } + } + + return nil, fmt.Errorf("couldn't find rule %s", ruleId) +} diff --git a/test/run_stage_test.go b/test/run_stage_test.go index 595f801..f3f2dbb 100644 --- a/test/run_stage_test.go +++ b/test/run_stage_test.go @@ -86,3 +86,11 @@ func (rt *runTest) aResultIsAddedToTheRunWithHelpText() *runTest { rt.run.AddResultDetails(rule, result, resultLocation) return rt } + +func (rt *runTest) gettingRuleByIdReturnsRule() { + rule, err := rt.run.GetRuleById("AWS001") + + assert.NoError(rt.t, err) + assert.Equal(rt.t, "AWS001", rule.ID) + assert.Equal(rt.t, "S3 Bucket has an ACL defined which allows public access.", rule.ShortDescription.Text) +} diff --git a/test/run_test.go b/test/run_test.go index eebe87d..047f43b 100644 --- a/test/run_test.go +++ b/test/run_test.go @@ -60,3 +60,11 @@ func Test_create_a_run_with_a_result_added_and_help_text_provided(t *testing.T) theRunIsConvertedToAString() then.theJSONStringRepresentationOfTheRunShouldBe(expected) } + +func Test_create_a_run_with_a_rule_and_get_the_rule_by_id(t *testing.T) { + given, when, then := createNewRunTest(t) + + given.aNewRunIsCreated() + when.aResultIsAddedToTheRunWithHelpText() + then.gettingRuleByIdReturnsRule() +}