gitea源码

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package oauth2_provider
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestGrantAdditionalScopes(t *testing.T) {
  9. tests := []struct {
  10. grantScopes string
  11. expectedScopes string
  12. }{
  13. {"", "all"}, // for old tokens without scope, treat it as "all"
  14. {"openid profile email", "all"},
  15. {"openid profile email groups", "all"},
  16. {"openid profile email all", "all"},
  17. {"openid profile email read:user all", "all"},
  18. {"openid profile email groups read:user", "read:user"},
  19. {"read:user read:repository", "read:repository,read:user"},
  20. {"read:user write:issue public-only", "public-only,write:issue,read:user"},
  21. {"openid profile email read:user", "read:user"},
  22. // TODO: at the moment invalid tokens are treated as "all" to avoid breaking 1.22 behavior (more details are in GrantAdditionalScopes)
  23. {"read:invalid_scope", "all"},
  24. {"read:invalid_scope,write:scope_invalid,just-plain-wrong", "all"},
  25. }
  26. for _, test := range tests {
  27. t.Run("scope:"+test.grantScopes, func(t *testing.T) {
  28. result := GrantAdditionalScopes(test.grantScopes)
  29. assert.Equal(t, test.expectedScopes, string(result))
  30. })
  31. }
  32. }