From bc6b5ddff179722eb86b2c9c7bd11d3d744fdb61 Mon Sep 17 00:00:00 2001 From: Sam Nolan Date: Thu, 15 Feb 2024 10:48:52 -0500 Subject: [PATCH] starting point for test cases and examples --- examples/example.py | 16 +++++++++ src/onepassword/client.py | 5 ++- src/tests/test_client.py | 70 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 examples/example.py create mode 100644 src/tests/test_client.py diff --git a/examples/example.py b/examples/example.py new file mode 100644 index 0000000..cc198a2 --- /dev/null +++ b/examples/example.py @@ -0,0 +1,16 @@ +from src.sdk import client as onepassword # temporary example syntax, must be changed before release +import os + +def main(): + # your 1Password service account token + token = os.environ['OP_SERVICE_ACCOUNT_TOKEN'] + + # initialize Client to connect to 1Password + client = onepassword.Client(token, onepassword.DEFAULT_INTEGRATION_NAME, onepassword.DEFAULT_INTEGRATION_VERSION) + + # resolve secret reference + result = client.secrets.resolve(reference="") + print(result) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/src/onepassword/client.py b/src/onepassword/client.py index ad673d9..a9a93ed 100644 --- a/src/onepassword/client.py +++ b/src/onepassword/client.py @@ -16,7 +16,6 @@ def __init__(self, auth, integration_name, integration_version): self.config = new_default_config(auth=auth, integration_name=integration_name, integration_version=integration_version) self.secrets = Secrets(client_id=InitClient(self.config)) - def new_default_config(auth, integration_name, integration_version): client_config_dict = { "SAToken": auth, @@ -27,7 +26,7 @@ def new_default_config(auth, integration_name, integration_version): "RequestLibraryName": DEFAULT_REQUEST_LIBRARY, "RequestLibraryVersion": sys.version_info[0] + "." + sys.version_info[1] + "." + sys.version_info[2], "SystemOS": platform.system(), - "SystemOSVersion": platform.architecture()[0], - "SystemArch": DEFAULT_OS_VERSION, + "SystemOSVersion": DEFAULT_OS_VERSION, + "SystemArch": platform.architecture()[0], } return client_config_dict diff --git a/src/tests/test_client.py b/src/tests/test_client.py new file mode 100644 index 0000000..ea94a91 --- /dev/null +++ b/src/tests/test_client.py @@ -0,0 +1,70 @@ +import unittest +from sdk import client as onepassword +import os + +TOKEN = os.environ['OP_SERVICE_ACCOUNT_TOKEN'] + +class TestPythonSDKClient(unittest.TestCase): + ## test resolve function + + # valid + def test_valid_resolve(self): + client = onepassword.Client(auth=TOKEN, integration_name=onepassword.DEFAULT_INTEGRATION_NAME, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + result = client.secrets.resolve(reference="test_username") + self.assertEqual(result, "password") # "password" is a temporary placeholder, replace with actual secret value + + # invalid + def test_invalid_resolve(self): + client = onepassword.Client(auth=TOKEN, integration_name=onepassword.DEFAULT_INTEGRATION_NAME, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + result = client.secrets.resolve(reference="invalid_reference") + + ## test client constructor + + # valid + def test_good_client_construction(self): + client = onepassword.Client(auth=TOKEN, integration_name=onepassword.DEFAULT_INTEGRATION_NAME, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + + # invalid + def test_client_construction_no_auth(self): + client = onepassword.Client(auth=TOKEN, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + + # invalid + def test_client_construction_no_name(self): + client = onepassword.Client(integration_name=onepassword.DEFAULT_INTEGRATION_NAME, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + + # invalid + def test_client_construction_no_version(self): + client = onepassword.Client(auth=TOKEN, integration_name=onepassword.DEFAULT_INTEGRATION_NAME) + + ## test config function + + # valid + def test_good_new_default_config(self): + config = onepassword.new_default_config(auth=TOKEN, integration_name=onepassword.DEFAULT_INTEGRATION_NAME, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + + # the commented out assertions may be untestable as they are device-specific + self.assertEqual(config["SAToken"], TOKEN) + self.assertEqual(config["Language"], onepassword.SDK_LANGUAGE) + self.assertEqual(config["SDKVersion"], onepassword.SDK_VERSION) + self.assertEqual(config["IntegrationName"], onepassword.DEFAULT_INTEGRATION_NAME) + self.assertEqual(config["IntegrationVersion"], onepassword.DEFAULT_INTEGRATION_VERSION) + self.assertEqual(config["RequestLibraryName"], onepassword.DEFAULT_REQUEST_LIBRARY) + # self.assertEqual(config["RequestLibraryVersion"], ) + # self.assertEqual(config["SystemOS"],) + self.assertEqual(config["SystemOSVersion"], onepassword.DEFAULT_OS_VERSION) + # self.assertEqual(config["SystemArch"], ) + + # invalid + def test_new_default_config_no_auth(self): + config = onepassword.new_default_config(integration_name=onepassword.DEFAULT_INTEGRATION_NAME, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + + # invalid + def test_new_default_config_no_name(self): + config = onepassword.new_default_config(auth=TOKEN, integration_version=onepassword.DEFAULT_INTEGRATION_VERSION) + + # invalid + def test_new_default_config_no_version(self): + config = onepassword.new_default_config(auth=TOKEN, integration_name=onepassword.DEFAULT_INTEGRATION_NAME) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file