mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-01 15:19:09 +01:00
7a89d321c8
Fixes: https://codeberg.org/forgejo/forgejo/issues/820 (cherry picked from commit6a7022ebbb
) (cherry picked from commit764eac47b5
) (cherry picked from commit1141eb7b6f
) (cherry picked from commit826b6509b6
) (cherry picked from commit9990d932b8
) (cherry picked from commit7eca570743
) (cherry picked from commit66e1d3f082
) (cherry picked from commit188226a8e6
) (cherry picked from commit4cd1bff25c
) (cherry picked from commitfad6b6d2c4
) (cherry picked from commit5b25c3d851
) (cherry picked from commit4746ece4dd
) (cherry picked from commit2a6f85afb3
) (cherry picked from commitc027d724ee
) (cherry picked from commitbe2f1eeaeb
) (cherry picked from commit3058a54fe9
) (cherry picked from commit53936d38a0
) (cherry picked from commit311983cc97
) (cherry picked from commit1651ae757b
) (cherry picked from commitd3dd8ea24d
) (cherry picked from commit9a80326ff3
) (cherry picked from commit66eb33235e
) (cherry picked from commit769e24d5a8
) (cherry picked from commit436cc21217
) (cherry picked from commit817faca7f0
) (cherry picked from commit80ee08aef1
) (cherry picked from commit15f8885d0c
) (cherry picked from commit0944a4442c
) (cherry picked from commit91631d41b0
) (cherry picked from commit0fbda3386f
) (cherry picked from commita464b0e2ba
) (cherry picked from commit0b98d50c92
) (cherry picked from commit6365d4b761
) (cherry picked from commit3af5715dbc
)
90 lines
3.2 KiB
Go
90 lines
3.2 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type scopeTestNormalize struct {
|
|
in AccessTokenScope
|
|
out AccessTokenScope
|
|
err error
|
|
}
|
|
|
|
func TestAccessTokenScope_Normalize(t *testing.T) {
|
|
tests := []scopeTestNormalize{
|
|
{"", "", nil},
|
|
{"write:misc,write:notification,read:package,write:notification,public-only", "public-only,write:misc,write:notification,read:package", nil},
|
|
{"all,sudo", "all", nil},
|
|
{"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 []string{"activitypub", "admin", "misc", "notification", "organization", "package", "issue", "repository", "user"} {
|
|
tests = append(tests,
|
|
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%s", scope)), AccessTokenScope(fmt.Sprintf("read:%s", scope)), nil},
|
|
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
|
|
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%[1]s,read:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
|
|
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
|
|
scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s,write:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
|
|
)
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|
|
|
|
type scopeTestHasScope struct {
|
|
in AccessTokenScope
|
|
scope AccessTokenScope
|
|
out bool
|
|
err error
|
|
}
|
|
|
|
func TestAccessTokenScope_HasScope(t *testing.T) {
|
|
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 []string{"activitypub", "admin", "misc", "notification", "organization", "package", "issue", "repository", "user"} {
|
|
tests = append(tests,
|
|
scopeTestHasScope{
|
|
AccessTokenScope(fmt.Sprintf("read:%s", scope)),
|
|
AccessTokenScope(fmt.Sprintf("read:%s", scope)), true, nil,
|
|
},
|
|
scopeTestHasScope{
|
|
AccessTokenScope(fmt.Sprintf("write:%s", scope)),
|
|
AccessTokenScope(fmt.Sprintf("write:%s", scope)), true, nil,
|
|
},
|
|
scopeTestHasScope{
|
|
AccessTokenScope(fmt.Sprintf("write:%s", scope)),
|
|
AccessTokenScope(fmt.Sprintf("read:%s", scope)), true, nil,
|
|
},
|
|
scopeTestHasScope{
|
|
AccessTokenScope(fmt.Sprintf("read:%s", scope)),
|
|
AccessTokenScope(fmt.Sprintf("write:%s", scope)), false, nil,
|
|
},
|
|
)
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(string(test.in), func(t *testing.T) {
|
|
hasScope, err := test.in.HasScope(test.scope)
|
|
assert.Equal(t, test.out, hasScope)
|
|
assert.Equal(t, test.err, err)
|
|
})
|
|
}
|
|
}
|