def test_post_with_missing_data(self): """Testing the POST review-requests/<id>/draft/diffs/ API with Invalid Form Data """ repository = self.create_repository(tool_name='Test') review_request = self.create_review_request(repository=repository, submitter=self.user) rsp = self.api_post(get_draft_diff_list_url(review_request), expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertIn('path', rsp['fields']) # Now test with a valid path and an invalid basedir. # This is necessary because basedir is "optional" as defined by # the resource, but may be required by the form that processes the # diff. review_request = self.create_review_request(repository=repository, submitter=self.user) diff = SimpleUploadedFile('diff', self.DEFAULT_GIT_README_DIFF, content_type='text/x-patch') rsp = self.api_post(get_draft_diff_list_url(review_request), {'path': diff}, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertIn('basedir', rsp['fields'])
def test_post_with_missing_data(self): """Testing the POST review-requests/<id>/draft/diffs/ API with Invalid Form Data """ repository = self.create_repository(tool_name="Test") review_request = self.create_review_request(repository=repository, submitter=self.user) rsp = self.api_post(get_draft_diff_list_url(review_request), expected_status=400) self.assertEqual(rsp["stat"], "fail") self.assertEqual(rsp["err"]["code"], INVALID_FORM_DATA.code) self.assertIn("path", rsp["fields"]) # Now test with a valid path and an invalid basedir. # This is necessary because basedir is "optional" as defined by # the resource, but may be required by the form that processes the # diff. review_request = self.create_review_request(repository=repository, submitter=self.user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), "testdata", "git_readme.diff") with open(diff_filename, "r") as f: rsp = self.api_post(get_draft_diff_list_url(review_request), {"path": f}, expected_status=400) self.assertEqual(rsp["stat"], "fail") self.assertEqual(rsp["err"]["code"], INVALID_FORM_DATA.code) self.assertIn("basedir", rsp["fields"])
def test_post_with_missing_data(self): """Testing the POST review-requests/<id>/draft/diffs/ API with Invalid Form Data """ repository = self.create_repository(tool_name='Test') review_request = self.create_review_request(repository=repository, submitter=self.user) rsp = self.apiPost(get_draft_diff_list_url(review_request), expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertTrue('path' in rsp['fields']) # Now test with a valid path and an invalid basedir. # This is necessary because basedir is "optional" as defined by # the resource, but may be required by the form that processes the # diff. review_request = self.create_review_request(repository=repository, submitter=self.user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') with open(diff_filename, 'r') as f: rsp = self.apiPost( get_draft_diff_list_url(review_request), {'path': f}, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertTrue('basedir' in rsp['fields'])
def test_post_with_missing_data(self): """Testing the POST review-requests/<id>/draft/diffs/ API with Invalid Form Data """ repository = self.create_repository(tool_name='Test') review_request = self.create_review_request(repository=repository, submitter=self.user) rsp = self.apiPost(get_draft_diff_list_url(review_request), expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertTrue('path' in rsp['fields']) # Now test with a valid path and an invalid basedir. # This is necessary because basedir is "optional" as defined by # the resource, but may be required by the form that processes the # diff. review_request = self.create_review_request(repository=repository, submitter=self.user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') with open(diff_filename, 'r') as f: rsp = self.apiPost(get_draft_diff_list_url(review_request), {'path': f}, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertTrue('basedir' in rsp['fields'])
def test_post_diffs_too_big(self): """Testing the POST review-requests/<id>/draft/diffs/ API with diff exceeding max size """ repository = self.create_repository() self.siteconfig.set('diffviewer_max_diff_size', 2) self.siteconfig.save() review_request = self.create_review_request(repository=repository, submitter=self.user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') with open(diff_filename, 'r') as f: rsp = self.apiPost( get_draft_diff_list_url(review_request), { 'path': f, 'basedir': "/trunk", }, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], DIFF_TOO_BIG.code) self.assertTrue('reason' in rsp) self.assertTrue('max_size' in rsp) self.assertEqual(rsp['max_size'], self.siteconfig.get('diffviewer_max_diff_size'))
def test_post_with_site(self): """Testing the POST review-requests/<id>/draft/diffs/ API with a local site """ user = self._login_user(local_site=True) repository = self.create_repository(with_local_site=True, tool_name='Test') review_request = self.create_review_request(with_local_site=True, repository=repository, submitter=user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') with open(diff_filename, 'r') as f: rsp = self.apiPost( get_draft_diff_list_url(review_request, self.local_site_name), { 'path': f, 'basedir': '/trunk', }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual(rsp['diff']['name'], 'git_readme.diff')
def test_post(self): """Testing the POST review-requests/<id>/draft/diffs/ API""" repository = self.create_repository(tool_name='Test') review_request = self.create_review_request(repository=repository, submitter=self.user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') with open(diff_filename, 'r') as f: rsp = self.apiPost( get_draft_diff_list_url(review_request), { 'path': f, 'basedir': '/trunk', 'base_commit_id': '1234', }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual(rsp['diff']['basedir'], '/trunk') self.assertEqual(rsp['diff']['base_commit_id'], '1234') diffset = DiffSet.objects.get(pk=rsp['diff']['id']) self.assertEqual(diffset.basedir, '/trunk') self.assertEqual(diffset.base_commit_id, '1234')
def test_post_adds_default_reviewers(self): """Testing the POST <URL> API adds default reviewers""" review_request = self.create_review_request(submitter=self.user, create_repository=True) group = self.create_review_group(name='group1') default_reviewer = DefaultReviewer.objects.create(name='default1', file_regex='.') default_reviewer.groups.add(group) default_reviewer.repository.add(review_request.repository) diff = SimpleUploadedFile('diff', self.DEFAULT_GIT_README_DIFF, content_type='text/x-patch') rsp = self.api_post(get_draft_diff_list_url(review_request), { 'path': diff, 'basedir': '/trunk', 'base_commit_id': '1234', }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') draft = review_request.get_draft() self.assertEqual(list(draft.target_groups.all()), [group])
def test_post_diffs_too_big(self): """Testing the POST review-requests/<id>/draft/diffs/ API with diff exceeding max size """ repository = self.create_repository() self.siteconfig.set('diffviewer_max_diff_size', 2) self.siteconfig.save() review_request = self.create_review_request(repository=repository, submitter=self.user) diff = SimpleUploadedFile('diff', self.DEFAULT_GIT_README_DIFF, content_type='text/x-patch') rsp = self.api_post(get_draft_diff_list_url(review_request), { 'path': diff, 'basedir': "/trunk", }, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], DIFF_TOO_BIG.code) self.assertIn('reason', rsp) self.assertIn('max_size', rsp) self.assertEqual(rsp['max_size'], self.siteconfig.get('diffviewer_max_diff_size'))
def test_post_diffs_too_big(self): """Testing the POST review-requests/<id>/draft/diffs/ API with diff exceeding max size """ repository = self.create_repository() self.siteconfig.set('diffviewer_max_diff_size', 2) self.siteconfig.save() review_request = self.create_review_request(repository=repository, submitter=self.user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') with open(diff_filename, 'r') as f: rsp = self.apiPost(get_draft_diff_list_url(review_request), { 'path': f, 'basedir': "/trunk", }, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], DIFF_TOO_BIG.code) self.assertTrue('reason' in rsp) self.assertTrue('max_size' in rsp) self.assertEqual(rsp['max_size'], self.siteconfig.get('diffviewer_max_diff_size'))
def test_post_diff_with_history(self): """Testing the POST <URL> API with a diff and a review request created with history support """ review_request = self.create_review_request(submitter=self.user, create_repository=True, create_with_history=True) with override_feature_check(dvcs_feature.feature_id, True): rsp = self.api_post(get_draft_diff_list_url(review_request), { 'path': SimpleUploadedFile('diff', self.DEFAULT_GIT_FILEDIFF_DATA_DIFF), 'basedir': '', }, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertEqual( rsp['reason'], 'This review request was created with support for multiple ' 'commits.\n\n' 'Create an empty diff revision and upload commits to that ' 'instead.')
def test_get_not_owner(self): """Testing the GET review-requests/<id>/draft/diffs/ API without owner with Permission Denied error """ review_request = self.create_review_request(create_repository=True) self.assertNotEqual(review_request.submitter, self.user) self.create_diffset(review_request, draft=True) self.api_get(get_draft_diff_list_url(review_request), expected_status=403)
def test_get_not_owner(self): """Testing the GET review-requests/<id>/draft/diffs/ API without owner with Permission Denied error """ review_request = self.create_review_request(create_repository=True) self.assertNotEqual(review_request.submitter, self.user) self.create_diffset(review_request, draft=True) self.apiGet(get_draft_diff_list_url(review_request), expected_status=403)
def setup_basic_get_test(self, user, with_local_site, local_site_name, populate_items): review_request = self.create_review_request( create_repository=True, with_local_site=with_local_site, submitter=user, publish=True ) if populate_items: items = [self.create_diffset(review_request, draft=True)] else: items = [] return (get_draft_diff_list_url(review_request, local_site_name), diff_list_mimetype, items)
def test_get(self): """Testing the GET review-requests/<id>/draft/diffs/ API""" review_request = self.create_review_request(create_repository=True, submitter=self.user) diffset = self.create_diffset(review_request, draft=True) rsp = self.apiGet(get_draft_diff_list_url(review_request), expected_mimetype=diff_list_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual(rsp['diffs'][0]['id'], diffset.pk) self.assertEqual(rsp['diffs'][0]['name'], diffset.name)
def test_get_with_site_no_access(self): """Testing the GET review-requests/<id>/draft/diffs/ API with a local site and Permission Denied error """ review_request = self.create_review_request(create_repository=True, with_local_site=True) self.assertNotEqual(review_request.submitter, self.user) self.create_diffset(review_request) self.apiGet( get_draft_diff_list_url(review_request, self.local_site_name), expected_status=403)
def setup_basic_get_test(self, user, with_local_site, local_site_name, populate_items): review_request = self.create_review_request( create_repository=True, with_local_site=with_local_site, submitter=user, publish=True) if populate_items: items = [self.create_diffset(review_request, draft=True)] else: items = [] return (get_draft_diff_list_url(review_request, local_site_name), diff_list_mimetype, items)
def test_post_empty_dvcs_disabled(self): """Testing the POST <URL> API without a diff with the DVCS feature disabled """ review_request = self.create_review_request(submitter=self.user, create_repository=True, create_with_history=False) with override_feature_check(dvcs_feature.feature_id, enabled=False): rsp = self.api_post(get_draft_diff_list_url(review_request), {}, expected_status=400) self.assertEqual(rsp['stat'], 'fail') self.assertEqual(rsp['err']['code'], INVALID_FORM_DATA.code) self.assertEqual(rsp['fields'], { 'path': ['This field is required.'], })
def test_get_with_site(self): """Testing the GET review-requests/<id>/draft/diffs/ API with a local site """ user = self._login_user(local_site=True) review_request = self.create_review_request(create_repository=True, with_local_site=True, submitter=user) diffset = self.create_diffset(review_request, draft=True) rsp = self.apiGet(get_draft_diff_list_url(review_request, self.local_site_name), expected_mimetype=diff_list_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual(rsp['diffs'][0]['id'], diffset.pk) self.assertEqual(rsp['diffs'][0]['name'], diffset.name)
def test_post_empty_with_history(self): """Testing the POST <URL> API creates an empty DiffSet for a review request created with history support with the DVCS feature enabled """ review_request = self.create_review_request(submitter=self.user, create_repository=True, create_with_history=True) with override_feature_check(dvcs_feature.feature_id, enabled=True): rsp = self.api_post(get_draft_diff_list_url(review_request), {}, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') item_rsp = rsp['diff'] diff = DiffSet.objects.get(pk=item_rsp['id']) self.compare_item(item_rsp, diff) self.assertEqual(diff.files.count(), 0)
def setup_basic_post_test(self, user, with_local_site, local_site_name, post_valid_data): repository = self.create_repository(tool_name="Test") review_request = self.create_review_request( with_local_site=with_local_site, repository=repository, submitter=user ) if post_valid_data: diff_filename = os.path.join(os.path.dirname(scmtools.__file__), "testdata", "git_readme.diff") post_data = {"path": open(diff_filename, "r"), "basedir": "/trunk", "base_commit_id": "1234"} else: post_data = {} return ( get_draft_diff_list_url(review_request, local_site_name), diff_item_mimetype, post_data, [review_request], )
def setup_basic_post_test(self, user, with_local_site, local_site_name, post_valid_data): repository = self.create_repository(tool_name='Test') review_request = self.create_review_request( with_local_site=with_local_site, repository=repository, submitter=user) if post_valid_data: diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') post_data = { 'path': open(diff_filename, 'r'), 'basedir': '/trunk', 'base_commit_id': '1234', } else: post_data = {} return (get_draft_diff_list_url(review_request, local_site_name), diff_item_mimetype, post_data, [review_request])
def setup_basic_post_test(self, user, with_local_site, local_site_name, post_valid_data): repository = self.create_repository(tool_name='Test') review_request = self.create_review_request( with_local_site=with_local_site, repository=repository, submitter=user) if post_valid_data: diff = SimpleUploadedFile('diff', self.DEFAULT_GIT_README_DIFF, content_type='text/x-patch') post_data = { 'path': diff, 'basedir': '/trunk', 'base_commit_id': '1234', } else: post_data = {} return (get_draft_diff_list_url(review_request, local_site_name), diff_item_mimetype, post_data, [review_request])
def test_post_diffs_too_big(self): """Testing the POST review-requests/<id>/draft/diffs/ API with diff exceeding max size """ repository = self.create_repository() self.siteconfig.set("diffviewer_max_diff_size", 2) self.siteconfig.save() review_request = self.create_review_request(repository=repository, submitter=self.user) diff_filename = os.path.join(os.path.dirname(scmtools.__file__), "testdata", "git_readme.diff") with open(diff_filename, "r") as f: rsp = self.api_post( get_draft_diff_list_url(review_request), {"path": f, "basedir": "/trunk"}, expected_status=400 ) self.assertEqual(rsp["stat"], "fail") self.assertEqual(rsp["err"]["code"], DIFF_TOO_BIG.code) self.assertIn("reason", rsp) self.assertIn("max_size", rsp) self.assertEqual(rsp["max_size"], self.siteconfig.get("diffviewer_max_diff_size"))
def setup_basic_post_test(self, user, with_local_site, local_site_name, post_valid_data): repository = self.create_repository(tool_name='Test') review_request = self.create_review_request( with_local_site=with_local_site, repository=repository, submitter=user) if post_valid_data: diff_filename = os.path.join(os.path.dirname(scmtools.__file__), 'testdata', 'git_readme.diff') post_data = { 'path': open(diff_filename, 'r'), 'basedir': '/trunk', 'base_commit_id': '1234', } else: post_data = {} return (get_draft_diff_list_url(review_request, local_site_name), diff_item_mimetype, post_data, [review_request])