Skip to content

Commit

Permalink
Add tests for ResolveMetadata protected header methods
Browse files Browse the repository at this point in the history
Implemented tests for the ResolveMetadata struct to verify the behavior of GetProtectedHeaderString and GetProtectedHeaderChain methods. This ensures correct functionality for different input scenarios, including cases where headers do not exist or values are of incorrect types.
  • Loading branch information
rolandgroen committed Nov 1, 2024
1 parent 9a80a81 commit fc20b9e
Showing 1 changed file with 99 additions and 1 deletion.
100 changes: 99 additions & 1 deletion vdr/resolver/did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"crypto/elliptic"
"crypto/rand"
"errors"
"github.com/nuts-foundation/go-did"
"github.com/lestrrat-go/jwx/v2/cert"
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/did"
"github.com/nuts-foundation/nuts-node/crypto/hash"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -241,3 +242,100 @@ func TestChainedDIDResolver_Resolve(t *testing.T) {
assert.ErrorIs(t, err, ErrNotFound)
})
}

func TestResolveMetadata_GetProtectedHeaderString(t *testing.T) {
tt := []struct {
name string
inputKey string
testKey string
inputValue any
expectedOk bool
}{
{"no JwtProtectedHeaders",
"",
"testKey",
"",
false,
},
{"key not exist",
"testKey",
"otherKey",
"testValue",
false,
},
{"key exists",
"testKey",
"testKey",
"testValue",
true,
},
{"no sting value",
"testKey",
"testKey",
time.Now(),
false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
meta := &ResolveMetadata{}
if tc.inputKey != "" {
meta.JwtProtectedHeaders = make(map[string]interface{})
meta.JwtProtectedHeaders[tc.inputKey] = tc.inputValue
}
foundValue, foundOk := meta.GetProtectedHeaderString(tc.testKey)
assert.Equal(t, tc.expectedOk, foundOk)
if tc.expectedOk {
assert.Equal(t, tc.inputValue, foundValue)
}
})
}
}
func TestResolveMetadata_GetProtectedHeaderChain(t *testing.T) {
tt := []struct {
name string
inputKey string
testKey string
inputValue any
expectedOk bool
}{
{"no JwtProtectedHeaders",
"",
"testKey",
"",
false,
},
{"key not exist",
"testKey",
"otherKey",
&cert.Chain{},
false,
},
{"key exists",
"testKey",
"testKey",
&cert.Chain{},
true,
},
{"no chain value",
"testKey",
"testKey",
time.Now(),
false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
meta := &ResolveMetadata{}
if tc.inputKey != "" {
meta.JwtProtectedHeaders = make(map[string]interface{})
meta.JwtProtectedHeaders[tc.inputKey] = tc.inputValue
}
foundValue, foundOk := meta.GetProtectedHeaderChain(tc.testKey)
assert.Equal(t, tc.expectedOk, foundOk)
if tc.expectedOk {
assert.Equal(t, tc.inputValue, foundValue)
}
})
}
}

0 comments on commit fc20b9e

Please sign in to comment.