{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31026196","id":31026196,"diff_hunk":"@@ -0,0 +1,175 @@\n+\"\"\"\n+Tests for bookmarks api.\n+\"\"\"\n+\n+from django.core.exceptions import ObjectDoesNotExist\n+\n+from opaque_keys.edx.keys import UsageKey\n+\n+from .. import api, DEFAULT_FIELDS, OPTIONAL_FIELDS\n+from .factories import BookmarkFactory\n+\n+from student.tests.factories import UserFactory\n+\n+from xmodule.modulestore.exceptions import ItemNotFoundError\n+from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory\n+from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase\n+\n+\n+class BookmarksAPITests(ModuleStoreTestCase):\n+    \"\"\"\n+    These tests cover the parts of the API methods.\n+    \"\"\"\n+\n+    def setUp(self):\n+        super(BookmarksAPITests, self).setUp()\n+\n+        self.user = UserFactory.create(password='test')\n+        self.other_user = UserFactory.create(password='test')\n+\n+        self.course = CourseFactory.create(display_name='An Introduction to API Testing')\n+        self.course_id = unicode(self.course.id)\n+\n+        self.chapter = ItemFactory.create(\n+            parent_location=self.course.location, category='chapter', display_name='Week 1'\n+        )\n+        self.sequential = ItemFactory.create(\n+            parent_location=self.chapter.location, category='sequential', display_name='Lesson 1'\n+        )\n+        self.vertical = ItemFactory.create(\n+            parent_location=self.sequential.location, category='vertical', display_name='Subsection 1'\n+        )\n+        self.vertical_1 = ItemFactory.create(\n+            parent_location=self.sequential.location, category='vertical', display_name='Subsection 1.1'\n+        )\n+        self.bookmark = BookmarkFactory.create(\n+            user=self.user,\n+            course_key=self.course_id,\n+            usage_key=self.vertical.location,\n+            display_name=self.vertical.display_name\n+        )\n+\n+        self.course_2 = CourseFactory.create(display_name='An Introduction to API Testing 2')\n+        self.chapter_2 = ItemFactory.create(\n+            parent_location=self.course_2.location, category='chapter', display_name='Week 2'\n+        )\n+        self.sequential_2 = ItemFactory.create(\n+            parent_location=self.chapter_2.location, category='sequential', display_name='Lesson 2'\n+        )\n+        self.vertical_2 = ItemFactory.create(\n+            parent_location=self.sequential_2.location, category='vertical', display_name='Subsection 2'\n+        )\n+        self.bookmark_2 = BookmarkFactory.create(\n+            user=self.user,\n+            course_key=self.course_2.id,\n+            usage_key=self.vertical_2.location,\n+            display_name=self.vertical_2.display_name\n+        )\n+        self.all_fields = DEFAULT_FIELDS + OPTIONAL_FIELDS\n+\n+    def assert_bookmark_response(self, response_data, bookmark, optional_fields=False):\n+        \"\"\"\n+        Determines if the given response data (dict) matches the given bookmark.\n+        \"\"\"\n+        self.assertEqual(response_data['id'], '%s,%s' % (self.user.username, unicode(bookmark.usage_key)))\n+        self.assertEqual(response_data['course_id'], unicode(bookmark.course_key))\n+        self.assertEqual(response_data['usage_id'], unicode(bookmark.usage_key))\n+        self.assertIsNotNone(response_data['created'])\n+\n+        if optional_fields:\n+            self.assertEqual(response_data['display_name'], bookmark.display_name)\n+            self.assertEqual(response_data['path'], bookmark.path)\n+\n+    def test_get_bookmark(self):\n+        \"\"\"\n+        Verifies that get_bookmark returns data as expected.\n+        \"\"\"\n+        bookmark_data = api.get_bookmark(user=self.user, usage_key=self.vertical.location)\n+        self.assert_bookmark_response(bookmark_data, self.bookmark)\n+\n+        # With Optional fields.\n+        bookmark_data = api.get_bookmark(\n+            user=self.user,\n+            usage_key=self.vertical.location,\n+            fields=self.all_fields\n+        )\n+        self.assert_bookmark_response(bookmark_data, self.bookmark, optional_fields=True)\n+\n+    def test_get_bookmark_raises_error(self):\n+        \"\"\"\n+        Verifies that get_bookmark raises error as expected.\n+        \"\"\"\n+        with self.assertRaises(ObjectDoesNotExist):\n+            api.get_bookmark(user=self.other_user, usage_key=self.vertical.location)\n+\n+    def test_get_bookmarks(self):\n+        \"\"\"\n+        Verifies that get_bookmarks returns data as expected.\n+        \"\"\"\n+        # Without course key.\n+        bookmarks_data = api.get_bookmarks(user=self.user)\n+        self.assertEqual(len(bookmarks_data), 2)\n+        # Assert them in ordered manner.\n+        self.assert_bookmark_response(bookmarks_data[0], self.bookmark_2)\n+        self.assert_bookmark_response(bookmarks_data[1], self.bookmark)\n+\n+        # With course key.\n+        bookmarks_data = api.get_bookmarks(user=self.user, course_key=self.course.id)\n+        self.assertEqual(len(bookmarks_data), 1)\n+        self.assert_bookmark_response(bookmarks_data[0], self.bookmark)\n+\n+        # With optional fields.\n+        bookmarks_data = api.get_bookmarks(user=self.user, course_key=self.course.id, fields=self.all_fields)\n+        self.assertEqual(len(bookmarks_data), 1)\n+        self.assert_bookmark_response(bookmarks_data[0], self.bookmark, optional_fields=True)\n+\n+        # Without Serialized.\n+        bookmarks = api.get_bookmarks(user=self.user, course_key=self.course.id, serialized=False)\n+        self.assertEqual(len(bookmarks), 1)\n+        self.assertEqual(bookmarks[0], self.bookmark)\n+\n+    def test_create_bookmark(self):\n+        \"\"\"\n+        Verifies that create_bookmark create & returns data as expected.\n+        \"\"\"\n+        self.assertEqual(len(api.get_bookmarks(user=self.user, course_key=self.course.id)), 1)\n+\n+        api.create_bookmark(user=self.user, usage_key=self.vertical_1.location)\n+\n+        self.assertEqual(len(api.get_bookmarks(user=self.user, course_key=self.course.id)), 2)\n+\n+    def test_create_bookmark_do_not_create_duplicates(self):\n+        \"\"\"\n+        Verifies that create_bookmark do not create duplicate bookmarks.\n+        \"\"\"\n+        self.assertEqual(len(api.get_bookmarks(user=self.user, course_key=self.course.id)), 1)\n+        api.create_bookmark(user=self.user, usage_key=self.vertical_1.location)\n+\n+        self.assertEqual(len(api.get_bookmarks(user=self.user, course_key=self.course.id)), 2)\n+\n+        api.create_bookmark(user=self.user, usage_key=self.vertical_1.location)\n+        self.assertEqual(len(api.get_bookmarks(user=self.user, course_key=self.course.id)), 2)\n+\n+    def test_create_bookmark_raises_error(self):\n+        \"\"\"\n+        Verifies that create_bookmark raises error as expected.\n+        \"\"\"\n+        with self.assertRaises(ItemNotFoundError):\n+            api.create_bookmark(user=self.user, usage_key=UsageKey.from_string('i4x://brb/100/html/340ef1771a0940'))\n+\n+    def test_delete_bookmark(self):\n+        \"\"\"\n+        Verifies that delete_bookmark removes bookmark as expected.\n+        \"\"\"\n+        self.assertEqual(len(api.get_bookmarks(user=self.user)), 2)\n+\n+        api.delete_bookmark(user=self.user, usage_key=self.vertical.location)\n+\n+        self.assertEqual(len(api.get_bookmarks(user=self.user)), 1)","path":"lms/djangoapps/bookmarks/tests/test_api.py","position":168,"original_position":168,"commit_id":"839387de772f4e28f6710fce6546a434e53a71a6","original_commit_id":"839387de772f4e28f6710fce6546a434e53a71a6","user":{"login":"symbolist","id":5305552,"avatar_url":"https://avatars.githubusercontent.com/u/5305552?v=3","gravatar_id":"","url":"https://api.github.com/users/symbolist","html_url":"https://github.com/symbolist","followers_url":"https://api.github.com/users/symbolist/followers","following_url":"https://api.github.com/users/symbolist/following{/other_user}","gists_url":"https://api.github.com/users/symbolist/gists{/gist_id}","starred_url":"https://api.github.com/users/symbolist/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/symbolist/subscriptions","organizations_url":"https://api.github.com/users/symbolist/orgs","repos_url":"https://api.github.com/users/symbolist/repos","events_url":"https://api.github.com/users/symbolist/events{/privacy}","received_events_url":"https://api.github.com/users/symbolist/received_events","type":"User","site_admin":false},"body":"Also check that the remaining bookmark's usage_id is different from the one which was deleted.","created_at":"2015-05-26T12:03:35Z","updated_at":"2015-05-26T12:03:35Z","html_url":"https://github.com/edx/edx-platform/pull/8082#discussion_r31026196","pull_request_url":"https://api.github.com/repos/edx/edx-platform/pulls/8082","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments/31026196"},"html":{"href":"https://github.com/edx/edx-platform/pull/8082#discussion_r31026196"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8082"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-platform/pulls/8082","id":35614489,"html_url":"https://github.com/edx/edx-platform/pull/8082","diff_url":"https://github.com/edx/edx-platform/pull/8082.diff","patch_url":"https://github.com/edx/edx-platform/pull/8082.patch","issue_url":"https://api.github.com/repos/edx/edx-platform/issues/8082","number":8082,"state":"open","locked":false,"title":"Bookmarks API implementation","user":{"login":"muzaffaryousaf","id":2994509,"avatar_url":"https://avatars.githubusercontent.com/u/2994509?v=3","gravatar_id":"","url":"https://api.github.com/users/muzaffaryousaf","html_url":"https://github.com/muzaffaryousaf","followers_url":"https://api.github.com/users/muzaffaryousaf/followers","following_url":"https://api.github.com/users/muzaffaryousaf/following{/other_user}","gists_url":"https://api.github.com/users/muzaffaryousaf/gists{/gist_id}","starred_url":"https://api.github.com/users/muzaffaryousaf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/muzaffaryousaf/subscriptions","organizations_url":"https://api.github.com/users/muzaffaryousaf/orgs","repos_url":"https://api.github.com/users/muzaffaryousaf/repos","events_url":"https://api.github.com/users/muzaffaryousaf/events{/privacy}","received_events_url":"https://api.github.com/users/muzaffaryousaf/received_events","type":"User","site_admin":false},"body":"Jira ticket: https://openedx.atlassian.net/browse/TNL-1945\r\n\r\nSpec: https://openedx.atlassian.net/wiki/display/TNL/Bookmarks+API","created_at":"2015-05-18T09:08:10Z","updated_at":"2015-05-26T12:03:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"646e46c0ba7da07459ccbeb5ef38756c84fce3fd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-platform/pulls/8082/commits","review_comments_url":"https://api.github.com/repos/edx/edx-platform/pulls/8082/comments","review_comment_url":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/edx-platform/issues/8082/comments","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/839387de772f4e28f6710fce6546a434e53a71a6","head":{"label":"edx:muzaffar/tnl1945-bookmarks-api","ref":"muzaffar/tnl1945-bookmarks-api","sha":"839387de772f4e28f6710fce6546a434e53a71a6","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":10391073,"name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-platform","description":"Open edX, the edX platform that powers http://edx.org","fork":false,"url":"https://api.github.com/repos/edx/edx-platform","forks_url":"https://api.github.com/repos/edx/edx-platform/forks","keys_url":"https://api.github.com/repos/edx/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-platform/teams","hooks_url":"https://api.github.com/repos/edx/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-platform/events","assignees_url":"https://api.github.com/repos/edx/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-platform/tags","blobs_url":"https://api.github.com/repos/edx/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-platform/languages","stargazers_url":"https://api.github.com/repos/edx/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-platform/subscription","commits_url":"https://api.github.com/repos/edx/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-platform/merges","archive_url":"https://api.github.com/repos/edx/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-platform/downloads","issues_url":"https://api.github.com/repos/edx/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-platform/releases{/id}","created_at":"2013-05-30T20:20:38Z","updated_at":"2015-05-26T08:30:38Z","pushed_at":"2015-05-26T12:03:33Z","git_url":"git://github.com/edx/edx-platform.git","ssh_url":"git@github.com:edx/edx-platform.git","clone_url":"https://github.com/edx/edx-platform.git","svn_url":"https://github.com/edx/edx-platform","homepage":"http://openedx.org/","size":1955403,"stargazers_count":1943,"watchers_count":1943,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1119,"mirror_url":null,"open_issues_count":178,"forks":1119,"open_issues":178,"watchers":1943,"default_branch":"master"}},"base":{"label":"edx:unit-bookmarking","ref":"unit-bookmarking","sha":"930ccd633256353fb421e522ea24f32144e8abf1","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":10391073,"name":"edx-platform","full_name":"edx/edx-platform","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-platform","description":"Open edX, the edX platform that powers http://edx.org","fork":false,"url":"https://api.github.com/repos/edx/edx-platform","forks_url":"https://api.github.com/repos/edx/edx-platform/forks","keys_url":"https://api.github.com/repos/edx/edx-platform/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-platform/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-platform/teams","hooks_url":"https://api.github.com/repos/edx/edx-platform/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-platform/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-platform/events","assignees_url":"https://api.github.com/repos/edx/edx-platform/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-platform/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-platform/tags","blobs_url":"https://api.github.com/repos/edx/edx-platform/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-platform/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-platform/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-platform/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-platform/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-platform/languages","stargazers_url":"https://api.github.com/repos/edx/edx-platform/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-platform/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-platform/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-platform/subscription","commits_url":"https://api.github.com/repos/edx/edx-platform/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-platform/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-platform/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-platform/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-platform/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-platform/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-platform/merges","archive_url":"https://api.github.com/repos/edx/edx-platform/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-platform/downloads","issues_url":"https://api.github.com/repos/edx/edx-platform/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-platform/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-platform/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-platform/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-platform/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-platform/releases{/id}","created_at":"2013-05-30T20:20:38Z","updated_at":"2015-05-26T08:30:38Z","pushed_at":"2015-05-26T12:03:33Z","git_url":"git://github.com/edx/edx-platform.git","ssh_url":"git@github.com:edx/edx-platform.git","clone_url":"https://github.com/edx/edx-platform.git","svn_url":"https://github.com/edx/edx-platform","homepage":"http://openedx.org/","size":1955403,"stargazers_count":1943,"watchers_count":1943,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1119,"mirror_url":null,"open_issues_count":178,"forks":1119,"open_issues":178,"watchers":1943,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8082"},"html":{"href":"https://github.com/edx/edx-platform/pull/8082"},"issue":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8082"},"comments":{"href":"https://api.github.com/repos/edx/edx-platform/issues/8082/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8082/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/edx-platform/pulls/8082/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-platform/statuses/839387de772f4e28f6710fce6546a434e53a71a6"}}}}