'Test v2 auth code paths'

def test_v2_auth(self):

    mock_token = None
    def fake_do_request(cls, url, method, headers=None, body=None):
        if ((not url.rstrip('/').endswith('v2.0/tokens')) or (url.count('2.0') != 1)):
            self.fail(('Invalid    v2.0    token    path    (%s)' % url))
        creds = jsonutils.loads(body)['auth']
        username = creds['passwordCredentials']['username']
        password = creds['passwordCredentials']['password']
        tenant = creds['tenantName']
        resp = webob.Response()
        if ((username != 'user1') or (password != 'pass') or (tenant != 'tenant-ok')):
            resp.status = http.UNAUTHORIZED
        else:
            resp.status = http.OK
            body = mock_token.token
        return (FakeResponse(resp), jsonutils.dumps(body))
    mock_token = V2Token()
    mock_token.add_service('image', ['RegionOne'])
    self.stubs.Set(auth.KeystoneStrategy, '_do_request', fake_do_request)
    unauthorized_creds = [{'username': 'wronguser', 'auth_url': 'http://localhost/v2.0', 'password': 'pass', 'tenant': 'tenant-ok', 'strategy': 'keystone', 'region': 'RegionOne'}, {'username': 'user1', 'auth_url': 'http://localhost/v2.0', 'password': 'badpass', 'tenant': 'tenant-ok', 'strategy': 'keystone', 'region': 'RegionOne'}, {'username': 'user1', 'auth_url': 'http://localhost/v2.0', 'password': 'pass', 'tenant': 'carterhayes', 'strategy': 'keystone', 'region': 'RegionOne'}]
    for creds in unauthorized_creds:
        try:
            plugin = auth.KeystoneStrategy(creds)
            plugin.authenticate()
            self.fail(('Failed    to    raise    NotAuthenticated    when    supplying    bad    credentials:    %r' % creds))
        except exception.NotAuthenticated:
            continue
    no_region_creds = {'username': 'user1', 'tenant': 'tenant-ok', 'auth_url': 'http://localhost/redirect/v2.0/', 'password': 'pass', 'strategy': 'keystone'}
    plugin = auth.KeystoneStrategy(no_region_creds)
    self.assertIsNone(plugin.authenticate())
    self.assertEqual('http://localhost:9292', plugin.management_url)
    mock_token.add_service('image', ['RegionTwo'])
    try:
        plugin = auth.KeystoneStrategy(no_region_creds)
        plugin.authenticate()
        self.fail(('Failed    to    raise    RegionAmbiguity    when    no    region    present    and    multiple    regions    exist:    %r' % no_region_creds))
    except exception.RegionAmbiguity:
        pass
    wrong_region_creds = {'username': 'user1', 'tenant': 'tenant-ok', 'auth_url': 'http://localhost/redirect/v2.0/', 'password': 'pass', 'strategy': 'keystone', 'region': 'NonExistentRegion'}
    try:
        plugin = auth.KeystoneStrategy(wrong_region_creds)
        plugin.authenticate()
        self.fail(('Failed    to    raise    NoServiceEndpoint    when    supplying    wrong    region:    %r' % wrong_region_creds))
    except exception.NoServiceEndpoint:
        pass
    no_strategy_creds = {'username': 'user1', 'tenant': 'tenant-ok', 'auth_url': 'http://localhost/redirect/v2.0/', 'password': 'pass', 'region': 'RegionOne'}
    try:
        plugin = auth.KeystoneStrategy(no_strategy_creds)
        plugin.authenticate()
        self.fail(('Failed    to    raise    MissingCredentialError    when    supplying    no    strategy:    %r' % no_strategy_creds))
    except exception.MissingCredentialError:
        pass
    bad_strategy_creds = {'username': 'user1', 'tenant': 'tenant-ok', 'auth_url': 'http://localhost/redirect/v2.0/', 'password': 'pass', 'region': 'RegionOne', 'strategy': 'keypebble'}
    try:
        plugin = auth.KeystoneStrategy(bad_strategy_creds)
        plugin.authenticate()
        self.fail(('Failed    to    raise    BadAuthStrategy    when    supplying    bad    auth    strategy:    %r' % bad_strategy_creds))
    except exception.BadAuthStrategy:
        pass
    mock_token = V2Token()
    mock_token.add_service('image', ['RegionOne', 'RegionTwo'])
    good_creds = [{'username': 'user1', 'auth_url': 'http://localhost/v2.0/', 'password': 'pass', 'tenant': 'tenant-ok', 'strategy': 'keystone', 'region': 'RegionOne'}, {'username': 'user1', 'auth_url': 'http://localhost/v2.0', 'password': 'pass', 'tenant': 'tenant-ok', 'strategy': 'keystone', 'region': 'RegionOne'}, {'username': 'user1', 'auth_url': 'http://localhost/v2.0', 'password': 'pass', 'tenant': 'tenant-ok', 'strategy': 'keystone', 'region': 'RegionTwo'}]
    for creds in good_creds:
        plugin = auth.KeystoneStrategy(creds)
        self.assertIsNone(plugin.authenticate())
        self.assertEqual('http://localhost:9292', plugin.management_url)
    ambiguous_region_creds = {'username': 'user1', 'auth_url': 'http://localhost/v2.0/', 'password': 'pass', 'tenant': 'tenant-ok', 'strategy': 'keystone', 'region': 'RegionOne'}
    mock_token = V2Token()
    mock_token.add_service('image', ['RegionOne'])
    mock_token.add_service('image', ['RegionOne'])
    try:
        plugin = auth.KeystoneStrategy(ambiguous_region_creds)
        plugin.authenticate()
        self.fail(('Failed    to    raise    RegionAmbiguity    when    non-unique    regions    exist:    %r' % ambiguous_region_creds))
    except exception.RegionAmbiguity:
        pass
    mock_token = V2Token()
    mock_token.add_service('bad-image', ['RegionOne'])
    good_creds = {'username': 'user1', 'auth_url': 'http://localhost/v2.0/', 'password': 'pass', 'tenant': 'tenant-ok', 'strategy': 'keystone', 'region': 'RegionOne'}
    try:
        plugin = auth.KeystoneStrategy(good_creds)
        plugin.authenticate()
        self.fail('Failed    to    raise    NoServiceEndpoint    when    bad    service    type    encountered')
    except exception.NoServiceEndpoint:
        pass
    mock_token = V2Token()
    mock_token.add_service_no_type()
    try:
        plugin = auth.KeystoneStrategy(good_creds)
        plugin.authenticate()
        self.fail('Failed    to    raise    NoServiceEndpoint    when    bad    service    type    encountered')
    except exception.NoServiceEndpoint:
        pass
    try:
        plugin = auth.KeystoneStrategy(good_creds, configure_via_auth=False)
        plugin.authenticate()
    except exception.NoServiceEndpoint:
        self.fail('NoServiceEndpoint    was    raised    when    authenticate    should    not    check    for    endpoint.')
