85 lines
2.9 KiB
Go
85 lines
2.9 KiB
Go
package identity
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"image"
|
|
"image/png"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type proofProvider struct {
|
|
*providerStub
|
|
subject, key string
|
|
profile Profile
|
|
facts Facts
|
|
calls int
|
|
}
|
|
|
|
func (p *proofProvider) Verification(_ context.Context, subject string) (Verification, error) {
|
|
p.subject = subject
|
|
return Verification{Status: "unverified"}, nil
|
|
}
|
|
func (p *proofProvider) SubmitVerification(_ context.Context, subject, key string, profile Profile, facts Facts, _, _ []byte) (Verification, error) {
|
|
p.subject = subject
|
|
p.key = key
|
|
p.profile = profile
|
|
p.facts = facts
|
|
p.calls++
|
|
return Verification{Status: "pending", SubmissionID: "receipt"}, nil
|
|
}
|
|
func proofImage(t *testing.T, width, height int) []byte {
|
|
t.Helper()
|
|
var body bytes.Buffer
|
|
require.NoError(t, png.Encode(&body, image.NewNRGBA(image.Rect(0, 0, width, height))))
|
|
return body.Bytes()
|
|
}
|
|
func TestVerificationUsesBoundSubjectAndStableCustomerKey(t *testing.T) {
|
|
s, _, base, a := fixture()
|
|
p := &proofProvider{providerStub: base}
|
|
s.Provider = p
|
|
ctx := t.Context()
|
|
result, err := s.Verification(ctx, a)
|
|
require.NoError(t, err)
|
|
require.Equal(t, a.Name, result.Profile.FullName)
|
|
require.Equal(t, "subject-1", p.subject)
|
|
photo := proofImage(t, 600, 600)
|
|
profile := Profile{FullName: "Legal Customer Name", DateOfBirth: "1990-04-23", ResidenceCountry: "PL"}
|
|
_, err = s.SubmitVerification(ctx, a, "0123456789012345", profile, photo, photo)
|
|
require.NoError(t, err)
|
|
key := p.key
|
|
require.Equal(t, a.ID.Hex(), p.facts.CustomerRef)
|
|
require.Equal(t, a.Login, p.facts.Email)
|
|
require.Equal(t, profile, p.profile)
|
|
_, err = s.SubmitVerification(ctx, a, "0123456789012345", profile, photo, photo)
|
|
require.NoError(t, err)
|
|
require.Equal(t, key, p.key)
|
|
require.Equal(t, 1, base.registrations)
|
|
other := a.Copy()
|
|
other.ID[0]++
|
|
_, err = s.SubmitVerification(ctx, other, "0123456789012345", profile, photo, photo)
|
|
require.NoError(t, err)
|
|
require.NotEqual(t, key, p.key)
|
|
}
|
|
func TestVerificationValidationBeforeExternalHandoff(t *testing.T) {
|
|
s, _, base, a := fixture()
|
|
p := &proofProvider{providerStub: base}
|
|
s.Provider = p
|
|
photo := proofImage(t, 600, 600)
|
|
for _, profile := range []Profile{{}, {FullName: "Name", DateOfBirth: "1990-02-30", ResidenceCountry: "PL"}, {FullName: "Name", DateOfBirth: "2990-04-23", ResidenceCountry: "PL"}, {FullName: "Name", DateOfBirth: "1990-04-23", ResidenceCountry: "USA"}} {
|
|
_, err := s.SubmitVerification(t.Context(), a, "0123456789012345", profile, photo, photo)
|
|
require.Error(t, err)
|
|
}
|
|
require.Zero(t, p.calls)
|
|
require.Zero(t, base.registrations)
|
|
require.Error(t, ValidatePhoto([]byte("not an image")))
|
|
require.Error(t, ValidatePhoto(proofImage(t, 599, 600)))
|
|
require.Error(t, ValidatePhoto(photo[:len(photo)-20]))
|
|
require.Error(t, ValidatePhoto(make([]byte, MaxPhotoBytes+1)))
|
|
var unavailableService *Service
|
|
_, err := unavailableService.Verification(t.Context(), a)
|
|
require.Error(t, err)
|
|
}
|