Skip to content

Commit e34bf0b

Browse files
committed
chore: add tests for misuses of NewAuthorizationHasher
Add tests for misuses of NewAuthorizationHasher and improve an error message.
1 parent 208ed46 commit e34bf0b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

authorization/hasher_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package authorization_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/go-crypt/crypt/algorithm"
7+
"github.com/influxdata/influxdb/v2/authorization"
8+
influxdb2_algo "github.com/influxdata/influxdb/v2/pkg/crypt/algorithm/influxdb2"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func Test_NewAuthorizationHasher_EmptyDecoderVariants(t *testing.T) {
13+
hasher, err := authorization.NewAuthorizationHasher(
14+
authorization.WithDecoderVariants([]influxdb2_algo.Variant{}),
15+
)
16+
17+
require.ErrorIs(t, err, authorization.ErrNoDecoders)
18+
require.Nil(t, hasher)
19+
}
20+
21+
func TestNewAuthorizationHasher_WithInvalidDecoderVariant(t *testing.T) {
22+
// Test that using an invalid decoder variant returns an error
23+
hasher, err := authorization.NewAuthorizationHasher(
24+
authorization.WithDecoderVariants([]influxdb2_algo.Variant{
25+
influxdb2_algo.Variant(-1), // Invalid variant
26+
}),
27+
)
28+
29+
// Should return an error and nil hasher
30+
require.ErrorIs(t, err, algorithm.ErrParameterInvalid)
31+
require.Contains(t, err.Error(), "registering variant")
32+
require.Nil(t, hasher)
33+
}
34+
35+
func TestNewAuthorizationHasher_WithHasherVariantInvalid(t *testing.T) {
36+
// Test that using VariantNone returns an error
37+
hasher, err := authorization.NewAuthorizationHasher(
38+
authorization.WithHasherVariant(influxdb2_algo.Variant(-1)),
39+
)
40+
41+
// Should return an error and nil hasher
42+
require.ErrorIs(t, err, algorithm.ErrParameterInvalid)
43+
require.ErrorContains(t, err, "creating hasher")
44+
require.Nil(t, hasher)
45+
}

pkg/crypt/algorithm/influxdb2/variant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (v Variant) RegisterDecoder(r algorithm.DecoderRegister) error {
6262
case VariantSHA512:
6363
return RegisterDecoderSHA512(r)
6464
default:
65-
return fmt.Errorf("RegisterDecoder with invalid variant %v", v)
65+
return fmt.Errorf("RegisterDecoder with invalid variant %v: %w", v, algorithm.ErrParameterInvalid)
6666
}
6767
}
6868

0 commit comments

Comments
 (0)