366 lines
9.4 KiB
Go
366 lines
9.4 KiB
Go
package identity
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestUSVerificationProvider(t *testing.T) {
|
|
provider := NewUSVerificationProvider("test-api-key", "https://test-api.privado.ai")
|
|
ctx := context.Background()
|
|
|
|
// Test citizenship verification
|
|
t.Run("VerifyCitizenship with valid ZKP proof", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "valid-zkp-proof-12345",
|
|
}
|
|
|
|
result, err := provider.VerifyCitizenship(ctx, userData)
|
|
if err != nil {
|
|
t.Errorf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if !result {
|
|
t.Error("Expected verification to pass")
|
|
}
|
|
})
|
|
|
|
t.Run("VerifyCitizenship with invalid ZKP proof", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "invalid-token",
|
|
}
|
|
|
|
result, err := provider.VerifyCitizenship(ctx, userData)
|
|
if err == nil {
|
|
t.Error("Expected error for invalid ZKP proof")
|
|
}
|
|
|
|
if result {
|
|
t.Error("Expected verification to fail")
|
|
}
|
|
})
|
|
|
|
t.Run("VerifyCitizenship with missing ZKP proof", func(t *testing.T) {
|
|
userData := map[string]string{}
|
|
|
|
result, err := provider.VerifyCitizenship(ctx, userData)
|
|
if err == nil {
|
|
t.Error("Expected error for missing ZKP proof")
|
|
}
|
|
|
|
if result {
|
|
t.Error("Expected verification to fail")
|
|
}
|
|
})
|
|
|
|
t.Run("VerifyCitizenship with service error", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "error-token",
|
|
}
|
|
|
|
result, err := provider.VerifyCitizenship(ctx, userData)
|
|
if err == nil {
|
|
t.Error("Expected error for service unavailable")
|
|
}
|
|
|
|
if result {
|
|
t.Error("Expected verification to fail")
|
|
}
|
|
})
|
|
|
|
// Test eligibility verification
|
|
t.Run("VerifyEligibility with true attestation and legal disclaimer", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"selfAttestedEligible": "true",
|
|
"legalDisclaimerAcknowledged": "true",
|
|
}
|
|
|
|
result, err := provider.VerifyEligibility(ctx, userData)
|
|
if err != nil {
|
|
t.Errorf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if !result {
|
|
t.Error("Expected eligibility to pass")
|
|
}
|
|
})
|
|
|
|
t.Run("VerifyEligibility with false attestation and legal disclaimer", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"selfAttestedEligible": "false",
|
|
"legalDisclaimerAcknowledged": "true",
|
|
}
|
|
|
|
result, err := provider.VerifyEligibility(ctx, userData)
|
|
if err != nil {
|
|
t.Errorf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if result {
|
|
t.Error("Expected eligibility to fail")
|
|
}
|
|
})
|
|
|
|
t.Run("VerifyEligibility without legal disclaimer acknowledgment", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"selfAttestedEligible": "true",
|
|
}
|
|
|
|
result, err := provider.VerifyEligibility(ctx, userData)
|
|
if err == nil {
|
|
t.Error("Expected error for missing legal disclaimer acknowledgment")
|
|
}
|
|
|
|
if result {
|
|
t.Error("Expected eligibility to fail")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCanadaVerificationProvider(t *testing.T) {
|
|
provider := NewCanadaVerificationProvider("test-api-key", "https://test-api.privado.ai")
|
|
ctx := context.Background()
|
|
|
|
// Test citizenship verification
|
|
t.Run("VerifyCitizenship with valid ZKP proof", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "valid-zkp-proof-canada",
|
|
}
|
|
|
|
result, err := provider.VerifyCitizenship(ctx, userData)
|
|
if err != nil {
|
|
t.Errorf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if !result {
|
|
t.Error("Expected verification to pass")
|
|
}
|
|
})
|
|
|
|
// Test eligibility verification
|
|
t.Run("VerifyEligibility with true attestation", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"selfAttestedEligible": "true",
|
|
"legalDisclaimerAcknowledged": "true",
|
|
}
|
|
|
|
result, err := provider.VerifyEligibility(ctx, userData)
|
|
if err != nil {
|
|
t.Errorf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if !result {
|
|
t.Error("Expected eligibility to pass")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPlaceholderVerificationProvider(t *testing.T) {
|
|
provider := NewPlaceholderVerificationProvider("UK")
|
|
ctx := context.Background()
|
|
userData := map[string]string{}
|
|
|
|
// Test citizenship verification
|
|
t.Run("VerifyCitizenship for placeholder provider", func(t *testing.T) {
|
|
result, err := provider.VerifyCitizenship(ctx, userData)
|
|
if err != nil {
|
|
t.Errorf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if !result {
|
|
t.Error("Expected placeholder verification to pass")
|
|
}
|
|
})
|
|
|
|
// Test eligibility verification
|
|
t.Run("VerifyEligibility for placeholder provider", func(t *testing.T) {
|
|
result, err := provider.VerifyEligibility(ctx, userData)
|
|
if err != nil {
|
|
t.Errorf("Expected no error, got %v", err)
|
|
}
|
|
|
|
if !result {
|
|
t.Error("Expected placeholder eligibility to pass")
|
|
}
|
|
})
|
|
|
|
// Test country code
|
|
t.Run("GetCountry for placeholder provider", func(t *testing.T) {
|
|
country := provider.GetCountry()
|
|
if country != "UK" {
|
|
t.Errorf("Expected country UK, got %s", country)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestVerificationService(t *testing.T) {
|
|
service := NewVerificationService()
|
|
ctx := context.Background()
|
|
|
|
// Register providers
|
|
usProvider := NewUSVerificationProvider("test-api-key", "https://test-api.privado.ai")
|
|
service.RegisterProvider(usProvider)
|
|
|
|
// Test US verification flow
|
|
t.Run("US verification with valid data", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "valid-zkp-proof-12345",
|
|
"selfAttestedEligible": "true",
|
|
"legalDisclaimerAcknowledged": "true",
|
|
}
|
|
|
|
result := service.VerifyUser(ctx, "US", userData)
|
|
|
|
if result.Status != StatusFullyEligible {
|
|
t.Errorf("Expected status %s, got %s", StatusFullyEligible, result.Status)
|
|
}
|
|
|
|
if !result.CitizenshipVerified {
|
|
t.Error("Expected citizenship to be verified")
|
|
}
|
|
|
|
if !result.EligibilityVerified {
|
|
t.Error("Expected eligibility to be verified")
|
|
}
|
|
})
|
|
|
|
t.Run("US verification with citizenship but not eligible", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "valid-zkp-proof-12345",
|
|
"selfAttestedEligible": "false",
|
|
"legalDisclaimerAcknowledged": "true",
|
|
}
|
|
|
|
result := service.VerifyUser(ctx, "US", userData)
|
|
|
|
if result.Status != StatusVerified {
|
|
t.Errorf("Expected status %s, got %s", StatusVerified, result.Status)
|
|
}
|
|
|
|
if !result.CitizenshipVerified {
|
|
t.Error("Expected citizenship to be verified")
|
|
}
|
|
|
|
if result.EligibilityVerified {
|
|
t.Error("Expected eligibility to not be verified")
|
|
}
|
|
})
|
|
|
|
t.Run("US verification with invalid ZKP", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "invalid-token",
|
|
}
|
|
|
|
result := service.VerifyUser(ctx, "US", userData)
|
|
|
|
if result.Status != StatusUnverified {
|
|
t.Errorf("Expected status %s, got %s", StatusUnverified, result.Status)
|
|
}
|
|
|
|
if result.CitizenshipVerified {
|
|
t.Error("Expected citizenship to not be verified")
|
|
}
|
|
|
|
if result.Error == nil {
|
|
t.Error("Expected an error")
|
|
}
|
|
|
|
verificationError, ok := result.Error.(*VerificationError)
|
|
if !ok {
|
|
t.Error("Expected a VerificationError")
|
|
} else if verificationError.Code != ErrorCodeInvalidZKP {
|
|
t.Errorf("Expected error code %s, got %s", ErrorCodeInvalidZKP, verificationError.Code)
|
|
}
|
|
})
|
|
|
|
t.Run("US verification with service unavailable", func(t *testing.T) {
|
|
userData := map[string]string{
|
|
"zkpProof": "error-token",
|
|
}
|
|
|
|
result := service.VerifyUser(ctx, "US", userData)
|
|
|
|
if result.Status != StatusUnverified {
|
|
t.Errorf("Expected status %s, got %s", StatusUnverified, result.Status)
|
|
}
|
|
|
|
if result.CitizenshipVerified {
|
|
t.Error("Expected citizenship to not be verified")
|
|
}
|
|
|
|
if result.Error == nil {
|
|
t.Error("Expected an error")
|
|
}
|
|
|
|
verificationError, ok := result.Error.(*VerificationError)
|
|
if !ok {
|
|
t.Error("Expected a VerificationError")
|
|
} else if verificationError.Code != ErrorCodeServiceUnavailable {
|
|
t.Errorf("Expected error code %s, got %s", ErrorCodeServiceUnavailable, verificationError.Code)
|
|
}
|
|
})
|
|
|
|
t.Run("Verification with timeout", func(t *testing.T) {
|
|
// Create a context with a short timeout
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Nanosecond)
|
|
defer cancel()
|
|
|
|
// Sleep to ensure the context times out
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
userData := map[string]string{
|
|
"zkpProof": "valid-zkp-proof-12345",
|
|
}
|
|
|
|
result := service.VerifyUser(timeoutCtx, "US", userData)
|
|
|
|
if result.Status != StatusUnverified {
|
|
t.Errorf("Expected status %s, got %s", StatusUnverified, result.Status)
|
|
}
|
|
|
|
if result.Error == nil {
|
|
t.Error("Expected an error")
|
|
}
|
|
})
|
|
|
|
// Test automatic provider creation for unknown country
|
|
t.Run("Unknown country gets placeholder provider", func(t *testing.T) {
|
|
userData := map[string]string{}
|
|
|
|
result := service.VerifyUser(ctx, "XYZ", userData)
|
|
|
|
if result.Status != StatusFullyEligible {
|
|
t.Errorf("Expected status %s, got %s", StatusFullyEligible, result.Status)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGetVerificationProvider(t *testing.T) {
|
|
t.Run("Get US provider", func(t *testing.T) {
|
|
provider := GetVerificationProvider("US")
|
|
|
|
if provider.GetCountry() != "US" {
|
|
t.Errorf("Expected US provider, got %s", provider.GetCountry())
|
|
}
|
|
|
|
_, isUSProvider := provider.(*USVerificationProvider)
|
|
if !isUSProvider {
|
|
t.Error("Expected USVerificationProvider type")
|
|
}
|
|
})
|
|
|
|
t.Run("Get placeholder provider for unknown country", func(t *testing.T) {
|
|
provider := GetVerificationProvider("XYZ")
|
|
|
|
if provider.GetCountry() != "XYZ" {
|
|
t.Errorf("Expected XYZ provider, got %s", provider.GetCountry())
|
|
}
|
|
|
|
_, isPlaceholderProvider := provider.(*PlaceholderVerificationProvider)
|
|
if !isPlaceholderProvider {
|
|
t.Error("Expected PlaceholderVerificationProvider type")
|
|
}
|
|
})
|
|
}
|