Skip to content
Snippets Groups Projects
Commit df826b19 authored by Lyuben Penkovski's avatar Lyuben Penkovski
Browse files

Unit tests for infohub export functionality

parent 4ede88fc
Branches
Tags
1 merge request!4Unit tests for infohub export functionality
Pipeline #51555 passed
......@@ -7,6 +7,7 @@ require (
github.com/hyperledger/aries-framework-go v0.1.8
github.com/kelseyhightower/envconfig v1.4.0
github.com/piprate/json-gold v0.4.1
github.com/stretchr/testify v1.7.0
go.mongodb.org/mongo-driver v1.9.1
go.uber.org/zap v1.21.0
goa.design/goa/v3 v3.7.5
......@@ -15,67 +16,35 @@ require (
require (
github.com/VictoriaMetrics/fastcache v1.5.7 // indirect
github.com/armon/go-metrics v0.3.9 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 // indirect
github.com/dimfeld/httptreemux/v5 v5.4.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/tink/go v1.6.1-0.20210519071714-58be99b3c4d0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v0.16.2 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.3 // indirect
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-secure-stdlib/mlock v0.1.1 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.5 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/go-version v1.2.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/vault/api v1.6.0 // indirect
github.com/hashicorp/vault/sdk v0.5.0 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/hyperledger/aries-framework-go/spi v0.0.0-20220322085443-50e8f9bd208b // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/kilic/bls12-381 v0.1.1-0.20210503002446-7b7597926c69 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/mr-tron/base58 v1.1.3 // indirect
github.com/multiformats/go-base32 v0.0.3 // indirect
github.com/multiformats/go-multibase v0.0.1 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pierrec/lz4 v2.5.2+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/smartystreets/assertions v1.13.0 // indirect
github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
......@@ -89,16 +58,11 @@ require (
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220317061510-51cd9980dadf // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
google.golang.org/grpc v1.46.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
This diff is collapsed.
package signer_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/piprate/json-gold/ld"
"github.com/stretchr/testify/assert"
"code.vereign.com/gaiax/tsa/infohub/internal/clients/signer"
)
const invalidPresentation = `{"invalid":"verifiable_presentation"}`
const validPresentation = `{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"id": "did:123",
"type": "VerifiablePresentation",
"verifiableCredential": [
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"credentialSubject": {
"allow": true,
"id": "example/example/1.0",
"taskID": "0123456789abcdef"
},
"issuanceDate": "2022-06-14T08:43:22.78309334Z",
"issuer": "https://example.com",
"type": "VerifiableCredential"
},
{
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://www.w3.org/2018/credentials/examples/v1"
],
"credentialSubject": {
"id": "example/example/2.0",
"result": {
"hello": "world"
}
},
"issuanceDate": "2022-06-14T08:43:22.783102173Z",
"issuer": "https://example.com",
"type": "VerifiableCredential"
}
]
}`
func TestClient_PresentationProof(t *testing.T) {
tests := []struct {
name string
ctx context.Context
vp *verifiable.Presentation
handler http.HandlerFunc
result *verifiable.Presentation
errtext string
}{
{
name: "error creating http request because of nil context",
errtext: "net/http: nil Context",
},
{
name: "signer server returns unexpected response code",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
},
errtext: "unexpected response from signer: 503 Service Unavailable",
},
{
name: "signer server returns empty response",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
errtext: "JSON unmarshalling of verifiable presentation: unexpected end of JSON input",
},
{
name: "signer server returns invalid verifiable presentation",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(invalidPresentation))
},
errtext: "verifiable presentation is not valid",
},
{
name: "signer server returns valid presentation",
ctx: context.Background(),
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(validPresentation))
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
signerSrv := httptest.NewServer(test.handler)
client := signer.New(signerSrv.URL, signer.WithHTTPClient(http.DefaultClient))
result, err := client.PresentationProof(test.ctx, test.vp)
if err != nil {
assert.Nil(t, result)
assert.NotEmpty(t, test.errtext)
assert.Contains(t, err.Error(), test.errtext)
} else {
assert.Empty(t, test.errtext)
assert.NotNil(t, result)
// parse the string representation to compare with the returned result
vp, err := verifiable.ParsePresentation(
[]byte(validPresentation),
verifiable.WithPresJSONLDDocumentLoader(ld.NewDefaultDocumentLoader(http.DefaultClient)),
verifiable.WithPresDisabledProofCheck(),
)
assert.NoError(t, err)
assert.Equal(t, vp, result)
}
})
}
}
// Code generated by counterfeiter. DO NOT EDIT.
package infohubfakes
import (
"context"
"sync"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub"
)
type FakeCache struct {
GetStub func(context.Context, string, string, string) ([]byte, error)
getMutex sync.RWMutex
getArgsForCall []struct {
arg1 context.Context
arg2 string
arg3 string
arg4 string
}
getReturns struct {
result1 []byte
result2 error
}
getReturnsOnCall map[int]struct {
result1 []byte
result2 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakeCache) Get(arg1 context.Context, arg2 string, arg3 string, arg4 string) ([]byte, error) {
fake.getMutex.Lock()
ret, specificReturn := fake.getReturnsOnCall[len(fake.getArgsForCall)]
fake.getArgsForCall = append(fake.getArgsForCall, struct {
arg1 context.Context
arg2 string
arg3 string
arg4 string
}{arg1, arg2, arg3, arg4})
stub := fake.GetStub
fakeReturns := fake.getReturns
fake.recordInvocation("Get", []interface{}{arg1, arg2, arg3, arg4})
fake.getMutex.Unlock()
if stub != nil {
return stub(arg1, arg2, arg3, arg4)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeCache) GetCallCount() int {
fake.getMutex.RLock()
defer fake.getMutex.RUnlock()
return len(fake.getArgsForCall)
}
func (fake *FakeCache) GetCalls(stub func(context.Context, string, string, string) ([]byte, error)) {
fake.getMutex.Lock()
defer fake.getMutex.Unlock()
fake.GetStub = stub
}
func (fake *FakeCache) GetArgsForCall(i int) (context.Context, string, string, string) {
fake.getMutex.RLock()
defer fake.getMutex.RUnlock()
argsForCall := fake.getArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
}
func (fake *FakeCache) GetReturns(result1 []byte, result2 error) {
fake.getMutex.Lock()
defer fake.getMutex.Unlock()
fake.GetStub = nil
fake.getReturns = struct {
result1 []byte
result2 error
}{result1, result2}
}
func (fake *FakeCache) GetReturnsOnCall(i int, result1 []byte, result2 error) {
fake.getMutex.Lock()
defer fake.getMutex.Unlock()
fake.GetStub = nil
if fake.getReturnsOnCall == nil {
fake.getReturnsOnCall = make(map[int]struct {
result1 []byte
result2 error
})
}
fake.getReturnsOnCall[i] = struct {
result1 []byte
result2 error
}{result1, result2}
}
func (fake *FakeCache) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.getMutex.RLock()
defer fake.getMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
}
return copiedInvocations
}
func (fake *FakeCache) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
fake.invocations = map[string][][]interface{}{}
}
if fake.invocations[key] == nil {
fake.invocations[key] = [][]interface{}{}
}
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ infohub.Cache = new(FakeCache)
// Code generated by counterfeiter. DO NOT EDIT.
package infohubfakes
import (
"sync"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
)
type FakeCredentials struct {
NewCredentialStub func([]string, string, map[string]interface{}, bool) (*verifiable.Credential, error)
newCredentialMutex sync.RWMutex
newCredentialArgsForCall []struct {
arg1 []string
arg2 string
arg3 map[string]interface{}
arg4 bool
}
newCredentialReturns struct {
result1 *verifiable.Credential
result2 error
}
newCredentialReturnsOnCall map[int]struct {
result1 *verifiable.Credential
result2 error
}
NewPresentationStub func([]string, ...*verifiable.Credential) (*verifiable.Presentation, error)
newPresentationMutex sync.RWMutex
newPresentationArgsForCall []struct {
arg1 []string
arg2 []*verifiable.Credential
}
newPresentationReturns struct {
result1 *verifiable.Presentation
result2 error
}
newPresentationReturnsOnCall map[int]struct {
result1 *verifiable.Presentation
result2 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakeCredentials) NewCredential(arg1 []string, arg2 string, arg3 map[string]interface{}, arg4 bool) (*verifiable.Credential, error) {
var arg1Copy []string
if arg1 != nil {
arg1Copy = make([]string, len(arg1))
copy(arg1Copy, arg1)
}
fake.newCredentialMutex.Lock()
ret, specificReturn := fake.newCredentialReturnsOnCall[len(fake.newCredentialArgsForCall)]
fake.newCredentialArgsForCall = append(fake.newCredentialArgsForCall, struct {
arg1 []string
arg2 string
arg3 map[string]interface{}
arg4 bool
}{arg1Copy, arg2, arg3, arg4})
stub := fake.NewCredentialStub
fakeReturns := fake.newCredentialReturns
fake.recordInvocation("NewCredential", []interface{}{arg1Copy, arg2, arg3, arg4})
fake.newCredentialMutex.Unlock()
if stub != nil {
return stub(arg1, arg2, arg3, arg4)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeCredentials) NewCredentialCallCount() int {
fake.newCredentialMutex.RLock()
defer fake.newCredentialMutex.RUnlock()
return len(fake.newCredentialArgsForCall)
}
func (fake *FakeCredentials) NewCredentialCalls(stub func([]string, string, map[string]interface{}, bool) (*verifiable.Credential, error)) {
fake.newCredentialMutex.Lock()
defer fake.newCredentialMutex.Unlock()
fake.NewCredentialStub = stub
}
func (fake *FakeCredentials) NewCredentialArgsForCall(i int) ([]string, string, map[string]interface{}, bool) {
fake.newCredentialMutex.RLock()
defer fake.newCredentialMutex.RUnlock()
argsForCall := fake.newCredentialArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
}
func (fake *FakeCredentials) NewCredentialReturns(result1 *verifiable.Credential, result2 error) {
fake.newCredentialMutex.Lock()
defer fake.newCredentialMutex.Unlock()
fake.NewCredentialStub = nil
fake.newCredentialReturns = struct {
result1 *verifiable.Credential
result2 error
}{result1, result2}
}
func (fake *FakeCredentials) NewCredentialReturnsOnCall(i int, result1 *verifiable.Credential, result2 error) {
fake.newCredentialMutex.Lock()
defer fake.newCredentialMutex.Unlock()
fake.NewCredentialStub = nil
if fake.newCredentialReturnsOnCall == nil {
fake.newCredentialReturnsOnCall = make(map[int]struct {
result1 *verifiable.Credential
result2 error
})
}
fake.newCredentialReturnsOnCall[i] = struct {
result1 *verifiable.Credential
result2 error
}{result1, result2}
}
func (fake *FakeCredentials) NewPresentation(arg1 []string, arg2 ...*verifiable.Credential) (*verifiable.Presentation, error) {
var arg1Copy []string
if arg1 != nil {
arg1Copy = make([]string, len(arg1))
copy(arg1Copy, arg1)
}
fake.newPresentationMutex.Lock()
ret, specificReturn := fake.newPresentationReturnsOnCall[len(fake.newPresentationArgsForCall)]
fake.newPresentationArgsForCall = append(fake.newPresentationArgsForCall, struct {
arg1 []string
arg2 []*verifiable.Credential
}{arg1Copy, arg2})
stub := fake.NewPresentationStub
fakeReturns := fake.newPresentationReturns
fake.recordInvocation("NewPresentation", []interface{}{arg1Copy, arg2})
fake.newPresentationMutex.Unlock()
if stub != nil {
return stub(arg1, arg2...)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeCredentials) NewPresentationCallCount() int {
fake.newPresentationMutex.RLock()
defer fake.newPresentationMutex.RUnlock()
return len(fake.newPresentationArgsForCall)
}
func (fake *FakeCredentials) NewPresentationCalls(stub func([]string, ...*verifiable.Credential) (*verifiable.Presentation, error)) {
fake.newPresentationMutex.Lock()
defer fake.newPresentationMutex.Unlock()
fake.NewPresentationStub = stub
}
func (fake *FakeCredentials) NewPresentationArgsForCall(i int) ([]string, []*verifiable.Credential) {
fake.newPresentationMutex.RLock()
defer fake.newPresentationMutex.RUnlock()
argsForCall := fake.newPresentationArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeCredentials) NewPresentationReturns(result1 *verifiable.Presentation, result2 error) {
fake.newPresentationMutex.Lock()
defer fake.newPresentationMutex.Unlock()
fake.NewPresentationStub = nil
fake.newPresentationReturns = struct {
result1 *verifiable.Presentation
result2 error
}{result1, result2}
}
func (fake *FakeCredentials) NewPresentationReturnsOnCall(i int, result1 *verifiable.Presentation, result2 error) {
fake.newPresentationMutex.Lock()
defer fake.newPresentationMutex.Unlock()
fake.NewPresentationStub = nil
if fake.newPresentationReturnsOnCall == nil {
fake.newPresentationReturnsOnCall = make(map[int]struct {
result1 *verifiable.Presentation
result2 error
})
}
fake.newPresentationReturnsOnCall[i] = struct {
result1 *verifiable.Presentation
result2 error
}{result1, result2}
}
func (fake *FakeCredentials) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.newCredentialMutex.RLock()
defer fake.newCredentialMutex.RUnlock()
fake.newPresentationMutex.RLock()
defer fake.newPresentationMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
}
return copiedInvocations
}
func (fake *FakeCredentials) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
fake.invocations = map[string][][]interface{}{}
}
if fake.invocations[key] == nil {
fake.invocations[key] = [][]interface{}{}
}
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ infohub.Credentials = new(FakeCredentials)
// Code generated by counterfeiter. DO NOT EDIT.
package infohubfakes
import (
"context"
"sync"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub"
)
type FakePolicy struct {
EvaluateStub func(context.Context, string, interface{}, string) ([]byte, error)
evaluateMutex sync.RWMutex
evaluateArgsForCall []struct {
arg1 context.Context
arg2 string
arg3 interface{}
arg4 string
}
evaluateReturns struct {
result1 []byte
result2 error
}
evaluateReturnsOnCall map[int]struct {
result1 []byte
result2 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakePolicy) Evaluate(arg1 context.Context, arg2 string, arg3 interface{}, arg4 string) ([]byte, error) {
fake.evaluateMutex.Lock()
ret, specificReturn := fake.evaluateReturnsOnCall[len(fake.evaluateArgsForCall)]
fake.evaluateArgsForCall = append(fake.evaluateArgsForCall, struct {
arg1 context.Context
arg2 string
arg3 interface{}
arg4 string
}{arg1, arg2, arg3, arg4})
stub := fake.EvaluateStub
fakeReturns := fake.evaluateReturns
fake.recordInvocation("Evaluate", []interface{}{arg1, arg2, arg3, arg4})
fake.evaluateMutex.Unlock()
if stub != nil {
return stub(arg1, arg2, arg3, arg4)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakePolicy) EvaluateCallCount() int {
fake.evaluateMutex.RLock()
defer fake.evaluateMutex.RUnlock()
return len(fake.evaluateArgsForCall)
}
func (fake *FakePolicy) EvaluateCalls(stub func(context.Context, string, interface{}, string) ([]byte, error)) {
fake.evaluateMutex.Lock()
defer fake.evaluateMutex.Unlock()
fake.EvaluateStub = stub
}
func (fake *FakePolicy) EvaluateArgsForCall(i int) (context.Context, string, interface{}, string) {
fake.evaluateMutex.RLock()
defer fake.evaluateMutex.RUnlock()
argsForCall := fake.evaluateArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
}
func (fake *FakePolicy) EvaluateReturns(result1 []byte, result2 error) {
fake.evaluateMutex.Lock()
defer fake.evaluateMutex.Unlock()
fake.EvaluateStub = nil
fake.evaluateReturns = struct {
result1 []byte
result2 error
}{result1, result2}
}
func (fake *FakePolicy) EvaluateReturnsOnCall(i int, result1 []byte, result2 error) {
fake.evaluateMutex.Lock()
defer fake.evaluateMutex.Unlock()
fake.EvaluateStub = nil
if fake.evaluateReturnsOnCall == nil {
fake.evaluateReturnsOnCall = make(map[int]struct {
result1 []byte
result2 error
})
}
fake.evaluateReturnsOnCall[i] = struct {
result1 []byte
result2 error
}{result1, result2}
}
func (fake *FakePolicy) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.evaluateMutex.RLock()
defer fake.evaluateMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
}
return copiedInvocations
}
func (fake *FakePolicy) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
fake.invocations = map[string][][]interface{}{}
}
if fake.invocations[key] == nil {
fake.invocations[key] = [][]interface{}{}
}
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ infohub.Policy = new(FakePolicy)
// Code generated by counterfeiter. DO NOT EDIT.
package infohubfakes
import (
"context"
"sync"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
)
type FakeSigner struct {
PresentationProofStub func(context.Context, *verifiable.Presentation) (*verifiable.Presentation, error)
presentationProofMutex sync.RWMutex
presentationProofArgsForCall []struct {
arg1 context.Context
arg2 *verifiable.Presentation
}
presentationProofReturns struct {
result1 *verifiable.Presentation
result2 error
}
presentationProofReturnsOnCall map[int]struct {
result1 *verifiable.Presentation
result2 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakeSigner) PresentationProof(arg1 context.Context, arg2 *verifiable.Presentation) (*verifiable.Presentation, error) {
fake.presentationProofMutex.Lock()
ret, specificReturn := fake.presentationProofReturnsOnCall[len(fake.presentationProofArgsForCall)]
fake.presentationProofArgsForCall = append(fake.presentationProofArgsForCall, struct {
arg1 context.Context
arg2 *verifiable.Presentation
}{arg1, arg2})
stub := fake.PresentationProofStub
fakeReturns := fake.presentationProofReturns
fake.recordInvocation("PresentationProof", []interface{}{arg1, arg2})
fake.presentationProofMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeSigner) PresentationProofCallCount() int {
fake.presentationProofMutex.RLock()
defer fake.presentationProofMutex.RUnlock()
return len(fake.presentationProofArgsForCall)
}
func (fake *FakeSigner) PresentationProofCalls(stub func(context.Context, *verifiable.Presentation) (*verifiable.Presentation, error)) {
fake.presentationProofMutex.Lock()
defer fake.presentationProofMutex.Unlock()
fake.PresentationProofStub = stub
}
func (fake *FakeSigner) PresentationProofArgsForCall(i int) (context.Context, *verifiable.Presentation) {
fake.presentationProofMutex.RLock()
defer fake.presentationProofMutex.RUnlock()
argsForCall := fake.presentationProofArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeSigner) PresentationProofReturns(result1 *verifiable.Presentation, result2 error) {
fake.presentationProofMutex.Lock()
defer fake.presentationProofMutex.Unlock()
fake.PresentationProofStub = nil
fake.presentationProofReturns = struct {
result1 *verifiable.Presentation
result2 error
}{result1, result2}
}
func (fake *FakeSigner) PresentationProofReturnsOnCall(i int, result1 *verifiable.Presentation, result2 error) {
fake.presentationProofMutex.Lock()
defer fake.presentationProofMutex.Unlock()
fake.PresentationProofStub = nil
if fake.presentationProofReturnsOnCall == nil {
fake.presentationProofReturnsOnCall = make(map[int]struct {
result1 *verifiable.Presentation
result2 error
})
}
fake.presentationProofReturnsOnCall[i] = struct {
result1 *verifiable.Presentation
result2 error
}{result1, result2}
}
func (fake *FakeSigner) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.presentationProofMutex.RLock()
defer fake.presentationProofMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
}
return copiedInvocations
}
func (fake *FakeSigner) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
fake.invocations = map[string][][]interface{}{}
}
if fake.invocations[key] == nil {
fake.invocations[key] = [][]interface{}{}
}
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ infohub.Signer = new(FakeSigner)
// Code generated by counterfeiter. DO NOT EDIT.
package infohubfakes
import (
"context"
"sync"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub"
"code.vereign.com/gaiax/tsa/infohub/internal/storage"
)
type FakeStorage struct {
ExportConfigurationStub func(context.Context, string) (*storage.ExportConfiguration, error)
exportConfigurationMutex sync.RWMutex
exportConfigurationArgsForCall []struct {
arg1 context.Context
arg2 string
}
exportConfigurationReturns struct {
result1 *storage.ExportConfiguration
result2 error
}
exportConfigurationReturnsOnCall map[int]struct {
result1 *storage.ExportConfiguration
result2 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakeStorage) ExportConfiguration(arg1 context.Context, arg2 string) (*storage.ExportConfiguration, error) {
fake.exportConfigurationMutex.Lock()
ret, specificReturn := fake.exportConfigurationReturnsOnCall[len(fake.exportConfigurationArgsForCall)]
fake.exportConfigurationArgsForCall = append(fake.exportConfigurationArgsForCall, struct {
arg1 context.Context
arg2 string
}{arg1, arg2})
stub := fake.ExportConfigurationStub
fakeReturns := fake.exportConfigurationReturns
fake.recordInvocation("ExportConfiguration", []interface{}{arg1, arg2})
fake.exportConfigurationMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeStorage) ExportConfigurationCallCount() int {
fake.exportConfigurationMutex.RLock()
defer fake.exportConfigurationMutex.RUnlock()
return len(fake.exportConfigurationArgsForCall)
}
func (fake *FakeStorage) ExportConfigurationCalls(stub func(context.Context, string) (*storage.ExportConfiguration, error)) {
fake.exportConfigurationMutex.Lock()
defer fake.exportConfigurationMutex.Unlock()
fake.ExportConfigurationStub = stub
}
func (fake *FakeStorage) ExportConfigurationArgsForCall(i int) (context.Context, string) {
fake.exportConfigurationMutex.RLock()
defer fake.exportConfigurationMutex.RUnlock()
argsForCall := fake.exportConfigurationArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeStorage) ExportConfigurationReturns(result1 *storage.ExportConfiguration, result2 error) {
fake.exportConfigurationMutex.Lock()
defer fake.exportConfigurationMutex.Unlock()
fake.ExportConfigurationStub = nil
fake.exportConfigurationReturns = struct {
result1 *storage.ExportConfiguration
result2 error
}{result1, result2}
}
func (fake *FakeStorage) ExportConfigurationReturnsOnCall(i int, result1 *storage.ExportConfiguration, result2 error) {
fake.exportConfigurationMutex.Lock()
defer fake.exportConfigurationMutex.Unlock()
fake.ExportConfigurationStub = nil
if fake.exportConfigurationReturnsOnCall == nil {
fake.exportConfigurationReturnsOnCall = make(map[int]struct {
result1 *storage.ExportConfiguration
result2 error
})
}
fake.exportConfigurationReturnsOnCall[i] = struct {
result1 *storage.ExportConfiguration
result2 error
}{result1, result2}
}
func (fake *FakeStorage) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.exportConfigurationMutex.RLock()
defer fake.exportConfigurationMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
}
return copiedInvocations
}
func (fake *FakeStorage) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
fake.invocations = map[string][][]interface{}{}
}
if fake.invocations[key] == nil {
fake.invocations[key] = [][]interface{}{}
}
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ infohub.Storage = new(FakeStorage)
......@@ -12,6 +12,12 @@ import (
"code.vereign.com/gaiax/tsa/infohub/internal/storage"
)
//go:generate counterfeiter . Storage
//go:generate counterfeiter . Policy
//go:generate counterfeiter . Cache
//go:generate counterfeiter . Credentials
//go:generate counterfeiter . Signer
var exportAccepted = map[string]interface{}{"result": "export request is accepted"}
type Storage interface {
......
package infohub_test
import (
"context"
"testing"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"code.vereign.com/gaiax/tsa/golib/errors"
goasigner "code.vereign.com/gaiax/tsa/infohub/gen/infohub"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub"
"code.vereign.com/gaiax/tsa/infohub/internal/service/infohub/infohubfakes"
"code.vereign.com/gaiax/tsa/infohub/internal/storage"
)
func TestNew(t *testing.T) {
svc := infohub.New(nil, nil, nil, nil, nil, zap.NewNop())
assert.Implements(t, (*goasigner.Service)(nil), svc)
}
func TestService_Export(t *testing.T) {
tests := []struct {
name string
req *goasigner.ExportRequest
storage *infohubfakes.FakeStorage
policy *infohubfakes.FakePolicy
cache *infohubfakes.FakeCache
cred *infohubfakes.FakeCredentials
signer *infohubfakes.FakeSigner
res interface{}
errkind errors.Kind
errtext string
}{
{
name: "export configuration not found",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return nil, errors.New(errors.NotFound, "export configuration not found")
},
},
errkind: errors.NotFound,
errtext: "export configuration not found",
},
{
name: "error getting export configuration",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return nil, errors.New("some error")
},
},
errkind: errors.Unknown,
errtext: "some error",
},
{
name: "error getting export data",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return nil, errors.New("some error")
},
},
errkind: errors.Unknown,
errtext: "some error",
},
{
name: "export data not found and triggering export process fails",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return nil, errors.New(errors.NotFound, "no data")
},
},
policy: &infohubfakes.FakePolicy{
EvaluateStub: func(ctx context.Context, policy string, input interface{}, cachekey string) ([]byte, error) {
return nil, errors.New("error evaluation policy")
},
},
errkind: errors.Unknown,
errtext: "error evaluation policy",
},
{
name: "export triggering successfully",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return nil, errors.New(errors.NotFound, "no data")
},
},
policy: &infohubfakes.FakePolicy{
EvaluateStub: func(ctx context.Context, policy string, input interface{}, cachekey string) ([]byte, error) {
return []byte(`{"allow":"true"}`), nil
},
},
res: map[string]interface{}{"result": "export request is accepted"},
},
{
name: "export data is not valid json",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return []byte(`invalid json`), nil
},
},
errkind: errors.Unknown,
errtext: "error creating export: invalid character",
},
{
name: "error creating verifiable credential for data",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return []byte(`{"allow":true}`), nil
},
},
cred: &infohubfakes.FakeCredentials{
NewCredentialStub: func(contexts []string, policy string, data map[string]interface{}, proof bool) (*verifiable.Credential, error) {
return nil, errors.New("cannot create credential")
},
},
errkind: errors.Unknown,
errtext: "cannot create credential",
},
{
name: "error creating verifiable presentation for data",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return []byte(`{"allow":true}`), nil
},
},
cred: &infohubfakes.FakeCredentials{
NewCredentialStub: func(contexts []string, policy string, data map[string]interface{}, proof bool) (*verifiable.Credential, error) {
return &verifiable.Credential{}, nil
},
NewPresentationStub: func(contexts []string, credentials ...*verifiable.Credential) (*verifiable.Presentation, error) {
return nil, errors.New("cannot create presentation")
},
},
errkind: errors.Unknown,
errtext: "cannot create presentation",
},
{
name: "error getting verifiable presentation proof",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return []byte(`{"allow":true}`), nil
},
},
cred: &infohubfakes.FakeCredentials{
NewCredentialStub: func(contexts []string, policy string, data map[string]interface{}, proof bool) (*verifiable.Credential, error) {
return &verifiable.Credential{}, nil
},
NewPresentationStub: func(contexts []string, credentials ...*verifiable.Credential) (*verifiable.Presentation, error) {
return &verifiable.Presentation{}, nil
},
},
signer: &infohubfakes.FakeSigner{
PresentationProofStub: func(ctx context.Context, presentation *verifiable.Presentation) (*verifiable.Presentation, error) {
return nil, errors.New(errors.ServiceUnavailable, "service unavailable")
},
},
errkind: errors.ServiceUnavailable,
errtext: "service unavailable",
},
{
name: "exported data is returned successfully",
req: &goasigner.ExportRequest{ExportName: "testexport"},
storage: &infohubfakes.FakeStorage{
ExportConfigurationStub: func(ctx context.Context, s string) (*storage.ExportConfiguration, error) {
return &storage.ExportConfiguration{
ExportName: "testexport",
Contexts: []string{"https://www.w3.org/2018/credentials/examples/v1"},
Policies: map[string]interface{}{"test/test/1.0": map[string]interface{}{"hello": "test world"}},
}, nil
},
},
cache: &infohubfakes.FakeCache{
GetStub: func(ctx context.Context, key string, namespace string, scope string) ([]byte, error) {
return []byte(`{"allow":true}`), nil
},
},
cred: &infohubfakes.FakeCredentials{
NewCredentialStub: func(contexts []string, policy string, data map[string]interface{}, proof bool) (*verifiable.Credential, error) {
return &verifiable.Credential{}, nil
},
NewPresentationStub: func(contexts []string, credentials ...*verifiable.Credential) (*verifiable.Presentation, error) {
return &verifiable.Presentation{}, nil
},
},
signer: &infohubfakes.FakeSigner{
PresentationProofStub: func(ctx context.Context, presentation *verifiable.Presentation) (*verifiable.Presentation, error) {
return &verifiable.Presentation{ID: "did:ocm"}, nil
},
},
res: &verifiable.Presentation{ID: "did:ocm"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
svc := infohub.New(test.storage, test.policy, test.cache, test.cred, test.signer, zap.NewNop())
res, err := svc.Export(context.Background(), test.req)
if err != nil {
assert.Nil(t, res)
assert.NotEmpty(t, test.errtext)
e, ok := err.(*errors.Error)
assert.True(t, ok)
assert.Equal(t, test.errkind, e.Kind)
assert.Contains(t, e.Error(), test.errtext)
} else {
assert.Empty(t, test.errtext)
assert.NotNil(t, res)
assert.Equal(t, test.res, res)
}
})
}
}
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment