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

Add tests with env vars #132

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
111 changes: 111 additions & 0 deletions example/item_crud_operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import asyncio
import os

from onepassword.client import Client
from onepassword.types import AutofillBehavior, ItemCategory, ItemCreateParams, ItemField, ItemFieldType, ItemSection, Website

# Perform CRUD operations on an item
async def item_crud_operation():
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
if not token:
raise ValueError("OP_SERVICE_ACCOUNT_TOKEN must be set.")
client = await Client.authenticate(
auth=token,
# Set the following to your own integration name and version.
integration_name="My 1Password Integration",
integration_version="v1.0.0",
)
# Fill in a real vault ID below
# You can find this by using the list_vaults.py example
# Or using `op vault list`
vault_id = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
to_create = ItemCreateParams(
title="MyName",
category=ItemCategory.LOGIN,
vault_id=vault_id,
fields=[
ItemField(
id="username",
title="username",
field_type=ItemFieldType.TEXT,
value="mynameisjeff",
),
ItemField(
id="password",
title="password",
field_type=ItemFieldType.CONCEALED,
value="jeff",
),
ItemField(
id="onetimepassword",
title="one-time-password",
field_type=ItemFieldType.TOTP,
section_id="totpsection",
value="otpauth://totp/my-example-otp?secret=jncrjgbdjnrncbjsr&issuer=1Password",
),
],
sections=[
ItemSection(id="", title=""),
ItemSection(id="totpsection", title=""),
],
tags=["test tag 1", "test tag 2"],
websites=[
Website(
label="my custom website",
url="https://example.com",
autofill_behavior=AutofillBehavior.NEVER,
)
],
)
created_item = await client.items.create(to_create)
# [developer-docs.sdk.python.create-item]-end

print(dict(created_item))

# [developer-docs.sdk.python.resolve-totp-code]-start
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
code = await client.secrets.resolve(
f"op://{created_item.vault_id}/{created_item.id}/TOTP_onetimepassword?attribute=totp"
)
print(code)
# [developer-docs.sdk.python.resolve-totp-code]-end

# [developer-docs.sdk.python.get-totp-item-crud]-start
# Fetch a totp code from the item
for f in created_item.fields:
if f.field_type == "Totp":
if f.details.content.error_message is not None:
print(f.details.content.error_message)
else:
print(f.details.content.code)
# [developer-docs.sdk.python.get-totp-item-crud]-end

# [developer-docs.sdk.python.get-item]-start
# Retrieve an item from your vault.
item = await client.items.get(created_item.vault_id, created_item.id)
# [developer-docs.sdk.python.get-item]-end

print(dict(item))

# [developer-docs.sdk.python.update-item]-start
# Update a field in your item
item.fields[0].value = "new_value"
item.websites.append(
Website(
label="my custom website 2",
url="https://example2.com",
autofill_behavior=AutofillBehavior.NEVER,
),
)
updated_item = await client.items.put(item)
# [developer-docs.sdk.python.update-item]-end

print(dict(updated_item))
# [developer-docs.sdk.python.delete-item]-start
# Delete a item from your vault.
await client.items.delete(created_item.vault_id, updated_item.id)
# [developer-docs.sdk.python.delete-item]-end


if __name__ == "__main__":
asyncio.run(item_crud_operation())
24 changes: 24 additions & 0 deletions example/list_items_in_vaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import asyncio
import os

from onepassword.client import Client

# list all items in all vaults
async def list_items_in_vaults():
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
if not token:
raise ValueError("OP_SERVICE_ACCOUNT_TOKEN must be set.")
client = await Client.authenticate(
auth=token,
# Set the following to your own integration name and version.
integration_name="My 1Password Integration",
integration_version="v1.0.0",
)
vaults = await client.vaults.list_all()
async for vault in vaults:
items = await client.items.list_all(vault.id)
async for item in items:
print(item.title)

if __name__ == "__main__":
asyncio.run(list_items_in_vaults())
22 changes: 22 additions & 0 deletions example/list_vaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import asyncio
import os

from onepassword.client import Client

# list all vaults
async def list_vaults():
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
if not token:
raise ValueError("OP_SERVICE_ACCOUNT_TOKEN must be set.")
client = await Client.authenticate(
auth=token,
# Set the following to your own integration name and version.
integration_name="My 1Password Integration",
integration_version="v1.0.0",
)
vaults = await client.vaults.list_all()
async for vault in vaults:
print(vault.title)

if __name__ == "__main__":
asyncio.run(list_vaults())
15 changes: 15 additions & 0 deletions example/resolve_secret_reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

from onepassword.secrets import Secrets

# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
def resolve_secret_reference():
# Replace this with a real secret reference
secret_reference = "op://vault/item/field"
try:
Secrets.validate_secret_reference(secret_reference)
print("Secret reference resolved successfully")
except Exception as error:
print(error)

if __name__ == "__main__":
resolve_secret_reference()
13 changes: 13 additions & 0 deletions example/validate_secret_reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from onepassword.secrets import Secrets

# Validate secret reference to ensure no syntax errors
def validate_secret_reference():
try:
Secrets.validate_secret_reference("op://vault/item/field")
print("Secret reference is of valid syntax")
except Exception as error:
print(error)

if __name__ == "__main__":
validate_secret_reference()
6 changes: 6 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To test, make sure the following env vars are defined:

```sh
export CORE_SDK_TEST_SECRET_REF='op://vault/item/field'
export CORE_SDK_TEST_VAULT_ID='' # can get from `op list vaults`
```
9 changes: 9 additions & 0 deletions tests/run_all_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# change working directory to the directory of the script

cd "$(dirname "$0")"

# Run all python files in the tests directory
for file in *.py
do
python3 $file
done
26 changes: 26 additions & 0 deletions tests/test_list_items_in_vaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import asyncio
import os

from onepassword.client import Client

async def test_list_items_in_vaults():
print("Starting list_items_in_vaults test")
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
if not token:
raise ValueError("OP_SERVICE_ACCOUNT_TOKEN must be set.")
client = await Client.authenticate(
auth=token,
# Set the following to your own integration name and version.
integration_name="My 1Password Integration",
integration_version="v1.0.0",
)
vaults = await client.vaults.list_all()
async for vault in vaults:
items = await client.items.list_all(vault.id)
async for item in items:
print(item.title)

print("list_items_in_vaults test completed successfully")

if __name__ == "__main__":
asyncio.run(test_list_items_in_vaults())
24 changes: 24 additions & 0 deletions tests/test_list_vaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import asyncio
import os

from onepassword.client import Client

async def test_list_vaults():
print("Starting list_vaults test")
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
if not token:
raise ValueError("OP_SERVICE_ACCOUNT_TOKEN must be set.")
client = await Client.authenticate(
auth=token,
# Set the following to your own integration name and version.
integration_name="My 1Password Integration",
integration_version="v1.0.0",
)
vaults = await client.vaults.list_all()
async for vault in vaults:
print(vault.title)

print("Finished list_vaults test successfully")

if __name__ == "__main__":
asyncio.run(test_list_vaults())
13 changes: 13 additions & 0 deletions tests/test_resolve_secret_reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from onepassword.secrets import Secrets
import os

def test_resolve_secret_reference():
print("Starting resolve_secret_reference test")
test_secret_ref = os.getenv("CORE_SDK_TEST_SECRET_REF")
if not test_secret_ref:
raise ValueError("CORE_SDK_TEST_SECRET_REF must be set.")
Secrets.validate_secret_reference(test_secret_ref)
print("Finished resolve_secret_reference test successfully")

if __name__ == "__main__":
test_resolve_secret_reference()
10 changes: 10 additions & 0 deletions tests/test_validate_secret_reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from onepassword.secrets import Secrets

# Validate secret reference to ensure no syntax errors
def test_validate_secret_reference():
print("Starting validate_secret_reference test")
Secrets.validate_secret_reference("op://vault/item/field")
print("Finished validate_secret_reference test successfully")

if __name__ == "__main__":
test_validate_secret_reference()