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 (
_ "embed"
"encoding/xml"
"fmt"
"go.jolheiser.com/go-spectre/testdata"
"testing"
)
// These are the exact same tests as spectre_test.go
// These are here just to make sure the CLI is giving the same outputs
func TestCLI(t *testing.T) {
var tests TestCases
if err := xml.Unmarshal(testsXML, &tests); err != nil {
t.Log("could not load test data")
cases, err := testdata.Cases()
if err != nil {
t.Log(err)
t.FailNow()
}
dc := tests.Cases[0]
for _, tc := range tests.Cases[1:] {
for _, tc := range cases {
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{
"--username", user,
"--secret", secret,
"--template", template,
"--counter", counter,
"--scope", scope,
siteName,
"--username", tc.UserName,
"--secret", tc.UserSecret,
"--template", tc.ResultType,
"--counter", tc.KeyCounter,
"--scope", tc.KeyPurpose,
tc.SiteName,
}
fmt.Println(args)
pw, err := doMain(args)
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 (
_ "embed"
"encoding/xml"
"fmt"
"go.jolheiser.com/go-spectre/testdata"
"strconv"
"testing"
@ -11,38 +11,29 @@ import (
)
func TestSpectre(t *testing.T) {
var tests TestCases
if err := xml.Unmarshal(testsXML, &tests); err != nil {
t.Log("could not load test data")
cases, err := testdata.Cases()
if err != nil {
t.Log(err)
t.FailNow()
}
dc := tests.Cases[0]
for _, tc := range tests.Cases[1:] {
for _, tc := range cases {
t.Run(tc.ID, func(t *testing.T) {
user := def(dc.UserName, tc.UserName)
secret := def(dc.UserSecret, tc.UserSecret)
s, err := spectre.New(user, secret)
s, err := spectre.New(tc.UserName, tc.UserSecret)
if err != nil {
t.Logf("could not initialize spectre: %v", err)
t.Fail()
}
siteName := def(dc.SiteName, tc.SiteName)
template := def(dc.ResultType, tc.ResultType)
counterStr := def(dc.KeyCounter, tc.KeyCounter)
counter, err := strconv.Atoi(counterStr)
counter, err := strconv.Atoi(tc.KeyCounter)
if err != nil {
t.Log("could not convert counter")
t.Fail()
}
scope := def(dc.KeyPurpose, tc.KeyPurpose)
pass := s.Site(siteName,
spectre.WithTemplate(spectre.Template(template)),
pass := s.Site(tc.SiteName,
spectre.WithTemplate(spectre.Template(tc.ResultType)),
spectre.WithCounter(counter),
spectre.WithScope(spectre.Scope(scope)),
spectre.WithScope(spectre.Scope(tc.KeyPurpose)),
)
if pass != tc.Result {
@ -83,28 +74,3 @@ func Example_second() {
fmt.Println(pw)
// 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
}