Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atammy-narmi committed Jun 14, 2024
1 parent 23395c3 commit 39bbf8a
Show file tree
Hide file tree
Showing 60 changed files with 15,647 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.7.0
golang.org/x/crypto v0.23.0
)

require (
Expand Down Expand Up @@ -76,7 +77,6 @@ require (
github.com/zclconf/go-cty v1.14.4 // indirect
go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
Expand Down
30 changes: 30 additions & 0 deletions internal/provider/onepassword_item_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,36 @@ func TestAccItemPasswordDatabase(t *testing.T) {
})
}

func TestAccItemSSHKey(t *testing.T) {
expectedItem := generateSSHKeyItem()
expectedVault := op.Vault{
ID: expectedItem.Vault.ID,
Name: "Name of the vault",
Description: "This vault will be retrieved",
}

testServer := setupTestServer(expectedItem, expectedVault, t)
defer testServer.Close()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProviderConfig(testServer.URL) + testAccItemDataSourceConfig(expectedItem.Vault.ID, expectedItem.ID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.onepassword_item.test", "id", fmt.Sprintf("vaults/%s/items/%s", expectedVault.ID, expectedItem.ID)),
resource.TestCheckResourceAttr("data.onepassword_item.test", "vault", expectedVault.ID),
resource.TestCheckResourceAttr("data.onepassword_item.test", "title", expectedItem.Title),
resource.TestCheckResourceAttr("data.onepassword_item.test", "uuid", expectedItem.ID),
resource.TestCheckResourceAttr("data.onepassword_item.test", "category", strings.ToLower(string(expectedItem.Category))),
resource.TestCheckResourceAttr("data.onepassword_item.test", "private_key", expectedItem.Fields[0].Value),
resource.TestCheckResourceAttr("data.onepassword_item.test", "public_key", expectedItem.Fields[1].Value),
),
},
},
})
}

func testAccItemDataSourceConfig(vault, uuid string) string {
return fmt.Sprintf(`
data "onepassword_item" "test" {
Expand Down
45 changes: 44 additions & 1 deletion internal/provider/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package provider

import "github.com/1Password/connect-sdk-go/onepassword"
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"

"github.com/1Password/connect-sdk-go/onepassword"
"golang.org/x/crypto/ssh"
)

func generateBaseItem() onepassword.Item {
item := onepassword.Item{}
Expand Down Expand Up @@ -61,6 +70,14 @@ func generateLoginItem() *onepassword.Item {
return &item
}

func generateSSHKeyItem() *onepassword.Item {
item := generateBaseItem()
item.Category = onepassword.SSHKey
item.Fields = generateSSHKeyFields()

return &item
}

func generateSecureNoteItem() *onepassword.Item {
item := generateBaseItem()
item.Category = onepassword.SecureNote
Expand Down Expand Up @@ -137,3 +154,29 @@ func generateLoginFields() []*onepassword.ItemField {
}
return fields
}

func generateSSHKeyFields() []*onepassword.ItemField {
bitSize := 2048
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
panic(err)
}
privateKeyPem := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
publicRSAKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
panic(err)
}
publicKey := "ssh-rsa " + base64.StdEncoding.EncodeToString(publicRSAKey.Marshal())

fields := []*onepassword.ItemField{
{
Label: "private key",
Value: string(pem.EncodeToMemory(privateKeyPem)),
},
{
Label: "public key",
Value: publicKey,
},
}
return fields
}
16 changes: 16 additions & 0 deletions vendor/golang.org/x/crypto/chacha20/chacha_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 39bbf8a

Please sign in to comment.