oidc_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package oauth2 // import "miniflux.app/v2/internal/oauth2"
  4. import (
  5. "slices"
  6. "testing"
  7. )
  8. func TestGroupsFromClaims(t *testing.T) {
  9. scenarios := []struct {
  10. name string
  11. claims map[string]any
  12. expectedGroups []string
  13. expectedFound bool
  14. expectedError bool
  15. }{
  16. {
  17. name: "missing claim",
  18. claims: map[string]any{"email": "user@example.org"},
  19. },
  20. {
  21. name: "single group",
  22. claims: map[string]any{"groups": "admins"},
  23. expectedGroups: []string{"admins"},
  24. expectedFound: true,
  25. },
  26. {
  27. name: "list of groups",
  28. claims: map[string]any{"groups": []any{"users", "admins"}},
  29. expectedGroups: []string{"users", "admins"},
  30. expectedFound: true,
  31. },
  32. {
  33. name: "empty list",
  34. claims: map[string]any{"groups": []any{}},
  35. expectedGroups: []string{},
  36. expectedFound: true,
  37. },
  38. {
  39. name: "list with a non-string entry",
  40. claims: map[string]any{"groups": []any{"admins", 42.0}},
  41. expectedFound: true,
  42. expectedError: true,
  43. },
  44. {
  45. name: "unsupported type",
  46. claims: map[string]any{"groups": map[string]any{"name": "admins"}},
  47. expectedFound: true,
  48. expectedError: true,
  49. },
  50. }
  51. for _, scenario := range scenarios {
  52. t.Run(scenario.name, func(t *testing.T) {
  53. groups, found, err := groupsFromClaims(scenario.claims, "groups")
  54. if (err != nil) != scenario.expectedError {
  55. t.Fatalf("Expected error %v, got %v", scenario.expectedError, err)
  56. }
  57. if found != scenario.expectedFound {
  58. t.Errorf("Expected found %v, got %v", scenario.expectedFound, found)
  59. }
  60. if !slices.Equal(groups, scenario.expectedGroups) {
  61. t.Errorf("Expected groups %v, got %v", scenario.expectedGroups, groups)
  62. }
  63. })
  64. }
  65. }