Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

current state #200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/provider/onepassword_item_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func (r *OnePasswordItemResource) Schema(ctx context.Context, req resource.Schem
"username": schema.StringAttribute{
MarkdownDescription: usernameDescription,
Optional: true,
Computed: true,
},
"password": schema.StringAttribute{
MarkdownDescription: passwordDescription,
Expand Down
62 changes: 61 additions & 1 deletion internal/provider/onepassword_item_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package provider

import (
"fmt"
"regexp"
"strings"
"testing"

op "github.com/1Password/connect-sdk-go/onepassword"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

/*
func TestAccItemResourceDatabase(t *testing.T) {
expectedItem := generateDatabaseItem()
expectedVault := op.Vault{
Expand Down Expand Up @@ -98,7 +98,52 @@ func TestAccItemResourceLogin(t *testing.T) {
},
})
}
*/

func TestAccItemResourceLogin2(t *testing.T) {
expectedItem = generateLoginItem()
expectedItem.Fields = nil

expectedVault := op.Vault{
ID: expectedItem.Vault.ID,
Name: "VaultName",
Description: "This vault will be retrieved for testing",
}
expectedItemUpdate := generateLoginItem()
setupExpectedItems(expectedItem, expectedVault, t)
testServer := setupTestServer( /*expectedItem, expectedVault,*/ t)
defer testServer.Close()

resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProviderConfig(testServer.URL) + testAccLoginUnsetResourceConfig(expectedItem),
Check: resource.ComposeAggregateTestCheckFunc(
// verify local values
resource.TestCheckResourceAttr("onepassword_item.test-database", "title", expectedItem.Title),
resource.TestCheckResourceAttr("onepassword_item.test-database", "category", strings.ToLower(string(expectedItem.Category))),
//resource.TestCheckResourceAttr("onepassword_item.test-database", "username", ""),
resource.TestCheckResourceAttr("onepassword_item.test-database", "url", expectedItem.URLs[0].URL),
resource.TestCheckResourceAttrSet("onepassword_item.test-database", "password"),
),
},
{
PreConfig: func() {
setupExpectedItems(expectedItemUpdate, expectedVault, t)
expectedItem = expectedItemUpdate
},
Config: testAccProviderConfig(testServer.URL) + testAccLoginUnsetResourceConfig(expectedItemUpdate),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("onepassword_item.test-database", "username", expectedItemUpdate.Fields[0].Value),
resource.TestCheckResourceAttr("onepassword_item.test-database", "password", expectedItemUpdate.Fields[1].Value),
),
},
},
})
}

/*
func TestAccItemResourceSecureNote(t *testing.T) {
expectedItem := generateSecureNoteItem()
expectedVault := op.Vault{
Expand Down Expand Up @@ -179,6 +224,8 @@ func TestAccItemResourceDocument(t *testing.T) {
})
}

*/

func testAccDataBaseResourceConfig(expectedItem *op.Item) string {
return fmt.Sprintf(`

Expand Down Expand Up @@ -228,6 +275,19 @@ resource "onepassword_item" "test-database" {
url = "%s"
}`, expectedItem.Vault.ID, expectedItem.Title, strings.ToLower(string(expectedItem.Category)), expectedItem.Fields[0].Value, expectedItem.URLs[0].URL)
}
func testAccLoginUnsetResourceConfig(expectedItem *op.Item) string {
return fmt.Sprintf(`

data "onepassword_vault" "acceptance-tests" {
uuid = "%s"
}
resource "onepassword_item" "test-database" {
vault = data.onepassword_vault.acceptance-tests.uuid
title = "%s"
category = "%s"
url = "%s"
}`, expectedItem.Vault.ID, expectedItem.Title, strings.ToLower(string(expectedItem.Category)), expectedItem.URLs[0].URL)
}

func testAccSecureNoteResourceConfig(expectedItem *op.Item) string {
return fmt.Sprintf(`
Expand Down
65 changes: 59 additions & 6 deletions internal/provider/test_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ import (
"github.com/1Password/connect-sdk-go/onepassword"
)

var (
itemBytes, itemListBytes, vaultBytes []byte
expectedItem *onepassword.Item
files []*onepassword.File
fileBytes [][]byte
)

// setupTestServer sets up a http server that can be used mock out 1Password Connect API calls
func setupTestServer(expectedItem *onepassword.Item, expectedVault onepassword.Vault, t *testing.T) *httptest.Server {
itemBytes, err := json.Marshal(expectedItem)
func setupExpectedItems(expectedItem *onepassword.Item, expectedVault onepassword.Vault, t *testing.T) {
var err error
itemBytes, err = json.Marshal(expectedItem)
if err != nil {
t.Errorf("error marshaling item for testing: %s", err)
}

files := expectedItem.Files
var fileBytes [][]byte
files = expectedItem.Files
for _, file := range files {
c, err := file.Content()
if err != nil {
Expand All @@ -31,16 +38,47 @@ func setupTestServer(expectedItem *onepassword.Item, expectedVault onepassword.V
fileBytes = append(fileBytes, c)
}

vaultBytes, err := json.Marshal(expectedVault)
vaultBytes, err = json.Marshal(expectedVault)
if err != nil {
t.Errorf("error marshaling vault for testing: %s", err)
}

itemList := []onepassword.Item{*expectedItem}
itemListBytes, err := json.Marshal(itemList)
itemListBytes, err = json.Marshal(itemList)
if err != nil {
t.Errorf("error marshaling itemlist for testing: %s", err)
}
}

// setupTestServer sets up a http server that can be used mock out 1Password Connect API calls
func setupTestServer( /*expectedItem *onepassword.Item, expectedVault onepassword.Vault,*/ t *testing.T) *httptest.Server {
/*
itemBytes, err := json.Marshal(expectedItem)
if err != nil {
t.Errorf("error marshaling item for testing: %s", err)
}

files := expectedItem.Files
var fileBytes [][]byte
for _, file := range files {
c, err := file.Content()
if err != nil {
t.Errorf("error getting file content: %s", err)
}
fileBytes = append(fileBytes, c)
}

vaultBytes, err := json.Marshal(expectedVault)
if err != nil {
t.Errorf("error marshaling vault for testing: %s", err)
}

itemList := []onepassword.Item{*expectedItem}
itemListBytes, err := json.Marshal(itemList)
if err != nil {
t.Errorf("error marshaling itemlist for testing: %s", err)
}
*/

return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filePath := regexp.MustCompile("/v1/vaults/[a-z0-9]*/items/[a-z0-9]*/files/[a-z0-9]*/content")
Expand Down Expand Up @@ -108,6 +146,21 @@ func setupTestServer(expectedItem *onepassword.Item, expectedVault onepassword.V
} else {
t.Errorf("Unexpected request: %s Consider adding this endpoint to the test server", r.URL.String())
}
} else if r.Method == http.MethodPut {
if r.URL.String() == fmt.Sprintf("/v1/vaults/%s/items/%s", expectedItem.Vault.ID, expectedItem.ID) {
itemToReturn := convertBodyToItem(r, t)
itemToReturn.ID = expectedItem.ID
itemBytes, err := json.Marshal(itemToReturn)

if err != nil {
t.Errorf("error marshaling item for testing: %s", err)
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(itemBytes)
if err != nil {
t.Errorf("error writing body: %s", err)
}
}
} else if r.Method == http.MethodDelete {
w.WriteHeader(http.StatusNoContent)
} else {
Expand Down