Skip to content

Commit

Permalink
Merge pull request #8 from owenrumney/add-get-rule-by-id
Browse files Browse the repository at this point in the history
Add GetRuleById
  • Loading branch information
owenrumney authored Feb 24, 2021
2 parents 01c6e4f + e7dd146 commit 537965e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions models/run.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package models

import (
"fmt"
)

// Run type represents a run of a tool
type Run struct {
Tool *tool `json:"tool"`
Expand Down Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions test/run_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 537965e

Please sign in to comment.