Skip to content

Commit

Permalink
starting point for test cases and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-1pass committed Feb 15, 2024
1 parent 45a7889 commit bc6b5dd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
16 changes: 16 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -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="<your-secret-reference>")
print(result)

if __name__ == '__main__':
main()
5 changes: 2 additions & 3 deletions src/onepassword/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
70 changes: 70 additions & 0 deletions src/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit bc6b5dd

Please sign in to comment.