gitea源码

access_token_scope_test.go 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package auth
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. type scopeTestNormalize struct {
  10. in AccessTokenScope
  11. out AccessTokenScope
  12. err error
  13. }
  14. func TestAccessTokenScope_Normalize(t *testing.T) {
  15. assert.Equal(t, []string{"activitypub", "admin", "issue", "misc", "notification", "organization", "package", "repository", "user"}, GetAccessTokenCategories())
  16. tests := []scopeTestNormalize{
  17. {"", "", nil},
  18. {"write:misc,write:notification,read:package,write:notification,public-only", "public-only,write:misc,write:notification,read:package", nil},
  19. {"all", "all", nil},
  20. {"write:activitypub,write:admin,write:misc,write:notification,write:organization,write:package,write:issue,write:repository,write:user", "all", nil},
  21. {"write:activitypub,write:admin,write:misc,write:notification,write:organization,write:package,write:issue,write:repository,write:user,public-only", "public-only,all", nil},
  22. }
  23. for _, scope := range GetAccessTokenCategories() {
  24. tests = append(tests,
  25. scopeTestNormalize{AccessTokenScope("read:" + scope), AccessTokenScope("read:" + scope), nil},
  26. scopeTestNormalize{AccessTokenScope("write:" + scope), AccessTokenScope("write:" + scope), nil},
  27. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%[1]s,read:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
  28. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
  29. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s,write:%[1]s", scope)), AccessTokenScope("write:" + scope), nil},
  30. )
  31. }
  32. for _, test := range tests {
  33. t.Run(string(test.in), func(t *testing.T) {
  34. scope, err := test.in.Normalize()
  35. assert.Equal(t, test.out, scope)
  36. assert.Equal(t, test.err, err)
  37. })
  38. }
  39. }
  40. type scopeTestHasScope struct {
  41. in AccessTokenScope
  42. scope AccessTokenScope
  43. out bool
  44. err error
  45. }
  46. func TestAccessTokenScope_HasScope(t *testing.T) {
  47. tests := []scopeTestHasScope{
  48. {"read:admin", "write:package", false, nil},
  49. {"all", "write:package", true, nil},
  50. {"write:package", "all", false, nil},
  51. {"public-only", "read:issue", false, nil},
  52. }
  53. for _, scope := range GetAccessTokenCategories() {
  54. tests = append(tests,
  55. scopeTestHasScope{
  56. AccessTokenScope("read:" + scope),
  57. AccessTokenScope("read:" + scope), true, nil,
  58. },
  59. scopeTestHasScope{
  60. AccessTokenScope("write:" + scope),
  61. AccessTokenScope("write:" + scope), true, nil,
  62. },
  63. scopeTestHasScope{
  64. AccessTokenScope("write:" + scope),
  65. AccessTokenScope("read:" + scope), true, nil,
  66. },
  67. scopeTestHasScope{
  68. AccessTokenScope("read:" + scope),
  69. AccessTokenScope("write:" + scope), false, nil,
  70. },
  71. )
  72. }
  73. for _, test := range tests {
  74. t.Run(string(test.in), func(t *testing.T) {
  75. hasScope, err := test.in.HasScope(test.scope)
  76. assert.Equal(t, test.out, hasScope)
  77. assert.Equal(t, test.err, err)
  78. })
  79. }
  80. }