authorizer_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package azure
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. )
  7. func TestClientSecretCredential_Validate(t *testing.T) {
  8. tests := map[string]struct {
  9. csc *ClientSecretCredential
  10. wantErr bool
  11. }{
  12. "missing TenantID": {
  13. csc: &ClientSecretCredential{
  14. TenantID: "",
  15. ClientID: "clientID",
  16. ClientSecret: "clientSecret",
  17. },
  18. wantErr: true,
  19. },
  20. "missing ClientID": {
  21. csc: &ClientSecretCredential{
  22. TenantID: "tenantID",
  23. ClientID: "",
  24. ClientSecret: "clientSecret",
  25. },
  26. wantErr: true,
  27. },
  28. "missing ClientSecret": {
  29. csc: &ClientSecretCredential{
  30. TenantID: "tenantID",
  31. ClientID: "clientID",
  32. ClientSecret: "",
  33. },
  34. wantErr: true,
  35. },
  36. "valid": {
  37. csc: &ClientSecretCredential{
  38. TenantID: "tenantID",
  39. ClientID: "clientID",
  40. ClientSecret: "clientSecret",
  41. },
  42. wantErr: false,
  43. },
  44. }
  45. for name, tt := range tests {
  46. t.Run(name, func(t *testing.T) {
  47. if err := tt.csc.Validate(); (err != nil) != tt.wantErr {
  48. t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
  49. }
  50. })
  51. }
  52. }
  53. func TestClientSecretCredential_Sanitize(t *testing.T) {
  54. tests := map[string]struct {
  55. csc *ClientSecretCredential
  56. want cloud.Config
  57. }{
  58. "Plain integration": {
  59. csc: &ClientSecretCredential{
  60. TenantID: "tenantID",
  61. ClientID: "clientID",
  62. ClientSecret: "clientSecret",
  63. },
  64. want: &ClientSecretCredential{
  65. TenantID: "tenantID",
  66. ClientID: "clientID",
  67. ClientSecret: cloud.Redacted,
  68. },
  69. },
  70. }
  71. for name, tt := range tests {
  72. t.Run(name, func(t *testing.T) {
  73. if got := tt.csc.Sanitize(); !reflect.DeepEqual(got, tt.want) {
  74. t.Errorf("Sanitize() = %v, want %v", got, tt.want)
  75. }
  76. })
  77. }
  78. }
  79. func TestClientSecretCredential_Equals(t *testing.T) {
  80. tests := map[string]struct {
  81. csc *ClientSecretCredential
  82. config cloud.Config
  83. want bool
  84. }{
  85. "compare nil": {
  86. csc: &ClientSecretCredential{
  87. TenantID: "tenantID",
  88. ClientID: "clientID",
  89. ClientSecret: "clientSecret",
  90. },
  91. config: nil,
  92. want: false,
  93. },
  94. "different config": {
  95. csc: &ClientSecretCredential{
  96. TenantID: "tenantID",
  97. ClientID: "clientID",
  98. ClientSecret: "clientSecret",
  99. },
  100. config: &DefaultAzureCredentialHolder{},
  101. want: false,
  102. },
  103. "different TenantID": {
  104. csc: &ClientSecretCredential{
  105. TenantID: "tenantID",
  106. ClientID: "clientID",
  107. ClientSecret: "clientSecret",
  108. },
  109. config: &ClientSecretCredential{
  110. TenantID: "tenantID2",
  111. ClientID: "clientID",
  112. ClientSecret: "clientSecret",
  113. },
  114. want: false,
  115. },
  116. "different ClientID": {
  117. csc: &ClientSecretCredential{
  118. TenantID: "tenantID",
  119. ClientID: "clientID",
  120. ClientSecret: "clientSecret",
  121. },
  122. config: &ClientSecretCredential{
  123. TenantID: "tenantID",
  124. ClientID: "clientID2",
  125. ClientSecret: "clientSecret",
  126. },
  127. want: false,
  128. },
  129. "different ClientSecret": {
  130. csc: &ClientSecretCredential{
  131. TenantID: "tenantID",
  132. ClientID: "clientID",
  133. ClientSecret: "clientSecret2",
  134. },
  135. config: &ClientSecretCredential{
  136. TenantID: "tenantID2",
  137. ClientID: "clientID",
  138. ClientSecret: "clientSecret",
  139. },
  140. want: false,
  141. },
  142. "equal": {
  143. csc: &ClientSecretCredential{
  144. TenantID: "tenantID",
  145. ClientID: "clientID",
  146. ClientSecret: "clientSecret",
  147. },
  148. config: &ClientSecretCredential{
  149. TenantID: "tenantID",
  150. ClientID: "clientID",
  151. ClientSecret: "clientSecret",
  152. },
  153. want: true,
  154. },
  155. }
  156. for name, tt := range tests {
  157. t.Run(name, func(t *testing.T) {
  158. if got := tt.csc.Equals(tt.config); got != tt.want {
  159. t.Errorf("Equals() = %v, want %v", got, tt.want)
  160. }
  161. })
  162. }
  163. }