authorizer_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package ibm
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/opencost/opencost/core/pkg/util/json"
  6. "github.com/opencost/opencost/pkg/cloud"
  7. )
  8. func TestAPIKey_Validate(t *testing.T) {
  9. testCases := map[string]struct {
  10. auth APIKey
  11. expected error
  12. }{
  13. "valid": {
  14. auth: APIKey{Key: "ibm-api-key"},
  15. expected: nil,
  16. },
  17. "missing key": {
  18. auth: APIKey{Key: ""},
  19. expected: fmt.Errorf("APIKey: missing apiKey"),
  20. },
  21. }
  22. for name, tc := range testCases {
  23. t.Run(name, func(t *testing.T) {
  24. actual := tc.auth.Validate()
  25. actualString := "nil"
  26. if actual != nil {
  27. actualString = actual.Error()
  28. }
  29. expectedString := "nil"
  30. if tc.expected != nil {
  31. expectedString = tc.expected.Error()
  32. }
  33. if actualString != expectedString {
  34. t.Errorf("errors do not match: Actual: '%s', Expected: '%s'", actualString, expectedString)
  35. }
  36. })
  37. }
  38. }
  39. func TestAPIKey_Equals(t *testing.T) {
  40. testCases := map[string]struct {
  41. left APIKey
  42. right cloud.Config
  43. expected bool
  44. }{
  45. "matching": {
  46. left: APIKey{Key: "key"},
  47. right: &APIKey{Key: "key"},
  48. expected: true,
  49. },
  50. "different key": {
  51. left: APIKey{Key: "key"},
  52. right: &APIKey{Key: "other"},
  53. expected: false,
  54. },
  55. "wrong type": {
  56. left: APIKey{Key: "key"},
  57. right: nil,
  58. expected: false,
  59. },
  60. }
  61. for name, tc := range testCases {
  62. t.Run(name, func(t *testing.T) {
  63. if tc.left.Equals(tc.right) != tc.expected {
  64. t.Errorf("incorrect Equals result")
  65. }
  66. })
  67. }
  68. }
  69. func TestAPIKey_Sanitize(t *testing.T) {
  70. auth := &APIKey{Key: "secret-key"}
  71. sanitized := auth.Sanitize().(*APIKey)
  72. if sanitized.Key != cloud.Redacted {
  73. t.Errorf("expected redacted key, got %q", sanitized.Key)
  74. }
  75. }
  76. func TestAPIKey_JSON(t *testing.T) {
  77. auth := &APIKey{Key: "secret-key"}
  78. b, err := json.Marshal(auth)
  79. if err != nil {
  80. t.Fatalf("marshal: %v", err)
  81. }
  82. var raw map[string]any
  83. if err := json.Unmarshal(b, &raw); err != nil {
  84. t.Fatalf("unmarshal map: %v", err)
  85. }
  86. if raw[cloud.AuthorizerTypeProperty] != APIKeyAuthorizerType {
  87. t.Errorf("authorizerType = %v, want %s", raw[cloud.AuthorizerTypeProperty], APIKeyAuthorizerType)
  88. }
  89. selected, err := SelectAuthorizerByType(APIKeyAuthorizerType)
  90. if err != nil {
  91. t.Fatalf("SelectAuthorizerByType: %v", err)
  92. }
  93. unmarshalled := selected.(*APIKey)
  94. if err := json.Unmarshal(b, unmarshalled); err != nil {
  95. t.Fatalf("unmarshal authorizer: %v", err)
  96. }
  97. if !auth.Equals(unmarshalled) {
  98. t.Error("round-trip authorizer mismatch")
  99. }
  100. }
  101. func TestSelectAuthorizerByType_Invalid(t *testing.T) {
  102. _, err := SelectAuthorizerByType("not-a-type")
  103. if err == nil {
  104. t.Fatal("expected error for invalid authorizer type")
  105. }
  106. }