-
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 de2fc6a
Showing
2 changed files
with
197 additions
and
13 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
internal/provider/teamapikey/team_permission_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,135 @@ | ||
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" | ||
"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), | ||
), | ||
}, | ||
{ | ||
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