Beispiel #1
0
    def __init__(self, resource):
        #        self.resource = resource
        self.get_patch = patch('requests.get',
                       _M( readfile("%s_get.xml" % resource)))
        self.all_patch = patch('requests.get',
                       _M( readfile("%s_all.xml" % resource)))
  

        self.client = Client()
Beispiel #2
0
class TestClient:
    def setup(self):
        self.c = Client(ssl=False)
        self.ssl_c = Client(ssl=True)

    def test_ssl_property_set_correct_api_location(self):
        assert self.ssl_c.api_location == 'https://www.pivotaltracker.com/services/v3/'
        assert self.c.api_location == 'http://www.pivotaltracker.com/services/v3/'

    @patch('requests.get', _M(stub_body))
    def test_passes_given_kwargs_to_restclient(self):
        kwargs = dict(key='value', key2=2)
        self.c.get('location', **kwargs)
        requests.get.assert_called_with(self.c._endpoint_for('location'),
                                        **kwargs)

    @patch('requests.get', _M(stub_body))
    def test_injects_token_header_if_given(self):
        kwargs = dict(key='value', key2=2)
        self.ssl_c.token = 'tok'
        self.ssl_c.get('location', **kwargs)
        requests.get.assert_called_with(self.ssl_c._endpoint_for('location'),
                                        headers={'X-TrackerToken': 'tok'},
                                        **kwargs)

    @raises(AccessDenied)
    @patch('requests.get', _M(stub_body, status_code=401))
    def test_raises_exception_on_access_denied(self):
        self.c.get('location')
Beispiel #3
0
class TestClient:
    def setup(self):
        self.c = Client(ssl=False)
        self.ssl_c = Client(ssl=True)

    def test_ssl_property_set_correct_api_location(self):
        assert self.ssl_c.api_location == 'https://www.pivotaltracker.com/services/v3/'
        assert self.c.api_location == 'http://www.pivotaltracker.com/services/v3/'

    @patch('requests.get', _M( stub_body))
    def test_passes_given_kwargs_to_restclient(self):
        kwargs = dict(key='value', key2=2)
        self.c.get('location', **kwargs)
        requests.get.assert_called_with(self.c._endpoint_for('location'), **kwargs)

    @patch('requests.get', _M(stub_body))
    def test_injects_token_header_if_given(self):
        kwargs = dict(key='value', key2=2)
        self.ssl_c.token = 'tok'
        self.ssl_c.get('location', **kwargs)
        requests.get.assert_called_with(self.ssl_c._endpoint_for('location'), headers={'X-TrackerToken':'tok'},  **kwargs)

    @raises(AccessDenied)
    @patch('requests.get', _M(stub_body, status_code=401))
    def test_raises_exception_on_access_denied(self):
        self.c.get('location')
Beispiel #4
0
    def __init__(self, user=None, password=None, token=None, ssl=True):
        """
        :param user: pivotal username (optional if token provided)
        :param password: pivotal password  (optional if token provided)
        :param token: pivotal api token  (optional if user and password provided)
        :param ssl: use https for api calls

        If no token provided it would be requested using given username
        and password.
        """
        self.client = Client(ssl=ssl)
        if token is None:
            token = self._get_token_for_credentials(user, password)
        self.client.token = token
        self._projects = None
Beispiel #5
0
    def test_can_be_moved(self):
        mock = _M(readfile('story_get.xml'))
        p = patch('requests.post', mock)
        s = Story()
        s.id = 10
        s.project_id = 1
        s.client = Client(token='tpken')

        p.start()
        new_story = s.move_after(15)
        assert mock.call_args[1]['params']['move[move]'] == 'after'
        assert mock.call_args[1]['params']['move[target]'] == 15
        assert new_story.id == 227
        s.move_before(new_story)
        assert mock.call_args[1]['params']['move[move]'] == 'before'
        assert mock.call_args[1]['params']['move[target]'] == 227
        p.stop()
Beispiel #6
0
class ClientMock:
    def __init__(self, resource):
        #        self.resource = resource
        self.get_patch = patch('requests.get',
                       _M( readfile("%s_get.xml" % resource)))
        self.all_patch = patch('requests.get',
                       _M( readfile("%s_all.xml" % resource)))
  

        self.client = Client()

    def get(self, *args, **kwargs):
        if caller() == 'get':
            p = self.get_patch
        if caller() == 'all':
            p = self.all_patch
        self._last_mock = p.start()
        result = self.client.get(*args, **kwargs)
        p.stop()
        return result
Beispiel #7
0
 def setup(self):
     self.c = Client(ssl=False)
     self.ssl_c = Client(ssl=True)
Beispiel #8
0
 def setup(self):
     self.c = Client(ssl=False)
     self.ssl_c = Client(ssl=True)
Beispiel #9
0
class PTracker(object):
    """
    Base api entry point
    """
    def __init__(self, user=None, password=None, token=None, ssl=True):
        """
        :param user: pivotal username (optional if token provided)
        :param password: pivotal password  (optional if token provided)
        :param token: pivotal api token  (optional if user and password provided)
        :param ssl: use https for api calls

        If no token provided it would be requested using given username
        and password.
        """
        self.client = Client(ssl=ssl)
        if token is None:
            token = self._get_token_for_credentials(user, password)
        # One could pass unicode as token and it would break requests headers
        self.client.token = str(token)
        self._projects = None

    @property
    def token(self):
        """
        User token,
        obtained via api or passed to :class:`~pyvotal.PTracker` constructor::

            from pyvotal import PTracker

            ptracker = PTracker(user='******', password='******')
            print 'SomeUser token is', ptraker.token
        """
        return self.client.token

    @property
    def projects(self):
        """
        :class:`~pyvotal.projects.ProjectManager` to manipulate user`s projects.
        """
        if self._projects is None:
            self._projects = ProjectManager(self.client)
        return self._projects

    def Project(self):
        """
        Factory method. This method creates new :class:`~pyvotal.projects.Project` objects.
        """
        p = Project()
        p.client = self.client
        return p

    def Membership(self):
        """
        Factory method. This method creates new :class:`~pyvotal.memberships.Membership` objects.
        """
        m = Membership()
        m.person = Person()
        m.client = self.client
        return m

    def Story(self):
        """
        Factory method. This method creates new :class:`~pyvotal.stories.Story` objects.
        """
        s = Story()
        s.client = self.client
        return s

    def Task(self):
        """
        Factory method. This method creates new :class:`~pyvotal.tasks.Task` objects.
        """
        t = Task()
        t.client = self.client
        return t

    def _get_token_for_credentials(self, user=None, password=None):
        if user is None or password is None:
            raise PyvotalException("Provide user AND password")
        tree = self.client.get('tokens/active', auth=(user, password))
        return tree.find('guid').text