From 919d2f244bd24fc5d9a0cc45b23f2988076c0d8a Mon Sep 17 00:00:00 2001 From: Tobiasz Heller Date: Sat, 27 Apr 2024 15:24:58 +0200 Subject: [PATCH] fix: pass tests --- .env | 1 + Makefile | 2 +- cmd/pass/main.go | 2 +- docker-compose.yml | 5 +++-- e2e-tests/pass/http.go | 2 +- internal/pass/pairing/pairing.go | 4 +--- internal/pass/server.go | 4 +--- internal/pass/sign/lib_test.go | 27 +-------------------------- internal/pass/sync/sync.go | 6 +++--- 9 files changed, 13 insertions(+), 40 deletions(-) diff --git a/.env b/.env index ac39e60..cfd79d7 100644 --- a/.env +++ b/.env @@ -15,6 +15,7 @@ SECURITY_RATE_LIMIT_BE=100 SECURITY_RATE_LIMIT_MOBILE=100 PASS_ADDR=:8082 +FAKE_MOBILE_PUSH=true AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test diff --git a/Makefile b/Makefile index fa733f3..2434b37 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ tests-e2e: ## run end to end tests go test ./e2e-tests/mobile/... -count=1 go test ./e2e-tests/support/... -count=1 go test ./e2e-tests/system/... -count=1 - go test ./e2e-tests/pass/... -count=1 + PASS_ADDR="localhost:8088" go test ./e2e-tests/pass/... -count=1 vendor-licenses: ## report vendor licenses go-licenses report ./cmd/api --template licenses.tpl > licenses.json 2> licenses-errors diff --git a/cmd/pass/main.go b/cmd/pass/main.go index 3c770bd..9d91080 100644 --- a/cmd/pass/main.go +++ b/cmd/pass/main.go @@ -9,7 +9,7 @@ import ( ) func main() { - logging.WithDefaultField("service_name", "pass") + logging.Init(logging.Fields{"service_name": "pass"}) var cfg config.PassConfig err := envconfig.Process("", &cfg) diff --git a/docker-compose.yml b/docker-compose.yml index 7c4cf75..0dac1df 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -89,10 +89,11 @@ services: group_add: - '1000' ports: - - "8084:8082" + - "8088:8082" environment: # overwrite AWS_ENDPOINT from .env file. One in env is used to running app from local also. AWS_ENDPOINT: http://localstack-main:4566 + AWS_REGION: us-east-1 env_file: - .env depends_on: @@ -113,7 +114,7 @@ services: timeout: 5s retries: 5 volumes: - - "./tests/localstack_init.sh:/etc/localstack/init/ready.d/localstack_init.sh" # ready hook + - "./e2e-tests/localstack_init.sh:/etc/localstack/init/ready.d/localstack_init.sh" # ready hook - "./data/localstack:/var/lib/localstack" - "/var/run/docker.sock:/var/run/docker.sock" diff --git a/e2e-tests/pass/http.go b/e2e-tests/pass/http.go index 2668b40..3dd78e0 100644 --- a/e2e-tests/pass/http.go +++ b/e2e-tests/pass/http.go @@ -106,7 +106,7 @@ func getMobileToken(fcm string) (string, error) { MobileSyncConfirmToken string `json:"mobile_sync_confirm_token"` } if err := request("GET", fmt.Sprintf("/mobile/sync/%s/token", fcm), "", nil, &resp); err != nil { - return "", fmt.Errorf("failed to get mobile token") + return "", fmt.Errorf("failed to get mobile token: %w", err) } return resp.MobileSyncConfirmToken, nil diff --git a/internal/pass/pairing/pairing.go b/internal/pass/pairing/pairing.go index dde520b..eb54b03 100644 --- a/internal/pass/pairing/pairing.go +++ b/internal/pass/pairing/pairing.go @@ -7,8 +7,6 @@ import ( "time" "github.com/gorilla/websocket" - "github.com/sirupsen/logrus" - "github.com/twofas/2fas-server/internal/common/logging" "github.com/twofas/2fas-server/internal/pass/connection" "github.com/twofas/2fas-server/internal/pass/sign" @@ -128,7 +126,7 @@ func (p *Pairing) ServePairingWS(w http.ResponseWriter, r *http.Request, extID s } } -func (p *Pairing) isExtensionPaired(ctx context.Context, extID string, log *logrus.Entry) (PairingInfo, bool) { +func (p *Pairing) isExtensionPaired(ctx context.Context, extID string, log logging.FieldLogger) (PairingInfo, bool) { pairingInfo, err := p.store.GetPairingInfo(ctx, extID) if err != nil { log.Warn("Failed to get pairing info") diff --git a/internal/pass/server.go b/internal/pass/server.go index 3f83373..b3db1f5 100644 --- a/internal/pass/server.go +++ b/internal/pass/server.go @@ -59,9 +59,7 @@ func NewServer(cfg config.PassConfig) *Server { router := gin.New() router.Use(recovery.RecoveryMiddleware()) - router.Use(httphelpers.RequestIdMiddleware()) - router.Use(httphelpers.CorrelationIdMiddleware()) - // TODO: don't log auth headers. + router.Use(httphelpers.LoggingMiddleware()) router.Use(httphelpers.RequestJsonLogger()) router.GET("/health", func(context *gin.Context) { diff --git a/internal/pass/sign/lib_test.go b/internal/pass/sign/lib_test.go index 7eabe6f..8f95c29 100644 --- a/internal/pass/sign/lib_test.go +++ b/internal/pass/sign/lib_test.go @@ -8,10 +8,6 @@ import ( "testing" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/credentials" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/kms" "github.com/golang-jwt/jwt/v5" "github.com/google/uuid" ) @@ -70,16 +66,6 @@ func createTestService(t *testing.T) Service { } func TestSignAndVerify(t *testing.T) { - sess, err := session.NewSession(&aws.Config{ - Region: aws.String("us-east-1"), - Credentials: credentials.NewStaticCredentials("test", "test", ""), - S3ForcePathStyle: aws.Bool(true), - Endpoint: aws.String("http://localhost:4566"), - }) - if err != nil { - t.Fatal(err) - } - kmsClient := kms.New(sess) srv := createTestService(t) now := time.Now() @@ -128,18 +114,7 @@ func TestSignAndVerify(t *testing.T) { { name: "invalid signature", tokenFn: func() string { - resp, err := kmsClient.CreateKey(&kms.CreateKeyInput{ - KeySpec: aws.String("ECC_NIST_P256"), - KeyUsage: aws.String("SIGN_VERIFY"), - }) - if err != nil { - t.Fatal(err) - } - serviceWithAnotherKey, err := NewService(*resp.KeyMetadata.KeyId, kmsClient) - if err != nil { - t.Fatal(err) - } - + serviceWithAnotherKey := createTestService(t) token, err := serviceWithAnotherKey.SignAndEncode(Message{ ConnectionID: uuid.New().String(), ExpiresAt: now.Add(-time.Hour), diff --git a/internal/pass/sync/sync.go b/internal/pass/sync/sync.go index 980aea2..fb02dba 100644 --- a/internal/pass/sync/sync.go +++ b/internal/pass/sync/sync.go @@ -75,7 +75,7 @@ func (s *Syncing) ServeSyncingRequestWS(w http.ResponseWriter, r *http.Request, if err := s.sendTokenAndCloseConn(fcmToken, conn); err != nil { log.Errorf("Failed to send token: %v", err) } - log.Infof("Paring ws finished") + log.Infof("Sync ws finished") return nil } @@ -89,7 +89,7 @@ func (s *Syncing) ServeSyncingRequestWS(w http.ResponseWriter, r *http.Request, for { select { case <-maxWaitC: - log.Info("Closing paring ws after timeout") + log.Info("Closing sync ws after timeout") return nil case <-connectedCheckTicker.C: if syncConfirmed := s.isSyncConfirmed(r.Context(), fcmToken); syncConfirmed { @@ -97,7 +97,7 @@ func (s *Syncing) ServeSyncingRequestWS(w http.ResponseWriter, r *http.Request, log.Errorf("Failed to send token: %v", err) return nil } - log.Infof("Paring ws finished") + log.Infof("Sync ws finished") return nil } }