Refactor tests
continuous-integration/woodpecker the build was successful Details
ci/woodpecker/tag/woodpecker Pipeline was successful Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main latest
jolheiser 2021-11-23 22:46:37 -06:00
parent f27be2ffff
commit 906de83072
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
4 changed files with 75 additions and 90 deletions

View File

@ -2,39 +2,29 @@ package main
import ( import (
_ "embed" _ "embed"
"encoding/xml" "go.jolheiser.com/go-spectre/testdata"
"fmt"
"testing" "testing"
) )
// These are the exact same tests as spectre_test.go // These are the exact same tests as spectre_test.go
// These are here just to make sure the CLI is giving the same outputs // These are here just to make sure the CLI is giving the same outputs
func TestCLI(t *testing.T) { func TestCLI(t *testing.T) {
var tests TestCases cases, err := testdata.Cases()
if err := xml.Unmarshal(testsXML, &tests); err != nil { if err != nil {
t.Log("could not load test data") t.Log(err)
t.FailNow() t.FailNow()
} }
dc := tests.Cases[0] for _, tc := range cases {
for _, tc := range tests.Cases[1:] {
t.Run(tc.ID, func(t *testing.T) { t.Run(tc.ID, func(t *testing.T) {
user := def(dc.UserName, tc.UserName)
secret := def(dc.UserSecret, tc.UserSecret)
siteName := def(dc.SiteName, tc.SiteName)
template := def(dc.ResultType, tc.ResultType)
counter := def(dc.KeyCounter, tc.KeyCounter)
scope := def(dc.KeyPurpose, tc.KeyPurpose)
args := []string{ args := []string{
"--username", user, "--username", tc.UserName,
"--secret", secret, "--secret", tc.UserSecret,
"--template", template, "--template", tc.ResultType,
"--counter", counter, "--counter", tc.KeyCounter,
"--scope", scope, "--scope", tc.KeyPurpose,
siteName, tc.SiteName,
} }
fmt.Println(args)
pw, err := doMain(args) pw, err := doMain(args)
if err != nil { if err != nil {
@ -49,28 +39,3 @@ func TestCLI(t *testing.T) {
}) })
} }
} }
//go:embed spectre_tests.xml
var testsXML []byte
type TestCases struct {
Cases []TestCase `xml:"case"`
}
type TestCase struct {
ID string `xml:"id,attr"`
UserName string `xml:"userName"`
UserSecret string `xml:"userSecret"`
SiteName string `xml:"siteName"`
ResultType string `xml:"resultType"`
KeyCounter string `xml:"keyCounter"`
KeyPurpose string `xml:"keyPurpose"`
Result string `xml:"result"`
}
func def(def, alt string) string {
if alt != "" {
return alt
}
return def
}

View File

@ -2,8 +2,8 @@ package spectre_test
import ( import (
_ "embed" _ "embed"
"encoding/xml"
"fmt" "fmt"
"go.jolheiser.com/go-spectre/testdata"
"strconv" "strconv"
"testing" "testing"
@ -11,38 +11,29 @@ import (
) )
func TestSpectre(t *testing.T) { func TestSpectre(t *testing.T) {
var tests TestCases cases, err := testdata.Cases()
if err := xml.Unmarshal(testsXML, &tests); err != nil { if err != nil {
t.Log("could not load test data") t.Log(err)
t.FailNow() t.FailNow()
} }
dc := tests.Cases[0] for _, tc := range cases {
for _, tc := range tests.Cases[1:] {
t.Run(tc.ID, func(t *testing.T) { t.Run(tc.ID, func(t *testing.T) {
user := def(dc.UserName, tc.UserName) s, err := spectre.New(tc.UserName, tc.UserSecret)
secret := def(dc.UserSecret, tc.UserSecret)
s, err := spectre.New(user, secret)
if err != nil { if err != nil {
t.Logf("could not initialize spectre: %v", err) t.Logf("could not initialize spectre: %v", err)
t.Fail() t.Fail()
} }
counter, err := strconv.Atoi(tc.KeyCounter)
siteName := def(dc.SiteName, tc.SiteName)
template := def(dc.ResultType, tc.ResultType)
counterStr := def(dc.KeyCounter, tc.KeyCounter)
counter, err := strconv.Atoi(counterStr)
if err != nil { if err != nil {
t.Log("could not convert counter") t.Log("could not convert counter")
t.Fail() t.Fail()
} }
scope := def(dc.KeyPurpose, tc.KeyPurpose)
pass := s.Site(siteName, pass := s.Site(tc.SiteName,
spectre.WithTemplate(spectre.Template(template)), spectre.WithTemplate(spectre.Template(tc.ResultType)),
spectre.WithCounter(counter), spectre.WithCounter(counter),
spectre.WithScope(spectre.Scope(scope)), spectre.WithScope(spectre.Scope(tc.KeyPurpose)),
) )
if pass != tc.Result { if pass != tc.Result {
@ -83,28 +74,3 @@ func Example_second() {
fmt.Println(pw) fmt.Println(pw)
// Output: Ig^JIcxD!*)TbefJBi6- // Output: Ig^JIcxD!*)TbefJBi6-
} }
//go:embed cmd/spectre/spectre_tests.xml
var testsXML []byte
type TestCases struct {
Cases []TestCase `xml:"case"`
}
type TestCase struct {
ID string `xml:"id,attr"`
UserName string `xml:"userName"`
UserSecret string `xml:"userSecret"`
SiteName string `xml:"siteName"`
ResultType string `xml:"resultType"`
KeyCounter string `xml:"keyCounter"`
KeyPurpose string `xml:"keyPurpose"`
Result string `xml:"result"`
}
func def(def, alt string) string {
if alt != "" {
return alt
}
return def
}

54
testdata/testdata.go vendored 100644
View File

@ -0,0 +1,54 @@
package testdata
import (
_ "embed"
"encoding/xml"
)
func Cases() ([]*TestCase, error) {
var tests TestCases
if err := xml.Unmarshal(testsXML, &tests); err != nil {
return nil, err
}
defaultCase := tests.Cases[0]
cases := make([]*TestCase, 0, len(tests.Cases[1:]))
for _, tc := range tests.Cases[1:] {
tc = &TestCase{
ID: tc.ID,
UserName: def(defaultCase.UserName, tc.UserName),
UserSecret: def(defaultCase.UserSecret, tc.UserSecret),
SiteName: def(defaultCase.SiteName, tc.SiteName),
ResultType: def(defaultCase.ResultType, tc.ResultType),
KeyCounter: def(defaultCase.KeyCounter, tc.KeyCounter),
KeyPurpose: def(defaultCase.KeyPurpose, tc.KeyPurpose),
Result: tc.Result,
}
cases = append(cases, tc)
}
return cases, nil
}
//go:embed spectre_tests.xml
var testsXML []byte
type TestCases struct {
Cases []*TestCase `xml:"case"`
}
type TestCase struct {
ID string `xml:"id,attr"`
UserName string `xml:"userName"`
UserSecret string `xml:"userSecret"`
SiteName string `xml:"siteName"`
ResultType string `xml:"resultType"`
KeyCounter string `xml:"keyCounter"`
KeyPurpose string `xml:"keyPurpose"`
Result string `xml:"result"`
}
func def(def, alt string) string {
if alt != "" {
return alt
}
return def
}