コード例 #1
0
def test_signin_fail(db, client):
    res = client.post('/auth/basic/signin/',
                      data={
                          'email': '*****@*****.**',
                          'password': '******'
                      })
    assert_error(response=res, status_code=401, detail='Unauthorized')
コード例 #2
0
def test_is_one_of_failure():
    """Ensuring that x is equal to 2 throw an assertion error."""
    obj = {"x": 4}
    validator = ensure(obj).respects(that("x").is_one_of([1, 2, 3]))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The value is 4, expected one of 1, 2, 3.")
コード例 #3
0
ファイル: test_is_empty.py プロジェクト: dylandoamaral/certum
def test_is_empty_failure():
    """Ensuring that x is not empty."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").is_empty())
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The target is not empty.")
コード例 #4
0
def test_is_instance_of_failure():
    """Ensuring that x is empty."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").is_instance_of(str))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The value is instance of int, expected str.")
コード例 #5
0
def test_apply_fail():
    """Ensuring that you can apply a function correctly."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").apply(fn))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => 2 != 1.")
コード例 #6
0
def test_equals_failure():
    """Ensuring that x is equal to 2 throw an assertion error."""
    obj = {"x": 3}
    validator = ensure(obj).respects(that("x").equals(2))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The value is 3, expected 2.")
コード例 #7
0
def test_exists_nested_failure():
    """Ensuring that exists error should return an assertion error."""
    obj = {}
    validator = ensure(obj).respects(that("x", "b").exists())
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The path is missing.")
コード例 #8
0
def test_has_length_of_dict_failure():
    """Ensuring that x is equal a dict of size 2."""
    obj = {"x": {"x": 2, "y": 3, "z": 4}}
    validator = ensure(obj).respects(that("x").has_length_of(2))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The length is 3, expected 2.")
コード例 #9
0
def test_has_length_of_list_failure():
    """Ensuring that x is equal a list of size 2 throw an assertion error."""
    obj = {"x": [2, 3, 4]}
    validator = ensure(obj).respects(that("x").has_length_of(2))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The length is 3, expected 2.")
コード例 #10
0
def test_unknown_path():
    """Ensuring that the rule doesn't start if the path is unknown."""
    obj = {}
    validator = ensure(obj).respects(that("x").exists())
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The path is missing.")
コード例 #11
0
def test_one():
    """Ensure that certum accumulate multiple type of errors together."""
    obj = {
        "a": 2,
        "b": {
            "c": [],
        },
        "d": "e",
    }

    validator = (ensure(obj).respects(
        that("a").equals(2),
        that("b").is_instance_of(list),
        that("b", "c").is_instance_of(int),
        that("x", "a").equals(2),
        that("x", "b").equals(4),
    ).using(AlphabeticalSorting()))

    with pytest.raises(CertumException) as error:
        validator.check()

    errors = [
        "[b] => The value is instance of dict, expected list.",
        "[b -> c] => The value is instance of list, expected int.",
        "[x] => The path is missing.",
    ]
    assert_error(error, "\n".join(errors))
コード例 #12
0
def test_has_unique_elements_list_failure():
    """Ensuring that foreach is applied correctly with a failure for a dict."""
    obj = {"x": [2, 2, 3]}
    validator = ensure(obj).respects(that("x").has_unique_elements())
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The row 0 and 1 are the same.")
コード例 #13
0
ファイル: test_foreach.py プロジェクト: dylandoamaral/certum
def test_type_failure():
    """Ensuring that foreach is not working if not a dict or a list."""
    obj = {"x": 2}
    rule = this.equals(2)
    validator = ensure(obj).respects(that("x").foreach(rule))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The path should be instance of dict or list.")
コード例 #14
0
def test_unknown_path():
    """Ensuring that the rule doesn't start if the path is unknown."""
    obj = {}
    rule = that("y").equals(2)
    validator = ensure(obj).respects(that("x").forsome(rule, keys=[0]))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The path is missing.")
コード例 #15
0
ファイル: test_foreach.py プロジェクト: dylandoamaral/certum
def test_foreach_list_failure():
    """Ensuring that foreach is applied correctly with a failure for a dict."""
    obj = {"x": [2, 2, 3]}
    rule = this.equals(2)
    validator = ensure(obj).respects(that("x").foreach(rule))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x -> 2] => The value is 3, expected 2.")
コード例 #16
0
def test_forsome_dict_failure():
    """Ensuring that forsome is applied correctly with a failure for a dict."""
    obj = {"x": {"a": 2, "b": 2, "c": 3}}
    rule = this.equals(2)
    validator = ensure(obj).respects(
        that("x").forsome(rule, keys=["a", "b", "c"]))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x -> c] => The value is 3, expected 2.")
コード例 #17
0
def test_signup_fail_user_exists(db, client):
    res = client.post('/auth/basic/signup/',
                      data={
                          'email': '*****@*****.**',
                          'password': '******',
                          'first_name': 'User100',
                          'last_name': 'Earth'
                      })
    assert_error(response=res, status_code=401, detail='Unauthorized')
コード例 #18
0
def test_authorize_fail_response_type(u3client):
    res = u3client.get('/auth/o/authorize/',
                       data={
                           'client_id': 2,
                           'response_type': 'implicit',
                           'state': 'yabba',
                           'redirect_uri': 'https://www.google.com/',
                           'scope': 'admin'
                       })
    assert_error(response=res, status_code=400, detail='Invalid')
コード例 #19
0
def test_refresh_token_fail_code_verifier(u1client, random_str1, random_str2):
    code = authorize(u1client=u1client, code_verifier=random_str1)
    res = u1client.post('/auth/o/token/',
                        data={
                            'client_id': 2,
                            'code_verifier': random_str2,
                            'code': code,
                            'state': 'yabba',
                            'grant_type': 'authorization_code'
                        })
    assert_error(response=res, status_code=401, detail='Invalid code_verifier')
コード例 #20
0
def test_refresh_token_fail_secret(u1client):
    code = authorize(u1client=u1client)
    res = u1client.post('/auth/o/token/',
                        data={
                            'client_id': 2,
                            'client_secret': 'blah',
                            'code': code,
                            'state': 'yabba',
                            'grant_type': 'authorization_code'
                        })
    assert_error(response=res, status_code=401, detail='Unauthorized')
コード例 #21
0
def test_authorize_fail_role(u3client):
    # u3 does not have admin role, therefore cannot authorize for scope admin
    res = u3client.get('/auth/o/authorize/',
                       data={
                           'client_id': 2,
                           'response_type': 'code',
                           'state': 'yabba',
                           'redirect_uri': 'https://www.google.com/',
                           'scope': 'admin'
                       })
    assert_error(response=res, status_code=403, detail='Requested scopes')
コード例 #22
0
def test_refresh_token_fail_grant_type(u1client):
    code = authorize(u1client=u1client)
    res = u1client.post('/auth/o/token/',
                        data={
                            'client_id': 2,
                            'client_secret': 'password123',
                            'code': code,
                            'state': 'yabba',
                            'grant_type': 'booya'
                        })
    assert_error(response=res, status_code=400, detail='Invalid')
コード例 #23
0
def test_access_token_fail_code_verifier(u1client, random_str1, random_str2):
    code = authorize(u1client=u1client, code_verifier=random_str1)
    refresh_token, access_token = tokens(u1client=u1client,
                                         code=code,
                                         code_verifier=random_str1)
    res = u1client.post('/auth/o/token/',
                        data={
                            'client_id': 2,
                            'code_verifier': random_str2,
                            'refresh_token': refresh_token,
                            'grant_type': 'refresh_token'
                        })
    assert_error(response=res, status_code=401, detail='Invalid code_verifier')
コード例 #24
0
def test_forsome_hybrid_success_and_failure():
    """Ensuring that forsome is applied correctly for a list."""
    obj = {"x": {"a": 2, "b": 3, "c": 3}}
    rule = this.equals(2)
    validator = (ensure(obj).respects(
        that("x").forsome(rule, keys=["a", "b",
                                      "d"])).using(AlphabeticalSorting()))
    with pytest.raises(CertumException) as error:
        validator.check()
    errors = [
        "[x -> b] => The value is 3, expected 2.",
        "[x -> d] => The path is missing.",
    ]
    assert_error(error, "\n".join(errors))
コード例 #25
0
def test_two():
    """Ensure that certum accumulate multiple type of errors together + strategies."""
    my_obj = {"name": 2, "entities": [1, 3, 3], "nested": {"value": 2}}

    validator = (ensure(my_obj).respects(
        that("name").is_instance_of(str),
        that("name").equals("Hello"),
        that("entities").foreach(this.equals(1)),
        that("nested", "value").equals(4),
    ).using(GroupedPrinting(), AlphabeticalSorting()))

    with pytest.raises(CertumException) as error:
        validator.check()

    errors = [
        "entities -> 1   => The value is 3, expected 1.",
        "entities -> 2   => The value is 3, expected 1.",
        "name            => The value is 2, expected Hello.",
        "                   The value is instance of int, expected str.",
        "nested -> value => The value is 2, expected 4.",
    ]
    assert_error(error, "\n".join(errors))
コード例 #26
0
def test_delete_fail(db, u1client):
    res = u1client.delete(f'/common/attachments/2323/')
    assert_error(response=res, status_code=404, detail='Not found')
コード例 #27
0
def test_switch_fail(db, u1client):
    res = u1client.post(f'/auth/workspaces/12121/switch/')
    assert_error(response=res, status_code=401, detail='Unauthorized')