def test_that_caching_happens(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[]) g.get_group("group name") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }]) # this triggers a second call because we're not using the cache. g.get_group("group name") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }]) # if we use cache=True this won't make another call. g.get_group("group name", cache=True) self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }])
def test_http_post(self, g): bundle = g.bundle("http") responses.add(responses.POST, "https://www.example.com/post1", body="post1") post1 = bundle.http_post("https://www.example.com/post1", data={}, cache=False) self.assertEqual(post1, "post1") responses.add(responses.POST, "https://www.example.com/post2", body="post2") post2 = bundle.http_post("https://www.example.com/post2", data=["a"], cache=False) self.assertEqual(post2, "post2") # make the same call again but since cache=True, it won't make a call. responses.add(responses.POST, "https://www.example.com/post2", body="post2") post2 = bundle.http_post("https://www.example.com/post2", data=["a"], cache=True) self.assertEqual(post2, "post2") self.assertEqual(get_calls(), [{ "method": "POST", "url": "https://www.example.com/post1", "body": {} }, { "method": "POST", "url": "https://www.example.com/post2", "body": ["a"] }])
def test_get_tags(self, g): # register the response for the API call we'll make. responses.add(responses.GET, "https://api.getguru.com/api/v1/whoami", json={ "team": { "id": "abcd" } }) responses.add(responses.GET, "https://api.getguru.com/api/v1/teams/abcd/tagcategories", json=[{ "id": "0000", "tags": [{ "id": "1111", "value": "case study" }, { "id": "2222", "value": "troubleshooting" }] }]) tags = g.get_tags() # assert that the only API activity was the one call we expected to see. self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/whoami" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/teams/abcd/tagcategories" }]) # do assertions about the objects we get back. self.assertEqual(len(tags), 2) self.assertEqual(tags[0].id, "1111") self.assertEqual(tags[0].value, "case study") self.assertEqual(tags[1].id, "2222") self.assertEqual(tags[1].value, "troubleshooting")
def test_pagination_on_get_calls(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/members?search=", json=[{}, {}, {}, {}, {}], headers={ "Link": "< https://api.getguru.com/api/v1/members?token=1>" }) responses.add(responses.GET, "https://api.getguru.com/api/v1/members?token=1", json=[{}, {}, {}, {}], headers={ "Link": "< https://api.getguru.com/api/v1/members?token=2>" }) responses.add(responses.GET, "https://api.getguru.com/api/v1/members?token=2", json=[{}, {}]) result = g.get_members() self.assertEqual(len(result), 11) self.assertEqual( get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/members?token=1" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/members?token=2" }])
def test_merge_invalid_tag(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/whoami", json={ "team": { "id": "abcd" } }) responses.add(responses.GET, "https://api.getguru.com/api/v1/teams/abcd/tagcategories", json=[{ "id": "0000", "tags": [{ "id": "1111", "value": "case study" }, { "id": "2222", "value": "troubleshooting" }] }]) tags = g.get_tags() g.merge_tags("case study", "3333") # assert that the only API activity was the one call we expected to see. self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/whoami" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/teams/abcd/tagcategories" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/teams/abcd/tagcategories" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/teams/abcd/tagcategories" }])
def test_delete_invalid_tag(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/whoami", json={ "team": { "id": "abcd" } }) responses.add(responses.GET, "https://api.getguru.com/api/v1/teams/abcd/tagcategories", json=[{ "id": "0000", "tags": [{ "id": "1111", "value": "case study" }] }]) result = g.delete_tag("3333") # assert that the only API activity was the one call we expected to see. self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/whoami" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/teams/abcd/tagcategories" }]) self.assertEqual(result, False)
def test_remove_user_from_group(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1234", "name": "Experts" }]) responses.add( responses.DELETE, "https://api.getguru.com/api/v1/groups/1234/members/[email protected]" ) g.remove_user_from_group("*****@*****.**", "Experts") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "DELETE", "url": "https://api.getguru.com/api/v1/groups/1234/members/[email protected]" }])
def test_remove_user_from_groups_with_invalid_group(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1234", "name": "Experts" }]) responses.add( responses.DELETE, "https://api.getguru.com/api/v1/groups/1234/members/[email protected]" ) result = g.remove_user_from_groups("*****@*****.**", "Experts", "other group") self.assertEqual(result, {"Experts": True, "other group": False}) self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "DELETE", "url": "https://api.getguru.com/api/v1/groups/1234/members/[email protected]" }])
def test_add_users_to_group(self, g): users = ["*****@*****.**", "*****@*****.**", "*****@*****.**"] responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1234", "name": "Experts" }]) responses.add(responses.POST, "https://api.getguru.com/api/v1/groups/1234/members", json=[{ "id": email } for email in users]) results = g.add_users_to_group(users, "Experts") self.assertEqual(results, {email: True for email in users}) self.assertEqual( get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "POST", "url": "https://api.getguru.com/api/v1/groups/1234/members", "body": users }])
def test_add_light_user_to_group(self, g): responses.add( responses.GET, "https://api.getguru.com/api/v1/members?search=user%40example.com", json=[{ "user": { "email": "*****@*****.**" }, "userAttributes": { "BILLING_TYPE": "FREE", "ACCESS_TYPE": "READ_ONLY" } }]) responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1234", "name": "Experts" }]) responses.add(responses.POST, "https://api.getguru.com/api/v1/groups/1234/members") result = g.add_user_to_group("*****@*****.**", "Experts") self.assertIsNone(result) self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=user%40example.com" }])
def test_add_group_to_collection(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "5678", "name": "Experts" }]) responses.add(responses.GET, "https://api.getguru.com/api/v1/collections", json=[{ "id": "abcd", "name": "General" }]) responses.add(responses.POST, "https://api.getguru.com/api/v1/collections/abcd/groups", json={}) g.add_group_to_collection("Experts", "General", guru.AUTHOR) # this makes get calls to look up the group and collection by name, then # a post call to add the group to the collection. self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/collections" }, { "method": "POST", "url": "https://api.getguru.com/api/v1/collections/abcd/groups", "body": { "groupId": "5678", "role": "AUTHOR" } }])
def test_make_collection(self, g): # make_collection() will look up the group by name so we need this to return something. responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1234", "name": "All Members" }]) responses.add(responses.POST, "https://api.getguru.com/api/v1/collections", json={}) g.make_collection("Test") self.assertEqual(get_calls(), [ { "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "POST", "url": "https://api.getguru.com/api/v1/collections", "body": { "name": "Test", "color": "#009688", "description": "", "collectionType": "INTERNAL", "publicCardsEnabled": True, "syncVerificationEnabled": False, "initialAdminGroupId": "1234" } } ])
def test_add_user_to_invalid_group(self, g): responses.add( responses.GET, "https://api.getguru.com/api/v1/members?search=user%40example.com", json=[{ "user": { "email": "*****@*****.**" }, "groups": [], "userAttributes": { "BILLING_TYPE": "CORE", "ACCESS_TYPE": "CORE" } }]) responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1234", "name": "Other Group" }]) g.add_user_to_group("*****@*****.**", "Experts") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=user%40example.com" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }])
def test_deleting_a_draft(self, g): # register the response for the API call we'll make. responses.add(responses.GET, "https://api.getguru.com/api/v1/drafts", json=[{ "content": "test", "title": "new card", "id": "abcd", "user": { "email": "*****@*****.**" }, "jsonContent": "{}", "saveType": "AUTO" }]) responses.add(responses.DELETE, "https://api.getguru.com/api/v1/drafts/abcd", status=204) # load your drafts then delete one. drafts = g.get_drafts() g.delete_draft(drafts[0]) # assert that the only API activity was the one call we expected to see. self.assertEqual( get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/drafts" }, { "method": "DELETE", "url": "https://api.getguru.com/api/v1/drafts/abcd" }])
def test_get_drafts_for_one_card(self, g): # register the response for the API call we'll make. responses.add(responses.GET, "https://api.getguru.com/api/v1/cards/1111/extended", json={"id": "1111"}) responses.add(responses.GET, "https://api.getguru.com/api/v1/drafts/1111", json=[{ "content": "test", "title": "new card", "id": "abcd", "user": { "email": "*****@*****.**" }, "jsonContent": "{}", "saveType": "AUTO" }]) # this should trigger the GET call we're expecting. g.get_drafts(card="1111") # assert that the only API activity was the one call we expected to see. self.assertEqual( get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/cards/1111/extended", }, { "method": "GET", "url": "https://api.getguru.com/api/v1/drafts/1111" }])
def test_invite_user_and_add_to_groups(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1234", "name": "Experts" }, { "id": "5678", "name": "other group" }]) responses.add(responses.POST, "https://api.getguru.com/api/v1/members/invite", json={}) responses.add( responses.GET, "https://api.getguru.com/api/v1/members?search=user%40example.com", json=[{ "user": { "email": "*****@*****.**" }, "groups": [], "userAttributes": { "BILLING_TYPE": "CORE", "ACCESS_TYPE": "CORE" } }]) responses.add(responses.POST, "https://api.getguru.com/api/v1/groups/1234/members") responses.add(responses.POST, "https://api.getguru.com/api/v1/groups/5678/members") g.invite_user("*****@*****.**", "Experts", "other group") self.assertEqual(get_calls(), [{ "method": "POST", "url": "https://api.getguru.com/api/v1/members/invite", "body": { "emails": "*****@*****.**", "teamMemberType": "CORE" } }, { "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=user%40example.com" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "POST", "url": "https://api.getguru.com/api/v1/groups/1234/members", "body": ["*****@*****.**"] }, { "method": "POST", "url": "https://api.getguru.com/api/v1/groups/5678/members", "body": ["*****@*****.**"] }])
def test_downgrade_core_user(self, g): responses.add( responses.GET, "https://api.getguru.com/api/v1/members?search=core%40example.com", json=[{ "id": "*****@*****.**", "user": { "email": "*****@*****.**" }, "groups": [], "userAttributes": { "BILLING_TYPE": "CORE", "ACCESS_TYPE": "CORE" } }]) responses.add( responses.GET, "https://api.getguru.com/api/v1/members?search=light%40example.com", json=[{ "id": "*****@*****.**", "user": { "email": "*****@*****.**" }, "userAttributes": { "BILLING_TYPE": "FREE", "ACCESS_TYPE": "READ_ONLY" } }]) responses.add( responses.POST, "https://api.getguru.com/api/v1/members/[email protected]/downgrade", json={}) responses.add( responses.POST, "https://api.getguru.com/api/v1/members/[email protected]/downgrade", json={}) core_user_result = g.downgrade_core_user("*****@*****.**") light_user_result = g.downgrade_core_user("*****@*****.**") self.assertEqual(core_user_result, True) self.assertIsNone(light_user_result) self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=core%40example.com" }, { "method": "POST", "url": "https://api.getguru.com/api/v1/members/[email protected]/downgrade", "body": {} }, { "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=light%40example.com" }])
def test_get_collection_by_id(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/collections/11111111-1111-1111-1111-111111111111", json={}) g.get_collection("11111111-1111-1111-1111-111111111111") self.assertEqual(get_calls(), [ { "method": "GET", "url": "https://api.getguru.com/api/v1/collections/11111111-1111-1111-1111-111111111111" } ])
def test_upload_content_to_invalid_collection(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/collections", json=[]) g.upload_content("General", "test.zip", "test.zip") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/collections" }])
def test_delete_invalid_group(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[]) g.delete_group("new group") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups", }])
def test_get_group(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[]) g.get_group("group name") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }])
def test_upload_content(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/collections", json=[{ "id": "1234", "name": "General" }]) responses.add(responses.POST, "https://api.getguru.com/app/contentupload?collectionId=1234", json={}) g.upload_content("General", "test.zip", "./tests/test.zip") post_body = get_calls()[1]["body"] self.assertEqual(b'Content-Disposition: form-data; name="contentFile"; filename="test.zip"\r\nContent-Type: application/zip\r\n\r\nzip file\r\n--' in post_body, True) self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/collections" }, { "method": "POST", "url": "https://api.getguru.com/app/contentupload?collectionId=1234", "body": post_body }])
def test_delete_collection_with_invalid_collection(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/collections", json=[]) responses.add(responses.DELETE, "https://api.getguru.com/api/v1/collections/abcd") g.delete_collection("General") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/collections" }])
def test_make_group_with_duplicate_name(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "name": "new group" }]) g.make_group("new group") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }])
def test_make_collection_with_missing_group(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[]) g.make_collection("Test") # this makes a GET call to look for the group called 'Test' # but it doesn't make the POST call because it doesn't find the group. self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }])
def test_add_users_to_invalid_group(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[]) g.add_users_to_group([], "Experts") self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }])
def test_get_questions_sent(self, g): # register the response for the API call we'll make. responses.add(responses.GET, "https://api.getguru.com/api/v1/tasks/questions?filter=SENT", json=[]) # the test that gets your inbox checks that we parse questions # and return the correct response, so this test just needs to # check that we correctly pass filter=SENT for this call. g.get_questions_sent() self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/tasks/questions?filter=SENT" }])
def test_get_members(self, g): responses.add(responses.GET, "https://api.getguru.com/api/v1/members?search=", json=[]) g.get_members() self.assertEqual( get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=" }])
def test_add_user_to_groups_with_invalid_user(self, g): responses.add( responses.GET, "https://api.getguru.com/api/v1/members?search=invalid%40example.com", json=[]) result = g.add_user_to_groups("*****@*****.**", "Experts") self.assertEqual(result, None) self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=invalid%40example.com" }])
def test_add_user_to_groups_where_the_user_is_already_in_one_group( self, g): responses.add( responses.GET, "https://api.getguru.com/api/v1/members?search=user%40example.com", json=[{ "user": { "email": "*****@*****.**" }, "groups": [{ "id": "1111", "name": "Experts" }], "userAttributes": { "BILLING_TYPE": "CORE", "ACCESS_TYPE": "CORE" } }]) responses.add(responses.GET, "https://api.getguru.com/api/v1/groups", json=[{ "id": "1111", "name": "Experts" }, { "id": "2222", "name": "other group" }]) responses.add(responses.POST, "https://api.getguru.com/api/v1/groups/2222/members") result = g.add_user_to_groups("*****@*****.**", "Experts", "other group") self.assertEqual(result, {"Experts": True, "other group": True}) self.assertEqual(get_calls(), [{ "method": "GET", "url": "https://api.getguru.com/api/v1/members?search=user%40example.com" }, { "method": "GET", "url": "https://api.getguru.com/api/v1/groups" }, { "method": "POST", "url": "https://api.getguru.com/api/v1/groups/2222/members", "body": ["*****@*****.**"] }])