Skip to content

Commit

Permalink
test fixes after test asset change
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst committed Oct 27, 2023
1 parent 47ab107 commit c092258
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 72 deletions.
2 changes: 1 addition & 1 deletion auth/services/oauth/relying_party_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestRelyingParty_RequestRFC021AccessToken(t *testing.T) {
writer.WriteHeader(http.StatusNotFound)
}
})
credentials := []vcr.VerifiableCredential{*credential.ValidNutsOrganizationCredential()}
credentials := []vcr.VerifiableCredential{credential.ValidNutsOrganizationCredential(t)}
tlsServer := http2.TestTLSServer(t, handler)
verifierDID := didweb.ServerURLToDIDWeb(t, tlsServer.URL)
authzServerMetadata.TokenEndpoint = tlsServer.URL + "/token"
Expand Down
4 changes: 1 addition & 3 deletions vcr/ambassador_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ func TestAmbassador_handleReprocessEvent(t *testing.T) {
ctx.vcr.ambassador.(*ambassador).writer = mockWriter

// load VC
vc := vc.VerifiableCredential{}
vcJSON, _ := os.ReadFile("test/vc.json")
json.Unmarshal(vcJSON, &vc)
vc := credential.ValidNutsOrganizationCredential(t)

// load key
pem, _ := os.ReadFile("test/private.pem")
Expand Down
4 changes: 2 additions & 2 deletions vcr/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ package vcr
import (
"github.com/nuts-foundation/nuts-node/core"
"github.com/nuts-foundation/nuts-node/jsonld"
"github.com/nuts-foundation/nuts-node/vcr/assets"
"github.com/nuts-foundation/nuts-node/vcr/credential"
"github.com/piprate/json-gold/ld"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
)

Expand All @@ -37,7 +37,7 @@ func TestNutsV1Context(t *testing.T) {
reader := jsonld.Reader{DocumentLoader: jsonldManager.DocumentLoader()}

t.Run("NutsOrganizationCredential", func(t *testing.T) {
vcJSON, _ := os.ReadFile("test/vc.json")
vcJSON, _ := assets.TestAssets.ReadFile("test_assets/vc.json")
documents, err := reader.ReadBytes(vcJSON)
if err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions vcr/credential/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestFindValidator(t *testing.T) {
})

t.Run("validator and builder found for NutsOrganizationCredential", func(t *testing.T) {
vc := ValidNutsOrganizationCredential()
v := FindValidator(*vc)
vc := ValidNutsOrganizationCredential(t)
v := FindValidator(vc)

assert.NotNil(t, v)
})
Expand Down
5 changes: 1 addition & 4 deletions vcr/credential/revocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ import (
"time"

ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/vc"
"github.com/stretchr/testify/assert"
)

func TestBuildRevocation(t *testing.T) {
target := vc.VerifiableCredential{}
vcData, _ := os.ReadFile("../test/vc.json")
json.Unmarshal(vcData, &target)
target := ValidNutsOrganizationCredential(t)

at := time.Now()
nowFunc = func() time.Time {
Expand Down
17 changes: 11 additions & 6 deletions vcr/credential/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ package credential
import (
"encoding/json"
"github.com/nuts-foundation/nuts-node/vcr/assets"
"testing"
"time"

ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/vc"
"github.com/nuts-foundation/nuts-node/vdr"
)

// assert pointing to "../test/vc.json"

func ValidNutsAuthorizationCredential() *vc.VerifiableCredential {
id := stringToURI(vdr.TestDIDA.String() + "#38E90E8C-F7E5-4333-B63A-F9DD155A0272")
return &vc.VerifiableCredential{
Expand All @@ -56,11 +55,17 @@ func ValidNutsAuthorizationCredential() *vc.VerifiableCredential {
}
}

func ValidNutsOrganizationCredential() *vc.VerifiableCredential {
func ValidNutsOrganizationCredential(t *testing.T) vc.VerifiableCredential {
inputVC := vc.VerifiableCredential{}
vcJSON, _ := assets.TestAssets.ReadFile("test_assets/vc.json")
_ = json.Unmarshal(vcJSON, &inputVC)
return &inputVC
vcJSON, err := assets.TestAssets.ReadFile("test_assets/vc.json")
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(vcJSON, &inputVC)
if err != nil {
t.Fatal(err)
}
return inputVC
}

func stringToURI(input string) ssi.URI {
Expand Down
70 changes: 35 additions & 35 deletions vcr/credential/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,72 +38,72 @@ func TestNutsOrganizationCredentialValidator_Validate(t *testing.T) {
validator := nutsOrganizationCredentialValidator{}

t.Run("ok", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)

err := validator.Validate(*v)
err := validator.Validate(v)

assert.NoError(t, err)
})

t.Run("failed - missing custom type", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
v.Type = []ssi.URI{vc.VerifiableCredentialTypeV1URI()}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: type 'NutsOrganizationCredential' is required")
})

t.Run("failed - missing credential subject", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
v.CredentialSubject = []interface{}{}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: single CredentialSubject expected")
})

t.Run("failed - missing organization", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
var credentialSubject = make(map[string]interface{})
credentialSubject["id"] = vdr.TestDIDB.String()
v.CredentialSubject = []interface{}{credentialSubject}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'credentialSubject.organization' is empty")
})

t.Run("failed - missing organization name", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
var credentialSubject = make(map[string]interface{})
credentialSubject["id"] = vdr.TestDIDB.String()
credentialSubject["organization"] = map[string]interface{}{
"city": "EIbergen",
}
v.CredentialSubject = []interface{}{credentialSubject}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'credentialSubject.name' is empty")
})

t.Run("failed - missing organization city", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
var credentialSubject = make(map[string]interface{})
credentialSubject["id"] = vdr.TestDIDB.String()
credentialSubject["organization"] = map[string]interface{}{
"name": "Because we care B.V.",
}
v.CredentialSubject = []interface{}{credentialSubject}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'credentialSubject.city' is empty")
})

t.Run("failed - empty organization city", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
var credentialSubject = make(map[string]interface{})
credentialSubject["id"] = vdr.TestDIDB.String()
credentialSubject["organization"] = map[string]interface{}{
Expand All @@ -112,13 +112,13 @@ func TestNutsOrganizationCredentialValidator_Validate(t *testing.T) {
}
v.CredentialSubject = []interface{}{credentialSubject}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'credentialSubject.city' is empty")
})

t.Run("failed - empty organization name", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
var credentialSubject = make(map[string]interface{})
credentialSubject["id"] = vdr.TestDIDB.String()
credentialSubject["organization"] = map[string]interface{}{
Expand All @@ -127,27 +127,27 @@ func TestNutsOrganizationCredentialValidator_Validate(t *testing.T) {
}
v.CredentialSubject = []interface{}{credentialSubject}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'credentialSubject.name' is empty")
})

t.Run("failed - missing credentialSubject.ID", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
var credentialSubject = make(map[string]interface{})
credentialSubject["organization"] = map[string]interface{}{
"name": "Because we care B.V.",
"city": "EIbergen",
}
v.CredentialSubject = []interface{}{credentialSubject}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'credentialSubject.ID' is nil")
})

t.Run("failed - invalid credentialSubject.ID", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
var credentialSubject = make(map[string]interface{})
credentialSubject["id"] = "invalid"
credentialSubject["organization"] = map[string]interface{}{
Expand All @@ -156,27 +156,27 @@ func TestNutsOrganizationCredentialValidator_Validate(t *testing.T) {
}
v.CredentialSubject = []interface{}{credentialSubject}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: invalid 'credentialSubject.id': invalid DID")
})

t.Run("failed - invalid ID", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
otherID := vdr.TestDIDB.URI()
v.ID = &otherID

err := validator.Validate(*v)
err := validator.Validate(v)

assert.Error(t, err)
assert.EqualError(t, err, "validation failed: credential ID must start with issuer")
})

t.Run("failed - missing nuts context", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
v.Context = []ssi.URI{stringToURI("https://www.w3.org/2018/credentials/v1")}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: context 'https://nuts.nl/credentials/v1' is required")
})
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestNutsAuthorizationCredentialValidator_Validate(t *testing.T) {
func TestAllFieldsDefinedValidator(t *testing.T) {
validator := AllFieldsDefinedValidator{jsonld.NewTestJSONLDManager(t).DocumentLoader()}
t.Run("ok", func(t *testing.T) {
inputVC := *ValidNutsOrganizationCredential()
inputVC := ValidNutsOrganizationCredential(t)

err := validator.Validate(inputVC)

Expand All @@ -387,7 +387,7 @@ func TestAllFieldsDefinedValidator(t *testing.T) {
"city": "EIbergen",
}

inputVC := *ValidNutsOrganizationCredential()
inputVC := ValidNutsOrganizationCredential(t)
inputVC.CredentialSubject[0] = invalidCredentialSubject

err := validator.Validate(inputVC)
Expand All @@ -400,7 +400,7 @@ func TestDefaultCredentialValidator(t *testing.T) {
validator := defaultCredentialValidator{}

t.Run("ok - NutsOrganizationCredential", func(t *testing.T) {
err := validator.Validate(*ValidNutsOrganizationCredential())
err := validator.Validate(ValidNutsOrganizationCredential(t))

assert.NoError(t, err)
})
Expand All @@ -420,37 +420,37 @@ func TestDefaultCredentialValidator(t *testing.T) {
})

t.Run("failed - missing ID", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
v.ID = nil

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'ID' is required")
})

t.Run("failed - missing proof", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
v.Proof = nil

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: 'proof' is required for JSON-LD credentials")
})

t.Run("failed - missing default context", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
v.Context = []ssi.URI{stringToURI(NutsV1Context)}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: default context is required")
})

t.Run("failed - missing default type", func(t *testing.T) {
v := ValidNutsOrganizationCredential()
v := ValidNutsOrganizationCredential(t)
v.Type = []ssi.URI{stringToURI(NutsOrganizationCredentialType)}

err := validator.Validate(*v)
err := validator.Validate(v)

assert.EqualError(t, err, "validation failed: type 'VerifiableCredential' is required")
})
Expand Down
10 changes: 3 additions & 7 deletions vcr/pe/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"encoding/json"
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/vc"
"github.com/nuts-foundation/nuts-node/vcr/credential"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
)

Expand Down Expand Up @@ -93,9 +93,7 @@ var testCredentialString = `
func TestMatch(t *testing.T) {
presentationDefinition := PresentationDefinition{}
_ = json.Unmarshal([]byte(testPresentationDefinition), &presentationDefinition)
verifiableCredential := vc.VerifiableCredential{}
vcJSON, _ := os.ReadFile("../test/vc.json")
_ = json.Unmarshal(vcJSON, &verifiableCredential)
verifiableCredential := credential.ValidNutsOrganizationCredential(t)

t.Run("Happy flow", func(t *testing.T) {
presentationSubmission, vcs, err := presentationDefinition.Match([]vc.VerifiableCredential{verifiableCredential})
Expand All @@ -116,9 +114,7 @@ func TestMatch(t *testing.T) {
}

func Test_matchFormat(t *testing.T) {
verifiableCredential := vc.VerifiableCredential{}
vcJSON, _ := os.ReadFile("../test/vc.json")
_ = json.Unmarshal(vcJSON, &verifiableCredential)
verifiableCredential := credential.ValidNutsOrganizationCredential(t)

t.Run("no format", func(t *testing.T) {
match := matchFormat(nil, vc.VerifiableCredential{})
Expand Down
Loading

0 comments on commit c092258

Please sign in to comment.