Skip to content

Commit

Permalink
split examples and tests into smaller units
Browse files Browse the repository at this point in the history
  • Loading branch information
CheatCod committed Nov 29, 2024
1 parent a56d5eb commit 4a0159f
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 44 deletions.
62 changes: 18 additions & 44 deletions tests/test.py → example/item_crud_operation.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,45 @@
import asyncio
import os

# [developer-docs.sdk.python.sdk-import]-start
from onepassword import *
# [developer-docs.sdk.python.sdk-import]-end
from onepassword.client import Client
from onepassword.types import AutofillBehavior, ItemCategory, ItemCreateParams, ItemField, ItemFieldType, ItemSection, Website


async def main():
# [developer-docs.sdk.python.client-initialization]-start
# Gets your service account token from the OP_SERVICE_ACCOUNT_TOKEN environment variable.
# Perform CRUD operations on an item
async def item_crud_operation():
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
test_vault_id = os.getenv("CORE_SDK_TEST_VAULT_ID")
test_secret_ref = os.getenv("CORE_SDK_TEST_SECRET_REF")

if not token or not test_vault_id or not test_secret_ref:
raise ValueError("OP_SERVICE_ACCOUNT_TOKEN, CORE_SDK_TEST_VAULT_ID, and CORE_SDK_TEST_SECRET_REF must be set.")

# Connects to 1Password.
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",
)
# [developer-docs.sdk.python.client-initialization]-end

# [developer-docs.sdk.python.list-vaults]-start
vaults = await client.vaults.list_all()
async for vault in vaults:
print(vault.title)
# [developer-docs.sdk.python.list-vaults]-end

# [developer-docs.sdk.python.list-items]-start
items = await client.items.list_all(vault.id)
async for item in items:
print(item.title)
# [developer-docs.sdk.python.list-items]-end

# [developer-docs.sdk.python.resolve-secret]-start
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
value = await client.secrets.resolve(test_secret_ref)
print(value)
# [developer-docs.sdk.python.resolve-secret]-end

# [developer-docs.sdk.python.create-item]-start
# Create an Item and add it to your vault.
# 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="Login",
vaultId = test_vault_id,
category=ItemCategory.LOGIN,
vault_id=vault_id,
fields=[
ItemField(
id="username",
title="username",
field_type="Text",
field_type=ItemFieldType.TEXT,
value="mynameisjeff",
),
ItemField(
id="password",
title="password",
field_type="Concealed",
field_type=ItemFieldType.CONCEALED,
value="jeff",
),
ItemField(
id="onetimepassword",
title="one-time-password",
field_type="Totp",
field_type=ItemFieldType.TOTP,
section_id="totpsection",
value="otpauth://totp/my-example-otp?secret=jncrjgbdjnrncbjsr&issuer=1Password",
),
Expand All @@ -79,7 +53,7 @@ async def main():
Website(
label="my custom website",
url="https://example.com",
autofill_behavior="AnywhereOnWebsite",
autofill_behavior=AutofillBehavior.NEVER,
)
],
)
Expand Down Expand Up @@ -120,7 +94,7 @@ async def main():
Website(
label="my custom website 2",
url="https://example2.com",
autofill_behavior="Never",
autofill_behavior=AutofillBehavior.NEVER,
),
)
updated_item = await client.items.put(item)
Expand All @@ -134,4 +108,4 @@ async def main():


if __name__ == "__main__":
asyncio.run(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()
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()

0 comments on commit 4a0159f

Please sign in to comment.