def test_personal_access_token(self): """Establish that if `tower-cli login` is called with a username and password, we obtain and write an oauth token to the config file """ # Invoke the command. mock_open = mock.mock_open() with mock.patch('tower_cli.cli.misc.open', mock_open, create=True): with mock.patch.object(os, 'chmod'): with client.test_mode as t: # You have to modify this internal private registry to # register a URL endpoint that _doesn't_ have the version # prefix prefix = Client().get_prefix(include_version=False) t._registry[URL(prefix + 'o/', method='HEAD')] = Resp( ''.encode('utf-8'), 200, {} ) t.register('/users/bob/personal_tokens/', json.dumps({ 'token': 'abc123' }), status_code=201, method='POST') result = self.runner.invoke( login, ['bob', '--password', 'secret', '--scope', 'read'] ) # Ensure that we got a zero exit status self.assertEqual(result.exit_code, 0) assert json.loads(t.requests[-1].body)['scope'] == 'read' # Ensure that the output seems to be correct. self.assertIn(mock.call(os.path.expanduser('~/.tower_cli.cfg'), 'w'), mock_open.mock_calls) self.assertIn(mock.call().write('oauth_token = abc123\n'), mock_open.mock_calls)
def test_public_application_scoped_token(self): """Establish that if `tower-cli login` is called with a username, password, and public OAuth2 client ID, we obtain and write an oauth token to the config file """ # Invoke the command. mock_open = mock.mock_open() with mock.patch('tower_cli.cli.misc.open', mock_open, create=True): with mock.patch.object(os, 'chmod'): with client.test_mode as t: # You have to modify this internal private registry to # register a URL endpoint that _doesn't_ have the version # prefix prefix = Client().get_prefix(include_version=False) t._registry[URL(prefix + 'o/', method='HEAD')] = Resp( ''.encode('utf-8'), 200, {} ) t._registry[URL(prefix + 'o/token/', method='POST')] = Resp( json.dumps({'access_token': 'abc123'}).encode('utf-8'), 201, {} ) result = self.runner.invoke( login, ['bob', '--password', 'secret', '--client-id', 'abc123'] ) # Ensure that we got a zero exit status self.assertEqual(result.exit_code, 0) data = urlparse.parse_qs(t.requests[-1].body) assert data['scope'] == ['write'] assert data['grant_type'] == ['password'] assert data['password'] == ['secret'] assert data['username'] == ['bob'] assert data['client_id'] == ['abc123'] # Ensure that the output seems to be correct. self.assertIn(mock.call(os.path.expanduser('~/.tower_cli.cfg'), 'w'), mock_open.mock_calls) self.assertIn(mock.call().write('oauth_token = abc123\n'), mock_open.mock_calls)
def test_oauth_unsupported(self): """Establish that if `tower-cli login` is used on a Tower that doesn't support OAuth2, it shows a meaningful error """ with client.test_mode as t: # You have to modify this internal private registry to # register a URL endpoint that _doesn't_ have the version # prefix prefix = Client().get_prefix(include_version=False) t._registry[URL(prefix + 'o/', method='HEAD')] = Resp(''.encode('utf-8'), 404, {}) result = self.runner.invoke(login, ['bob', '--password', 'secret']) # Ensure that we got a non-zero exit status self.assertNotEqual(result.exit_code, 0) self.assertIn('Error: This version of Tower does not support OAuth2.0', result.output)
def test_personal_access_invalid(self): """Establish that if `tower-cli login` is called with an invalid username and password, it shows a meaningful error """ with client.test_mode as t: # You have to modify this internal private registry to # register a URL endpoint that _doesn't_ have the version # prefix prefix = Client().get_prefix(include_version=False) t._registry[URL(prefix + 'o/', method='HEAD')] = Resp(''.encode('utf-8'), 200, {}) t.register('/users/bob/personal_tokens/', json.dumps({}), status_code=401, method='POST') result = self.runner.invoke(login, ['bob', '--password', 'secret']) # Ensure that we got a non-zero exit status self.assertNotEqual(result.exit_code, 0) self.assertIn('Error: Invalid Tower authentication credentials', result.output)