def test_signin_fail(db, client): res = client.post('/auth/basic/signin/', data={ 'email': '*****@*****.**', 'password': '******' }) assert_error(response=res, status_code=401, detail='Unauthorized')
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.")
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.")
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.")
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.")
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.")
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.")
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.")
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.")
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.")
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))
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.")
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.")
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.")
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.")
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.")
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')
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')
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')
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')
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')
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')
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')
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))
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))
def test_delete_fail(db, u1client): res = u1client.delete(f'/common/attachments/2323/') assert_error(response=res, status_code=404, detail='Not found')
def test_switch_fail(db, u1client): res = u1client.post(f'/auth/workspaces/12121/switch/') assert_error(response=res, status_code=401, detail='Unauthorized')