def test_defaults():
    request = Request('Create', 'abc')
    r = SecretProvider()
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get('Length') == 30
    assert r.get('Alphabet') == r.request_schema['properties']['Alphabet']['default']
    assert not r.get('ReturnSecret')
    assert r.get('KeyAlias') == 'alias/aws/ssm'
    assert r.get('Description') == ''
def test_defaults():
    request = Request('Create', 'abc')
    r = SecretProvider()
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get('Length') == 30
    assert r.get('Alphabet') == r.request_schema['properties']['Alphabet']['default']
    assert not r.get('ReturnSecret')
    assert r.get('KeyAlias') == 'alias/aws/ssm'
    assert r.get('Description') == ''
def test_defaults():
    request = Request("Create", "abc")
    r = SecretProvider()
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get("Length") == 30
    assert r.get(
        "Alphabet") == r.request_schema["properties"]["Alphabet"]["default"]
    assert not r.get("ReturnSecret")
    assert r.get("KeyAlias") == "alias/aws/ssm"
    assert r.get("Description") == ""
    assert isinstance(r.get("NoEcho"), bool) and r.get("NoEcho")
def test_type_convert():
    request = Request('Create', 'abc')
    request['ResourceProperties']['Length'] = '62'
    request['ResourceProperties']['ReturnSecret'] = 'true'
    request['ResourceProperties']['RefreshOnUpdate'] = 'true'
    r = SecretProvider()
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get('Length') == 62
    assert r.get('ReturnSecret')

    request['ResourceProperties']['Length'] = 'fouteboole62'
    r = SecretProvider()
    r.set_request(request, {})
    assert not r.is_valid_request()

    request['ResourceProperties']['Length'] = u'62'
    request['ResourceProperties']['ReturnSecret'] = u'true'
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get('Length') == 62
    assert r.get('ReturnSecret')
def test_type_convert():
    request = Request("Create", "abc")
    request["ResourceProperties"]["Length"] = "62"
    request["ResourceProperties"]["ReturnSecret"] = "true"
    request["ResourceProperties"]["RefreshOnUpdate"] = "true"
    r = SecretProvider()
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get("Length") == 62
    assert r.get("ReturnSecret")

    request["ResourceProperties"]["Length"] = "fouteboole62"
    r = SecretProvider()
    r.set_request(request, {})
    assert not r.is_valid_request()

    request["ResourceProperties"]["Length"] = u"62"
    request["ResourceProperties"]["ReturnSecret"] = u"true"
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get("Length") == 62
    assert r.get("ReturnSecret")
def test_type_convert():
    request = Request('Create', 'abc')
    request['ResourceProperties']['Length'] = '62'
    request['ResourceProperties']['ReturnSecret'] = 'true'
    request['ResourceProperties']['RefreshOnUpdate'] = 'true'
    r = SecretProvider()
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get('Length') == 62
    assert r.get('ReturnSecret')

    request['ResourceProperties']['Length'] = 'fouteboole62'
    r = SecretProvider()
    r.set_request(request, {})
    assert not r.is_valid_request()

    request['ResourceProperties']['Length'] = u'62'
    request['ResourceProperties']['ReturnSecret'] = u'true'
    r.set_request(request, {})
    assert r.is_valid_request()
    assert r.get('Length') == 62
    assert r.get('ReturnSecret')
def test_generate_password():
    request = Request("Create", "abc")
    request["ResourceProperties"]["Required"] = [
        {
            "Count": 2,
            "Alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        },
        {
            "Count": 2,
            "Alphabet": "abcdefghijklmnopqrstuvwxyz"
        },
        {
            "Count": 1,
            "Alphabet": "0123456789"
        },
        {
            "Count": 1,
            "Alphabet": "!@#$%^&*()_+=-`~"
        },
    ]
    request["ResourceProperties"]["Length"] = 5
    provider = SecretProvider()
    provider.set_request(request, {})
    assert not provider.is_valid_request()
    assert (provider.reason ==
            "the length should at least exceed the 6 required characters")

    request["ResourceProperties"]["Length"] = 6
    password = provider.generate_password()
    character_counts = Counter(password)
    for r in request["ResourceProperties"]["Required"]:
        filtered = list(
            filter(lambda c: c[0] in r["Alphabet"], character_counts.items()))
        assert r["Count"] == sum(map(lambda c: c[1], filtered)), r["Alphabet"]

    request["ResourceProperties"]["Length"] = 30
    password = provider.generate_password()
    character_counts = Counter(password)
    for r in request["ResourceProperties"]["Required"]:
        filtered = list(
            filter(lambda c: c[0] in r["Alphabet"], character_counts.items()))
        assert sum(map(lambda c: c[1], filtered)) >= r["Count"], r["Alphabet"]

    del request["ResourceProperties"]["Required"]
    password = provider.generate_password()
    assert len(password) == 30