1
+ package fwprovider_test
2
+
3
+ import (
4
+ "fmt"
5
+ {{- if ne $.TargetVersionName "ga" }}
6
+ "regexp"
7
+ {{- end }}
8
+ "testing"
9
+
10
+ "github.com/hashicorp/terraform-plugin-testing/helper/resource"
11
+ "github.com/hashicorp/terraform-provider-google/google/acctest"
12
+ "github.com/hashicorp/terraform-provider-google/google/transport"
13
+ )
14
+
15
+ // TestAccFwProvider_scopes is a series of acc tests asserting how the PF provider handles scopes arguments
16
+ // It is PF specific because the HCL used provisions PF-implemented resources
17
+ // It is a counterpart to TestAccSdkProvider_scopes
18
+ func TestAccFwProvider_scopes(t *testing.T) {
19
+ testCases := map[string]func(t *testing.T){
20
+ // Configuring the provider using inputs
21
+ "default scopes are used when there are no user inputs": testAccFwProvider_scopes_providerDefault,
22
+ "scopes can be set in config": testAccFwProvider_scopes_setInConfig,
23
+ //no ENVs to test
24
+
25
+ // Schema-level validation
26
+ "when scopes is set to an empty array in the config the value is ignored and default scopes are used": testAccFwProvider_scopes_emptyArray,
27
+
28
+ // Usage
29
+ {{- if ne $.TargetVersionName "ga" }}
30
+ // Beta-only generation is needed because we need to access a PF-implemented data source linked to resource in an API.
31
+ // Currently this only exists in TPGB.
32
+ "the scopes argument impacts provisioning resources": testAccFwProvider_scopes_usage,
33
+ {{- else }}
34
+ // No usage test cases are implemented in the GA provider because the only PF-implemented data sources are Beta-only
35
+ {{- end }}
36
+ }
37
+
38
+ for name, tc := range testCases {
39
+ // shadow the tc variable into scope so that when
40
+ // the loop continues, if t.Run hasn't executed tc(t)
41
+ // yet, we don't have a race condition
42
+ // see https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
43
+ tc := tc
44
+ t.Run(name, func(t *testing.T) {
45
+ tc(t)
46
+ })
47
+ }
48
+ }
49
+
50
+ func testAccFwProvider_scopes_providerDefault(t *testing.T) {
51
+ acctest.SkipIfVcr(t) // Test doesn't interact with API
52
+
53
+ acctest.VcrTest(t, resource.TestCase{
54
+ // No PreCheck for checking ENVs
55
+ ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
56
+ Steps: []resource.TestStep{
57
+ {
58
+ Config: testAccFwProvider_scopes_unset(),
59
+ Check: resource.ComposeTestCheckFunc(
60
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.#", fmt.Sprintf("%d", len(transport.DefaultClientScopes))),
61
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.0", transport.DefaultClientScopes[0]),
62
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.1", transport.DefaultClientScopes[1]),
63
+ ),
64
+ },
65
+ },
66
+ })
67
+ }
68
+
69
+ func testAccFwProvider_scopes_setInConfig(t *testing.T) {
70
+ acctest.SkipIfVcr(t) // Test doesn't interact with API
71
+
72
+ scopes := []string{"https://www.googleapis.com/auth/cloud-platform"} // first of the two default scopes
73
+ context := map[string]interface{}{
74
+ "scopes": fmt.Sprintf("[\"%s\"]", scopes[0]),
75
+ }
76
+
77
+ acctest.VcrTest(t, resource.TestCase{
78
+ // No PreCheck for checking ENVs
79
+ ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
80
+ Steps: []resource.TestStep{
81
+ {
82
+ Config: testAccFwProvider_scopes_inProviderBlock(context),
83
+ Check: resource.ComposeTestCheckFunc(
84
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.#", fmt.Sprintf("%d", len(scopes))),
85
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.0", scopes[0]),
86
+ ),
87
+ },
88
+ },
89
+ })
90
+ }
91
+
92
+ func testAccFwProvider_scopes_emptyArray(t *testing.T) {
93
+ acctest.SkipIfVcr(t) // Test doesn't interact with API
94
+
95
+ context := map[string]interface{}{
96
+ "scopes": "[]",
97
+ }
98
+
99
+ acctest.VcrTest(t, resource.TestCase{
100
+ // No PreCheck for checking ENVs
101
+ ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
102
+ Steps: []resource.TestStep{
103
+ {
104
+ Config: testAccFwProvider_scopes_inProviderBlock(context),
105
+ Check: resource.ComposeTestCheckFunc(
106
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.#", fmt.Sprintf("%d", len(transport.DefaultClientScopes))),
107
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.0", transport.DefaultClientScopes[0]),
108
+ resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.1", transport.DefaultClientScopes[1]),
109
+ ),
110
+ },
111
+ },
112
+ })
113
+ }
114
+
115
+ {{ if ne $.TargetVersionName `ga` -}}
116
+ func testAccFwProvider_scopes_usage(t *testing.T) {
117
+ acctest.SkipIfVcr(t) // Skip because Firebase is weird with VCR, and we have to use Firebase resources in the test
118
+
119
+ // We include scopes that aren't sufficient to enable provisioning the resources in the config below
120
+ context := map[string]interface{}{
121
+ "scopes": "[\"https://www.googleapis.com/auth/pubsub\"]",
122
+ "random_suffix": acctest.RandString(t, 10),
123
+
124
+ "bundle_id": "apple.app." + acctest.RandString(t, 5),
125
+ "display_name": "tf-test Display Name AppleAppConfig DataSource",
126
+ "app_store_id": 12345,
127
+ "team_id": 1234567890,
128
+ }
129
+
130
+ acctest.VcrTest(t, resource.TestCase{
131
+ // No PreCheck for checking ENVs
132
+ ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
133
+ Steps: []resource.TestStep{
134
+ {
135
+ Config: testAccFwProvider_access_token_useScopes(context),
136
+ ExpectError: regexp.MustCompile("Request had insufficient authentication scopes"),
137
+ },
138
+ },
139
+ })
140
+ }
141
+ {{- end }}
142
+
143
+ // testAccFwProvider_scopes_inProviderBlock allows setting the scopes argument in a provider block.
144
+ // This function uses data.google_provider_config_plugin_framework because it is implemented with the PF
145
+ func testAccFwProvider_scopes_inProviderBlock(context map[string]interface{}) string {
146
+ return acctest.Nprintf(`
147
+ provider "google" {
148
+ scopes = %{scopes}
149
+ }
150
+
151
+ data "google_provider_config_plugin_framework" "default" {}
152
+ `, context)
153
+ }
154
+
155
+ // testAccFwProvider_scopes_inEnvsOnly allows testing when the scopes argument is not set
156
+ func testAccFwProvider_scopes_unset() string {
157
+ return `
158
+ data "google_provider_config_plugin_framework" "default" {}
159
+ `
160
+ }
161
+
162
+ {{ if ne $.TargetVersionName `ga` -}}
163
+ func testAccFwProvider_access_token_useScopes(context map[string]interface{}) string {
164
+ return acctest.Nprintf(`
165
+ provider "google" {} // default scopes used
166
+
167
+ provider "google" {
168
+ alias = "under-scoped"
169
+ scopes = %{scopes}
170
+ }
171
+
172
+ data "google_provider_config_plugin_framework" "default" {
173
+ }
174
+
175
+ resource "google_firebase_apple_app" "my_app_config" {
176
+ project = data.google_provider_config_plugin_framework.default.project
177
+ bundle_id = "%{bundle_id}"
178
+ display_name = "%{display_name}"
179
+ app_store_id = "%{app_store_id}"
180
+ team_id = "%{team_id}"
181
+ }
182
+
183
+ // This is implemented with plugin-framework so tests our use of scopes in a PF specific way
184
+ data "google_firebase_apple_app_config" "my_app_config" {
185
+ provider = google.under-scoped
186
+ app_id = google_firebase_apple_app.my_app_config.app_id
187
+ }
188
+ `, context)
189
+ }
190
+ {{- end }}
0 commit comments