Merge "Create Authentication Plugins"
This commit is contained in:
		
							
								
								
									
										0
									
								
								auth/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								auth/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -17,6 +17,7 @@ import httpretty
 | 
				
			|||||||
import mock
 | 
					import mock
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from keystoneclient.auth import base
 | 
				
			||||||
from keystoneclient import exceptions
 | 
					from keystoneclient import exceptions
 | 
				
			||||||
from keystoneclient import session as client_session
 | 
					from keystoneclient import session as client_session
 | 
				
			||||||
from keystoneclient.tests import utils
 | 
					from keystoneclient.tests import utils
 | 
				
			||||||
@@ -252,3 +253,47 @@ class ConstructSessionFromArgsTests(utils.TestCase):
 | 
				
			|||||||
            args = {key: value}
 | 
					            args = {key: value}
 | 
				
			||||||
            self.assertEqual(getattr(self._s(args), key), value)
 | 
					            self.assertEqual(getattr(self._s(args), key), value)
 | 
				
			||||||
            self.assertNotIn(key, args)
 | 
					            self.assertNotIn(key, args)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class AuthPlugin(base.BaseAuthPlugin):
 | 
				
			||||||
 | 
					    """Very simple debug authentication plugin.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Takes Parameters such that it can throw exceptions at the right times.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TEST_TOKEN = 'aToken'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __init__(self, token=TEST_TOKEN):
 | 
				
			||||||
 | 
					        self.token = token
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_token(self, session):
 | 
				
			||||||
 | 
					        return self.token
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SessionAuthTests(utils.TestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TEST_URL = 'http://127.0.0.1:5000/'
 | 
				
			||||||
 | 
					    TEST_JSON = {'hello': 'world'}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @httpretty.activate
 | 
				
			||||||
 | 
					    def test_auth_plugin_default_with_plugin(self):
 | 
				
			||||||
 | 
					        self.stub_url('GET', base_url=self.TEST_URL, json=self.TEST_JSON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # if there is an auth_plugin then it should default to authenticated
 | 
				
			||||||
 | 
					        auth = AuthPlugin()
 | 
				
			||||||
 | 
					        sess = client_session.Session(auth=auth)
 | 
				
			||||||
 | 
					        resp = sess.get(self.TEST_URL)
 | 
				
			||||||
 | 
					        self.assertDictEqual(resp.json(), self.TEST_JSON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertRequestHeaderEqual('X-Auth-Token', AuthPlugin.TEST_TOKEN)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @httpretty.activate
 | 
				
			||||||
 | 
					    def test_auth_plugin_disable(self):
 | 
				
			||||||
 | 
					        self.stub_url('GET', base_url=self.TEST_URL, json=self.TEST_JSON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        auth = AuthPlugin()
 | 
				
			||||||
 | 
					        sess = client_session.Session(auth=auth)
 | 
				
			||||||
 | 
					        resp = sess.get(self.TEST_URL, authenticated=False)
 | 
				
			||||||
 | 
					        self.assertDictEqual(resp.json(), self.TEST_JSON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertRequestHeaderEqual('X-Auth-Token', None)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user