def test_script(self): h = HttpRequest(self.hostname, auth=self.auth) # get a project that does not exist self.assertRaises(NotFoundError, h.get, '/api/2/txlib/') # create a project data = json.dumps(dict(slug='txlib', name='Txlib project')) path = '/api/2/projects/' h.post(path, data=data) # creating the same project results in an error raised. self.assertRaises(ConflictError, h.post, path, data=data) # Using a non-existent attribute for projects data = json.dumps(dict(name='New name', anyone_submitt=True)) path = '/api/2/project/txlib/' self.assertRaises(RequestError, h.put, path, data) # make sure the field has the default value p = h.get(path + '?details') self.assertFalse(p['anyone_submit']) # update the details of the project data = json.dumps(dict(name='New name', anyone_submit=True)) h.put(path, data) # make sure the change has been saved p = h.get(path + '?details') self.assertTrue(p['anyone_submit']) # delete the project h.delete(path)
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_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_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)
def test_anonymous_requests(self): """Test anonymous requests. They should all fail, since transifex currently does not allow anonymous access to the API. """ auth = AnonymousAuth() h = HttpRequest(self.hostname, auth=auth) self.assertRaises(AuthorizationError, h.get, '/api/2/projects/')
def test_update(self): with responses.RequestsMock() as rsps: rsps.add(responses.PUT, "{}/api/2/project/txlib/".format(self.hostname), status=400) rsps.add(responses.GET, "{}/api/2/project/txlib/?details".format(self.hostname), json={"anyone_submit": False}, match_querystring=True, status=200) rsps.add(responses.PUT, "{}/api/2/project/txlib/".format(self.hostname), json={"anyone_submit": True}, status=200) rsps.add(responses.GET, "{}/api/2/project/txlib/?details".format(self.hostname), json={"anyone_submit": True}, match_querystring=True, status=200) rsps.add(responses.DELETE, "{}/api/2/project/txlib/".format(self.hostname), status=200) h = HttpRequest(self.hostname, auth=self.auth) # Using a non-existent attribute for projects data = json.dumps(dict(name='New name', anyone_submitt=True)) path = '/api/2/project/txlib/' with pytest.raises(RequestError): h.put(path, data) # make sure the field has the default value p = h.get(path + '?details') assert p['anyone_submit'] is False # update the details of the project data = json.dumps(dict(name='New name', anyone_submit=True)) h.put(path, data) # make sure the change has been saved p = h.get(path + '?details') assert p['anyone_submit'] is True # delete the project h.delete(path)
def setup_registry(): """Initializes the registry and sets up an `http_handler`.""" conn = HttpRequest('http://doesntmatter.org') registry.setup({'http_handler': conn})
def test_auth(self): """Test authenticated requests.""" path = '/api/2/projects/' h = HttpRequest(self.hostname, auth=self.auth) res = h.get(path) # Succeeds!
def test_wrong_auth(self): """Test response for requests with wrong authentication info.""" auth = BasicAuth(self.username, 'wrong') h = HttpRequest(self.hostname, auth=auth) self.assertRaises(AuthorizationError, h.get, '/api/2/projects/')