1
0
mirror of https://github.com/strongdm/comply synced 2025-12-06 06:14:09 +00:00

Add tests (#48)

This commit is contained in:
Rodolfo Campos
2021-10-12 12:14:30 +02:00
parent 53a65f6c00
commit 1c80e1ce66
5 changed files with 183 additions and 12 deletions

78
internal/model/fs_test.go Normal file
View File

@@ -0,0 +1,78 @@
package model
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/strongdm/comply/internal/config"
"github.com/strongdm/comply/internal/path"
"gopkg.in/yaml.v2"
)
// TestReadNarratives calls model.ReadNarratives checking for a valid return value.
func TestReadNarratives(t *testing.T) {
mockConfig()
path.Narratives = func() ([]path.File, error) {
filePath := fmt.Sprintf("%s/narratives/control.md", getRootPath())
fileInfo, _ := os.Lstat(filePath)
return []path.File{
{FullPath: filePath, Info: fileInfo},
}, nil
}
_, err := ReadNarratives()
if err != nil {
t.Fatalf(`ReadNarratives() returned an error %v`, err)
}
}
// TestReadNarratives calls model.ReadNarratives checking for a valid return when
// there are no narratives to process
func TestReadNarrativesWhenThereAreNoNarratives(t *testing.T) {
mockConfig()
path.Narratives = func() ([]path.File, error) {
return []path.File{}, nil
}
_, err := ReadNarratives()
if err != nil {
t.Fatalf(`ReadNarratives() returned an error %v`, err)
}
}
// TestReadNarratives calls model.ReadNarratives checking for an error return when
// there is an invalid narrative
func TestReadNarrativesFailsWhenInvalidNarrative(t *testing.T) {
mockConfig()
path.Narratives = func() ([]path.File, error) {
filePath := fmt.Sprintf("%s/../fixtures/narratives/invalid-control.md", getRootPath())
fileInfo, _ := os.Lstat(filePath)
return []path.File{
{FullPath: filePath, Info: fileInfo},
}, nil
}
_, err := ReadNarratives()
if err == nil {
t.Fatal(`ReadNarratives() was expected to fail`)
}
}
func mockConfig() {
config.Config = func() *config.Project {
p := config.Project{}
cfgBytes, _ := ioutil.ReadFile(filepath.Join(getRootPath(), "comply.yml.example"))
yaml.Unmarshal(cfgBytes, &p)
return &p
}
}
func getRootPath() string {
_, fileName, _, _ := runtime.Caller(0)
fileDir := filepath.Dir(fileName)
return fmt.Sprintf("%s/../../example", fileDir)
}

View File

@@ -9,22 +9,22 @@ import (
func TestMarshal(t *testing.T) {
d := Data{
Tickets: []*Ticket{
&Ticket{
{
ID: "t1",
},
},
Audits: []*Audit{
&Audit{
{
ID: "a1",
},
},
Procedures: []*Procedure{
&Procedure{
Code: "pro1",
{
ID: "pro1",
},
},
Policies: []*Policy{
&Policy{
Policies: []*Document{
{
Name: "pol1",
},
},
@@ -40,5 +40,4 @@ func TestMarshal(t *testing.T) {
!strings.Contains(encoded, "pol1") {
t.Error("identifier not found in marshalled string")
}
}