Skip to content

Commit

Permalink
auto-format record tests
Browse files Browse the repository at this point in the history
  • Loading branch information
idimov-keeper committed Sep 13, 2023
1 parent 9ce6056 commit 1b32689
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions sdk/python/core/tests/record_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import tempfile
import os
import tempfile
import unittest

from keeper_secrets_manager_core.storage import FileKeyValueStorage
from keeper_secrets_manager_core import SecretsManager
Expand All @@ -20,34 +20,40 @@ def tearDown(self):
os.chdir(self.orig_working_dir)

def test_the_login_record_password(self):

""" If the record type is login, the password will be placed in the instance attribute.
""" If the record type is login, the password will be placed
in the instance attribute.
"""

try:
with tempfile.NamedTemporaryFile("w", delete=False) as fh:
fh.write(MockConfig.make_json())
fh.seek(0)
secrets_manager = SecretsManager(config=FileKeyValueStorage(config_file_location=fh.name))
secrets_manager = SecretsManager(
config=FileKeyValueStorage(config_file_location=fh.name))

# A good record.
# 'fields': [...{'type': 'password', 'value': ['My Password']}...]
# 'fields':[{'type': 'password', 'value': ['My Password']}...]
good_res = mock.Response()
good = good_res.add_record(title="Good Record", record_type='login')
good = good_res.add_record(
title="Good Record", record_type='login')
good.field("login", "My Login")
good.field("password", "My Password")

# A bad record. This would be like if someone removed a password text from an existing field.
# A bad record. This would be like if someone removed
# a password text from an existing field.
# 'fields': [...{'type': 'password', 'value': []}...]
bad_res = mock.Response()
bad = bad_res.add_record(title="Bad Record", record_type='login')
bad = bad_res.add_record(
title="Bad Record", record_type='login')
bad.field("login", "My Login")
bad.field("password", [])

# An ugly record. The application didn't even add the field. We need to set flags to prune empty fields.
# An ugly record. The application didn't even add the field.
# We need to set flags to prune empty fields.
# 'fields': [...]
ugly_res = mock.Response(flags={"prune_empty_fields": True})
ugly = ugly_res.add_record(title="Ugly Record", record_type='login')
ugly = ugly_res.add_record(
title="Ugly Record", record_type='login')
ugly.field("login", "My Login")

# this will be removed from the fields array.
Expand All @@ -59,16 +65,23 @@ def test_the_login_record_password(self):
res_queue.add_response(ugly_res)

records = secrets_manager.get_secrets()
self.assertEqual(1, len(records), "didn't get 1 record for the good")
self.assertEqual("My Password", records[0].password, "did not get correct password for the good")
self.assertEqual(
1, len(records), "didn't get 1 record for the good")
self.assertEqual(
"My Password", records[0].password,
"did not get correct password for the good")

records = secrets_manager.get_secrets()
self.assertEqual(1, len(records), "didn't get 1 record for the bad")
self.assertIsNone(records[0].password, "password is defined for the bad")
self.assertEqual(
1, len(records), "didn't get 1 record for the bad")
self.assertIsNone(records[0].password,
"password is defined for the bad")

records = secrets_manager.get_secrets()
self.assertEqual(1, len(records), "didn't get 1 record for the ugly")
self.assertIsNone(records[0].password, "password is defined for the ugly")
self.assertEqual(
1, len(records), "didn't get 1 record for the ugly")
self.assertIsNone(records[0].password,
"password is defined for the ugly")
finally:
try:
os.unlink(fh.name)
Expand All @@ -77,17 +90,21 @@ def test_the_login_record_password(self):

def test_record_field(self):

rf = RecordField(field_type="login", value="test", label="Test", required=True, enforceGeneration=False,
rf = RecordField(field_type="login", value="test", label="Test",
required=True, enforceGeneration=False,
privacyScreen=True, complexity={"foo": "bar"})

value = helpers.obj_to_dict(rf)
self.assertEqual("login", value.get("type"), "type is not correct")
self.assertEqual(["test"], value.get("value"), "value is not correct")
self.assertEqual("Test", value.get("label"), "label is not correct")
self.assertTrue(value.get("required"), "required is not correct")
self.assertFalse(value.get("enforceGeneration"), "enforceGeneration is not correct")
self.assertTrue(value.get("privacyScreen"), "privacyScreen is not correct")
self.assertIsNotNone(value.get("complexity"), "complexity is not correct")
self.assertFalse(value.get("enforceGeneration"),
"enforceGeneration is not correct")
self.assertTrue(value.get("privacyScreen"),
"privacyScreen is not correct")
self.assertIsNotNone(value.get("complexity"),
"complexity is not correct")

rf = RecordField(field_type="login", value="test", privacyScreen=None)

Expand All @@ -96,20 +113,22 @@ def test_record_field(self):
self.assertEqual(["test"], value.get("value"), "value is not correct")
self.assertIsNone(value.get("label"), "label is not correct")
self.assertIsNone(value.get("required"), "required is not correct")
self.assertIsNone(value.get("enforceGeneration"), "enforceGeneration is not correct")
self.assertIsNone(value.get("enforceGeneration"),
"enforceGeneration is not correct")
assert "privacyScreen" not in value, "privacyScreen exists in dictionary"
self.assertIsNone(value.get("privacyScreen"), "privacyScreen is not correct")
self.assertIsNone(value.get("privacyScreen"),
"privacyScreen is not correct")
self.assertIsNone(value.get("complexity"), "complexity is not correct")

def test_missing_fields_section(self):

""" Test for clients that may set "fields": null in JSON data """

try:
with tempfile.NamedTemporaryFile("w", delete=False) as fh:
fh.write(MockConfig.make_json())
fh.seek(0)
secrets_manager = SecretsManager(config=FileKeyValueStorage(config_file_location=fh.name))
secrets_manager = SecretsManager(
config=FileKeyValueStorage(config_file_location=fh.name))

res = mock.Response()
rec = res.add_record(title="MyLogin", record_type='login')
Expand All @@ -118,7 +137,8 @@ def test_missing_fields_section(self):
res_queue.add_response(res)

records = secrets_manager.get_secrets()
self.assertEqual(1, len(records), "didn't get 1 record for MyLogin")
self.assertEqual(
1, len(records), "didn't get 1 record for MyLogin")
self.assertEqual([], records[0].dict.get('fields'))
finally:
try:
Expand Down

0 comments on commit 1b32689

Please sign in to comment.