Browse Source
			
			
			Merge pull request #355 from marcospereirampj/test/finalize
			
				
		Merge pull request #355 from marcospereirampj/test/finalize
	
		
	
			
				Test/finalizepull/357/head v1.9.1
							committed by
							
								 GitHub
								GitHub
							
						
					
				
				
				  
				  No known key found for this signature in database
				  
				  	
						GPG Key ID: 4AEE18F83AFDEB23
				  	
				  
				
			
		
		
		
	
				 7 changed files with 151 additions and 17 deletions
			
			
		- 
					1src/keycloak/authorization/role.py
- 
					10src/keycloak/connection.py
- 
					16src/keycloak/uma_permissions.py
- 
					42tests/test_authorization.py
- 
					41tests/test_connection.py
- 
					20tests/test_exceptions.py
- 
					38tests/test_uma_permissions.py
| @ -0,0 +1,42 @@ | |||
| """Test authorization module.""" | |||
| import pytest | |||
| 
 | |||
| from keycloak.authorization import Permission, Policy, Role | |||
| from keycloak.exceptions import KeycloakAuthorizationConfigError | |||
| 
 | |||
| 
 | |||
| def test_authorization_objects(): | |||
|     """Test authorization objects.""" | |||
|     # Test permission | |||
|     p = Permission(name="test", type="test", logic="test", decision_strategy="test") | |||
|     assert p.name == "test" | |||
|     assert p.type == "test" | |||
|     assert p.logic == "test" | |||
|     assert p.decision_strategy == "test" | |||
|     p.resources = ["test"] | |||
|     assert p.resources == ["test"] | |||
|     p.scopes = ["test"] | |||
|     assert p.scopes == ["test"] | |||
| 
 | |||
|     # Test policy | |||
|     p = Policy(name="test", type="test", logic="test", decision_strategy="test") | |||
|     assert p.name == "test" | |||
|     assert p.type == "test" | |||
|     assert p.logic == "test" | |||
|     assert p.decision_strategy == "test" | |||
|     p.roles = ["test"] | |||
|     assert p.roles == ["test"] | |||
|     p.permissions = ["test"] | |||
|     assert p.permissions == ["test"] | |||
|     p.add_permission(permission="test2") | |||
|     assert p.permissions == ["test", "test2"] | |||
|     with pytest.raises(KeycloakAuthorizationConfigError): | |||
|         p.add_role(role="test2") | |||
| 
 | |||
|     # Test role | |||
|     r = Role(name="test") | |||
|     assert r.name == "test" | |||
|     assert not r.required | |||
|     assert r.get_name() == "test" | |||
|     assert r == r | |||
|     assert r == "test" | |||
| @ -0,0 +1,41 @@ | |||
| """Connection test module.""" | |||
| 
 | |||
| import pytest | |||
| 
 | |||
| from keycloak.connection import ConnectionManager | |||
| from keycloak.exceptions import KeycloakConnectionError | |||
| 
 | |||
| 
 | |||
| def test_connection_proxy(): | |||
|     """Test proxies of connection manager.""" | |||
|     cm = ConnectionManager( | |||
|         base_url="http://test.test", proxies={"http://test.test": "localhost:8080"} | |||
|     ) | |||
|     assert cm._s.proxies == {"http://test.test": "localhost:8080"} | |||
| 
 | |||
| 
 | |||
| def test_headers(): | |||
|     """Test headers manipulation.""" | |||
|     cm = ConnectionManager(base_url="http://test.test", headers={"H": "A"}) | |||
|     assert cm.param_headers(key="H") == "A" | |||
|     assert cm.param_headers(key="A") is None | |||
|     cm.clean_headers() | |||
|     assert cm.headers == dict() | |||
|     cm.add_param_headers(key="H", value="B") | |||
|     assert cm.exist_param_headers(key="H") | |||
|     assert not cm.exist_param_headers(key="B") | |||
|     cm.del_param_headers(key="H") | |||
|     assert not cm.exist_param_headers(key="H") | |||
| 
 | |||
| 
 | |||
| def test_bad_connection(): | |||
|     """Test bad connection.""" | |||
|     cm = ConnectionManager(base_url="http://not.real.domain") | |||
|     with pytest.raises(KeycloakConnectionError): | |||
|         cm.raw_get(path="bad") | |||
|     with pytest.raises(KeycloakConnectionError): | |||
|         cm.raw_delete(path="bad") | |||
|     with pytest.raises(KeycloakConnectionError): | |||
|         cm.raw_post(path="bad", data={}) | |||
|     with pytest.raises(KeycloakConnectionError): | |||
|         cm.raw_put(path="bad", data={}) | |||
| @ -0,0 +1,20 @@ | |||
| """Test the exceptions module.""" | |||
| 
 | |||
| from unittest.mock import Mock | |||
| 
 | |||
| import pytest | |||
| 
 | |||
| from keycloak.exceptions import KeycloakOperationError, raise_error_from_response | |||
| 
 | |||
| 
 | |||
| def test_raise_error_from_response_from_dict(): | |||
|     """Test raise error from response using a dictionary.""" | |||
|     response = Mock() | |||
|     response.json.return_value = {"key": "value"} | |||
|     response.status_code = 408 | |||
|     response.content = "Error" | |||
| 
 | |||
|     with pytest.raises(KeycloakOperationError): | |||
|         raise_error_from_response( | |||
|             response=response, error=dict(), expected_codes=[200], skip_exists=False | |||
|         ) | |||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue