Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nacx committed Feb 14, 2024
1 parent 9f37091 commit 6772f8e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 7 additions & 10 deletions internal/oidc/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
31 changes: 31 additions & 0 deletions internal/oidc/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
68 changes: 68 additions & 0 deletions internal/oidc/token_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6772f8e

Please sign in to comment.