def test_auth_token_can_be_refreshed(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response, '{"access_token":"abc"}')
        ms = MetadataService()

        _ = ms.auth_token
        ms.refresh()
        _ = ms.auth_token

        self.assertEqual(2, mock_request.call_count)
    def test_auth_token_can_be_refreshed(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response,
                          '{"access_token":"abc"}')
        ms = MetadataService()

        _ = ms.auth_token
        ms.refresh()
        _ = ms.auth_token

        self.assertEqual(2, mock_request.call_count)
    def test_auth_token_can_be_updated(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response, '{"access_token":"abc"}')
        ms = MetadataService()

        auth_token1 = ms.auth_token

        self._setup_mocks(mock_request, mock_response, '{"access_token":"xyz"}')
        ms.refresh()
        auth_token2 = ms.auth_token

        self.assertEqual("abc", auth_token1)
        self.assertEqual("xyz", auth_token2)
    def test_auth_token_can_be_updated(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response,
                          '{"access_token":"abc"}')
        ms = MetadataService()

        auth_token1 = ms.auth_token

        self._setup_mocks(mock_request, mock_response,
                          '{"access_token":"xyz"}')
        ms.refresh()
        auth_token2 = ms.auth_token

        self.assertEqual('abc', auth_token1)
        self.assertEqual('xyz', auth_token2)
    def test_project_id_is_cached(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response, 'project1')
        ms = MetadataService()

        _ = ms.project_id
        _ = ms.project_id
        self.assertEqual(1, mock_request.call_count)
    def test_auth_token_is_returned(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response,
                          '{"access_token":"abc"}')
        ms = MetadataService()

        auth_token = ms.auth_token
        self.assertEqual('abc', auth_token)
    def test_project_id_is_returned(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response, 'project1')
        ms = MetadataService()

        project_id = ms.project_id
        self.assertEqual('project1', project_id)
    def test_raises_error_on_missing_data(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response, '{"data":"abc"}')
        ms = MetadataService()

        with self.assertRaises(RuntimeError):
            _ = ms.auth_token
    def test_raises_error_on_bad_json(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response, 'abc')
        ms = MetadataService()

        with self.assertRaises(RuntimeError):
            _ = ms.auth_token
    def test_raises_error_on_failure(self, mock_request, mock_response):
        self._setup_mocks(mock_request, mock_response, '', 500)
        ms = MetadataService()

        with self.assertRaises(RuntimeError):
            _ = ms.project_id