def add_card_to_trello(request): req = request.get_json() params = req['queryResult']['parameters'] target_list = params['list'] action = params['action'] items = params['items'][0].split() if target_list not in ['shopping', 'todo']: return generate_response('リスト名が間違っています') if action not in ['add', 'delete', 'get']: return generate_response('追加, 削除, 一覧取得 が可能です') key = os.environ.get("TRELLO_KEY") token = os.environ.get("TRELLO_TOKEN") shopping_list_id = os.environ.get("TRELLO_LIST_ID_SHOPPING") todo_list_id = os.environ.get("TRELLO_LIST_ID_TODO") client = TrelloApiClient(key, token) list_id = shopping_list_id if target_list == 'shopping' else todo_list_id if action == 'add': added_cards = client.add_cards(items, list_id) return generate_response('%sを追加しました' % ' '.join(added_cards)) elif action == 'delete': deleted_cards = client.close_cards_by_titles(list_id, items) return generate_response('%sを削除しました' % ' '.join(deleted_cards)) elif action == 'gets': return generate_response(' '.join(client.get_cards_on_list(list_id))) return generate_response('')
def test_close_cards_by_title(self): client = TrelloApiClient('apikey', 'apitoken') cards_on_list = [{ 'id': 'xxx1', 'name': 'title1' }, { 'id': 'xxx2', 'name': 'title2' }, { 'id': 'xxx3', 'name': 'title1' }] client.get_cards_on_list = Mock(return_value=cards_on_list) def _close_by_id(id): for cards in cards_on_list: if cards['id'] == id: return cards['name'] return None client._close_card_by_id = Mock(side_effect=_close_by_id) card_title = ['title1', 'title2', 'title3'] expected = ['title1', 'title2'] actual = client.close_cards_by_titles(card_title, 'list_id') self.assertEqual(expected, sorted(actual))
def test_add_cards(self): client = TrelloApiClient('apikey', 'apitoken') card_titles = ['title1', 'title2', 'title3'] client._add_card = Mock( side_effect=lambda x, y: x if x != 'title3' else None) expected = ['title1', 'title2'] actual = client.add_cards(card_titles, 'list_id') self.assertEqual(expected, actual)
def test__close_card_by_id(self, mock): client = TrelloApiClient('apikey', 'apitoken') resp: requests.Response = requests.Response() resp.status_code = 400 mock.return_value = resp self.assertIsNone(client._close_card_by_id('list_id')) resp.status_code = 200 resp.json = Mock(return_value={'id': 'xxx', 'name': 'title1'}) mock.return_value = resp expected = 'title1' self.assertEqual(expected, client._close_card_by_id('card_id'))
def test__add_card(self, mock: MagicMock): client = TrelloApiClient('apikey', 'apitoken') resp: requests.Response = requests.Response() resp.status_code = 400 mock.return_value = resp self.assertIsNone(client._add_card('title', 'list_id')) json_mock = Mock() json_mock.return_value = {'id': 'xxx', 'name': 'title'} resp.json = json_mock resp.status_code = 200 mock.return_value = resp actual = client._add_card('title', 'list_id') self.assertEqual(actual, 'title')
def test_get_cards_on_list(self, mock): client = TrelloApiClient('apikey', 'apitoken') resp: requests.Response = requests.Response() resp.status_code = 400 mock.return_value = resp self.assertEqual([], client.get_cards_on_list('list_id')) resp.status_code = 200 resp.json = Mock(return_value=[{ 'id': 'xxx', 'name': 'title1' }, { 'id': 'yyy', 'name': 'title2' }]) expected = [{ 'id': 'xxx', 'name': 'title1' }, { 'id': 'yyy', 'name': 'title2' }] self.assertEqual(expected, client.get_cards_on_list('list_id'))
def test__RequestApi(self): client = TrelloApiClient('apikey', 'apitoken') resp = client._RequestApi('', 'DELETE') self.assertEqual(resp, 0)