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

Get full items by title #38

Merged
merged 15 commits into from
May 12, 2022
15 changes: 12 additions & 3 deletions connect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (rs *restClient) GetItemByTitle(title string, vaultUUID string) (*onepasswo
return nil, fmt.Errorf("Found %d item(s) in vault %q with title %q", len(items), vaultUUID, title)
}

return rs.GetItem(items[0].ID, items[0].Vault.ID)
return &items[0], nil
}

func (rs *restClient) GetItemsByTitle(title string, vaultUUID string) ([]onepassword.Item, error) {
Expand All @@ -259,11 +259,20 @@ func (rs *restClient) GetItemsByTitle(title string, vaultUUID string) ([]onepass
return nil, err
}

var items []onepassword.Item
if err := parseResponse(response, http.StatusOK, &items); err != nil {
var itemSummaries []onepassword.Item
if err := parseResponse(response, http.StatusOK, &itemSummaries); err != nil {
return nil, err
}

items := make([]onepassword.Item, len(itemSummaries))
for i, itemSummary := range itemSummaries {
tempItem, err := rs.GetItem(itemSummary.ID, itemSummary.Vault.ID)
if err != nil {
return nil, err
}
items[i] = *tempItem
}

return items, nil
}

Expand Down
88 changes: 58 additions & 30 deletions connect/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"net/http"
"os"
"reflect"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"

Expand All @@ -31,6 +31,9 @@ var testUserAgent string

var testServerDefaultVersion = version{1, 3, 0}

const testItemUUID = "2a47aa139ef74d7ca17918035e"
const testVaultUUID = "5b52aa139ef74d7ca17918nmf8"

type mockClient struct {
Dofunc func(req *http.Request) (*http.Response, error)
}
Expand Down Expand Up @@ -277,8 +280,8 @@ func Test_restClient_GetItems(t *testing.T) {
}

func Test_restClient_GetItemsByTitle(t *testing.T) {
mockHTTPClient.Dofunc = listItems
items, err := testClient.GetItemsByTitle("test", testID)
mockHTTPClient.Dofunc = listItemsOrGetItem
items, err := testClient.GetItemsByTitle("test-item", testVaultUUID)

if err != nil {
t.Logf("Unable to get item: %s", err.Error())
Expand All @@ -289,6 +292,11 @@ func Test_restClient_GetItemsByTitle(t *testing.T) {
t.Logf("Expected 1 item to exist in vault, found %d", len(items))
t.FailNow()
}

assert.Equal(t, items[0].Title, "test-item")
assert.NotEqual(t, len(items[0].Fields), 0)
assert.Equal(t, items[0].Fields[0].Value, "wendy")
assert.Equal(t, items[0].Fields[1].Value, "appleseed")
}

func Test_restClient_GetItemByTitle(t *testing.T) {
Expand All @@ -306,6 +314,10 @@ func Test_restClient_GetItemByTitle(t *testing.T) {
t.Log("Expected 1 item to exist")
t.FailNow()
}

assert.Equal(t, item.Fields[0].Value, "wendy")
assert.Equal(t, item.Fields[1].Value, "appleseed")

}

func Test_restClient_GetItemByNonUniqueTitle(t *testing.T) {
Expand Down Expand Up @@ -526,39 +538,57 @@ func getVault(vault *onepassword.Vault) func(req *http.Request) (*http.Response,
}
}

func generateComplexItem(vaultUUID string, itemUUID string) *onepassword.Item {
return &onepassword.Item{
ID: itemUUID,
func generateComplexItem(vaultUUID string) onepassword.Item {
return onepassword.Item{
Title: "test-item",
ID: testItemUUID,
Vault: onepassword.ItemVault{
ID: vaultUUID,
},
Sections: []*onepassword.ItemSection{{
ID: "",
Label: "section",
}},
Fields: []*onepassword.ItemField{{
ID: testID,
Label: "username",
Value: "wendy",
}, {
ID: testID,
Label: "password",
Value: "appleseed",
Section: &onepassword.ItemSection{
ID: "",
Label: "section",
Fields: []*onepassword.ItemField{
{
ID: testID,
Label: "username",
Value: "wendy",
},
{
Label: "password",
Value: "appleseed",
Section: &onepassword.ItemSection{
ID: "",
Label: "section",
},
},
},
},
}
}

func generateItem(vaultUUID string) *onepassword.Item {
return &onepassword.Item{
ID: testID,
Vault: onepassword.ItemVault{
ID: vaultUUID,
},
item := generateComplexItem(vaultUUID)
return &item
}

func listItemsOrGetItem(req *http.Request) (*http.Response, error) {
if strings.Contains(req.URL.RequestURI(), "test-item") {
json, _ := json.Marshal([]onepassword.Item{generateComplexItem(testVaultUUID)})
return &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(json)),
Header: req.Header,
}, nil
} else {
json, _ := json.Marshal(generateComplexItem(testVaultUUID))
return &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(json)),
Header: req.Header,
}, nil
}
}

Expand Down Expand Up @@ -608,12 +638,11 @@ func getItemByID(req *http.Request) (*http.Response, error) {
}

func getComplexItem(req *http.Request) (*http.Response, error) {
vaultUUID := ""
itemUUID := ""
vaultUUID := testVaultUUID
excessPath := ""
fmt.Sscanf(req.URL.Path, "/v1/vaults/%s%s", vaultUUID, excessPath)

json, _ := json.Marshal(generateComplexItem(vaultUUID, itemUUID))
json, _ := json.Marshal(generateComplexItem(vaultUUID))
return &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Expand Down Expand Up @@ -647,8 +676,7 @@ func createItem(req *http.Request) (*http.Response, error) {
return nil, err
}

newUUID := uuid.New()
item.ID = newUUID.String()
item.ID = testItemUUID
item.CreatedAt = time.Now()

vaultUUID := ""
Expand Down Expand Up @@ -691,8 +719,8 @@ func updateItem(req *http.Request) (*http.Response, error) {
}

func deleteItem(req *http.Request) (*http.Response, error) {
vaultUUID := ""
itemUUID := ""
vaultUUID := strings.ToLower(testVaultUUID)
itemUUID := strings.ToLower(testItemUUID)
fmt.Sscanf(req.URL.Path, "/v1/vaults/%s/items/%s", vaultUUID, itemUUID)

return &http.Response{
Expand Down