Skip to content

Commit

Permalink
share fixture helper functions between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Aug 28, 2016
1 parent 301f596 commit 117db2a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 67 deletions.
33 changes: 6 additions & 27 deletions control/summary_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package control_test

import (
"bytes"
"os"
"path/filepath"
"text/template"

"github.com/jbowtie/gokogiri/xml"
. "github.com/opencontrol/fedramp-templater/control"
"github.com/opencontrol/fedramp-templater/docx/helper"
"github.com/opencontrol/fedramp-templater/opencontrols"
"github.com/opencontrol/fedramp-templater/fixtures"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -21,7 +19,7 @@ type tableData struct {
}

func docFixture(control string) *xml.XmlDocument {
path := filepath.Join("..", "fixtures", "simplified_table.xml")
path := fixtures.FixturePath("simplified_table.xml")
tpl, err := template.ParseFiles(path)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -42,32 +40,12 @@ func getTable(control string) xml.Node {
return tables[0]
}

func openControlFixturePath() string {
path := filepath.Join("..", "fixtures", "opencontrols")
path, err := filepath.Abs(path)
Expect(err).NotTo(HaveOccurred())
_, err = os.Stat(path)
Expect(err).NotTo(HaveOccurred())

return path
}

func openControlFixture() opencontrols.Data {
path := openControlFixturePath()
data, errors := opencontrols.LoadFrom(path)
for _, err := range errors {
Expect(err).NotTo(HaveOccurred())
}

return data
}

var _ = Describe("SummaryTable", func() {
Describe("Fill", func() {
It("fills in the Responsible Role for controls", func() {
table := getTable("AC-2")
ct := SummaryTable{Root: table}
openControlData := openControlFixture()
openControlData := fixtures.LoadOpenControlFixture()

ct.Fill(openControlData)

Expand All @@ -77,21 +55,22 @@ var _ = Describe("SummaryTable", func() {
It("fills in the Responsible Role for control enhancements", func() {
table := getTable("AC-2 (1)")
ct := SummaryTable{Root: table}
openControlData := openControlFixture()
openControlData := fixtures.LoadOpenControlFixture()

ct.Fill(openControlData)

Expect(table.Content()).To(ContainSubstring(`Responsible Role: Amazon Elastic Compute Cloud: AWS Staff`))
})
})

Describe("Diff", func() {
It("detects no diff when the value of responsible role is empty", func() {
doc := docFixture("AC-2")
tables, _ := doc.Search("//w:tbl")
table := tables[0]

ct := SummaryTable{Root: table}
openControlData := openControlFixture()
openControlData := fixtures.LoadOpenControlFixture()
diff, err := ct.Diff(openControlData)

Expect(diff).To(Equal([]reporter.Reporter{}))
Expand Down
43 changes: 43 additions & 0 deletions fixtures/fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fixtures

import (
"os"
"path/filepath"

. "github.com/onsi/gomega"
"github.com/opencontrol/fedramp-templater/opencontrols"
"github.com/opencontrol/fedramp-templater/ssp"
)

func FixturePath(name string) string {
path := filepath.Join("..", "fixtures", name)
path, err := filepath.Abs(path)
Expect(err).NotTo(HaveOccurred())
// ensure the file/directory exists
_, err = os.Stat(path)
Expect(err).NotTo(HaveOccurred())

return path
}

func OpenControlFixturePath() string {
return FixturePath("opencontrols")
}

func LoadSSP(name string) *ssp.Document {
sspPath := FixturePath(name)
doc, err := ssp.Load(sspPath)
Expect(err).NotTo(HaveOccurred())

return doc
}

func LoadOpenControlFixture() opencontrols.Data {
openControlDir := OpenControlFixturePath()
openControlData, errors := opencontrols.LoadFrom(openControlDir)
for _, err := range errors {
Expect(err).NotTo(HaveOccurred())
}

return openControlData
}
14 changes: 3 additions & 11 deletions ssp/document_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package ssp_test

import (
"path/filepath"

"github.com/opencontrol/fedramp-templater/fixtures"
. "github.com/opencontrol/fedramp-templater/ssp"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func loadSSP(name string) *Document {
path := filepath.Join("..", "fixtures", name)
doc, err := Load(path)
Expect(err).NotTo(HaveOccurred())
return doc
}

var _ = Describe("SSP", func() {
Describe("Load", func() {
It("gets the content from the doc", func() {
doc := loadSSP("FedRAMP_ac-2-1_v2.1.docx")
doc := fixtures.LoadSSP("FedRAMP_ac-2-1_v2.1.docx")
defer doc.Close()

Expect(doc.Content()).To(ContainSubstring("Control Enhancement"))
Expand All @@ -33,7 +25,7 @@ var _ = Describe("SSP", func() {

Describe("SummaryTables", func() {
It("returns the tables", func() {
doc := loadSSP("FedRAMP_ac-2_v2.1.docx")
doc := fixtures.LoadSSP("FedRAMP_ac-2_v2.1.docx")
defer doc.Close()

tables, err := doc.SummaryTables()
Expand Down
34 changes: 5 additions & 29 deletions templater/templater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,15 @@ package templater_test

import (
"bytes"
"path/filepath"

"github.com/opencontrol/fedramp-templater/opencontrols"
"github.com/opencontrol/fedramp-templater/ssp"
"github.com/opencontrol/fedramp-templater/fixtures"
. "github.com/opencontrol/fedramp-templater/templater"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/opencontrol/fedramp-templater/reporter"
)

func loadSSP(name string) *ssp.Document {
sspPath := filepath.Join("..", "fixtures", name)
doc, err := ssp.Load(sspPath)
Expect(err).NotTo(HaveOccurred())

return doc
}

func loadOpenControlFixture() opencontrols.Data {
openControlDir := filepath.Join("..", "fixtures", "opencontrols")
openControlDir, err := filepath.Abs(openControlDir)
Expect(err).NotTo(HaveOccurred())
openControlData, errors := opencontrols.LoadFrom(openControlDir)
for _, err := range errors {
Expect(err).NotTo(HaveOccurred())
}

return openControlData
}

func extractDiffReport(reporters []reporter.Reporter) string {
report := &bytes.Buffer{}
for _, rept := range reporters {
Expand All @@ -44,9 +22,9 @@ func extractDiffReport(reporters []reporter.Reporter) string {
var _ = Describe("Templater", func() {
Describe("TemplatizeSSP", func() {
It("fills in the Responsible Role fields", func() {
doc := loadSSP("FedRAMP_ac-2-1_v2.1.docx")
doc := fixtures.LoadSSP("FedRAMP_ac-2-1_v2.1.docx")
defer doc.Close()
openControlData := loadOpenControlFixture()
openControlData := fixtures.LoadOpenControlFixture()

err := TemplatizeSSP(doc, openControlData)

Expand All @@ -62,14 +40,12 @@ var _ = Describe("Templater", func() {

By("Loading the SSP with the Responsible Role being 'OpenControl Role Placeholder' " +
"for Control 'AC-2 (1)'")
sspPath := filepath.Join("..", "fixtures", "FedRAMP_ac-2-1_v2.1.docx")
s, err := ssp.Load(sspPath)
Expect(err).NotTo(HaveOccurred())
s := fixtures.LoadSSP("FedRAMP_ac-2-1_v2.1.docx")
defer s.Close()

By("Loading the data from the opencontrol workspace with the Responsible Role being " +
"'Amazon Elastic Compute Cloud: AWS Staff' for Control 'AC-2 (1)'")
openControlData := loadOpenControlFixture()
openControlData := fixtures.LoadOpenControlFixture()

By("Calling 'diff' on the SSP")
diffInfo, err := DiffSSP(s, openControlData)
Expand Down

0 comments on commit 117db2a

Please sign in to comment.