|
| 1 | +package apps |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/databricks/databricks-sdk-go/experimental/mocks" |
| 7 | + "github.com/databricks/databricks-sdk-go/service/oauth2" |
| 8 | + "github.com/stretchr/testify/mock" |
| 9 | + |
| 10 | + "github.com/databricks/terraform-provider-databricks/qa" |
| 11 | +) |
| 12 | + |
| 13 | +func TestResourceCustomAppIntegrationCreate(t *testing.T) { |
| 14 | + qa.ResourceFixture{ |
| 15 | + MockAccountClientFunc: func(a *mocks.MockAccountClient) { |
| 16 | + api := a.GetMockCustomAppIntegrationAPI().EXPECT() |
| 17 | + api.Create(mock.Anything, oauth2.CreateCustomAppIntegration{ |
| 18 | + Name: "custom_integration_name", |
| 19 | + RedirectUrls: []string{ |
| 20 | + "https://example.com", |
| 21 | + }, |
| 22 | + Scopes: []string{ |
| 23 | + "all", |
| 24 | + }, |
| 25 | + TokenAccessPolicy: &oauth2.TokenAccessPolicy{ |
| 26 | + AccessTokenTtlInMinutes: 60, |
| 27 | + RefreshTokenTtlInMinutes: 30, |
| 28 | + }, |
| 29 | + }).Return(&oauth2.CreateCustomAppIntegrationOutput{ |
| 30 | + ClientId: "client_id", |
| 31 | + ClientSecret: "client_secret", |
| 32 | + IntegrationId: "integration_id", |
| 33 | + }, nil) |
| 34 | + api.GetByIntegrationId(mock.Anything, "integration_id").Return( |
| 35 | + &oauth2.GetCustomAppIntegrationOutput{ |
| 36 | + Name: "custom_integration_name", |
| 37 | + RedirectUrls: []string{ |
| 38 | + "https://example.com", |
| 39 | + }, |
| 40 | + Scopes: []string{ |
| 41 | + "all", |
| 42 | + }, |
| 43 | + TokenAccessPolicy: &oauth2.TokenAccessPolicy{ |
| 44 | + AccessTokenTtlInMinutes: 60, |
| 45 | + RefreshTokenTtlInMinutes: 30, |
| 46 | + }, |
| 47 | + ClientId: "client_id", |
| 48 | + IntegrationId: "integration_id", |
| 49 | + }, nil, |
| 50 | + ) |
| 51 | + }, |
| 52 | + Create: true, |
| 53 | + AccountID: "account_id", |
| 54 | + HCL: ` |
| 55 | + name = "custom_integration_name" |
| 56 | + redirect_urls = ["https://example.com"] |
| 57 | + scopes = ["all"] |
| 58 | + token_access_policy { |
| 59 | + access_token_ttl_in_minutes = 60 |
| 60 | + refresh_token_ttl_in_minutes = 30 |
| 61 | + }`, |
| 62 | + Resource: ResourceCustomAppIntegration(), |
| 63 | + }.ApplyAndExpectData(t, map[string]any{ |
| 64 | + "name": "custom_integration_name", |
| 65 | + "integration_id": "integration_id", |
| 66 | + "client_id": "client_id", |
| 67 | + "client_secret": "client_secret", |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func TestResourceCustomAppIntegrationRead(t *testing.T) { |
| 72 | + qa.ResourceFixture{ |
| 73 | + MockAccountClientFunc: func(a *mocks.MockAccountClient) { |
| 74 | + a.GetMockCustomAppIntegrationAPI().EXPECT().GetByIntegrationId(mock.Anything, "integration_id").Return( |
| 75 | + &oauth2.GetCustomAppIntegrationOutput{ |
| 76 | + Name: "custom_integration_name", |
| 77 | + RedirectUrls: []string{ |
| 78 | + "https://example.com", |
| 79 | + }, |
| 80 | + Scopes: []string{ |
| 81 | + "all", |
| 82 | + }, |
| 83 | + TokenAccessPolicy: &oauth2.TokenAccessPolicy{ |
| 84 | + AccessTokenTtlInMinutes: 60, |
| 85 | + RefreshTokenTtlInMinutes: 30, |
| 86 | + }, |
| 87 | + ClientId: "client_id", |
| 88 | + IntegrationId: "integration_id", |
| 89 | + }, nil, |
| 90 | + ) |
| 91 | + }, |
| 92 | + Resource: ResourceCustomAppIntegration(), |
| 93 | + Read: true, |
| 94 | + New: true, |
| 95 | + AccountID: "account_id", |
| 96 | + ID: "integration_id", |
| 97 | + }.ApplyAndExpectData(t, map[string]any{ |
| 98 | + "name": "custom_integration_name", |
| 99 | + "integration_id": "integration_id", |
| 100 | + "client_id": "client_id", |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +func TestResourceCustomAppIntegrationUpdate(t *testing.T) { |
| 105 | + qa.ResourceFixture{ |
| 106 | + MockAccountClientFunc: func(a *mocks.MockAccountClient) { |
| 107 | + api := a.GetMockCustomAppIntegrationAPI().EXPECT() |
| 108 | + api.Update(mock.Anything, oauth2.UpdateCustomAppIntegration{ |
| 109 | + IntegrationId: "integration_id", |
| 110 | + RedirectUrls: []string{ |
| 111 | + "https://example.com", |
| 112 | + }, |
| 113 | + TokenAccessPolicy: &oauth2.TokenAccessPolicy{ |
| 114 | + AccessTokenTtlInMinutes: 30, |
| 115 | + RefreshTokenTtlInMinutes: 30, |
| 116 | + }, |
| 117 | + }).Return(nil) |
| 118 | + api.GetByIntegrationId(mock.Anything, "integration_id").Return( |
| 119 | + &oauth2.GetCustomAppIntegrationOutput{ |
| 120 | + Name: "custom_integration_name", |
| 121 | + RedirectUrls: []string{ |
| 122 | + "https://example.com", |
| 123 | + }, |
| 124 | + Scopes: []string{ |
| 125 | + "all", |
| 126 | + }, |
| 127 | + TokenAccessPolicy: &oauth2.TokenAccessPolicy{ |
| 128 | + AccessTokenTtlInMinutes: 30, |
| 129 | + RefreshTokenTtlInMinutes: 30, |
| 130 | + }, |
| 131 | + ClientId: "client_id", |
| 132 | + IntegrationId: "integration_id", |
| 133 | + }, nil, |
| 134 | + ) |
| 135 | + }, |
| 136 | + Resource: ResourceCustomAppIntegration(), |
| 137 | + Update: true, |
| 138 | + HCL: ` |
| 139 | + name = "custom_integration_name" |
| 140 | + redirect_urls = ["https://example.com"] |
| 141 | + scopes = ["all"] |
| 142 | + token_access_policy { |
| 143 | + access_token_ttl_in_minutes = 30 |
| 144 | + refresh_token_ttl_in_minutes = 30 |
| 145 | + }`, |
| 146 | + InstanceState: map[string]string{ |
| 147 | + "name": "custom_integration_name", |
| 148 | + "integration_id": "integration_id", |
| 149 | + "client_id": "client_id", |
| 150 | + "scopes.#": "1", |
| 151 | + "scopes.0": "all", |
| 152 | + "redirect_urls.#": "1", |
| 153 | + "redirect_urls.0": "https://example.com", |
| 154 | + "token_access_policy.access_token_ttl_in_minutes": "30", |
| 155 | + "token_access_policy.refresh_token_ttl_in_minutes": "30", |
| 156 | + }, |
| 157 | + AccountID: "account_id", |
| 158 | + ID: "integration_id", |
| 159 | + }.ApplyAndExpectData(t, map[string]any{ |
| 160 | + "name": "custom_integration_name", |
| 161 | + "token_access_policy.0.access_token_ttl_in_minutes": 30, |
| 162 | + }) |
| 163 | +} |
| 164 | + |
| 165 | +func TestResourceCustomAppIntegrationDelete(t *testing.T) { |
| 166 | + qa.ResourceFixture{ |
| 167 | + MockAccountClientFunc: func(a *mocks.MockAccountClient) { |
| 168 | + a.GetMockCustomAppIntegrationAPI().EXPECT().DeleteByIntegrationId(mock.Anything, "integration_id").Return(nil) |
| 169 | + }, |
| 170 | + Resource: ResourceCustomAppIntegration(), |
| 171 | + AccountID: "account_id", |
| 172 | + Delete: true, |
| 173 | + ID: "integration_id", |
| 174 | + }.ApplyAndExpectData(t, nil) |
| 175 | +} |
0 commit comments