diff --git a/tests/test.py b/example/item_crud_operation.py similarity index 63% rename from tests/test.py rename to example/item_crud_operation.py index e234c01..521007c 100644 --- a/tests/test.py +++ b/example/item_crud_operation.py @@ -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", ), @@ -79,7 +53,7 @@ async def main(): Website( label="my custom website", url="https://example.com", - autofill_behavior="AnywhereOnWebsite", + autofill_behavior=AutofillBehavior.NEVER, ) ], ) @@ -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) @@ -134,4 +108,4 @@ async def main(): if __name__ == "__main__": - asyncio.run(main()) + asyncio.run(item_crud_operation()) \ No newline at end of file diff --git a/example/list_items_in_vaults.py b/example/list_items_in_vaults.py new file mode 100644 index 0000000..a965c37 --- /dev/null +++ b/example/list_items_in_vaults.py @@ -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()) \ No newline at end of file diff --git a/example/list_vaults.py b/example/list_vaults.py new file mode 100644 index 0000000..782cb75 --- /dev/null +++ b/example/list_vaults.py @@ -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()) \ No newline at end of file diff --git a/example/resolve_secret_reference.py b/example/resolve_secret_reference.py new file mode 100644 index 0000000..58b4443 --- /dev/null +++ b/example/resolve_secret_reference.py @@ -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() \ No newline at end of file diff --git a/example/validate_secret_reference.py b/example/validate_secret_reference.py new file mode 100644 index 0000000..0a3ee1d --- /dev/null +++ b/example/validate_secret_reference.py @@ -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() \ No newline at end of file diff --git a/tests/run_all_tests.sh b/tests/run_all_tests.sh new file mode 100755 index 0000000..bc247de --- /dev/null +++ b/tests/run_all_tests.sh @@ -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 \ No newline at end of file diff --git a/tests/test_list_items_in_vaults.py b/tests/test_list_items_in_vaults.py new file mode 100644 index 0000000..2b4cf9e --- /dev/null +++ b/tests/test_list_items_in_vaults.py @@ -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()) \ No newline at end of file diff --git a/tests/test_list_vaults.py b/tests/test_list_vaults.py new file mode 100644 index 0000000..776c94d --- /dev/null +++ b/tests/test_list_vaults.py @@ -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()) \ No newline at end of file diff --git a/tests/test_resolve_secret_reference.py b/tests/test_resolve_secret_reference.py new file mode 100644 index 0000000..7950370 --- /dev/null +++ b/tests/test_resolve_secret_reference.py @@ -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() diff --git a/tests/test_validate_secret_reference.py b/tests/test_validate_secret_reference.py new file mode 100644 index 0000000..4756919 --- /dev/null +++ b/tests/test_validate_secret_reference.py @@ -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()