@@ -85,6 +85,8 @@ func TestAccTFEOrganization_full(t *testing.T) {
8585 "tfe_organization.foobar" , "allow_force_delete_workspaces" , "false" ),
8686 resource .TestCheckResourceAttr (
8787 "tfe_organization.foobar" , "speculative_plan_management_enabled" , "true" ),
88+ resource .TestCheckResourceAttr (
89+ "tfe_organization.foobar" , "user_tokens_enabled" , "true" ),
8890 ),
8991 },
9092 },
@@ -205,6 +207,80 @@ func TestAccTFEOrganization_update_costEstimation(t *testing.T) {
205207 })
206208}
207209
210+ func TestAccTFEOrganization_user_tokens_enabled (t * testing.T ) {
211+ // this test is a bit tricky because once user tokens are disabled, we cannot use a user token to re-enable them
212+ // through the API.
213+ // Therefore, we need to create an org, generate an owners team token for that org, and then use that token
214+ // in the go-tfe client to test the user_tokens_enabled setting.
215+
216+ org := & tfe.Organization {}
217+ rInt := rand .New (rand .NewSource (time .Now ().UnixNano ())).Int ()
218+ orgName := fmt .Sprintf ("tst-terraform-%d" , rInt )
219+
220+ resource .Test (t , resource.TestCase {
221+ PreCheck : func () { testAccPreCheck (t ) },
222+ ProtoV6ProviderFactories : testAccMuxedProviders ,
223+ CheckDestroy : testAccCheckTFEOrganizationDestroy ,
224+ Steps : []resource.TestStep {
225+ {
226+ Config : testAccTFEOrganization_basic (rInt ),
227+ Check : resource .ComposeTestCheckFunc (
228+ testAccCheckTFEOrganizationExists (
229+ "tfe_organization.foobar" , org ),
230+ testAccCheckTFEOrganizationAttributesBasic (org , orgName ),
231+ resource .TestCheckResourceAttr (
232+ "tfe_organization.foobar" , "user_tokens_enabled" , "true" ),
233+ ),
234+ },
235+ {
236+ PreConfig : func () {
237+ // create a team token for the owners team in the org,
238+ // then set the provider token to that value
239+ ownersTeams , err := testAccConfiguredClient .Client .Teams .List (ctx , org .Name , & tfe.TeamListOptions {
240+ Names : []string {"owners" },
241+ })
242+ if err != nil {
243+ t .Fatal (err )
244+ }
245+ if len (ownersTeams .Items ) != 1 {
246+ t .Fatalf ("expected to find 1 owners team, found %d" , len (ownersTeams .Items ))
247+ }
248+ ownersTeam := ownersTeams .Items [0 ]
249+
250+ teamToken , err := testAccConfiguredClient .Client .TeamTokens .Create (ctx , ownersTeam .ID )
251+ if err != nil {
252+ t .Fatal (err )
253+ }
254+
255+ tfeClient , err := getClientWithToken (teamToken .ID )
256+ if err != nil {
257+ t .Fatal (err )
258+ }
259+
260+ testAccConfiguredClient = & ConfiguredClient {
261+ Client : tfeClient ,
262+ Organization : org .Name ,
263+ }
264+ },
265+ Config : testAccTFEOrganization_userTokensEnabled (rInt , false ),
266+ Check : resource .ComposeTestCheckFunc (
267+ resource .TestCheckResourceAttr (
268+ "tfe_organization.foobar" , "user_tokens_enabled" , "false" ),
269+ testAccCheckTFEOrganizationUserTokensEnabled (org , orgName , false ),
270+ ),
271+ },
272+ {
273+ Config : testAccTFEOrganization_userTokensEnabled (rInt , true ),
274+ Check : resource .ComposeTestCheckFunc (
275+ resource .TestCheckResourceAttr (
276+ "tfe_organization.foobar" , "user_tokens_enabled" , "true" ),
277+ testAccCheckTFEOrganizationUserTokensEnabled (org , orgName , true ),
278+ ),
279+ },
280+ },
281+ })
282+ }
283+
208284func TestAccTFEOrganization_case (t * testing.T ) {
209285 org := & tfe.Organization {}
210286 rInt := rand .New (rand .NewSource (time .Now ().UnixNano ())).Int ()
@@ -343,6 +419,20 @@ func testAccCheckTFEOrganizationAttributesFull(
343419 }
344420}
345421
422+ func testAccCheckTFEOrganizationUserTokensEnabled (
423+ org * tfe.Organization , expectedOrgName string , expectedUserTokensEnabled bool ) resource.TestCheckFunc {
424+ return func (s * terraform.State ) error {
425+ if org .Name != expectedOrgName {
426+ return fmt .Errorf ("Bad name: %s" , org .Name )
427+ }
428+
429+ if org .UserTokensEnabled != nil && * org .UserTokensEnabled != expectedUserTokensEnabled {
430+ return fmt .Errorf ("Bad user tokens enabled: %v" , org .UserTokensEnabled )
431+ }
432+ return nil
433+ }
434+ }
435+
346436func testAccCheckTFEOrganizationAttributesUpdated (
347437 org * tfe.Organization , expectedOrgName string , expectedCostEstimationEnabled bool ) resource.TestCheckFunc {
348438 return func (s * terraform.State ) error {
@@ -441,3 +531,12 @@ resource "tfe_organization" "foobar" {
441531 allow_force_delete_workspaces = %t
442532}` , orgName , orgEmail , costEstimationEnabled , assessmentsEnforced , allowForceDeleteWorkspaces )
443533}
534+
535+ func testAccTFEOrganization_userTokensEnabled (rInt int , userTokensEnabled bool ) string {
536+ return fmt .Sprintf (`
537+ resource "tfe_organization" "foobar" {
538+ name = "tst-terraform-%d"
539+ 540+ user_tokens_enabled = %t
541+ }` , rInt , userTokensEnabled )
542+ }
0 commit comments