File size: 31,585 Bytes
5980447
1
2
{"repo": "sarumont/py-trello", "pull_number": 52, "instance_id": "sarumont__py-trello-52", "issue_numbers": "", "base_commit": "81f93a2272a7855410c78b194316f64e757f6bf7", "patch": "diff --git a/setup.py b/setup.py\n--- a/setup.py\n+++ b/setup.py\n@@ -4,7 +4,7 @@\n \n setup(\n         name = \"py-trello\",\n-        version = \"0.1.5\",\n+        version = \"0.2.2\",\n \n         description = 'Python wrapper around the Trello API',\n         long_description = open('README.rst').read(),\n@@ -19,8 +19,12 @@\n             'License :: OSI Approved :: BSD License',\n             'Operating System :: OS Independent',\n             'Programming Language :: Python',\n+            'Programming Language :: Python 2',\n+            'Programming Language :: Python 2.7',\n+            'Programming Language :: Python 3',\n+            'Programming Language :: Python 3.3',\n             ],\n-        install_requires = ['httplib2 >= 0.9', 'oauth2',],\n+        install_requires = [\"requests\", \"requests-oauthlib >= 0.4.1\",],\n         packages = find_packages(),\n         include_package_data = True,\n         )       \ndiff --git a/trello/__init__.py b/trello/__init__.py\n--- a/trello/__init__.py\n+++ b/trello/__init__.py\n@@ -1,14 +1,7 @@\n-from httplib2 import Http\n-from urllib import urlencode\n from datetime import datetime\n-import exceptions\n import json\n-import oauth2 as oauth\n-import os\n-import random\n-import time\n-import urlparse\n-import urllib2\n+import requests\n+from requests_oauthlib import OAuth1\n \n \n class ResourceUnavailable(Exception):\n@@ -17,10 +10,10 @@ class ResourceUnavailable(Exception):\n     def __init__(self, msg, http_response):\n         Exception.__init__(self)\n         self._msg = msg\n-        self._status = http_response.status\n+        self._status = http_response.status_code\n \n     def __str__(self):\n-        return \"Resource unavailable: %s (HTTP status: %s)\" % (\n+        return \"%s (HTTP status: %s)\" % (\n         self._msg, self._status)\n \n \n@@ -46,22 +39,18 @@ def __init__(self, api_key, api_secret=None, token=None, token_secret=None):\n         :token_secret: the OAuth client secret for the given OAuth token\n         \"\"\"\n \n-        if api_key and api_secret and token and token_secret:\n-            # oauth\n-            self.oauth_consumer = oauth.Consumer(key=api_key, secret=api_secret)\n-            self.oauth_token = oauth.Token(key=token, secret=token_secret)\n-            self.client = oauth.Client(self.oauth_consumer, self.oauth_token)\n-\n-        elif api_key:\n-            self.client = Http()\n-\n-        if token is None:\n-            self.public_only = True\n+        # client key and secret for oauth1 session\n+        if api_key or token:\n+            self.oauth = OAuth1(client_key=api_key, client_secret=api_secret,\n+                                resource_owner_key=token, resource_owner_secret=token_secret)\n         else:\n-            self.public_only = False\n+            self.oauth = None\n \n+        self.public_only = token is None\n         self.api_key = api_key\n-        self.auth_token = token\n+        self.api_secret = api_secret\n+        self.resource_owner_key = token\n+        self.resource_owner_secret = token_secret\n \n     def info_for_all_boards(self, actions):\n         \"\"\"\n@@ -81,33 +70,6 @@ def logout(self):\n \n         raise NotImplementedError()\n \n-    def build_url(self, path, query={}):\n-        \"\"\"\n-        Builds a Trello URL.\n-\n-        :path: URL path\n-        :params: dict of key-value pairs for the query string\n-        \"\"\"\n-        url = 'https://api.trello.com/1'\n-        if path[0:1] != '/':\n-            url += '/'\n-        url += path\n-\n-        if hasattr(self, 'oauth_token'):\n-            url += '?'\n-            url += \"key=\" + self.oauth_consumer.key\n-            url += \"&token=\" + self.oauth_token.key\n-        else:\n-            url += '?'\n-            url += \"key=\" + self.api_key\n-            if self.public_only is False:\n-                url += \"&token=\" + self.auth_token\n-\n-        if len(query) > 0:\n-            url += '&' + urlencode(query)\n-\n-        return url\n-\n     def list_boards(self):\n         \"\"\"\n         Returns all boards for your Trello user\n@@ -135,14 +97,14 @@ def get_board(self, board_id):\n     def add_board(self, board_name):\n         obj = self.fetch_json('/boards', http_method='POST',\n                               post_args={'name': board_name})\n-        board = Board(self, obj['id'], name=obj['name'].encode('utf-8'))\n+        board = Board(self, obj['id'], name=obj['name'])\n         board.closed = obj['closed']\n         return board\n \n     def get_list(self, list_id):\n         obj = self.fetch_json('/lists/' + list_id)\n         list = List(self.get_board(obj['idBoard']), obj['id'],\n-                    name=obj['name'].encode('utf-8'))\n+                    name=obj['name'])\n         list.closed = obj['closed']\n         return list\n \n@@ -153,32 +115,43 @@ def fetch_json(\n             self,\n             uri_path,\n             http_method='GET',\n-            headers={},\n-            query_params={},\n-            post_args={}):\n+            headers=None,\n+            query_params=None,\n+            post_args=None):\n         \"\"\" Fetch some JSON from Trello \"\"\"\n \n-        if http_method in (\"POST\", \"PUT\", \"DELETE\"):\n-            headers['Content-Type'] = 'application/json'\n+        # explicit values here to avoid mutable default values\n+        if headers is None:\n+            headers = {}\n+        if query_params is None:\n+            query_params = {}\n+        if post_args is None:\n+            post_args = {}\n \n+        # set content type and accept headers to handle JSON\n+        if http_method in (\"POST\", \"PUT\", \"DELETE\"):\n+            headers['Content-Type'] = 'application/json; charset=utf-8'\n         headers['Accept'] = 'application/json'\n-        url = self.build_url(uri_path, query_params)\n-        response, content = self.client.request(\n-            url,\n-            http_method,\n-            headers=headers,\n-            body=json.dumps(post_args))\n \n-        # error checking\n-        if response.status == 401:\n-            raise Unauthorized(url, response)\n-        if response.status != 200:\n-            raise ResourceUnavailable(url, response)\n-        return json.loads(content)\n+        # construct the full URL without query parameters\n+        if uri_path[0] == '/':\n+            uri_path = uri_path[1:]\n+        url = 'https://api.trello.com/1/%s' % uri_path\n+\n+        # perform the HTTP requests, if possible uses OAuth authentication\n+        response = requests.request(http_method, url, params=query_params,\n+                                    headers=headers, data=json.dumps(post_args), auth=self.oauth)\n+\n+        if response.status_code == 401:\n+            raise Unauthorized(\"%s at %s\" % (response.text, url), response)\n+        if response.status_code != 200:\n+            raise ResourceUnavailable(\"%s at %s\" % (response.text, url), response)\n+\n+        return response.json()\n \n     def _board_from_json(self, json):\n-        board = Board(self, json['id'], name=json['name'].encode('utf-8'))\n-        board.description = json.get('desc', '').encode('utf-8')\n+        board = Board(self, json['id'], name=json['name'])\n+        board.description = json.get('desc', '')\n         board.closed = json['closed']\n         board.url = json['url']\n         return board\n@@ -188,13 +161,13 @@ def list_hooks(self, token=None):\n         Returns a list of all hooks associated with a specific token. If you don't pass in a token,\n         it tries to use the token associated with the TrelloClient object (if it exists)\n         \"\"\"\n+        token = token or self.resource_owner_key\n \n-        if token is None and self.auth_token is None:\n+        if token is None:\n             raise TokenError(\"You need to pass an auth token in to list hooks.\")\n         else:\n-            using_token = token if self.auth_token is None else self.auth_token\n-            url = \"/tokens/%s/webhooks\" % using_token\n-            return self._existing_hook_objs(self.fetch_json(url), using_token)\n+            url = \"/tokens/%s/webhooks\" % token\n+            return self._existing_hook_objs(self.fetch_json(url), token)\n \n     def _existing_hook_objs(self, hooks, token):\n         \"\"\"\n@@ -216,29 +189,22 @@ def create_hook(self, callback_url, id_model, desc=None, token=None):\n         There seems to be some sort of bug that makes you unable to create a\n         hook using httplib2, so I'm using urllib2 for that instead.\n         \"\"\"\n+        token = token or self.resource_owner_key\n \n-        if token is None and self.auth_token is None:\n-            raise TokenError(\n-                \"You need to pass an auth token in to create a hook.\")\n+        if token is None:\n+            raise TokenError(\"You need to pass an auth token in to create a hook.\")\n+\n+        url = \"https://trello.com/1/tokens/%s/webhooks/\" % token\n+        data = {'callbackURL': callback_url, 'idModel': id_model,\n+                'description': desc}\n+\n+        response = requests.post(url, data=data, auth=self.oauth)\n+\n+        if response.status_code == 200:\n+            hook_id = response.json()['id']\n+            return WebHook(self, token, hook_id, desc, id_model, callback_url, True)\n         else:\n-            using_token = token if self.auth_token is None else self.auth_token\n-            url = \"https://trello.com/1/tokens/%s/webhooks/?key=%s\" % (\n-            using_token, self.api_key)\n-            data = urlencode({'callbackURL': callback_url, 'idModel': id_model,\n-                              \"description\": desc})\n-\n-            # TODO - error checking for invalid responses\n-            # Before spending too much time doing that with urllib2, might be worth trying\n-            # and getting it working with urllib2 for consistency\n-            req = urllib2.Request(url, data)\n-            response = urllib2.urlopen(req)\n-\n-            if response.code == 200:\n-                hook_id = json.loads(response.read())['id']\n-                return WebHook(self, using_token, hook_id, desc, id_model,\n-                               callback_url, True)\n-            else:\n-                return False\n+            return False\n \n \n class Board(object):\n@@ -263,7 +229,7 @@ def __repr__(self):\n     def fetch(self):\n         \"\"\"Fetch all attributes for this board\"\"\"\n         json_obj = self.client.fetch_json('/boards/' + self.id)\n-        self.name = json_obj['name'].encode('utf-8')\n+        self.name = json_obj['name']\n         self.description = json_obj.get('desc', '')\n         self.closed = json_obj['closed']\n         self.url = json_obj['url']\n@@ -298,7 +264,7 @@ def get_lists(self, list_filter):\n             query_params={'cards': 'none', 'filter': list_filter})\n         lists = list()\n         for obj in json_obj:\n-            l = List(self, obj['id'], name=obj['name'].encode('utf-8'))\n+            l = List(self, obj['id'], name=obj['name'])\n             l.closed = obj['closed']\n             lists.append(l)\n \n@@ -314,7 +280,7 @@ def add_list(self, name):\n             '/lists',\n             http_method='POST',\n             post_args={'name': name, 'idBoard': self.id}, )\n-        list = List(self, obj['id'], name=obj['name'].encode('utf-8'))\n+        list = List(self, obj['id'], name=obj['name'])\n         list.closed = obj['closed']\n         return list\n \n@@ -358,9 +324,9 @@ def get_cards(self, filters=None):\n         cards = list()\n         for card_json in json_obj:\n             card = Card(self, card_json['id'],\n-                        name=card_json['name'].encode('utf-8'))\n+                        name=card_json['name'])\n \n-            for card_key, card_val in card_json.iteritems():\n+            for card_key, card_val in card_json.items():\n                 if card_key in ['id', 'name']:\n                     continue\n \n@@ -400,7 +366,7 @@ def __repr__(self):\n     def fetch(self):\n         \"\"\"Fetch all attributes for this list\"\"\"\n         json_obj = self.client.fetch_json('/lists/' + self.id)\n-        self.name = json_obj['name'].encode('utf-8')\n+        self.name = json_obj['name']\n         self.closed = json_obj['closed']\n \n     def list_cards(self):\n@@ -408,8 +374,8 @@ def list_cards(self):\n         json_obj = self.client.fetch_json('/lists/' + self.id + '/cards')\n         cards = list()\n         for c in json_obj:\n-            card = Card(self, c['id'], name=c['name'].encode('utf-8'))\n-            card.description = c.get('desc', '').encode('utf-8')\n+            card = Card(self, c['id'], name=c['name'])\n+            card.description = c.get('desc', '')\n             card.closed = c['closed']\n             card.url = c['url']\n             card.member_ids = c['idMembers']\n@@ -506,14 +472,14 @@ def fetch(self):\n         json_obj = self.client.fetch_json(\n             '/cards/' + self.id,\n             query_params={'badges': False})\n-        self.name = json_obj['name'].encode('utf-8')\n+        self.name = json_obj['name']\n         self.description = json_obj.get('desc', '')\n         self.closed = json_obj['closed']\n         self.url = json_obj['url']\n-        self.member_ids = json_obj['idMembers']\n-        self.short_id = json_obj['idShort']\n-        self.list_id = json_obj['idList']\n-        self.board_id = json_obj['idBoard']\n+        self.idMembers = json_obj['idMembers']\n+        self.idShort = json_obj['idShort']\n+        self.idList = json_obj['idList']\n+        self.idBoard = json_obj['idBoard']\n         self.labels = json_obj['labels']\n         self.badges = json_obj['badges']\n         self.due = json_obj['due']\n@@ -600,7 +566,7 @@ def change_board(self, board_id, list_id=None):\n             http_method='PUT',\n             post_args=args)\n \n-    def add_checklist(self, title, items, itemstates=[]):\n+    def add_checklist(self, title, items, itemstates=None):\n \n         \"\"\"Add a checklist to this card\n \n@@ -609,6 +575,9 @@ def add_checklist(self, title, items, itemstates=[]):\n         :itemstates: a list of the state (True/False) of each item\n         :return: the checklist\n         \"\"\"\n+        if itemstates is None:\n+            itemstates = []\n+\n         json_obj = self.client.fetch_json(\n             '/cards/' + self.id + '/checklists',\n             http_method='POST',\n@@ -649,13 +618,13 @@ def fetch(self):\n         json_obj = self.client.fetch_json(\n             '/members/' + self.id,\n             query_params={'badges': False})\n-        self.status = json_obj['status'].encode('utf-8')\n+        self.status = json_obj['status']\n         self.id = json_obj.get('id', '')\n         self.bio = json_obj.get('bio', '')\n         self.url = json_obj.get('url', '')\n-        self.username = json_obj['username'].encode('utf-8')\n-        self.full_name = json_obj['fullName'].encode('utf-8')\n-        self.initials = json_obj['initials'].encode('utf-8')\n+        self.username = json_obj['username']\n+        self.full_name = json_obj['fullName']\n+        self.initials = json_obj['initials']\n         return self\n \n \ndiff --git a/trello/util.py b/trello/util.py\n--- a/trello/util.py\n+++ b/trello/util.py\n@@ -1,10 +1,8 @@\n import os\n-import urlparse\n \n-import oauth2 as oauth\n+from requests_oauthlib import OAuth1Session\n \n-\n-def create_oauth_token():\n+def create_oauth_token(expiration=None, scope=None, key=None, secret=None):\n     \"\"\"\n     Script to obtain an OAuth token from Trello.\n \n@@ -19,44 +17,46 @@ def create_oauth_token():\n     authorize_url = 'https://trello.com/1/OAuthAuthorizeToken'\n     access_token_url = 'https://trello.com/1/OAuthGetAccessToken'\n \n-    expiration = os.environ.get('TRELLO_EXPIRATION', None)\n-    scope = os.environ.get('TRELLO_SCOPE', 'read,write')\n-    trello_key = os.environ['TRELLO_API_KEY']\n-    trello_secret = os.environ['TRELLO_API_SECRET']\n-\n-    consumer = oauth.Consumer(trello_key, trello_secret)\n-    client = oauth.Client(consumer)\n+    expiration = expiration or os.environ.get('TRELLO_EXPIRATION', \"30days\")\n+    scope = scope or os.environ.get('TRELLO_SCOPE', 'read,write')\n+    trello_key = key or os.environ['TRELLO_API_KEY']\n+    trello_secret = secret or os.environ['TRELLO_API_SECRET']\n \n     # Step 1: Get a request token. This is a temporary token that is used for\n     # having the user authorize an access token and to sign the request to obtain\n     # said access token.\n \n-    resp, content = client.request(request_token_url, \"GET\")\n-    if resp['status'] != '200':\n-        raise Exception(\"Invalid response %s.\" % resp['status'])\n-\n-    request_token = dict(urlparse.parse_qsl(content))\n+    session = OAuth1Session(client_key=trello_key, client_secret=trello_secret)\n+    response = session.fetch_request_token(request_token_url)\n+    resource_owner_key, resource_owner_secret = response.get('oauth_token'), response.get('oauth_token_secret')\n \n-    print \"Request Token:\"\n-    print \"    - oauth_token        = %s\" % request_token['oauth_token']\n-    print \"    - oauth_token_secret = %s\" % request_token['oauth_token_secret']\n-    print\n+    print(\"Request Token:\")\n+    print(\"    - oauth_token        = %s\" % resource_owner_key)\n+    print(\"    - oauth_token_secret = %s\" % resource_owner_secret)\n+    print(\"\")\n \n     # Step 2: Redirect to the provider. Since this is a CLI script we do not\n     # redirect. In a web application you would redirect the user to the URL\n     # below.\n \n-    print \"Go to the following link in your browser:\"\n-    print \"{authorize_url}?oauth_token={oauth_token}&scope={scope}&expiration={expiration}\".format(\n+    print(\"Go to the following link in your browser:\")\n+    print(\"{authorize_url}?oauth_token={oauth_token}&scope={scope}&expiration={expiration}\".format(\n         authorize_url=authorize_url,\n-        oauth_token=request_token['oauth_token'],\n+        oauth_token=resource_owner_key,\n         expiration=expiration,\n         scope=scope,\n-    )\n+    ))\n \n     # After the user has granted access to you, the consumer, the provider will\n     # redirect you to whatever URL you have told them to redirect to. You can\n     # usually define this in the oauth_callback argument as well.\n+\n+    # Python 3 compatibility (raw_input was renamed to input)\n+    try:\n+        raw_input\n+    except NameError:\n+        raw_input = input\n+\n     accepted = 'n'\n     while accepted.lower() == 'n':\n         accepted = raw_input('Have you authorized me? (y/n) ')\n@@ -67,20 +67,17 @@ def create_oauth_token():\n     # request token to sign this request. After this is done you throw away the\n     # request token and use the access token returned. You should store this\n     # access token somewhere safe, like a database, for future use.\n-    token = oauth.Token(request_token['oauth_token'],\n-                        request_token['oauth_token_secret'])\n-    token.set_verifier(oauth_verifier)\n-    client = oauth.Client(consumer, token)\n-\n-    resp, content = client.request(access_token_url, \"POST\")\n-    access_token = dict(urlparse.parse_qsl(content))\n-\n-    print \"Access Token:\"\n-    print \"    - oauth_token        = %s\" % access_token['oauth_token']\n-    print \"    - oauth_token_secret = %s\" % access_token['oauth_token_secret']\n-    print\n-    print \"You may now access protected resources using the access tokens above.\"\n-    print\n+    session = OAuth1Session(client_key=trello_key, client_secret=trello_secret,\n+                            resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret,\n+                            verifier=oauth_verifier)\n+    access_token = session.fetch_access_token(access_token_url)\n+\n+    print(\"Access Token:\")\n+    print(\"    - oauth_token        = %s\" % access_token['oauth_token'])\n+    print(\"    - oauth_token_secret = %s\" % access_token['oauth_token_secret'])\n+    print(\"\")\n+    print(\"You may now access protected resources using the access tokens above.\")\n+    print(\"\")\n \n if __name__ == '__main__':\n     create_oauth_token()\n", "test_patch": "diff --git a/test/test_trello.py b/test/test_trello.py\n--- a/test/test_trello.py\n+++ b/test/test_trello.py\n@@ -2,133 +2,163 @@\n import unittest\n import os\n \n-class TrelloClientTestCase(unittest.TestCase):\n \n-\t\"\"\"\n+class TrelloClientTestCase(unittest.TestCase):\n+    \"\"\"\n \tTests for TrelloClient API. Note these test are in order to preserve dependencies, as an API\n \tintegration cannot be tested independently.\n \t\"\"\"\n \n-\tdef setUp(self):\n-\t\tself._trello = TrelloClient(os.environ['TRELLO_API_KEY'],\n+    def setUp(self):\n+        self._trello = TrelloClient(os.environ['TRELLO_API_KEY'],\n                                     token=os.environ['TRELLO_TOKEN'])\n \n-\tdef test01_list_boards(self):\n-\t\tself.assertEquals(\n-\t\t\t\tlen(self._trello.list_boards()),\n-\t\t\t\tint(os.environ['TRELLO_TEST_BOARD_COUNT']))\n-\n-\tdef test10_board_attrs(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tfor b in boards:\n-\t\t\tself.assertIsNotNone(b.id, msg=\"id not provided\")\n-\t\t\tself.assertIsNotNone(b.name, msg=\"name not provided\")\n-\t\t\tself.assertIsNotNone(b.description, msg=\"description not provided\")\n-\t\t\tself.assertIsNotNone(b.closed, msg=\"closed not provided\")\n-\t\t\tself.assertIsNotNone(b.url, msg=\"url not provided\")\n-\n-\tdef test20_board_all_lists(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tfor b in boards:\n-\t\t\ttry:\n-\t\t\t\tb.all_lists()\n-\t\t\texcept Exception as e:\n-\t\t\t\tself.fail(\"Caught Exception getting lists\")\n-\n-\tdef test21_board_open_lists(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tfor b in boards:\n-\t\t\ttry:\n-\t\t\t\tb.open_lists()\n-\t\t\texcept Exception as e:\n-\t\t\t\tself.fail(\"Caught Exception getting open lists\")\n-\n-\tdef test22_board_closed_lists(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tfor b in boards:\n-\t\t\ttry:\n-\t\t\t\tb.closed_lists()\n-\t\t\texcept Exception as e:\n-\t\t\t\tself.fail(\"Caught Exception getting closed lists\")\n-\n-\tdef test30_list_attrs(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tfor b in boards:\n-\t\t\tfor l in b.all_lists():\n-\t\t\t\tself.assertIsNotNone(l.id, msg=\"id not provided\")\n-\t\t\t\tself.assertIsNotNone(l.name, msg=\"name not provided\")\n-\t\t\t\tself.assertIsNotNone(l.closed, msg=\"closed not provided\")\n-\t\t\tbreak # only need to test one board's lists\n-\n-\tdef test40_list_cards(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tfor b in boards:\n-\t\t\tfor l in b.all_lists():\n-\t\t\t\tfor c in l.list_cards():\n-\t\t\t\t\tself.assertIsNotNone(c.id, msg=\"id not provided\")\n-\t\t\t\t\tself.assertIsNotNone(c.name, msg=\"name not provided\")\n-\t\t\t\t\tself.assertIsNotNone(c.description, msg=\"description not provided\")\n-\t\t\t\t\tself.assertIsNotNone(c.closed, msg=\"closed not provided\")\n-\t\t\t\t\tself.assertIsNotNone(c.url, msg=\"url not provided\")\n-\t\t\t\tbreak\n-\t\t\tbreak\n-\t\tpass\n-\n-\tdef test50_add_card(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tboard_id = None\n-\t\tfor b in boards:\n-\t\t\tif b.name != os.environ['TRELLO_TEST_BOARD_NAME']:\n-\t\t\t\tcontinue\n-\n-\t\t\tfor l in b.open_lists():\n-\t\t\t\ttry:\n-\t\t\t\t\tname = \"Testing from Python - no desc\"\n-\t\t\t\t\tcard = l.add_card(name)\n-\t\t\t\texcept Exception as e:\n-\t\t\t\t\tprint str(e)\n-\t\t\t\t\tself.fail(\"Caught Exception adding card\")\n-\n-\t\t\t\tself.assertIsNotNone(card, msg=\"card is None\")\n-\t\t\t\tself.assertIsNotNone(card.id, msg=\"id not provided\")\n-\t\t\t\tself.assertEquals(card.name, name)\n-\t\t\t\tself.assertIsNotNone(card.closed, msg=\"closed not provided\")\n-\t\t\t\tself.assertIsNotNone(card.url, msg=\"url not provided\")\n-\t\t\t\tbreak\n-\t\t\tbreak\n-\t\tif not card:\n-\t\t\tself.fail(\"No card created\")\n-\n-\tdef test51_add_card(self):\n-\t\tboards = self._trello.list_boards()\n-\t\tboard_id = None\n-\t\tfor b in boards:\n-\t\t\tif b.name != os.environ['TRELLO_TEST_BOARD_NAME']:\n-\t\t\t\tcontinue\n-\n-\t\t\tfor l in b.open_lists():\n-\t\t\t\ttry:\n-\t\t\t\t\tname = \"Testing from Python\"\n-\t\t\t\t\tdescription = \"Description goes here\"\n-\t\t\t\t\tcard = l.add_card(name, description)\n-\t\t\t\texcept Exception as e:\n-\t\t\t\t\tprint str(e)\n-\t\t\t\t\tself.fail(\"Caught Exception adding card\")\n-\n-\t\t\t\tself.assertIsNotNone(card, msg=\"card is None\")\n-\t\t\t\tself.assertIsNotNone(card.id, msg=\"id not provided\")\n-\t\t\t\tself.assertEquals(card.name, name)\n-\t\t\t\tself.assertEquals(card.description, description)\n-\t\t\t\tself.assertIsNotNone(card.closed, msg=\"closed not provided\")\n-\t\t\t\tself.assertIsNotNone(card.url, msg=\"url not provided\")\n-\t\t\t\tbreak\n-\t\t\tbreak\n-\t\tif not card:\n-\t\t\tself.fail(\"No card created\")\n+    def test01_list_boards(self):\n+        self.assertEquals(\n+            len(self._trello.list_boards()),\n+            int(os.environ['TRELLO_TEST_BOARD_COUNT']))\n+\n+    def test10_board_attrs(self):\n+        boards = self._trello.list_boards()\n+        for b in boards:\n+            self.assertIsNotNone(b.id, msg=\"id not provided\")\n+            self.assertIsNotNone(b.name, msg=\"name not provided\")\n+            self.assertIsNotNone(b.description, msg=\"description not provided\")\n+            self.assertIsNotNone(b.closed, msg=\"closed not provided\")\n+            self.assertIsNotNone(b.url, msg=\"url not provided\")\n+\n+    def test20_board_all_lists(self):\n+        boards = self._trello.list_boards()\n+        for b in boards:\n+            try:\n+                b.all_lists()\n+            except Exception as e:\n+                self.fail(\"Caught Exception getting lists\")\n+\n+    def test21_board_open_lists(self):\n+        boards = self._trello.list_boards()\n+        for b in boards:\n+            try:\n+                b.open_lists()\n+            except Exception as e:\n+                self.fail(\"Caught Exception getting open lists\")\n+\n+    def test22_board_closed_lists(self):\n+        boards = self._trello.list_boards()\n+        for b in boards:\n+            try:\n+                b.closed_lists()\n+            except Exception as e:\n+                self.fail(\"Caught Exception getting closed lists\")\n+\n+    def test30_list_attrs(self):\n+        boards = self._trello.list_boards()\n+        for b in boards:\n+            for l in b.all_lists():\n+                self.assertIsNotNone(l.id, msg=\"id not provided\")\n+                self.assertIsNotNone(l.name, msg=\"name not provided\")\n+                self.assertIsNotNone(l.closed, msg=\"closed not provided\")\n+            break  # only need to test one board's lists\n+\n+    def test40_list_cards(self):\n+        boards = self._trello.list_boards()\n+        for b in boards:\n+            for l in b.all_lists():\n+                for c in l.list_cards():\n+                    self.assertIsNotNone(c.id, msg=\"id not provided\")\n+                    self.assertIsNotNone(c.name, msg=\"name not provided\")\n+                    self.assertIsNotNone(c.description, msg=\"description not provided\")\n+                    self.assertIsNotNone(c.closed, msg=\"closed not provided\")\n+                    self.assertIsNotNone(c.url, msg=\"url not provided\")\n+                break\n+            break\n+        pass\n+\n+\n+    def test50_add_card(self):\n+        boards = self._trello.list_boards()\n+        board_id = None\n+        for b in boards:\n+            if b.name != os.environ['TRELLO_TEST_BOARD_NAME']:\n+                continue\n+\n+            for l in b.open_lists():\n+                try:\n+                    name = \"Testing from Python - no desc\"\n+                    card = l.add_card(name)\n+                except Exception as e:\n+                    print(str(e))\n+                    self.fail(\"Caught Exception adding card\")\n+\n+                self.assertIsNotNone(card, msg=\"card is None\")\n+                self.assertIsNotNone(card.id, msg=\"id not provided\")\n+                self.assertEquals(card.name, name)\n+                self.assertIsNotNone(card.closed, msg=\"closed not provided\")\n+                self.assertIsNotNone(card.url, msg=\"url not provided\")\n+                break\n+            break\n+        if not card:\n+            self.fail(\"No card created\")\n+\n+    def test51_add_card(self):\n+        boards = self._trello.list_boards()\n+        board_id = None\n+        for b in boards:\n+            if b.name != os.environ['TRELLO_TEST_BOARD_NAME']:\n+                continue\n+\n+            for l in b.open_lists():\n+                try:\n+                    name = \"Testing from Python\"\n+                    description = \"Description goes here\"\n+                    card = l.add_card(name, description)\n+                except Exception as e:\n+                    print(str(e))\n+                    self.fail(\"Caught Exception adding card\")\n+\n+                self.assertIsNotNone(card, msg=\"card is None\")\n+                self.assertIsNotNone(card.id, msg=\"id not provided\")\n+                self.assertEquals(card.name, name)\n+                self.assertEquals(card.description, description)\n+                self.assertIsNotNone(card.closed, msg=\"closed not provided\")\n+                self.assertIsNotNone(card.url, msg=\"url not provided\")\n+                break\n+            break\n+        if not card:\n+            self.fail(\"No card created\")\n+\n+\n+    def test52_get_cards(self):\n+        boards = [board for board in self._trello.list_boards() if board.name == os.environ['TRELLO_TEST_BOARD_NAME']]\n+        self.assertEquals(len(boards), 1, msg=\"Test board not found\")\n+\n+        board = boards[0]\n+        cards = board.get_cards()\n+        self.assertEqual(len(cards), 2, msg=\"Unexpected number of cards in testboard\")\n+\n+        for card in cards:\n+            if card.name == 'Testing from Python':\n+                self.assertEqual(card.description, 'Description goes here')\n+            elif card.name == 'Testing from Python - no desc':\n+                self.assertEqual(card.description, '')\n+            else:\n+                self.fail(msg='Unexpected card found')\n+\n+\n+    def test60_delete_cards(self):\n+        boards = [board for board in self._trello.list_boards() if board.name == os.environ['TRELLO_TEST_BOARD_NAME']]\n+        self.assertEquals(len(boards), 1, msg=\"Test board not found\")\n+\n+        board = boards[0]\n+        cards = board.get_cards()\n+        for card in cards:\n+            card.delete()\n+\n \n def suite():\n-\ttests = ['test01_list_boards', 'test10_board_attrs', 'test20_add_card']\n-\treturn unittest.TestSuite(map(TrelloClientTestCase, tests))\n+    tests = ['test01_list_boards', 'test10_board_attrs', 'test20_add_card']\n+    return unittest.TestSuite(map(TrelloClientTestCase, tests))\n+\n \n if __name__ == \"__main__\":\n-\tunittest.main()\n+    unittest.main()\n", "problem_statement": "", "hints_text": "", "created_at": "2014-07-11T14:34:33Z"}