Ejemplo n.º 1
0
    def test_get_access(self):
        auth_plugin = authv1.PasswordPlugin(**self.options)
        with mock.patch('swiftclient.authv1.time.time', return_value=1234.56):
            access = auth_plugin.get_access(self.mock_session)

        self.assertEqual(self.mock_session.get.mock_calls, [
            mock.call(self.options['auth_url'],
                      authenticated=False,
                      log=False,
                      headers={
                          'X-Auth-User': self.options['username'],
                          'X-Auth-Key': self.options['password'],
                      })
        ])

        self.assertEqual(self.options['username'], access.username)
        # `openstack token issue` requires a user_id property
        self.assertEqual(self.options['username'], access.user_id)
        self.assertEqual(self.storage_url, access.storage_url)
        self.assertEqual(self.token, access.auth_token)
        self.assertEqual(1234.56, access._issued)
        self.assertIs(datetime.datetime, type(auth_plugin.auth_ref.issued))
        self.assertIsNone(access.expires)

        # `openstack catalog list/show` require a catalog property
        catalog = access.service_catalog.catalog
        self.assertEqual('swift', catalog[0].get('name'))
        self.assertEqual('object-store', catalog[0].get('type'))
        self.assertIn('endpoints', catalog[0])
        self.assertIn(self.storage_url,
                      [e.get('publicURL') for e in catalog[0]['endpoints']])
Ejemplo n.º 2
0
    def test_set_state(self):
        auth_plugin = authv1.PasswordPlugin(**self.options)
        self.assertIsNone(auth_plugin.auth_ref)

        auth_plugin.auth_ref = object()
        auth_plugin.set_auth_state(None)
        self.assertIsNone(auth_plugin.get_auth_state())

        state = json.dumps(
            {
                'auth_url': self.options['auth_url'],
                'username': self.options['username'],
                'account': self.options.get('project_name'),
                'issued': 1234.56,
                'storage_url': self.storage_url,
                'auth_token': self.token,
                'expires': None,
            },
            sort_keys=True)
        auth_plugin.set_auth_state(state)
        self.assertIsInstance(auth_plugin.auth_ref, authv1.AccessInfoV1)

        self.assertEqual(self.options['username'],
                         auth_plugin.auth_ref.username)
        self.assertEqual(self.options['auth_url'],
                         auth_plugin.auth_ref.auth_url)
        self.assertEqual(self.storage_url, auth_plugin.auth_ref.storage_url)
        self.assertEqual(self.options.get('project_name'), auth_plugin.account)
        self.assertEqual(self.token, auth_plugin.auth_ref.auth_token)
        self.assertEqual(1234.56, auth_plugin.auth_ref._issued)
        self.assertIs(datetime.datetime, type(auth_plugin.auth_ref.issued))
        self.assertIsNone(auth_plugin.auth_ref._expires)
        self.assertIsNone(auth_plugin.auth_ref.expires)
Ejemplo n.º 3
0
    def test_get_access_bad_expiry(self):
        auth_plugin = authv1.PasswordPlugin(**self.options)
        self.mock_response.headers['X-Auth-Token-Expires'] = 'foo'
        access = auth_plugin.get_access(self.mock_session)
        self.assertIsNone(access.expires)

        self.assertIs(False, access.will_expire_soon(60))
        self.assertIs(False, access.will_expire_soon(1e20))
Ejemplo n.º 4
0
    def test_get_access_with_expiry(self):
        auth_plugin = authv1.PasswordPlugin(**self.options)
        self.mock_response.headers['X-Auth-Token-Expires'] = '78.9'
        with mock.patch('swiftclient.authv1.time.time',
                        return_value=1234.56) as mock_time:
            access = auth_plugin.get_access(self.mock_session)
            self.assertEqual(1234.56 + 78.9, access._expires)
            self.assertIs(datetime.datetime,
                          type(auth_plugin.auth_ref.expires))

            self.assertIs(True, access.will_expire_soon(90))
            self.assertIs(False, access.will_expire_soon(60))
        self.assertEqual(3, len(mock_time.mock_calls))
Ejemplo n.º 5
0
    def test_get_endpoint(self):
        auth_plugin = authv1.PasswordPlugin(**self.options)

        object_store_endpoint = auth_plugin.get_endpoint(
            self.mock_session, service_type='object-store')
        self.assertEqual(object_store_endpoint, self.expected_endpoint)

        auth_endpoint = auth_plugin.get_endpoint(
            self.mock_session, interface=plugin.AUTH_INTERFACE)
        self.assertEqual(auth_endpoint, self.options['auth_url'])

        with self.assertRaises(exceptions.EndpointNotFound) as exc_mgr:
            auth_plugin.get_endpoint(self.mock_session)
        self.assertEqual('public endpoint for None service not found',
                         str(exc_mgr.exception))

        with self.assertRaises(exceptions.EndpointNotFound) as exc_mgr:
            auth_plugin.get_endpoint(self.mock_session,
                                     service_type='identity',
                                     region_name='DFW')
        self.assertEqual(
            'public endpoint for identity service in DFW region not found',
            str(exc_mgr.exception))

        with self.assertRaises(exceptions.EndpointNotFound) as exc_mgr:
            auth_plugin.get_endpoint(self.mock_session,
                                     service_type='image',
                                     service_name='glance')
        self.assertEqual(
            'public endpoint for image service named glance not found',
            str(exc_mgr.exception))

        with self.assertRaises(exceptions.EndpointNotFound) as exc_mgr:
            auth_plugin.get_endpoint(self.mock_session,
                                     service_type='compute',
                                     service_name='nova',
                                     region_name='IAD')
        self.assertEqual(
            'public endpoint for compute service named nova in '
            'IAD region not found', str(exc_mgr.exception))
Ejemplo n.º 6
0
    def test_get_state(self):
        auth_plugin = authv1.PasswordPlugin(**self.options)
        self.assertIsNone(auth_plugin.get_auth_state())

        with mock.patch('swiftclient.authv1.time.time', return_value=1234.56):
            auth_plugin.auth_ref = authv1.AccessInfoV1(
                self.options['auth_url'], self.storage_url,
                self.options.get('project_name'), self.options['username'],
                self.token, 60)

        expected = json.dumps(
            {
                'auth_url': self.options['auth_url'],
                'username': self.options['username'],
                'account': self.options.get('project_name'),
                'issued': 1234.56,
                'storage_url': self.storage_url,
                'auth_token': self.token,
                'expires': 1234.56 + 60,
            },
            sort_keys=True)
        self.assertEqual(expected, auth_plugin.auth_ref.get_state())
        self.assertEqual(expected, auth_plugin.get_auth_state())
Ejemplo n.º 7
0
 def test_get_access_missing_url(self):
     auth_plugin = authv1.PasswordPlugin(**self.options)
     self.mock_response.headers.pop('X-Storage-Url')
     self.assertRaises(exceptions.InvalidResponse, auth_plugin.get_access,
                       self.mock_session)
Ejemplo n.º 8
0
 def test_get_access_accepts_storage_token(self):
     auth_plugin = authv1.PasswordPlugin(**self.options)
     self.mock_response.headers.pop('X-Auth-Token')
     self.mock_response.headers['X-Storage-Token'] = 'yet another token'
     access = auth_plugin.get_access(self.mock_session)
     self.assertEqual('yet another token', access.auth_token)
Ejemplo n.º 9
0
 def test_get_access_bad_status(self):
     auth_plugin = authv1.PasswordPlugin(**self.options)
     self.mock_response.status_code = 401
     self.assertRaises(exceptions.InvalidResponse, auth_plugin.get_access,
                       self.mock_session)