s3_iam_role_selection_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package s3api
  2. import (
  3. "testing"
  4. "github.com/seaweedfs/seaweedfs/weed/iam/providers"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestSelectPrimaryRole(t *testing.T) {
  8. s3iam := &S3IAMIntegration{}
  9. t.Run("empty_roles_returns_empty", func(t *testing.T) {
  10. identity := &providers.ExternalIdentity{Attributes: make(map[string]string)}
  11. result := s3iam.selectPrimaryRole([]string{}, identity)
  12. assert.Equal(t, "", result)
  13. })
  14. t.Run("single_role_returns_that_role", func(t *testing.T) {
  15. identity := &providers.ExternalIdentity{Attributes: make(map[string]string)}
  16. result := s3iam.selectPrimaryRole([]string{"admin"}, identity)
  17. assert.Equal(t, "admin", result)
  18. })
  19. t.Run("multiple_roles_returns_first", func(t *testing.T) {
  20. identity := &providers.ExternalIdentity{Attributes: make(map[string]string)}
  21. roles := []string{"viewer", "manager", "admin"}
  22. result := s3iam.selectPrimaryRole(roles, identity)
  23. assert.Equal(t, "viewer", result, "Should return first role")
  24. })
  25. t.Run("order_matters", func(t *testing.T) {
  26. identity := &providers.ExternalIdentity{Attributes: make(map[string]string)}
  27. // Test different orderings
  28. roles1 := []string{"admin", "viewer", "manager"}
  29. result1 := s3iam.selectPrimaryRole(roles1, identity)
  30. assert.Equal(t, "admin", result1)
  31. roles2 := []string{"viewer", "admin", "manager"}
  32. result2 := s3iam.selectPrimaryRole(roles2, identity)
  33. assert.Equal(t, "viewer", result2)
  34. roles3 := []string{"manager", "admin", "viewer"}
  35. result3 := s3iam.selectPrimaryRole(roles3, identity)
  36. assert.Equal(t, "manager", result3)
  37. })
  38. t.Run("complex_enterprise_roles", func(t *testing.T) {
  39. identity := &providers.ExternalIdentity{Attributes: make(map[string]string)}
  40. roles := []string{
  41. "finance-readonly",
  42. "hr-manager",
  43. "it-system-admin",
  44. "guest-viewer",
  45. }
  46. result := s3iam.selectPrimaryRole(roles, identity)
  47. // Should return the first role
  48. assert.Equal(t, "finance-readonly", result, "Should return first role in list")
  49. })
  50. }