-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4779b8d
commit 2b33a1c
Showing
3 changed files
with
214 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
internal/provider/teamapikey/team_api_key_resource_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package teamapikey_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/futurice/terraform-provider-dependencytrack/internal/testutils" | ||
"github.com/futurice/terraform-provider-dependencytrack/internal/testutils/teamtestutils" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
"os" | ||
"testing" | ||
) | ||
|
||
var testDependencyTrack *testutils.TestDependencyTrack | ||
|
||
func TestMain(m *testing.M) { | ||
if os.Getenv(resource.EnvTfAcc) != "" { | ||
var cleanup func() | ||
testDependencyTrack, cleanup = testutils.InitTestDependencyTrack() | ||
defer cleanup() | ||
} | ||
|
||
m.Run() | ||
} | ||
|
||
func TestAccTeamResource_basic(t *testing.T) { | ||
ctx := testutils.CreateTestContext(t) | ||
|
||
teamName := acctest.RandomWithPrefix("test-team") | ||
|
||
teamResourceName := teamtestutils.CreateTeamResourceName("test") | ||
otherTeamResourceName := teamtestutils.CreateTeamResourceName("test-other") | ||
apiKeyResourceName := teamtestutils.CreateTeamAPIKeyResourceName("test") | ||
|
||
var teamID, otherTeamID, teamAPIKey, otherTeamAPIKey string | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testutils.TestAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTeamAPIKeyConfigBasic(testDependencyTrack, teamName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
teamtestutils.TestAccCheckGetTeamSingleAPIKey(ctx, testDependencyTrack, teamResourceName, &teamAPIKey), | ||
testutils.TestAccCheckGetResourceID(teamResourceName, &teamID), | ||
resource.TestCheckResourceAttrPtr(apiKeyResourceName, "team_id", &teamID), | ||
resource.TestCheckResourceAttrPtr(apiKeyResourceName, "value", &teamAPIKey), | ||
), | ||
}, | ||
{ | ||
ResourceName: apiKeyResourceName, | ||
ImportStateIdFunc: func(state *terraform.State) (string, error) { | ||
return fmt.Sprintf("%s/%s", teamID, teamAPIKey), nil | ||
}, | ||
ImportState: true, | ||
// Unable to verify since the resource has no ID and no non-sensitive ID can be synthesised; we are just smoke-testing the import | ||
ImportStateVerify: false, | ||
}, | ||
{ | ||
Config: testAccTeamAPIKeyConfigOtherTeam(testDependencyTrack, teamName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
teamtestutils.TestAccCheckTeamHasNoAPIKeys(ctx, testDependencyTrack, teamResourceName), | ||
teamtestutils.TestAccCheckGetTeamSingleAPIKey(ctx, testDependencyTrack, otherTeamResourceName, &otherTeamAPIKey), | ||
testutils.TestAccCheckGetResourceID(otherTeamResourceName, &otherTeamID), | ||
resource.TestCheckResourceAttrPtr(apiKeyResourceName, "team_id", &otherTeamID), | ||
resource.TestCheckResourceAttrPtr(apiKeyResourceName, "value", &otherTeamAPIKey), | ||
), | ||
}, | ||
{ | ||
Config: testAccTeamPermissionConfigNoAPIKey(testDependencyTrack, teamName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
teamtestutils.TestAccCheckTeamHasNoAPIKeys(ctx, testDependencyTrack, teamResourceName), | ||
teamtestutils.TestAccCheckTeamHasNoAPIKeys(ctx, testDependencyTrack, otherTeamResourceName), | ||
), | ||
}, | ||
}, | ||
// CheckDestroy is not practical here since the team is destroyed as well, and we can no longer query its permissions | ||
}) | ||
} | ||
|
||
func testAccTeamAPIKeyConfigBasic(testDependencyTrack *testutils.TestDependencyTrack, teamName string) string { | ||
return testDependencyTrack.AddProviderConfiguration( | ||
testutils.ComposeConfigs( | ||
fmt.Sprintf(` | ||
resource "dependencytrack_team" "test" { | ||
name = %[1]q | ||
} | ||
`, | ||
teamName, | ||
), | ||
` | ||
resource "dependencytrack_team_api_key" "test" { | ||
team_id = dependencytrack_team.test.id | ||
} | ||
`, | ||
), | ||
) | ||
} | ||
|
||
func testAccTeamAPIKeyConfigOtherTeam(testDependencyTrack *testutils.TestDependencyTrack, teamName string) string { | ||
return testDependencyTrack.AddProviderConfiguration( | ||
testutils.ComposeConfigs( | ||
fmt.Sprintf(` | ||
resource "dependencytrack_team" "test" { | ||
name = %[1]q | ||
} | ||
`, | ||
teamName, | ||
), | ||
fmt.Sprintf(` | ||
resource "dependencytrack_team" "test-other" { | ||
name = "%[1]s-other" | ||
} | ||
`, | ||
teamName, | ||
), | ||
` | ||
resource "dependencytrack_team_api_key" "test" { | ||
team_id = dependencytrack_team.test-other.id | ||
} | ||
`, | ||
), | ||
) | ||
} | ||
|
||
func testAccTeamPermissionConfigNoAPIKey(testDependencyTrack *testutils.TestDependencyTrack, teamName string) string { | ||
return testDependencyTrack.AddProviderConfiguration( | ||
testutils.ComposeConfigs( | ||
fmt.Sprintf(` | ||
resource "dependencytrack_team" "test" { | ||
name = %[1]q | ||
} | ||
`, | ||
teamName, | ||
), | ||
fmt.Sprintf(` | ||
resource "dependencytrack_team" "test-other" { | ||
name = "%[1]s-other" | ||
} | ||
`, | ||
teamName, | ||
), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters