dropbox_share: Add tests to increase coverage.

Increase tests coverage to 100%.
This commit is contained in:
novokrest 2018-06-05 02:05:54 +03:00 committed by Tim Abbott
parent f808dfb2cc
commit df428b4821

View file

@ -11,7 +11,7 @@ from zulip_bots.bots.dropbox_share.test_util import (
MockHttpResponse MockHttpResponse
) )
def get_files_list(*args, **kwargs): def get_root_files_list(*args, **kwargs):
return MockListFolderResult( return MockListFolderResult(
entries = [ entries = [
MockFileMetadata('foo', '/foo'), MockFileMetadata('foo', '/foo'),
@ -20,6 +20,21 @@ def get_files_list(*args, **kwargs):
has_more = False has_more = False
) )
def get_folder_files_list(*args, **kwargs):
return MockListFolderResult(
entries = [
MockFileMetadata('moo', '/foo/moo'),
MockFileMetadata('noo', '/foo/noo'),
],
has_more = False
)
def get_empty_files_list(*args, **kwargs):
return MockListFolderResult(
entries = [],
has_more = False
)
def create_file(*args, **kwargs): def create_file(*args, **kwargs):
return MockFileMetadata('foo', '/foo') return MockFileMetadata('foo', '/foo')
@ -36,6 +51,9 @@ def search_files(*args, **kwargs):
) )
]) ])
def get_empty_search_result(*args, **kwargs):
return MockSearchResult([])
def get_shared_link(*args, **kwargs): def get_shared_link(*args, **kwargs):
return MockPathLinkMetadata('http://www.foo.com/boo') return MockPathLinkMetadata('http://www.foo.com/boo')
@ -64,10 +82,31 @@ class TestDropboxBot(BotTestCase):
self.verify_reply('', get_help()) self.verify_reply('', get_help())
self.verify_reply('help', get_help()) self.verify_reply('help', get_help())
def test_dbx_ls(self): def test_dbx_ls_root(self):
bot_response = " - [foo](https://www.dropbox.com/home/foo)\n"\ bot_response = " - [foo](https://www.dropbox.com/home/foo)\n"\
" - [boo](https://www.dropbox.com/home/boo)" " - [boo](https://www.dropbox.com/home/boo)"
with patch('dropbox.dropbox.Dropbox.files_list_folder', side_effect=get_files_list), \ with patch('dropbox.dropbox.Dropbox.files_list_folder', side_effect=get_root_files_list), \
self.mock_config_info(self.config_info):
self.verify_reply("ls", bot_response)
def test_dbx_ls_folder(self):
bot_response = " - [moo](https://www.dropbox.com/home/foo/moo)\n"\
" - [noo](https://www.dropbox.com/home/foo/noo)"
with patch('dropbox.dropbox.Dropbox.files_list_folder', side_effect=get_folder_files_list), \
self.mock_config_info(self.config_info):
self.verify_reply("ls foo", bot_response)
def test_dbx_ls_empty(self):
bot_response = '`No files available`'
with patch('dropbox.dropbox.Dropbox.files_list_folder', side_effect=get_empty_files_list), \
self.mock_config_info(self.config_info):
self.verify_reply("ls", bot_response)
def test_dbx_ls_error(self):
bot_response = "Please provide a correct folder path\n"\
"Usage: `ls <foldername>` to list folders in directory\n"\
"or simply `ls` for listing folders in the root directory"
with patch('dropbox.dropbox.Dropbox.files_list_folder', side_effect=Exception()), \
self.mock_config_info(self.config_info): self.mock_config_info(self.config_info):
self.verify_reply("ls", bot_response) self.verify_reply("ls", bot_response)
@ -77,24 +116,52 @@ class TestDropboxBot(BotTestCase):
self.mock_config_info(self.config_info): self.mock_config_info(self.config_info):
self.verify_reply('mkdir foo', bot_response) self.verify_reply('mkdir foo', bot_response)
def test_dbx_mkdir_error(self):
bot_response = "Please provide a correct folder path and name.\n"\
"Usage: `mkdir <foldername>` to create a folder."
with patch('dropbox.dropbox.Dropbox.files_create_folder', side_effect=Exception()), \
self.mock_config_info(self.config_info):
self.verify_reply('mkdir foo/bar', bot_response)
def test_dbx_rm(self): def test_dbx_rm(self):
bot_response = "DELETED File/Folder : [foo](https://www.dropbox.com/home/foo)" bot_response = "DELETED File/Folder : [foo](https://www.dropbox.com/home/foo)"
with patch('dropbox.dropbox.Dropbox.files_delete', side_effect=create_file), \ with patch('dropbox.dropbox.Dropbox.files_delete', side_effect=create_file), \
self.mock_config_info(self.config_info): self.mock_config_info(self.config_info):
self.verify_reply('rm foo', bot_response) self.verify_reply('rm foo', bot_response)
def test_dbx_rm_error(self):
bot_response = "Please provide a correct folder path and name.\n"\
"Usage: `rm <foldername>` to delete a folder in root directory."
with patch('dropbox.dropbox.Dropbox.files_delete', side_effect=Exception()), \
self.mock_config_info(self.config_info):
self.verify_reply('rm foo', bot_response)
def test_dbx_write(self): def test_dbx_write(self):
bot_response = "Written to file: [foo](https://www.dropbox.com/home/foo)" bot_response = "Written to file: [foo](https://www.dropbox.com/home/foo)"
with patch('dropbox.dropbox.Dropbox.files_upload', side_effect=create_file), \ with patch('dropbox.dropbox.Dropbox.files_upload', side_effect=create_file), \
self.mock_config_info(self.config_info): self.mock_config_info(self.config_info):
self.verify_reply('write foo boo', bot_response) self.verify_reply('write foo boo', bot_response)
def test_dbx_write_error(self):
bot_response = "Incorrect file path or file already exists.\n"\
"Usage: `write <filename> CONTENT`"
with patch('dropbox.dropbox.Dropbox.files_upload', side_effect=Exception()), \
self.mock_config_info(self.config_info):
self.verify_reply('write foo boo', bot_response)
def test_dbx_read(self): def test_dbx_read(self):
bot_response = "**foo** :\nboo" bot_response = "**foo** :\nboo"
with patch('dropbox.dropbox.Dropbox.files_download', side_effect=download_file), \ with patch('dropbox.dropbox.Dropbox.files_download', side_effect=download_file), \
self.mock_config_info(self.config_info): self.mock_config_info(self.config_info):
self.verify_reply('read foo', bot_response) self.verify_reply('read foo', bot_response)
def test_dbx_read_error(self):
bot_response = "Please provide a correct file path\n"\
"Usage: `read <filename>` to read content of a file"
with patch('dropbox.dropbox.Dropbox.files_download', side_effect=Exception()), \
self.mock_config_info(self.config_info):
self.verify_reply('read foo', bot_response)
def test_dbx_search(self): def test_dbx_search(self):
bot_response = " - [foo](https://www.dropbox.com/home/foo)\n"\ bot_response = " - [foo](https://www.dropbox.com/home/foo)\n"\
" - [fooboo](https://www.dropbox.com/home/fooboo)" " - [fooboo](https://www.dropbox.com/home/fooboo)"
@ -102,12 +169,59 @@ class TestDropboxBot(BotTestCase):
self.mock_config_info(self.config_info): self.mock_config_info(self.config_info):
self.verify_reply('search foo', bot_response) self.verify_reply('search foo', bot_response)
def test_dbx_search_empty(self):
bot_response = "No files/folders found matching your query.\n"\
"For file name searching, the last token is used for prefix matching"\
" (i.e. “bat c” matches “bat cave” but not “batman car”)."
with patch('dropbox.dropbox.Dropbox.files_search', side_effect=get_empty_search_result), \
self.mock_config_info(self.config_info):
self.verify_reply('search boo --fd foo', bot_response)
def test_dbx_search_error(self):
bot_response = "Usage: `search <foldername> query --mr 10 --fd <folderName>`\n"\
"Note:`--mr <int>` is optional and is used to specify maximun results.\n"\
" `--fd <folderName>` to search in specific folder."
with patch('dropbox.dropbox.Dropbox.files_search', side_effect=Exception()), \
self.mock_config_info(self.config_info):
self.verify_reply('search foo', bot_response)
def test_dbx_share(self): def test_dbx_share(self):
bot_response = 'http://www.foo.com/boo' bot_response = 'http://www.foo.com/boo'
with patch('dropbox.dropbox.Dropbox.sharing_create_shared_link', side_effect=get_shared_link), \ with patch('dropbox.dropbox.Dropbox.sharing_create_shared_link', side_effect=get_shared_link), \
self.mock_config_info(self.config_info): self.mock_config_info(self.config_info):
self.verify_reply('share boo', bot_response) self.verify_reply('share boo', bot_response)
def test_dbx_share_error(self):
bot_response = "Please provide a correct file name.\n"\
"Usage: `share <filename>`"
with patch('dropbox.dropbox.Dropbox.sharing_create_shared_link', side_effect=Exception()), \
self.mock_config_info(self.config_info):
self.verify_reply('share boo', bot_response)
def test_dbx_help(self):
bot_response = 'syntax: ls <optional_path>'
with self.mock_config_info(self.config_info):
self.verify_reply('help ls', bot_response)
def test_dbx_usage(self):
bot_response = '''
Usage:
```
@dropbox ls - Shows files/folders in the root folder.
@dropbox mkdir foo - Make folder named foo.
@dropbox ls foo/boo - Shows the files/folders in foo/boo folder.
@dropbox write test hello world - Write "hello world" to the file 'test'.
@dropbox rm test - Remove the file/folder test.
@dropbox read foo - Read the contents of file/folder foo.
@dropbox share foo - Get shareable link for the file/folder foo.
@dropbox search boo - Search for boo in root folder and get at max 20 results.
@dropbox search boo --mr 10 - Search for boo and get at max 10 results.
@dropbox search boo --fd foo - Search for boo in folder foo.
```
'''
with self.mock_config_info(self.config_info):
self.verify_reply('usage', bot_response)
def test_invalid_commands(self): def test_invalid_commands(self):
ls_error_response = "ERROR: syntax: ls <optional_path>" ls_error_response = "ERROR: syntax: ls <optional_path>"
mkdir_error_response = "ERROR: syntax: mkdir <path>" mkdir_error_response = "ERROR: syntax: mkdir <path>"
@ -115,6 +229,7 @@ class TestDropboxBot(BotTestCase):
write_error_response = "ERROR: syntax: write <path> <some_text>" write_error_response = "ERROR: syntax: write <path> <some_text>"
search_error_response = "ERROR: syntax: search <path> <some_text> <max_results>" search_error_response = "ERROR: syntax: search <path> <some_text> <max_results>"
share_error_response = "ERROR: syntax: share <path>" share_error_response = "ERROR: syntax: share <path>"
usage_error_response = "ERROR: syntax: usage"
with self.mock_config_info(self.config_info): with self.mock_config_info(self.config_info):
# ls # ls
@ -127,3 +242,24 @@ class TestDropboxBot(BotTestCase):
self.verify_reply("write foo", write_error_response) self.verify_reply("write foo", write_error_response)
# share # share
self.verify_reply("share foo boo", share_error_response) self.verify_reply("share foo boo", share_error_response)
# usage
self.verify_reply("usage foo", usage_error_response)
def test_unkown_command(self):
bot_response = '''ERROR: unrecognized command
Example commands:
```
@mention-bot usage: see usage examples
@mention-bot mkdir: create a folder
@mention-bot ls: list a folder
@mention-bot write: write text
@mention-bot rm: remove a file or folder
@mention-bot read: read a file
@mention-bot search: search a file/folder
@mention-bot share: get a shareable link for the file/folder
```
'''
with self.mock_config_info(self.config_info):
self.verify_reply('unknown command', bot_response)