def test_not_found(self): h = HttpRequest(self.hostname, auth=self.auth) responses.add(responses.GET, "{}/api/2/txlib/".format(self.hostname), status=404) # get a project that does not exist with pytest.raises(NotFoundError): h.get('/api/2/txlib/')
def test_wrong_auth(self): """Test response for requests with wrong authentication info.""" responses.add(responses.GET, "{}/api/2/projects/".format(self.hostname), status=401) auth = BasicAuth(self.username, 'wrong') h = HttpRequest(self.hostname, auth=auth) with pytest.raises(AuthorizationError): h.get('/api/2/projects/')
def test_auth_with_headers(self): """Test authenticated requests with custom headers.""" with responses.RequestsMock() as rsps: rsps.add(responses.GET, "{}/api/2/projects/".format(self.hostname), body='{}', content_type="application/json") path = '/api/2/projects/' req = HttpRequest(self.hostname, auth=self.auth_with_headers) req.get(path) # Assert that cusotm headers have been added to the request for header, value in self.headers.items(): assert rsps.calls[0][0].headers[header] == value
def test_anonymous_requests(self): """Test anonymous requests. They should all fail, since transifex currently does not allow anonymous access to the API. """ responses.add(responses.GET, "{}/api/2/projects/".format(self.hostname), status=401) auth = AnonymousAuth() h = HttpRequest(self.hostname, auth=auth) with pytest.raises(AuthorizationError): h.get('/api/2/projects/')
def test_auth(self): """Test authenticated requests.""" with responses.RequestsMock() as rsps: rsps.add(responses.GET, "{}/api/2/projects/".format(self.hostname), body='{}', content_type="application/json") path = '/api/2/projects/' req = HttpRequest(self.hostname, auth=self.auth) req.get(path) # Succeeds! # Assert that custom headers do not exist in the request for header, value in self.headers.items(): with pytest.raises(KeyError): assert rsps.calls[0][0].headers[header] == value
def test_create(self): with responses.RequestsMock() as rsps: h = HttpRequest(self.hostname, auth=self.auth) # create a project path = '/api/2/projects/' rsps.add(responses.POST, "{}/api/2/projects/".format(self.hostname), status=201) rsps.add(responses.POST, "{}/api/2/projects/".format(self.hostname), status=409) data = json.dumps(dict(slug='txlib', name='Txlib project')) h.post(path, data=data) with pytest.raises(ConflictError): h.post(path, data=data)