From 6772f8e1ceb5f19f1142dba6e0b7fbde4bad86f5 Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Wed, 14 Feb 2024 17:02:55 +0100 Subject: [PATCH] more tests --- .github/codecov.yml | 3 +- internal/oidc/redis_test.go | 17 ++++----- internal/oidc/session_test.go | 31 ++++++++++++++++ internal/oidc/token_test.go | 68 +++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 internal/oidc/token_test.go diff --git a/.github/codecov.yml b/.github/codecov.yml index 18dcfbb..d55b309 100644 --- a/.github/codecov.yml +++ b/.github/codecov.yml @@ -14,11 +14,10 @@ ignore: coverage: status: - # require coverage to not be worse than previously project: default: target: auto - threshold: 0% + threshold: 5% patch: default: target: auto diff --git a/internal/oidc/redis_test.go b/internal/oidc/redis_test.go index 0f4206f..f0a8664 100644 --- a/internal/oidc/redis_test.go +++ b/internal/oidc/redis_test.go @@ -20,8 +20,6 @@ import ( "time" "github.com/alicebob/miniredis/v2" - "github.com/lestrrat-go/jwx/jwa" - "github.com/lestrrat-go/jwx/jwt" "github.com/redis/go-redis/v9" "github.com/stretchr/testify/require" ) @@ -69,12 +67,11 @@ func TestRedisTokenResponse(t *testing.T) { require.Empty(t, rt.RefreshToken) } -func newToken() string { - token, _ := jwt.NewBuilder(). - Issuer("authservice"). - Subject("user"). - Expiration(time.Now().Add(time.Hour)). - Build() - signed, _ := jwt.Sign(token, jwa.HS256, []byte("key")) - return string(signed) +func TestRedisPingError(t *testing.T) { + mr := miniredis.RunT(t) + client := redis.NewClient(&redis.Options{Addr: mr.Addr()}) + mr.SetError("ping error") + + _, err := NewRedisStore(&Clock{}, client, 0, 1*time.Minute) + require.EqualError(t, err, "ping error") } diff --git a/internal/oidc/session_test.go b/internal/oidc/session_test.go index a599e8d..704a543 100644 --- a/internal/oidc/session_test.go +++ b/internal/oidc/session_test.go @@ -93,3 +93,34 @@ func TestSessionStoreFactory(t *testing.T) { require.Equal(t, redis1.Addr(), store.Get(config.Chains[2].Filters[0].GetOidc()).(*redisStore).client.(*redis.Client).Options().Addr) require.Equal(t, redis2.Addr(), store.Get(config.Chains[3].Filters[0].GetOidc()).(*redisStore).client.(*redis.Client).Options().Addr) } + +func TestSessionStoreFactoryRedisFails(t *testing.T) { + mr := miniredis.RunT(t) + config := &configv1.Config{ + ListenAddress: "0.0.0.0", + ListenPort: 8080, + LogLevel: "debug", + Threads: 1, + Chains: []*configv1.FilterChain{ + { + Name: "redis", + Filters: []*configv1.Filter{ + { + Type: &configv1.Filter_Oidc{ + Oidc: &oidcv1.OIDCConfig{ + RedisSessionStoreConfig: &oidcv1.RedisConfig{ServerUri: "redis://" + mr.Addr()}, + }, + }, + }, + }, + }, + }, + } + + store := SessionStoreFactory{Config: config} + g := run.Group{Logger: telemetry.NoopLogger()} + g.Register(&store) + + mr.SetError("server error") + require.ErrorContains(t, g.Run(), "server error") +} diff --git a/internal/oidc/token_test.go b/internal/oidc/token_test.go new file mode 100644 index 0000000..0fb618c --- /dev/null +++ b/internal/oidc/token_test.go @@ -0,0 +1,68 @@ +// Copyright 2024 Tetrate +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oidc + +import ( + "testing" + "time" + + "github.com/lestrrat-go/jwx/jwa" + "github.com/lestrrat-go/jwx/jwt" + "github.com/stretchr/testify/require" +) + +func TestTokenResponse(t *testing.T) { + t.Run("valid", func(t *testing.T) { + tr := &TokenResponse{ + IDToken: newToken(), + AccessToken: newToken(), + RefreshToken: newToken(), + } + + it, err := tr.GetIDToken() + require.NoError(t, err) + require.Equal(t, "authservice", it.Issuer()) + + at, err := tr.GetAccessToken() + require.NoError(t, err) + require.Equal(t, "authservice", at.Issuer()) + + rt, err := tr.GetRefreshToken() + require.NoError(t, err) + require.Equal(t, "authservice", rt.Issuer()) + }) + + t.Run("invalid", func(t *testing.T) { + tr := &TokenResponse{} + _, err := tr.GetIDToken() + require.Error(t, err) + + _, err = tr.GetAccessToken() + require.Error(t, err) + + _, err = tr.GetRefreshToken() + require.Error(t, err) + }) +} + +func newToken() string { + token, _ := jwt.NewBuilder(). + Issuer("authservice"). + Subject("user"). + Expiration(time.Now().Add(time.Hour)). + Build() + signed, _ := jwt.Sign(token, jwa.HS256, []byte("key")) + return string(signed) +}