class TermAssert(unittest.TestCase):

    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def test_str(self):
        """
        Test _term_assert with term "goat"
        """
        term = "goat"
        self.api._term_assert(term, "term")
 
    def test_str_non_ascii(self):
        """
        Test _term_assert with term "¤"
        """
        term = "¤"
        self.api._term_assert(term, "term")
 
    def test_str_multiple_words(self):
        """
        Test _term_assert with term "goat horn"
        """
        term = "goat horn"
        self.api._term_assert(term, "term")

    def test_str_empty(self):
        """
        Test _term_assert with illegal term ""
        """
        term = ""
        with self.assertRaises(IllegalTerm):
            self.api._term_assert(term, "term")
Exemplo n.º 2
0
 def setUp(self):
     try:
         key = os.environ["TNP_KEY"]
         secret = os.environ["TNP_SECRET"]
     except KeyError:
         raise unittest.SkipTest(
             "We skip tests sending actual requests if the key and secret are not set as environment variables."
         )
     self.api = API(key, secret)
     self.limit = 2
     self.common_attributes = {
         "permalink", "id", "sponsor", "sponsor_id", "sponsor_campaign_link"
     }
     self.usage_attributes = {"limits", "usage"}
     self.usage_time_attributes = {"daily", "hourly", "monthly"}
     self.report_usage_attributes = {"licenses_consumed", "result"}
Exemplo n.º 3
0
class UserCollectionsURLs(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_user_collections(self, user_id):
        """
        Helper function to call api's get_user_collections in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_user_collections(user_id).url

    def test_get_user_collections_legal(self):
        """
        Test URL for get_user_collections with user_id 12
        """
        expected = "http://api.thenounproject.com/user/12/collections"
        user_id = 12
        result = self._test_get_user_collections(user_id)
        self.assertEqual(expected, result)

    def test_get_user_collections_illegal_id(self):
        """
        Test URL for get_user_collections with user_id None
        """
        user_id = None
        with self.assertRaises(IncorrectType):
            self._test_get_user_collections(user_id)
class CollectionsURLs(unittest.TestCase):

    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_collections(self, limit=None, offset=None, page=None):
        """
        Helper function to call api's get_collections in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_collections(limit=limit, offset=offset, page=page).url

    def test_get_collections(self):
        """
        Test URL for get_collections
        """
        expected = "http://api.thenounproject.com/collections"
        result = self._test_get_collections()
        self.assertEqual(expected, result)

    def test_get_collections_limit_offset(self):
        """
        Test URL for get_collections, with limit 12 and offset 3
        """
        expected = "http://api.thenounproject.com/collections?limit=12&offset=3"
        limit = 12
        offset = 3
        result = self._test_get_collections(limit=limit, offset=offset)
        self.assertEqual(expected, result)
Exemplo n.º 5
0
class IdAssert(unittest.TestCase):

    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def test_id_12(self):
        """
        Test _id_assert with id 12
        """
        id = 12
        self.api._id_assert(id, "id")
    
    def test_id_neg_12(self):
        """
        Test _id_assert with illegal id -12
        """
        id = -12
        with self.assertRaises(NonPositive):
            self.api._id_assert(id, "id")
 
    def test_id_0(self):
        """
        Test _id_assert with illegal id 0
        """
        id = 0
        with self.assertRaises(NonPositive):
            self.api._id_assert(id, "id")
class CollectionCustomURLs(unittest.TestCase):

    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_collection(self, identifier):
        """
        Helper function to call api's get_collection in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_collection(identifier).url

    def test_get_collection_int(self):
        """
        Test URL for get_collection with identifier 12
        """
        identifier = 12
        expected = "http://api.thenounproject.com/collection/12"
        result = self._test_get_collection(identifier)
        self.assertEqual(expected, result)

    def test_get_collection_str(self):
        """
        Test URL for get_collection with identifier "goat"
        """
        identifier = "goat"
        expected = "http://api.thenounproject.com/collection/goat"
        result = self._test_get_collection(identifier)
        self.assertEqual(expected, result)
    
    def test_get_collection_none(self):
        """
        Test URL for get_collection with illegal identifier None
        """
        identifier = None
        with self.assertRaises(IncorrectType):
            self._test_get_collection(identifier)

    def test_get_collection_bytes(self):
        """
        Test URL for get_collection with identifier b"goat"
        """
        identifier = b"goat"
        with self.assertRaises(IncorrectType):
            self._test_get_collection(identifier)
Exemplo n.º 7
0
class GetUsageURLs(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_usage(self):
        """
        Helper function to call api's get_usage in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_usage().url

    def test_get_usage(self):
        """
        Test URL for get_usage
        """
        expected = "http://api.thenounproject.com/oauth/usage"
        result = self._test_get_usage()
        self.assertEqual(expected, result)
class SlugAssert(unittest.TestCase):

    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def test_str(self):
        """
        Test _slug_assert with slug "goat"
        """
        slug = "goat"
        self.api._slug_assert(slug, "slug")
    
    def test_str_empty(self):
        """
        Test _slug_assert with illegal slug ""
        """
        slug = ""
        with self.assertRaises(IllegalSlug):
            self.api._slug_assert(slug, "slug")
 
    def test_str_non_ascii(self):
        """
        Test _slug_assert with illegal slug "¤"
        """
        slug = "¤"
        with self.assertRaises(IllegalSlug):
            self.api._slug_assert(slug, "slug")
 
    def test_str_multiple_words(self):
        """
        Test _slug_assert with illegal slug "goat horn"
        """
        slug = "goat horn"
        with self.assertRaises(IllegalSlug):
            self.api._slug_assert(slug, "slug")
class UserUploadsURLs(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_user_uploads(self, username):
        """
        Helper function to call api's get_user_uploads in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_user_uploads(username).url

    def test_get_user_uploads_legal(self):
        """
        Test URL for get_user_uploads with username "goat"
        """
        expected = "http://api.thenounproject.com/user/goat/uploads"
        username = "******"
        result = self._test_get_user_uploads(username)
        self.assertEqual(expected, result)

    def test_get_user_uploads_illegal_user_none(self):
        """
        Test URL for get_user_uploads with username None
        """
        username = None
        with self.assertRaises(IncorrectType):
            self._test_get_user_uploads(username)

    def test_get_user_uploads_illegal_user_empty(self):
        """
        Test URL for get_user_uploads with username ""
        """
        username = ""
        with self.assertRaises(IllegalTerm):
            self._test_get_user_uploads(username)
 def setUp(self):
     key = "mock api key to satisfy type check in api._get_oauth()"
     secret = "mock secret key to satisfy type check in api._get_oauth()"
     self.api = API(key, secret, testing=True)
Exemplo n.º 11
0
class IconsURLs(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_icons_by_term(self,
                                term,
                                public_domain_only=False,
                                limit=None,
                                offset=None,
                                page=None):
        """
        Helper function to call api's get_icons_by_term in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_icons_by_term(term, public_domain_only, limit,
                                          offset, page).url

    def test_get_icons_by_term_legal_goat(self):
        """
        Test URL for get_icons_by_term with term "goat"
        """
        term = "goat"
        expected = "http://api.thenounproject.com/icons/goat?limit_to_public_domain=0"
        result = self._test_get_icons_by_term(term)
        self.assertEqual(expected, result)

    def test_get_icons_by_term_illegal_term_float(self):
        """
        Test URL for get_icons_by_term with term 12.0
        """
        term = 12.0
        with self.assertRaises(IncorrectType):
            self._test_get_icons_by_term(term)

    def test_get_icons_by_term_illegal_term_none(self):
        """
        Test URL for get_icons_by_term with term None
        """
        term = None
        with self.assertRaises(IncorrectType):
            self._test_get_icons_by_term(term)

    def test_get_icons_by_term_public_domain_true(self):
        """
        Test URL for get_icons_by_term with term "goat", with public_domain_only as True
        """
        term = "goat"
        public_domain_only = True
        expected = "http://api.thenounproject.com/icons/goat?limit_to_public_domain=1"
        result = self._test_get_icons_by_term(
            term, public_domain_only=public_domain_only)
        self.assertEqual(expected, result)

    def test_get_icons_by_term_public_domain_false(self):
        """
        Test URL for get_icons_by_term with term "goat", with public_domain_only as False
        """
        term = "goat"
        public_domain_only = False
        expected = "http://api.thenounproject.com/icons/goat?limit_to_public_domain=0"
        result = self._test_get_icons_by_term(
            term, public_domain_only=public_domain_only)
        self.assertEqual(expected, result)

    def test_get_icons_by_term_public_domain_int(self):
        """
        Test URL for get_icons_by_term with term "goat", with public_domain_only as 12
        Note that I'll allow any integer value, even zero and negative numbers.
        """
        term = "goat"
        public_domain_only = 12
        expected = "http://api.thenounproject.com/icons/goat?limit_to_public_domain=12"
        result = self._test_get_icons_by_term(
            term, public_domain_only=public_domain_only)
        self.assertEqual(expected, result)

    def test_get_icons_by_term_public_domain_illegal(self):
        """
        Test URL for get_icons_by_term with term "goat", with public_domain_only as "goat"
        """
        term = "goat"
        public_domain_only = "goat"
        with self.assertRaises(IncorrectType):
            self._test_get_icons_by_term(term,
                                         public_domain_only=public_domain_only)
Exemplo n.º 12
0
class ApiKeys(unittest.TestCase):

    def setUp(self):
        self.api = API(testing=True)
        self.key = "mock api key to satisfy type check in api._get_oauth()"
        self.secret = "mock secret key to satisfy type check in api._get_oauth()"

    def test_no_api_key_no_secret(self):
        """
        Attempt a request with no api key and no secret key
        """
        with self.assertRaises(APIKeyNotSet):
            self.api.get_usage()

    def test_no_api_key(self):
        """
        Attempt a request with no api key
        """
        self.api.set_secret_key(self.secret)
        with self.assertRaises(APIKeyNotSet):
            self.api.get_usage()
    
    def test_no_secret(self):
        """
        Attempt a request with no secret key
        """
        self.api.set_api_key(self.key)
        with self.assertRaises(APIKeyNotSet):
            self.api.get_usage()
    
    def test_both(self):
        """
        Attempt a request with both api and secret keys set
        """
        self.api.set_secret_key(self.secret)
        self.api.set_api_key(self.key)
        self.api.get_usage()
class CollectionIconsCustomURLs(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_collection_icons(self,
                                   identifier,
                                   limit=None,
                                   offset=None,
                                   page=None):
        """
        Helper function to call api's get_collection_icons in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_collection_icons(identifier,
                                             limit=limit,
                                             offset=offset,
                                             page=page).url

    def test_get_collection_icons_legal_int(self):
        """
        Test URL for get_collection_icons with identifier 12
        """
        identifier = 12
        expected = "http://api.thenounproject.com/collection/12/icons"
        result = self._test_get_collection_icons(identifier)
        self.assertEqual(expected, result)

    def test_get_collection_icons_legal_string(self):
        """
        Test URL for get_collection_icons with identifier "goat"
        """
        identifier = "goat"
        expected = "http://api.thenounproject.com/collection/goat/icons"
        result = self._test_get_collection_icons(identifier)
        self.assertEqual(expected, result)

    def test_get_collection_icons_limit_page(self):
        """
        Test URL for get_collection_icons with identifier 12, with limit 12 and page 3
        """
        identifier = 12
        expected = "http://api.thenounproject.com/collection/12/icons?limit=12&page=3"
        limit = 12
        page = 3
        result = self._test_get_collection_icons(identifier,
                                                 limit=limit,
                                                 page=page)
        self.assertEqual(expected, result)

    def test_get_collection_icons_offset(self):
        """
        Test URL for get_collection_icons with identifier 12, with offset 12
        """
        identifier = 12
        expected = "http://api.thenounproject.com/collection/12/icons?offset=12"
        offset = 12
        result = self._test_get_collection_icons(identifier, offset=offset)
        self.assertEqual(expected, result)

    def test_get_collection_icons_illegal_identifier_none(self):
        """
        Test URL for get_collection_icons with ilegal identifier None
        """
        identifier = None
        with self.assertRaises(IncorrectType):
            self._test_get_collection_icons(identifier)

    def test_get_collection_icons_illegal_identifier_float(self):
        """
        Test URL for get_collection_icons with illegal identifier 12.0
        """
        identifier = 12.0
        with self.assertRaises(IncorrectType):
            self._test_get_collection_icons(identifier)

    def test_get_collection_icons_illegal_identifier_limit_page(self):
        """
        Test URL for get_collection_icons with illegal identifier [12, 4, 5], with legal limit 12 and page 3
        """
        identifier = [12, 4, 5]
        limit = 12
        page = 3
        with self.assertRaises(IncorrectType):
            self._test_get_collection_icons(identifier, limit=limit, page=page)
class CollectionIconsURLs(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_collection_icons_by_id(self,
                                         _id,
                                         limit=None,
                                         offset=None,
                                         page=None):
        """
        Helper function to call api's get_collection_icons_by_id in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_collection_icons_by_id(_id,
                                                   limit=limit,
                                                   offset=offset,
                                                   page=page).url

    def test_get_collection_icons_by_id_legal(self):
        """
        Test URL for get_collection_icons with id 12
        """
        _id = 12
        expected = "http://api.thenounproject.com/collection/12/icons"
        result = self._test_get_collection_icons_by_id(_id)
        self.assertEqual(expected, result)

    def test_get_collection_icons_by_id_limit_page(self):
        """
        Test URL for get_collection_icons with id 12, with limit 12 and page 3
        """
        _id = 12
        expected = "http://api.thenounproject.com/collection/12/icons?limit=12&page=3"
        limit = 12
        page = 3
        result = self._test_get_collection_icons_by_id(_id,
                                                       limit=limit,
                                                       page=page)
        self.assertEqual(expected, result)

    def test_get_collection_icons_by_id_offset(self):
        """
        Test URL for get_collection_icons with id 12, with offset 12
        """
        _id = 12
        expected = "http://api.thenounproject.com/collection/12/icons?offset=12"
        offset = 12
        result = self._test_get_collection_icons_by_id(_id, offset=offset)
        self.assertEqual(expected, result)

    def test_get_collection_icons_by_id_illegal_identifier(self):
        """
        Test URL for get_collection_icons with illegal id 12.0
        """
        _id = 12.0
        with self.assertRaises(IncorrectType):
            self._test_get_collection_icons_by_id(_id)

    def _test_get_collection_icons_by_slug(self,
                                           slug,
                                           limit=None,
                                           offset=None,
                                           page=None):
        """
        Helper function to call api's get_collection_icons_by_slug in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_collection_icons_by_slug(slug,
                                                     limit=limit,
                                                     offset=offset,
                                                     page=page).url

    def test_get_collection_icons_by_slug_legal(self):
        """
        Test URL for get_collection_icons with slug "goat"
        """
        slug = "goat"
        expected = "http://api.thenounproject.com/collection/goat/icons"
        result = self._test_get_collection_icons_by_slug(slug)
        self.assertEqual(expected, result)

    def test_get_collection_icons_by_slug_limit_page(self):
        """
        Test URL for get_collection_icons with slug "goat", with limit 12 and page 3
        """
        slug = "goat"
        expected = "http://api.thenounproject.com/collection/goat/icons?limit=12&page=3"
        limit = 12
        page = 3
        result = self._test_get_collection_icons_by_slug(slug,
                                                         limit=limit,
                                                         page=page)
        self.assertEqual(expected, result)

    def test_get_collection_icons_by_slug_offset(self):
        """
        Test URL for get_collection_icons with slug "goat", with offset 12
        """
        slug = "goat"
        expected = "http://api.thenounproject.com/collection/goat/icons?offset=12"
        offset = 12
        result = self._test_get_collection_icons_by_slug(slug, offset=offset)
        self.assertEqual(expected, result)

    def test_get_collection_icons_by_slug_illegal_identifier(self):
        """
        Test URL for get_collection_icons with illegal slug 12
        """
        slug = 12
        with self.assertRaises(IncorrectType):
            self._test_get_collection_icons_by_slug(slug)
Exemplo n.º 15
0
class SlugAssert(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_type(self, param, param_name, types):
        """
        Helper method to call api's _type_assert
        """
        self.api._type_assert(param, param_name, types)

    def test_type_legal_none(self):
        """
        Test to see that param as None with type(None) in types does not throw an exception
        """
        goat = None
        param_name = "goat"
        types = (type(None), int, str)
        self._test_type(goat, param_name, types)

    def test_type_legal_str(self):
        """
        Test to see that param as str with str in types does not throw an exception
        """
        goat = "goat"
        param_name = "goat"
        types = (type(None), int, str)
        self._test_type(goat, param_name, types)

    def test_type_legal_int(self):
        """
        Test to see that param as int with int in types does not throw an exception
        """
        goat = 12
        param_name = "goat"
        types = int
        self._test_type(goat, param_name, types)

    def test_type_illegal_none(self):
        """
        Test to see that param as None with type(None) not in types throws an exception
        """
        goat = None
        param_name = "goat"
        types = (tuple, int, str)
        with self.assertRaises(IncorrectType):
            self._test_type(goat, param_name, types)

    def test_type_illegal_float(self):
        """
        Test to see that param as float with float not in types throws an exception
        """
        goat = 12.0
        param_name = "goat"
        types = (int, str)
        with self.assertRaises(IncorrectType):
            self._test_type(goat, param_name, types)

    def test_type_illegal_bytes(self):
        """
        Test to see that param as bytes with bytes not in types throws an exception
        """
        goat = b"goat"
        param_name = "goat"
        types = str
        with self.assertRaises(IncorrectType):
            self._test_type(goat, param_name, types)
Exemplo n.º 16
0
class SlugAssert(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_report_usage(self, icons, test=False):
        """
        Helper method to call api's report_usage
        """
        return self.api.report_usage(icons, test)

    def _check_set(self, body, input_set):
        """
        Helper method for comparing PreparedRequest body with a set of icon ids.
        """
        json_data = json.loads(body.decode())
        icons_str = json_data['icons']
        icons_set = set(icons_str.split(","))
        return icons_set == {str(item) for item in input_set}

    def test_report_usage_int(self):
        """
        Test URL and body for report_usage, with icons as 12
        """
        icons = 12
        expected_url = "http://api.thenounproject.com/notify/publish"
        expected_body = b'{"icons": "12"}'
        result = self._test_report_usage(icons)
        self.assertEqual(result.url, expected_url)
        self.assertEqual(result.body, expected_body)

    def test_report_usage_str(self):
        """
        Test URL and body for report_usage, with icons as "12"
        """
        icons = "12"
        expected_url = "http://api.thenounproject.com/notify/publish"
        expected_body = b'{"icons": "12"}'
        result = self._test_report_usage(icons)
        self.assertEqual(result.url, expected_url)
        self.assertEqual(result.body, expected_body)

    def test_report_usage_set(self):
        """
        Test URL and body for report_usage, with icons as {12, "4", 8, 12}
        """
        icons = {12, "4", 8, 12}
        expected_url = "http://api.thenounproject.com/notify/publish"
        result = self._test_report_usage(icons)
        self.assertEqual(result.url, expected_url)
        self.assertTrue(self._check_set(result.body, icons))

    def test_report_usage_list(self):
        """
        Test URL and body for report_usage, with icons as ["4", 8, 12]
        """
        icons = ["4", 8, 12]
        expected_url = "http://api.thenounproject.com/notify/publish"
        expected_body = b'{"icons": "4,8,12"}'
        result = self._test_report_usage(icons)
        self.assertEqual(result.url, expected_url)
        self.assertEqual(result.body, expected_body)

    def test_report_usage_none(self):
        """
        Test URL and body for report_usage, with icons as None
        """
        icons = None
        with self.assertRaises(IncorrectType):
            self._test_report_usage(icons)

    def test_report_usage_float(self):
        """
        Test URL and body for report_usage, with icons as 12.0
        """
        icons = 12.0
        with self.assertRaises(IncorrectType):
            self._test_report_usage(icons)

    def test_report_usage_int_test(self):
        """
        Test URL and body for report_usage, with icons as "12", and test as True
        """
        icons = "12"
        expected_url = "http://api.thenounproject.com/notify/publish?test=1"
        expected_body = b'{"icons": "12"}'
        result = self._test_report_usage(icons, test=True)
        self.assertEqual(result.url, expected_url)
        self.assertEqual(result.body, expected_body)

    def test_report_usage_set_test(self):
        """
        Test URL and body for report_usage, with icons as {12, "4", 8, 12}, and test as True
        """
        icons = {12, "4", 8, 12}
        expected_url = "http://api.thenounproject.com/notify/publish?test=1"
        result = self._test_report_usage(icons, test=True)
        self.assertEqual(result.url, expected_url)
        self.assertTrue(self._check_set(result.body, icons))
Exemplo n.º 17
0
class CollectionURLs(unittest.TestCase):
    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_get_collection_by_id(self, _id):
        """
        Helper function to call api's get_collection_by_id in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_collection_by_id(_id).url

    def test_get_collection_by_id_legal_12(self):
        """
        Test URL for get_collection_by_id with id 12
        """
        _id = 12
        expected = "http://api.thenounproject.com/collection/12"
        result = self._test_get_collection_by_id(_id)
        self.assertEqual(expected, result)

    def test_get_collection_by_id_illegal_id_float(self):
        """
        Test URL for get_collection_by_id with id 12.0
        """
        _id = 12.0
        with self.assertRaises(IncorrectType):
            self._test_get_collection_by_id(_id)

    def test_get_collection_by_id_illegal_id_none(self):
        """
        Test URL for get_collection_by_id with id None
        """
        _id = None
        with self.assertRaises(IncorrectType):
            self._test_get_collection_by_id(_id)

    def _test_get_collection_by_slug(self, slug):
        """
        Helper function to call api's get_collection_by_slug in such a way that we only get the URL
        and don't actually make the request.
        """
        return self.api.get_collection_by_slug(slug).url

    def test_get_collection_by_slug_legal_goat(self):
        """
        Test URL for get_collection_by_slug with slug "goat"
        """
        slug = "goat"
        expected = "http://api.thenounproject.com/collection/goat"
        result = self._test_get_collection_by_slug(slug)
        self.assertEqual(expected, result)

    def test_get_collection_by_slug_illegal_slug_float(self):
        """
        Test URL for get_collection_by_slug with slug 12.0
        """
        slug = 12.0
        with self.assertRaises(IncorrectType):
            self._test_get_collection_by_slug(slug)

    def test_get_collection_by_slug_illegal_slug_none(self):
        """
        Test URL for get_collection_by_slug with slug None
        """
        slug = None
        with self.assertRaises(IncorrectType):
            self._test_get_collection_by_slug(slug)
Exemplo n.º 18
0
class Endpoints(unittest.TestCase):
    def setUp(self):
        try:
            key = os.environ["TNP_KEY"]
            secret = os.environ["TNP_SECRET"]
        except KeyError:
            raise unittest.SkipTest(
                "We skip tests sending actual requests if the key and secret are not set as environment variables."
            )
        self.api = API(key, secret)
        self.limit = 2
        self.common_attributes = {
            "permalink", "id", "sponsor", "sponsor_id", "sponsor_campaign_link"
        }
        self.usage_attributes = {"limits", "usage"}
        self.usage_time_attributes = {"daily", "hourly", "monthly"}
        self.report_usage_attributes = {"licenses_consumed", "result"}

    def tearDown(self):
        self.api._close_session()

    def test_collection_by_id(self):
        """
        Check if the result has self.common_attributes as attributes.
        """
        result = self.api.get_collection(12)
        self.assertTrue(self.common_attributes <= set(result.json.keys()))

    def test_collection_by_slug(self):
        """
        Check if the result has self.common_attributes as attributes.
        """
        result = self.api.get_collection("cue")
        self.assertTrue(self.common_attributes <= set(result.json.keys()))

    def test_collections(self):
        """
        Check if the result has a list of objects with self.common_attributes as attributes.
        """
        result = self.api.get_collections(limit=self.limit)
        for collection in result:
            self.assertTrue(
                self.common_attributes <= set(collection.json.keys()))

    def test_user_collections(self):
        """
        Check if the result has a list of objects with self.common_attributes as attributes.
        """
        result = self.api.get_user_collections(6)
        for collection in result:
            self.assertTrue(
                self.common_attributes <= set(collection.json.keys()))

    def test_user_collection(self):
        result = self.api.get_user_collection(6, "truck")
        self.assertTrue(self.common_attributes <= set(result.json.keys()))

    def test_report_usage(self):
        """
        Check if the result has self.report_usage_attributes as attributes.
        """
        result = self.api.report_usage([3, 8, 12], test=True)
        self.assertTrue(
            self.report_usage_attributes <= set(result.json.keys()))

    def test_collection_icons_by_id(self):
        """
        Check if the result.collection has self.common_attributes as attributes.
        Also ensure that the each icon under result has self.common_attributes as attributes.
        Lastly check that result has generated_at as attribute.
        """
        result = self.api.get_collection_icons(12, limit=self.limit)
        self.assertTrue(hasattr(result, "generated_at"))
        self.assertTrue(
            self.common_attributes <= set(result.collection.keys()))
        for icon in result:
            self.assertTrue(self.common_attributes <= set(icon.json.keys()))

    def test_collection_icons_by_slug(self):
        """
        Check if the result.collection has self.common_attributes as attributes.
        Also ensure that the each icon under result has self.common_attributes as attributes.
        Lastly check that result has generated_at as attribute.
        """
        result = self.api.get_collection_icons("cue", limit=self.limit)
        self.assertTrue(hasattr(result, "generated_at"))
        self.assertTrue(
            self.common_attributes <= set(result.collection.keys()))
        for icon in result:
            self.assertTrue(self.common_attributes <= set(icon.json.keys()))

    def test_icon_by_id(self):
        """
        Check if the result has self.common_attributes as attributes.
        Also ensure that the result.collection list has self.common_attributes as attributes for all of its elements.
        """
        result = self.api.get_icon(12)
        self.assertTrue(self.common_attributes <= set(result.json.keys()))
        for collection in result.collections:
            self.assertTrue(self.common_attributes <= set(
                CollectionModel.parse(collection).json.keys()))

    def test_icon_by_term(self):
        """
        Check if the result has self.common_attributes as attributes.
        Also ensure that the result.collection list has self.common_attributes as attributes for all of its elements.
        """
        result = self.api.get_icon("baggage")
        self.assertTrue(self.common_attributes <= set(result.json.keys()))
        for collection in result.collections:
            self.assertTrue(self.common_attributes <= set(
                CollectionModel.parse(collection).json.keys()))

    def test_recent_icons(self):
        """
        Check if each icon under result has self.common_attributes as attributes.
        Also ensure that result has generated_at as attribute.
        """
        result = self.api.get_recent_icons(limit=self.limit)
        self.assertTrue(hasattr(result, "generated_at"))
        for icon in result:
            self.assertTrue(self.common_attributes <= set(icon.json.keys()))

    def test_icons_by_term(self):
        """
        Check if each icon under result has self.common_attributes as attributes.
        Also ensure that result has generated_at as attribute.
        """
        result = self.api.get_icons_by_term("goat", limit=self.limit)
        self.assertTrue(hasattr(result, "generated_at"))
        for icon in result:
            self.assertTrue(self.common_attributes <= set(icon.json.keys()))

    def test_user_uploads(self):
        """
        Check if each icon under result has self.common_attributes as attributes.
        Also ensure that result has generated_at as attribute.
        """
        result = self.api.get_user_uploads("dan", limit=self.limit)
        self.assertTrue(hasattr(result, "generated_at"))
        for icon in result:
            self.assertTrue(self.common_attributes <= set(icon.json.keys()))

    def test_usage(self):
        """
        Check if the result has self.usage_attributes as attributes.
        Also check if result.limits 
        """
        result = self.api.get_usage()
        self.assertTrue(set(result.json.keys()) == self.usage_attributes)
        self.assertTrue(
            set(result.json.limits.keys()) == self.usage_time_attributes)
        self.assertTrue(
            set(result.json.usage.keys()) == self.usage_time_attributes)

    def test_invalid_icon(self):
        """
        Check if getting an icon with an invalid id produces the correct NotFound exception.
        """
        invalid_id = int(1e15)
        with self.assertRaises(NotFound):
            self.api.get_icon(invalid_id)
Exemplo n.º 19
0
class SlugAssert(unittest.TestCase):

    def setUp(self):
        key = "mock api key to satisfy type check in api._get_oauth()"
        secret = "mock secret key to satisfy type check in api._get_oauth()"
        self.api = API(key, secret, testing=True)

    def _test_lop(self, limit=None, offset=None, page=None):
        """
        Helper method to call api's _lop_assert
        """
        self.api._lop_assert(limit, offset, page)

    def test_lop_limit_none(self):
        """
        Test to see that limit as None does not throw an exception
        """
        limit = None
        self._test_lop(limit=limit)

    def test_lop_limit_int(self):
        """
        Test to see that limit as 12 does not throw an exception
        """
        limit = 12
        self._test_lop(limit=limit)

    def test_lop_limit_illegal(self):
        """
        Test to see that limit as "goat" throws an exception
        """
        limit = "goat"
        with self.assertRaises(IncorrectType):
            self._test_lop(limit=limit)

    def test_lop_offset_none(self):
        """
        Test to see that offset as None does not throw an exception
        """
        offset = None
        self._test_lop(offset=offset)

    def test_lop_offset_int(self):
        """
        Test to see that offset as 12 does not throw an exception
        """
        offset = 12
        self._test_lop(offset=offset)

    def test_lop_offset_illegal(self):
        """
        Test to see that offset as "goat" throws an exception
        """
        offset = "goat"
        with self.assertRaises(IncorrectType):
            self._test_lop(offset=offset)

    def test_lop_page_none(self):
        """
        Test to see that page as None does not throw an exception
        """
        page = None
        self._test_lop(page=page)

    def test_lop_page_int(self):
        """
        Test to see that page as 12 does not throw an exception
        """
        page = 12
        self._test_lop(page=page)

    def test_lop_page_illegal(self):
        """
        Test to see that page as "goat" throws an exception
        """
        page = "goat"
        with self.assertRaises(IncorrectType):
            self._test_lop(page=page)