Files

92 lines
3.0 KiB
Go
Raw Permalink Normal View History

2023-01-17 16:46:03 -05:00
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package auth
import (
2023-06-04 14:57:16 -04:00
"fmt"
2023-01-17 16:46:03 -05:00
"testing"
"github.com/stretchr/testify/assert"
)
2023-06-04 14:57:16 -04:00
type scopeTestNormalize struct {
in AccessTokenScope
out AccessTokenScope
err error
}
2023-01-17 16:46:03 -05:00
func TestAccessTokenScope_Normalize(t *testing.T) {
assert.Equal(t, []string{"activitypub", "admin", "issue", "misc", "notification", "organization", "package", "repository", "user"}, GetAccessTokenCategories())
2023-06-04 14:57:16 -04:00
tests := []scopeTestNormalize{
2023-01-17 16:46:03 -05:00
{"", "", nil},
2023-06-04 14:57:16 -04:00
{"write:misc,write:notification,read:package,write:notification,public-only", "public-only,write:misc,write:notification,read:package", nil},
2023-01-17 16:46:03 -05:00
{"all", "all", nil},
2023-06-04 14:57:16 -04:00
{"write:activitypub,write:admin,write:misc,write:notification,write:organization,write:package,write:issue,write:repository,write:user", "all", nil},
{"write:activitypub,write:admin,write:misc,write:notification,write:organization,write:package,write:issue,write:repository,write:user,public-only", "public-only,all", nil},
}
for _, scope := range GetAccessTokenCategories() {
2023-06-04 14:57:16 -04:00
tests = append(tests,
2025-04-01 12:14:01 +02:00
scopeTestNormalize{AccessTokenScope("read:" + scope), AccessTokenScope("read:" + scope), nil},
scopeTestNormalize{AccessTokenScope("write:" + scope), AccessTokenScope("write:" + scope), nil},
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%[1]s,read:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s,write:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
2023-06-04 14:57:16 -04:00
)
2023-01-17 16:46:03 -05:00
}
for _, test := range tests {
t.Run(string(test.in), func(t *testing.T) {
scope, err := test.in.Normalize()
assert.Equal(t, test.out, scope)
assert.Equal(t, test.err, err)
})
}
}
2023-06-04 14:57:16 -04:00
type scopeTestHasScope struct {
in AccessTokenScope
scope AccessTokenScope
out bool
err error
}
2023-01-17 16:46:03 -05:00
func TestAccessTokenScope_HasScope(t *testing.T) {
2023-06-04 14:57:16 -04:00
tests := []scopeTestHasScope{
{"read:admin", "write:package", false, nil},
{"all", "write:package", true, nil},
{"write:package", "all", false, nil},
{"public-only", "read:issue", false, nil},
}
for _, scope := range GetAccessTokenCategories() {
2023-06-04 14:57:16 -04:00
tests = append(tests,
scopeTestHasScope{
2025-04-01 12:14:01 +02:00
AccessTokenScope("read:" + scope),
AccessTokenScope("read:" + scope), true, nil,
2023-06-04 14:57:16 -04:00
},
scopeTestHasScope{
2025-04-01 12:14:01 +02:00
AccessTokenScope("write:" + scope),
AccessTokenScope("write:" + scope), true, nil,
2023-06-04 14:57:16 -04:00
},
scopeTestHasScope{
2025-04-01 12:14:01 +02:00
AccessTokenScope("write:" + scope),
AccessTokenScope("read:" + scope), true, nil,
2023-06-04 14:57:16 -04:00
},
scopeTestHasScope{
2025-04-01 12:14:01 +02:00
AccessTokenScope("read:" + scope),
AccessTokenScope("write:" + scope), false, nil,
2023-06-04 14:57:16 -04:00
},
)
2023-01-17 16:46:03 -05:00
}
for _, test := range tests {
t.Run(string(test.in), func(t *testing.T) {
2023-06-04 14:57:16 -04:00
hasScope, err := test.in.HasScope(test.scope)
assert.Equal(t, test.out, hasScope)
2023-01-17 16:46:03 -05:00
assert.Equal(t, test.err, err)
})
}
}