You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

token_scope_test.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. tests := []scopeTestNormalize{
  16. {"", "", nil},
  17. {"write:misc,write:notification,read:package,write:notification,public-only", "public-only,write:misc,write:notification,read:package", nil},
  18. {"all", "all", nil},
  19. {"write:activitypub,write:admin,write:misc,write:notification,write:organization,write:package,write:issue,write:repository,write:user", "all", nil},
  20. {"write:activitypub,write:admin,write:misc,write:notification,write:organization,write:package,write:issue,write:repository,write:user,public-only", "public-only,all", nil},
  21. }
  22. for _, scope := range []string{"activitypub", "admin", "misc", "notification", "organization", "package", "issue", "repository", "user"} {
  23. tests = append(tests,
  24. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%s", scope)), AccessTokenScope(fmt.Sprintf("read:%s", scope)), nil},
  25. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
  26. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("write:%[1]s,read:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
  27. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
  28. scopeTestNormalize{AccessTokenScope(fmt.Sprintf("read:%[1]s,write:%[1]s,write:%[1]s", scope)), AccessTokenScope(fmt.Sprintf("write:%s", scope)), nil},
  29. )
  30. }
  31. for _, test := range tests {
  32. t.Run(string(test.in), func(t *testing.T) {
  33. scope, err := test.in.Normalize()
  34. assert.Equal(t, test.out, scope)
  35. assert.Equal(t, test.err, err)
  36. })
  37. }
  38. }
  39. type scopeTestHasScope struct {
  40. in AccessTokenScope
  41. scope AccessTokenScope
  42. out bool
  43. err error
  44. }
  45. func TestAccessTokenScope_HasScope(t *testing.T) {
  46. tests := []scopeTestHasScope{
  47. {"read:admin", "write:package", false, nil},
  48. {"all", "write:package", true, nil},
  49. {"write:package", "all", false, nil},
  50. {"public-only", "read:issue", false, nil},
  51. }
  52. for _, scope := range []string{"activitypub", "admin", "misc", "notification", "organization", "package", "issue", "repository", "user"} {
  53. tests = append(tests,
  54. scopeTestHasScope{
  55. AccessTokenScope(fmt.Sprintf("read:%s", scope)),
  56. AccessTokenScope(fmt.Sprintf("read:%s", scope)), true, nil,
  57. },
  58. scopeTestHasScope{
  59. AccessTokenScope(fmt.Sprintf("write:%s", scope)),
  60. AccessTokenScope(fmt.Sprintf("write:%s", scope)), true, nil,
  61. },
  62. scopeTestHasScope{
  63. AccessTokenScope(fmt.Sprintf("write:%s", scope)),
  64. AccessTokenScope(fmt.Sprintf("read:%s", scope)), true, nil,
  65. },
  66. scopeTestHasScope{
  67. AccessTokenScope(fmt.Sprintf("read:%s", scope)),
  68. AccessTokenScope(fmt.Sprintf("write:%s", scope)), false, nil,
  69. },
  70. )
  71. }
  72. for _, test := range tests {
  73. t.Run(string(test.in), func(t *testing.T) {
  74. hasScope, err := test.in.HasScope(test.scope)
  75. assert.Equal(t, test.out, hasScope)
  76. assert.Equal(t, test.err, err)
  77. })
  78. }
  79. }