{"action":"created","comment":{"url":"https://api.github.com/repos/edx/credentials/pulls/comments/48515870","id":48515870,"diff_hunk":"@@ -0,0 +1,123 @@\n+\"\"\"\n+Tests for Issuer class.\n+\"\"\"\n+import ddt\n+from django.contrib.contenttypes.models import ContentType\n+from django.db import IntegrityError\n+from django.test import TestCase\n+from testfixtures import LogCapture\n+\n+from credentials.apps.api.tests.factories import ProgramCertificateFactory\n+from credentials.apps.credentials.issuers import ProgramCertificateIssuer\n+from credentials.apps.credentials.models import ProgramCertificate, UserCredential\n+\n+LOGGER_NAME = 'credentials.apps.credentials.issuers'\n+\n+\n+@ddt.ddt\n+class TestProgramCertificateIssuer(TestCase):\n+    \"\"\" Tests for Issuer class and its methods. For incoming credential request\n+    issuer class is responsible to generate the credential. This is atomic operation and\n+    in case of any error data will be roll backed.\n+    \"\"\"\n+    def setUp(self):\n+        super(TestProgramCertificateIssuer, self).setUp()\n+        self.issuer = ProgramCertificateIssuer()\n+        self.program_certificate = ProgramCertificateFactory.create()\n+        self.username = 'tester'\n+        self.data = {\n+            \"program_id\": self.program_certificate.program_id,\n+            \"attributes\": [{\"namespace\": \"whitelist\", \"name\": \"grade\", \"value\": \"0.5\"}]\n+        }\n+\n+    def test_issuer_type(self):\n+        \"\"\" Verify ProgramsCertificateIssuer has valid issuer type. \"\"\"\n+        self.assertEqual(self.issuer.issued_credential_type, ProgramCertificate)\n+\n+    def test_issue_credential(self):\n+        \"\"\" Verify issue_credential issues user credential for given user. \"\"\"\n+        isseued_credential = self.issuer.issue_credential(self.username, **self.data)\n+        db_credential = UserCredential.objects.get(username=self.username)\n+        self._assert_values(isseued_credential, db_credential)\n+\n+        attrs = db_credential.attributes.all()[0]\n+        self.assertEqual(attrs.namespace, 'whitelist')\n+        self.assertEqual(attrs.name, 'grade')\n+        self.assertEqual(attrs.value, '0.5')\n+\n+    def test_issue_credential_with_missing_attributes(self):\n+        \"\"\" Verify issue_credential issues credential even if attributes\n+        dict is empty any field any field is missing.\n+        \"\"\"\n+        # data without namespace and reason.\n+        data = {\n+            \"program_id\": self.program_certificate.program_id,\n+            \"attributes\": [{\"name\": \"Whitelist\"}]\n+        }\n+\n+        isseued_credential = self.issuer.issue_credential(self.username, **data)\n+        db_credential = UserCredential.objects.get(username=self.username)\n+        self._assert_values(isseued_credential, db_credential)\n+        self.assertFalse(db_credential.attributes.exists())\n+\n+    def test_issue_credential_with_duplicate_attributes(self):\n+        \"\"\" Verify issuer raises exception and rolled backed the whole\n+        operation in case of duplicate attributes.\n+        \"\"\"\n+\n+        data = {\n+            \"program_id\": self.program_certificate.program_id,\n+            \"attributes\": [\n+                {\"namespace\": \"whitelist2\", \"name\": \"grade\", \"value\": \"0.5\"},\n+                {\"namespace\": \"whitelist2\", \"name\": \"grade\", \"value\": \"0.5\"}\n+            ]\n+        }\n+        with self.assertRaises(IntegrityError):\n+            self.issuer.issue_credential(self.username, **data)\n+        self.assertFalse(UserCredential.objects.filter(username=self.username).exists())\n+\n+    def test_credential_already_exists(self):\n+        \"\"\" Verify that credential will not be issued if user has already issued credential.\"\"\"\n+        self.issuer.issue_credential(self.username, **self.data)\n+\n+        # Create credentials with same information.\n+        self.issuer.issue_credential(self.username, **self.data)\n+\n+        # Verify only one record exists in database.\n+        self.assertEqual(UserCredential.objects.all().count(), 1)\n+\n+        # Verify log is captured.\n+        msg = 'User [{username}] already has a credential for program [{program_id}].'.format(\n+            username=self.username, program_id=self.program_certificate.program_id)\n+        with LogCapture(LOGGER_NAME) as l:\n+            self.issuer.issue_credential(self.username, **self.data)\n+            l.check((LOGGER_NAME, 'WARNING', msg))\n+\n+    def _assert_values(self, isseued_credential, db_credential):","path":"credentials/apps/credentials/tests/test_issuer.py","position":96,"original_position":96,"commit_id":"48efb2bdb6d81f539ef6c2c198881e22235f3259","original_commit_id":"48efb2bdb6d81f539ef6c2c198881e22235f3259","user":{"login":"clintonb","id":910510,"avatar_url":"https://avatars.githubusercontent.com/u/910510?v=3","gravatar_id":"","url":"https://api.github.com/users/clintonb","html_url":"https://github.com/clintonb","followers_url":"https://api.github.com/users/clintonb/followers","following_url":"https://api.github.com/users/clintonb/following{/other_user}","gists_url":"https://api.github.com/users/clintonb/gists{/gist_id}","starred_url":"https://api.github.com/users/clintonb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clintonb/subscriptions","organizations_url":"https://api.github.com/users/clintonb/orgs","repos_url":"https://api.github.com/users/clintonb/repos","events_url":"https://api.github.com/users/clintonb/events{/privacy}","received_events_url":"https://api.github.com/users/clintonb/received_events","type":"User","site_admin":false},"body":"`issued_credential`","created_at":"2015-12-29T00:52:52Z","updated_at":"2015-12-29T00:52:52Z","html_url":"https://github.com/edx/credentials/pull/2#discussion_r48515870","pull_request_url":"https://api.github.com/repos/edx/credentials/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/edx/credentials/pulls/comments/48515870"},"html":{"href":"https://github.com/edx/credentials/pull/2#discussion_r48515870"},"pull_request":{"href":"https://api.github.com/repos/edx/credentials/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/edx/credentials/pulls/2","id":53697609,"html_url":"https://github.com/edx/credentials/pull/2","diff_url":"https://github.com/edx/credentials/pull/2.diff","patch_url":"https://github.com/edx/credentials/pull/2.patch","issue_url":"https://api.github.com/repos/edx/credentials/issues/2","number":2,"state":"open","locked":false,"title":"User Credentials api","user":{"login":"awais786","id":445320,"avatar_url":"https://avatars.githubusercontent.com/u/445320?v=3","gravatar_id":"","url":"https://api.github.com/users/awais786","html_url":"https://github.com/awais786","followers_url":"https://api.github.com/users/awais786/followers","following_url":"https://api.github.com/users/awais786/following{/other_user}","gists_url":"https://api.github.com/users/awais786/gists{/gist_id}","starred_url":"https://api.github.com/users/awais786/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awais786/subscriptions","organizations_url":"https://api.github.com/users/awais786/orgs","repos_url":"https://api.github.com/users/awais786/repos","events_url":"https://api.github.com/users/awais786/events{/privacy}","received_events_url":"https://api.github.com/users/awais786/received_events","type":"User","site_admin":false},"body":"ECOM-2999\r\n\r\n* Adding the end-points to create the user credentials.\r\n* Patch functionality to update the status.\r\n* Classes implementation.\r\n* /api/v1/credentials/: List all credentials\r\n* /api/v1/credentials/:id/: Retrieve a single credential\r\n* Added filtering to your list endpoint: /api/v1/credentials/?username=<username>&status=<status>.\r\n\r\nReference: https://github.com/edx/edx-platform/commit/84699260e06199b8a41c3765260dc35cf619e4c1\r\n\r\nFiltering for Programs and Courses will be added in separate story [ECOM-3263](https://openedx.atlassian.net/browse/ECOM-3263).","created_at":"2015-12-15T12:36:53Z","updated_at":"2015-12-29T00:52:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f29707c1afe7fc16cacefa4c4b52e3978a582f4e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/credentials/pulls/2/commits","review_comments_url":"https://api.github.com/repos/edx/credentials/pulls/2/comments","review_comment_url":"https://api.github.com/repos/edx/credentials/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/credentials/issues/2/comments","statuses_url":"https://api.github.com/repos/edx/credentials/statuses/48efb2bdb6d81f539ef6c2c198881e22235f3259","head":{"label":"edx:awais786/ECOM-2999-user-credential-apis","ref":"awais786/ECOM-2999-user-credential-apis","sha":"48efb2bdb6d81f539ef6c2c198881e22235f3259","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":47830104,"name":"credentials","full_name":"edx/credentials","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/credentials","description":"Django application used as the backend to support Course or Program based credentials.","fork":false,"url":"https://api.github.com/repos/edx/credentials","forks_url":"https://api.github.com/repos/edx/credentials/forks","keys_url":"https://api.github.com/repos/edx/credentials/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/credentials/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/credentials/teams","hooks_url":"https://api.github.com/repos/edx/credentials/hooks","issue_events_url":"https://api.github.com/repos/edx/credentials/issues/events{/number}","events_url":"https://api.github.com/repos/edx/credentials/events","assignees_url":"https://api.github.com/repos/edx/credentials/assignees{/user}","branches_url":"https://api.github.com/repos/edx/credentials/branches{/branch}","tags_url":"https://api.github.com/repos/edx/credentials/tags","blobs_url":"https://api.github.com/repos/edx/credentials/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/credentials/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/credentials/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/credentials/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/credentials/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/credentials/languages","stargazers_url":"https://api.github.com/repos/edx/credentials/stargazers","contributors_url":"https://api.github.com/repos/edx/credentials/contributors","subscribers_url":"https://api.github.com/repos/edx/credentials/subscribers","subscription_url":"https://api.github.com/repos/edx/credentials/subscription","commits_url":"https://api.github.com/repos/edx/credentials/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/credentials/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/credentials/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/credentials/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/credentials/contents/{+path}","compare_url":"https://api.github.com/repos/edx/credentials/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/credentials/merges","archive_url":"https://api.github.com/repos/edx/credentials/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/credentials/downloads","issues_url":"https://api.github.com/repos/edx/credentials/issues{/number}","pulls_url":"https://api.github.com/repos/edx/credentials/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/credentials/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/credentials/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/credentials/labels{/name}","releases_url":"https://api.github.com/repos/edx/credentials/releases{/id}","created_at":"2015-12-11T14:09:03Z","updated_at":"2015-12-14T20:00:22Z","pushed_at":"2015-12-29T00:35:04Z","git_url":"git://github.com/edx/credentials.git","ssh_url":"git@github.com:edx/credentials.git","clone_url":"https://github.com/edx/credentials.git","svn_url":"https://github.com/edx/credentials","homepage":"http://open-edx-credentials-guide.readthedocs.org/en/latest/ ","size":1918,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"de562a648747e635254ebce127f498ec2e222e5f","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":47830104,"name":"credentials","full_name":"edx/credentials","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/credentials","description":"Django application used as the backend to support Course or Program based credentials.","fork":false,"url":"https://api.github.com/repos/edx/credentials","forks_url":"https://api.github.com/repos/edx/credentials/forks","keys_url":"https://api.github.com/repos/edx/credentials/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/credentials/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/credentials/teams","hooks_url":"https://api.github.com/repos/edx/credentials/hooks","issue_events_url":"https://api.github.com/repos/edx/credentials/issues/events{/number}","events_url":"https://api.github.com/repos/edx/credentials/events","assignees_url":"https://api.github.com/repos/edx/credentials/assignees{/user}","branches_url":"https://api.github.com/repos/edx/credentials/branches{/branch}","tags_url":"https://api.github.com/repos/edx/credentials/tags","blobs_url":"https://api.github.com/repos/edx/credentials/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/credentials/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/credentials/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/credentials/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/credentials/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/credentials/languages","stargazers_url":"https://api.github.com/repos/edx/credentials/stargazers","contributors_url":"https://api.github.com/repos/edx/credentials/contributors","subscribers_url":"https://api.github.com/repos/edx/credentials/subscribers","subscription_url":"https://api.github.com/repos/edx/credentials/subscription","commits_url":"https://api.github.com/repos/edx/credentials/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/credentials/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/credentials/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/credentials/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/credentials/contents/{+path}","compare_url":"https://api.github.com/repos/edx/credentials/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/credentials/merges","archive_url":"https://api.github.com/repos/edx/credentials/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/credentials/downloads","issues_url":"https://api.github.com/repos/edx/credentials/issues{/number}","pulls_url":"https://api.github.com/repos/edx/credentials/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/credentials/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/credentials/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/credentials/labels{/name}","releases_url":"https://api.github.com/repos/edx/credentials/releases{/id}","created_at":"2015-12-11T14:09:03Z","updated_at":"2015-12-14T20:00:22Z","pushed_at":"2015-12-29T00:35:04Z","git_url":"git://github.com/edx/credentials.git","ssh_url":"git@github.com:edx/credentials.git","clone_url":"https://github.com/edx/credentials.git","svn_url":"https://github.com/edx/credentials","homepage":"http://open-edx-credentials-guide.readthedocs.org/en/latest/ ","size":1918,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/credentials/pulls/2"},"html":{"href":"https://github.com/edx/credentials/pull/2"},"issue":{"href":"https://api.github.com/repos/edx/credentials/issues/2"},"comments":{"href":"https://api.github.com/repos/edx/credentials/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/credentials/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/credentials/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/credentials/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/edx/credentials/statuses/48efb2bdb6d81f539ef6c2c198881e22235f3259"}}}}