Skip to content

Commit afb30a4

Browse files
committed
Add GetPrefix method to AccessKey and update tests for consistency
1 parent 0011d9d commit afb30a4

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

access_key.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func (a AccessKey) GetProjectID() (projectID uint64, err error) {
4141
return 0, errors.Join(errs...)
4242
}
4343

44+
func (a AccessKey) GetPrefix() string {
45+
parts := strings.Split(a.String(), Separator)
46+
if len(parts) < 2 {
47+
return ""
48+
}
49+
return strings.Join(parts[:len(parts)-1], Separator)
50+
}
51+
4452
func NewAccessKey(ctx context.Context, projectID uint64) AccessKey {
4553
version, ok := GetVersion(ctx)
4654
if !ok {
@@ -55,14 +63,6 @@ func NewAccessKey(ctx context.Context, projectID uint64) AccessKey {
5563
return ""
5664
}
5765

58-
func GetAccessKeyPrefix(accessKey AccessKey) string {
59-
parts := strings.Split(accessKey.String(), Separator)
60-
if len(parts) < 2 {
61-
return ""
62-
}
63-
return strings.Join(parts[:len(parts)-1], Separator)
64-
}
65-
6666
type Encoding interface {
6767
Version() byte
6868
Encode(ctx context.Context, projectID uint64) AccessKey

access_key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ func TestAccessKeyEncoding(t *testing.T) {
3333
ctx := authcontrol.WithVersion(context.Background(), 2)
3434
projectID := uint64(12345)
3535
accessKey := authcontrol.NewAccessKey(ctx, projectID)
36-
t.Log("=> k", accessKey, "| prefix =>", authcontrol.GetAccessKeyPrefix(accessKey))
36+
t.Log("=> k", accessKey, "| prefix =>", accessKey.GetPrefix())
3737
outID, err := accessKey.GetProjectID()
3838
require.NoError(t, err)
3939
require.Equal(t, projectID, outID)
4040

4141
ctx = authcontrol.WithPrefix(ctx, "newprefix:dev")
4242

4343
accessKey2 := authcontrol.NewAccessKey(ctx, projectID)
44-
t.Log("=> k", accessKey2, "| prefix =>", authcontrol.GetAccessKeyPrefix(accessKey2))
44+
t.Log("=> k", accessKey2, "| prefix =>", accessKey2.GetPrefix())
4545
outID, err = accessKey2.GetProjectID()
4646
require.NoError(t, err)
4747
require.Equal(t, projectID, outID)
@@ -55,5 +55,5 @@ func TestAccessKeyEncoding(t *testing.T) {
5555
func TestDecode(t *testing.T) {
5656
ctx := authcontrol.WithVersion(context.Background(), 2)
5757
accessKey := authcontrol.NewAccessKey(ctx, 237)
58-
t.Log("=> k", accessKey, "| prefix =>", authcontrol.GetAccessKeyPrefix(accessKey))
58+
t.Log("=> k", accessKey, "| prefix =>", accessKey.GetPrefix())
5959
}

middleware_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ func TestCustomErrHandler(t *testing.T) {
325325

326326
r.Handle("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
327327

328-
var claims map[string]any
329-
claims = map[string]any{"service": "client_service"}
328+
claims := map[string]any{"service": "client_service"}
330329

331330
// Valid Request
332331
ok, err := executeRequest(t, ctx, r, fmt.Sprintf("/rpc/%s/%s", ServiceName, MethodName), accessKey(AccessKey), jwt(authcontrol.S2SToken(JWTSecret, claims)))

0 commit comments

Comments
 (0)