From dd81dfb36d5a740e141fb4268b9ff18545ca922d Mon Sep 17 00:00:00 2001 From: Chris Rollins <3865054+chrisrollins65@users.noreply.github.com> Date: Wed, 16 Apr 2025 15:34:44 +0200 Subject: [PATCH 1/2] Set correct expiry time for UK access token --- uk_vat_service.go | 2 +- uk_vat_service_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/uk_vat_service.go b/uk_vat_service.go index dfcc63f..364c651 100644 --- a/uk_vat_service.go +++ b/uk_vat_service.go @@ -136,7 +136,7 @@ func GenerateUKAccessToken(opts ValidatorOpts) (*UKAccessToken, error) { return nil, ErrUnableToGenerateUKAccessToken{Err: err} } // set the expiration time to 1 minute before the actual expiration for safety - token.ExpiresAt = time.Now().Add(time.Duration(token.SecondsUntilExpires - 60)) + token.ExpiresAt = time.Now().Add(time.Duration(token.SecondsUntilExpires-60) * time.Second) if opts.IsUKTest { token.IsTest = true } diff --git a/uk_vat_service_test.go b/uk_vat_service_test.go index 6f0dd37..de6d5fa 100644 --- a/uk_vat_service_test.go +++ b/uk_vat_service_test.go @@ -6,6 +6,7 @@ package vat import ( "errors" "testing" + "time" ) // test numbers to use with the UK VAT service API in their sandbox environment: @@ -33,6 +34,14 @@ func TestUKVATService(t *testing.T) { if err != nil { t.Fatal(err) } + // expect the token to be valid for 14400 seconds (4 hours), + // but with ExpireAt 60 seconds earlier to help ensure we generate a new one before old one expires + expectedExpiresAt := time.Now().Add(time.Duration(14400-60) * time.Second) + isExpiresAtCorrect := expectedExpiresAt.Sub(token.ExpiresAt).Seconds() <= 1 // 1 second leeway for test to run + + if token.SecondsUntilExpires != 14400 || !isExpiresAtCorrect { + t.Errorf("Expected token to be valid for 14400 seconds, got %d", token.SecondsUntilExpires) + } opts.UKAccessToken = token for _, test := range ukTests { From 11627d2bb1b4f9f9a05441290c5e8fda8ebcb3d8 Mon Sep 17 00:00:00 2001 From: Chris Rollins <3865054+chrisrollins65@users.noreply.github.com> Date: Tue, 24 Feb 2026 10:47:37 +0100 Subject: [PATCH 2/2] Fix tests and load uk creds for tests from .env --- .gitignore | 1 + go.mod | 2 ++ go.sum | 2 ++ rates_test.go | 4 ++-- uk_vat_service_test.go | 17 +++++++++++++++-- vies_service_test.go | 2 ++ 6 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 9887811..55f91a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor c.out +.env diff --git a/go.mod b/go.mod index be2ce61..5d204ad 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module github.com/teamwork/vat/v3 go 1.21 require github.com/golang/mock v1.6.0 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go.sum b/go.sum index d067127..79eaa87 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= diff --git a/rates_test.go b/rates_test.go index a566cce..b34b861 100644 --- a/rates_test.go +++ b/rates_test.go @@ -17,8 +17,8 @@ func TestCountryRates_GetRate(t *testing.T) { } c, _ = GetCountryRates("RO") - if r, _ := c.GetRate("standard"); r != 19 { - t.Errorf("Standard VAT rate for RO is supposed to be 19. Got %.2f", r) + if r, _ := c.GetRate("standard"); r != 21 { + t.Errorf("Standard VAT rate for RO is supposed to be 21. Got %.2f", r) } } diff --git a/uk_vat_service_test.go b/uk_vat_service_test.go index de6d5fa..13a4ae5 100644 --- a/uk_vat_service_test.go +++ b/uk_vat_service_test.go @@ -5,8 +5,11 @@ package vat import ( "errors" + "os" "testing" "time" + + "github.com/joho/godotenv" ) // test numbers to use with the UK VAT service API in their sandbox environment: @@ -25,11 +28,21 @@ var ukTests = []struct { // TestUKVATService tests the UK VAT service. Just meant to be a quick way to check that this service is working. // Makes external calls that sometimes might fail. Do not include them in CI/CD. func TestUKVATService(t *testing.T) { + // Load environment variables from .env file + if err := godotenv.Load(); err != nil { + t.Fatalf("Error loading .env file: %v", err) + } + opts := ValidatorOpts{ - UKClientID: "yourClientID", // insert your own client ID and secret here. Do not commit real values. - UKClientSecret: "yourClientSecret", + UKClientID: os.Getenv("CLIENT_ID"), + UKClientSecret: os.Getenv("SECRET"), IsUKTest: true, } + + if opts.UKClientID == "" || opts.UKClientSecret == "" { + t.Fatal("CLIENT_ID and SECRET must be set in .env file") + } + token, err := GenerateUKAccessToken(opts) if err != nil { t.Fatal(err) diff --git a/vies_service_test.go b/vies_service_test.go index 0468e14..2c343cd 100644 --- a/vies_service_test.go +++ b/vies_service_test.go @@ -6,6 +6,7 @@ package vat import ( "errors" "testing" + "time" ) var viesTests = []struct { @@ -26,5 +27,6 @@ func TestViesService(t *testing.T) { if !errors.Is(err, test.expectedError) { t.Errorf("Expected <%v> for %v, got <%v>", test.expectedError, test.vatNumber, err) } + time.Sleep(time.Second * 2) // delay to prevent rate limiting } }