Permalink
Browse files

add check to ensure that project and resource slugs are valid; add de…

…lete_resource api call
  • Loading branch information...
1 parent 47f0123 commit 331f1c295fbf9da0652eb31fdc940292b8679cf7 @jakul committed Jan 6, 2012
Showing with 97 additions and 0 deletions.
  1. +26 −0 transifex/api.py
  2. +71 −0 transifex/tests/api.py
View
@@ -43,6 +43,9 @@ def new_project(self, slug, name=None, source_language_code=None,
@raises `TransifexAPIException`
if project was not created properly
"""
+
+ if slug != slugify(slug):
+ raise TransifexAPIException('%r is not a valid slug')
if name is None:
name = slug
if source_language_code is None:
@@ -125,6 +128,9 @@ def new_resource(self, project_slug, path_to_pofile, resource_slug=None,
__, filename = os.path.split(path_to_pofile)
if resource_slug is None:
resource_slug = slugify(filename)
+ else:
+ if resource_slug != slugify(resource_slug):
+ raise TransifexAPIException('%r is not a valid slug')
if resource_name is None:
resource_name = resource_slug
@@ -183,6 +189,26 @@ def update_source_translation(self, project_slug, resource_slug,
raise TransifexAPIException(response)
else:
return json.loads(response.content)
+
+ def delete_resource(self, project_slug, resource_slug):
+ """
+ Deletes the given resource
+
+ @param project_slug
+ the project slug
+ @param resource_slug
+ the resource slug
+
+ @return None
+
+ @raises `TransifexAPIException`
+ """
+ url = '%s/project/%s/resource/%s/' % (
+ self._base_api_url, project_slug, resource_slug
+ )
+ response = requests.delete(url, auth=self._auth)
+ if response.status_code != requests.codes['NO_CONTENT']:
+ raise TransifexAPIException(response)
def new_translation(self, project_slug, resource_slug, language_code,
path_to_pofile):
View
@@ -35,6 +35,20 @@ def side_effect(*args, **kwargs):
mock_requests.side_effect = side_effect
self.api.new_project(slug='abc')
+
+ def test_new_project_bad_slug(self):
+ """
+ Test the `new_project` api call when the slug is invalid
+ """
+ self.assertRaises(TransifexAPIException, self.api.new_project,
+ slug='.'
+ )
+ self.assertRaises(TransifexAPIException, self.api.new_project,
+ slug='/'
+ )
+ self.assertRaises(TransifexAPIException, self.api.new_project,
+ slug='%@$'
+ )
@patch('requests.post')
def test_new_project_with_optional_args(self, mock_requests):
@@ -156,6 +170,26 @@ def side_effect(*args, **kwargs):
self.assertRaises(IOError, self.api.new_resource,
project_slug='abc', path_to_pofile='/aaa/file.po'
)
+
+ @patch('__builtin__.open', create=True)
+ def test_new_resource_bad_slug(self, mock_open):
+ """
+ Test the `new_resource` api call when the slug is invalid
+ """
+ file_contents = 'aaaaaa\nggggg'
+ mock_open.return_value = MagicMock(spec=file)
+ mock_open.return_value.read = lambda: file_contents
+
+ self.assertRaises(TransifexAPIException, self.api.new_resource,
+ project_slug='aaa', resource_slug='.', path_to_pofile='/aaa/file.po'
+ )
+ self.assertRaises(TransifexAPIException, self.api.new_resource,
+ project_slug='aaa', resource_slug='/', path_to_pofile='/aaa/file.po'
+ )
+ self.assertRaises(TransifexAPIException, self.api.new_resource,
+ project_slug='aaa', resource_slug='%@$',
+ path_to_pofile='/aaa/file.po'
+ )
@patch('__builtin__.open', create=True)
@patch('requests.post')
@@ -418,3 +452,40 @@ def side_effect(*args, **kwargs):
path_to_pofile='/abc/pofile.po'
)
+
+
+ @patch('requests.delete')
+ def test_delete_resource(self, mock_requests):
+ """
+ Test the `delete_resource` api call
+ """
+
+ def side_effect(*args, **kwargs):
+ mock_response = Mock()
+ mock_response.status_code = 204
+ return mock_response
+
+ mock_requests.side_effect = side_effect
+
+ self.api.delete_resource(project_slug='abc', resource_slug='def')
+ self.assertTrue(mock_requests.called)
+
+
+
+ @patch('requests.delete')
+ def test_delete_resource_server_error(self, mock_requests):
+ """
+ Test the `delete_resource` api call when the transifex server
+ returns an error
+ """
+
+ def side_effect(*args, **kwargs):
+ response = Mock()
+ response.status_code = 404
+ return response
+
+ mock_requests.side_effect = side_effect
+ self.assertRaises(
+ TransifexAPIException, self.api.delete_resource,
+ project_slug='abc', resource_slug='def'
+ )

0 comments on commit 331f1c2

Please sign in to comment.