Skip to content

Commit 97edd98

Browse files
authored
Merge pull request #262 from 1Password/jill/add-account-e2e-tests
Add account e2e tests
2 parents 89263ae + 9917fa7 commit 97edd98

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export MAIN_BRANCH ?= main
22

33
.DEFAULT_GOAL := help
4-
.PHONY: test testacc build clean test/coverage release/prepare release/tag .check_bump_type .check_git_clean help test-e2e test-e2e-service-account test-e2e-connect
4+
.PHONY: test testacc build clean test/coverage release/prepare release/tag .check_bump_type .check_git_clean help test-e2e test-e2e-service-account test-e2e-connect test-e2e-account
55

66
GIT_BRANCH := $(shell git symbolic-ref --short HEAD 2>/dev/null || echo "")
77
WORKTREE_CLEAN := $(shell git status --porcelain 1>/dev/null 2>&1; echo $$?)
@@ -33,6 +33,15 @@ test-e2e-connect: ## Run e2e tests using Connect (requires OP_CONNECT_TOKEN and
3333
@echo "[INFO] Running e2e tests with Connect authentication..."
3434
@sh -c 'unset OP_SERVICE_ACCOUNT_TOKEN; OP_CONNECT_TOKEN="$(OP_CONNECT_TOKEN)" OP_CONNECT_HOST="$(OP_CONNECT_HOST)" TF_ACC=1 go test -v ./test/e2e/... -timeout 30m'
3535

36+
test-e2e-account: ## Run e2e test using account methodology with Touch ID (requires OP_ACCOUNT and OP_TEST_VAULT_NAME, manual only). Creates multiple items to verify biometrics is only prompted once.
37+
@test -n "$(OP_ACCOUNT)" || (echo "[ERROR] OP_ACCOUNT environment variable is not set."; exit 1)
38+
@test -n "$(OP_TEST_VAULT_NAME)" || (echo "[ERROR] OP_TEST_VAULT_NAME environment variable is not set."; exit 1)
39+
@echo "[INFO] Running e2e test with account-based authentication (Touch ID)..."
40+
@echo "[WARNING] This test will prompt for Touch ID/biometric authentication."
41+
@echo "[WARNING] Please ensure you add a vault titled 'terraform-provider-acceptance-tests' or set OP_TEST_VAULT_NAME environment variable for this test to pass."
42+
@echo "[INFO] Ensure that biometrics are only prompted once."
43+
@sh -c 'unset OP_CONNECT_TOKEN OP_CONNECT_HOST OP_SERVICE_ACCOUNT_TOKEN; OP_ACCOUNT="$(OP_ACCOUNT)" TF_ACC=1 go test -v ./test/e2e/... -run TestAccItemResource -timeout 30m'
44+
3645
build: clean ## Build project
3746
go build -o ./dist/terraform-provider-onepassword .
3847

test/e2e/item_resource_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/1Password/terraform-provider-onepassword/v2/test/e2e/utils/password"
2020
"github.com/1Password/terraform-provider-onepassword/v2/test/e2e/utils/sections"
2121
uuidutil "github.com/1Password/terraform-provider-onepassword/v2/test/e2e/utils/uuid"
22+
"github.com/1Password/terraform-provider-onepassword/v2/test/e2e/utils/vault"
2223
)
2324

2425
type testResourceItem struct {
@@ -116,6 +117,8 @@ func TestAccItemResource(t *testing.T) {
116117
{category: model.SecureNote, name: "SecureNote"},
117118
}
118119

120+
testVaultID := vault.GetTestVaultID(t)
121+
119122
for _, tc := range testCases {
120123
t.Run(tc.name, func(t *testing.T) {
121124
// Generate unique identifier for this test run to avoid conflicts in parallel execution
@@ -214,6 +217,8 @@ func TestAccItemResourcePasswordGeneration(t *testing.T) {
214217
{name: "InvalidLength65", recipe: password.PasswordRecipe{Length: 65}},
215218
}
216219

220+
testVaultID := vault.GetTestVaultID(t)
221+
217222
// Test both Login and Password items
218223
items := []model.ItemCategory{model.Login, model.Password}
219224

@@ -266,6 +271,8 @@ func TestAccItemResourcePasswordGeneration_InvalidLetters(t *testing.T) {
266271
{name: "LettersFalse", letters: false},
267272
}
268273

274+
testVaultID := vault.GetTestVaultID(t)
275+
269276
item := testItemsToCreate[model.Login]
270277

271278
for _, tc := range testCases {
@@ -315,6 +322,8 @@ func TestAccItemResourceSectionFieldPasswordGeneration(t *testing.T) {
315322
{name: "InvalidLength", recipe: password.PasswordRecipe{Length: 0}},
316323
}
317324

325+
testVaultID := vault.GetTestVaultID(t)
326+
318327
item := testItemsToCreate[model.Login]
319328

320329
for _, tc := range testCases {
@@ -470,6 +479,8 @@ func TestAccItemResourceSectionsAndFields(t *testing.T) {
470479

471480
items := []model.ItemCategory{model.Login}
472481

482+
testVaultID := vault.GetTestVaultID(t)
483+
473484
for _, tc := range testCases {
474485
for _, item := range items {
475486
item := testItemsToCreate[item]
@@ -547,6 +558,8 @@ func TestAccItemResourceTags(t *testing.T) {
547558
// {"REMOVE_2_TAGS", []string{"firstTestTag"}},
548559
}
549560

561+
testVaultID := vault.GetTestVaultID(t)
562+
550563
var testSteps []resource.TestStep
551564

552565
for _, step := range testCases {
@@ -577,6 +590,8 @@ func TestAccRecreateNonExistingItem(t *testing.T) {
577590
uniqueID := uuid.New().String()
578591

579592
item := testItemsToCreate[model.Login]
593+
testVaultID := vault.GetTestVaultID(t)
594+
580595
// Create a copy of item attributes and update title with unique ID
581596
createAttrs := maps.Clone(item.Attrs)
582597
createAttrs["title"] = addUniqueIDToTitle(createAttrs["title"].(string), uniqueID)
@@ -661,6 +676,8 @@ func TestAccItemResource_DetectManualChanges(t *testing.T) {
661676
// Generate unique identifier for this test run to avoid conflicts in parallel execution
662677
uniqueID := uuid.New().String()
663678
var itemUUID string
679+
testVaultID := vault.GetTestVaultID(t)
680+
664681
initialAttrs := maps.Clone(testItemsToCreate[model.Login].Attrs)
665682

666683
initialAttrs["title"] = addUniqueIDToTitle(initialAttrs["title"].(string), uniqueID)

test/e2e/utils/client/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func CreateTestClient(ctx context.Context) (onepassword.Client, error) {
1212
ConnectHost: os.Getenv("OP_CONNECT_HOST"),
1313
ConnectToken: os.Getenv("OP_CONNECT_TOKEN"),
1414
ServiceAccountToken: os.Getenv("OP_SERVICE_ACCOUNT_TOKEN"),
15+
Account: os.Getenv("OP_ACCOUNT"),
1516
OpCLIPath: "op",
1617
ProviderUserAgent: "terraform-provider-onepassword/test",
1718
})

test/e2e/utils/vault/vault.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package vault
2+
3+
import (
4+
"context"
5+
"os"
6+
"sync"
7+
"testing"
8+
9+
"github.com/1Password/terraform-provider-onepassword/v2/test/e2e/utils/client"
10+
)
11+
12+
var (
13+
testVaultIDOnce sync.Once
14+
testVaultID string
15+
)
16+
17+
// GetTestVaultID returns the vault ID by querying by name once and caching the result.
18+
func GetTestVaultID(t *testing.T) string {
19+
testVaultIDOnce.Do(func() {
20+
vaultName := os.Getenv("OP_TEST_VAULT_NAME")
21+
if vaultName == "" {
22+
vaultName = "terraform-provider-acceptance-tests"
23+
}
24+
25+
ctx := context.Background()
26+
client, err := client.CreateTestClient(ctx)
27+
if err != nil {
28+
t.Fatalf("failed to create test client: %v", err)
29+
}
30+
31+
vaults, err := client.GetVaultsByTitle(ctx, vaultName)
32+
if err != nil {
33+
t.Fatalf("failed to get vault by name %q: %v", vaultName, err)
34+
}
35+
36+
if len(vaults) == 0 {
37+
t.Fatalf("no vault found with name %q", vaultName)
38+
}
39+
if len(vaults) > 1 {
40+
t.Fatalf("multiple vaults found with name %q", vaultName)
41+
}
42+
43+
testVaultID = vaults[0].ID
44+
})
45+
return testVaultID
46+
}

0 commit comments

Comments
 (0)