def setup_basic_get_test(self, user, with_local_site, local_site_name): review_request = self.create_review_request( create_repository=True, with_local_site=with_local_site, submitter=user ) diffset = self.create_diffset(review_request, draft=True) return (get_draft_diff_item_url(review_request, diffset.revision, local_site_name), diff_item_mimetype, diffset)
def test_put_finalize_dvcs_feature_disabled(self): """Testing the PUT <URL> API with finalize_commit_series=1 when the DVCS feature is disabled """ with override_feature_check(dvcs_feature.feature_id, enabled=False): review_request = self.create_review_request( create_repository=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) cumulative_diff = SimpleUploadedFile('diff', b'', content_type='text/x-patch') rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info({}), }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.compare_item(rsp['diff'], diffset)
def test_put_finalize_empty_commit_series(self): """Testing the PUT <URL> API with finalize_commit_series=1 for an empty commit series """ with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) cumulative_diff = SimpleUploadedFile('diff', b'', content_type='text/x-patch') rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info({}), }, expected_status=400) self.assertEqual( rsp, { 'stat': 'fail', 'err': { 'code': INVALID_ATTRIBUTE.code, 'msg': INVALID_ATTRIBUTE.msg, }, 'reason': 'Cannot finalize an empty commit series.', })
def test_put_finalized_with_parent(self): """Testing the PUT <URL> API with finalize_commit_series=1 and a parent diff """ def _get_file_exists(repository, path, revision, base_commit_id=None, request=None): self.assertEqual(path, 'README') self.assertEqual(revision, 'f00f00') return True self.spy_on(Repository.get_file_exists, call_fake=_get_file_exists) with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) commit = self.create_diffcommit(diffset=diffset) filediff = FileDiff.objects.get() filediff.parent_diff = (b'diff --git a/README b/README\n' b'index f00f00..94bdd3e\n' b'--- a/README\n' b'+++ b/README\n') filediff.extra_data[FileDiff._IS_PARENT_EMPTY_KEY] = True filediff.save(update_fields=('extra_data', )) cumulative_diff = SimpleUploadedFile('diff', filediff.diff, content_type='text/x-patch') parent_diff = SimpleUploadedFile('parent_diff', filediff.parent_diff, content_type='text/x-patch') validation_info = update_validation_info( {}, commit_id=commit.commit_id, parent_id=commit.parent_id, filediffs=[filediff]) rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'parent_diff': parent_diff, 'validation_info': serialize_validation_info(validation_info), }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.compare_item(rsp['diff'], diffset)
def test_get_not_modified(self): """Testing the GET review-requests/<id>/draft/diffs/<revision>/ API with Not Modified response """ review_request = self.create_review_request(create_repository=True, submitter=self.user) diffset = self.create_diffset(review_request, draft=True) self._testHttpCaching(get_draft_diff_item_url(review_request, diffset.revision), check_last_modified=True)
def test_get_not_owner(self): """Testing the GET review-requests/<id>/draft/diffs/<revision>/ API without owner with Permission Denied error """ review_request = self.create_review_request(create_repository=True) self.assertNotEqual(review_request.submitter, self.user) diffset = self.create_diffset(review_request, draft=True) self.api_get(get_draft_diff_item_url(review_request, diffset.revision), expected_status=403)
def test_put_finalize_adds_default_reviewers(self): """Testing the PUT <URL> API with finalize_commit_series=1 adds default reviewers """ @self.spy_for(Repository.get_file_exists, owner=Repository) def _get_file_exists(repository, path, revision, **kwargs): self.assertEqual(path, 'README') self.assertEqual(revision, '94bdd3e') return True with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) # Create the state needed for the default reviewer. 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) # Create the state needed for the diff to post. diffset = self.create_diffset(review_request=review_request, draft=True) commit = self.create_diffcommit(diffset=diffset) filediff = FileDiff.objects.get() cumulative_diff = SimpleUploadedFile('diff', filediff.diff, content_type='text/x-patch') validation_info = update_validation_info( {}, commit_id=commit.commit_id, parent_id=commit.parent_id, filediffs=[filediff]) # Post the diff. rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info(validation_info), }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.compare_item(rsp['diff'], diffset) draft = review_request.get_draft() self.assertEqual(list(draft.target_groups.all()), [group])
def setup_basic_get_test(self, user, with_local_site, local_site_name): review_request = self.create_review_request( create_repository=True, with_local_site=with_local_site, submitter=user) diffset = self.create_diffset(review_request, draft=True) return (get_draft_diff_item_url(review_request, diffset.revision, local_site_name), diff_item_mimetype, diffset)
def test_get_not_owner(self): """Testing the GET review-requests/<id>/draft/diffs/<revision>/ API without owner with Permission Denied error """ review_request = self.create_review_request(create_repository=True) self.assertNotEqual(review_request.submitter, self.user) diffset = self.create_diffset(review_request, draft=True) self.apiGet(get_draft_diff_item_url(review_request, diffset.revision), expected_status=403)
def test_put_finalize_validation_info_missing_commits(self): """Testing the PUT <URL> API with finalize_commit_series=1 when validation_info does not contain all commits """ with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) commits = [ self.create_diffcommit(diffset=diffset, **kwargs) for kwargs in ( {'commit_id': 'r1', 'parent_id': 'r0'}, {'commit_id': 'r2', 'parent_id': 'r1'}, ) ] filediff = commits[0].files.first() validation_info = update_validation_info( {}, commit_id=commits[0].commit_id, parent_id=commits[0].parent_id, filediffs=[filediff]) cumulative_diff = SimpleUploadedFile('diff', filediff.diff, content_type='text/x-patch') rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info( validation_info), }, expected_status=400) self.assertEqual(rsp, { 'stat': 'fail', 'err': { 'code': INVALID_FORM_DATA.code, 'msg': INVALID_FORM_DATA.msg, }, 'fields': { 'validation_info': [ 'The following commits exist but are not present in ' 'validation_info: r2', ], } })
def test_get_with_site_no_access(self): """Testing the GET review-requests/<id>/draft/diffs/<revision>/ 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) diffset = self.create_diffset(review_request) self.apiGet(get_draft_diff_item_url(review_request, diffset.revision, self.local_site_name), expected_status=403)
def test_put_finalize_validation_info_extra_commits(self): """Testing the PUT <URL> API with finalize_commit_series=1 when validation_info contains commits that do not exist """ with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) commit = self.create_diffcommit(diffset=diffset) filediff = commit.files.first() validation_info = update_validation_info( {}, commit_id=commit.commit_id, parent_id=commit.parent_id, filediffs=[filediff]) validation_info = update_validation_info( validation_info, commit_id='f00', parent_id=commit.commit_id, filediffs=[]) cumulative_diff = SimpleUploadedFile('diff', filediff.diff, content_type='text/x-patch') rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info(validation_info), }, expected_status=400) self.assertEqual( rsp, { 'stat': 'fail', 'err': { 'code': INVALID_FORM_DATA.code, 'msg': INVALID_FORM_DATA.msg, }, 'fields': { 'validation_info': [ 'The following commits are specified in validation_info ' 'but do not exist: f00' ], }, })
def test_get(self): """Testing the GET review-requests/<id>/draft/diffs/<revision>/ 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_item_url(review_request, diffset.revision), expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual(rsp['diff']['id'], diffset.pk) self.assertEqual(rsp['diff']['name'], diffset.name)
def test_put_finalize(self): """Testing the PUT <URL> API with finalize_commit_series=1""" def _get_file_exists(repository, path, revision, base_commit_id=None, request=None): self.assertEqual(path, 'README') self.assertEqual(revision, '94bdd3e') return True self.spy_on(Repository.get_file_exists, owner=Repository, call_fake=_get_file_exists) with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) commit = self.create_diffcommit(diffset=diffset) filediff = FileDiff.objects.get() cumulative_diff = SimpleUploadedFile('diff', filediff.diff, content_type='text/x-patch') validation_info = update_validation_info( {}, commit_id=commit.commit_id, parent_id=commit.parent_id, filediffs=[filediff]) rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info(validation_info), }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.compare_item(rsp['diff'], diffset)
def test_put_finalize_invalid_validation_info_not_json(self): """Testing the PUT <URL> API with finalize_commit_series=1 when validation_info is JSON in the incorrect format """ with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) self.create_diffcommit(diffset=diffset) cumulative_diff = SimpleUploadedFile('diff', b'', content_type='text/x-patch') validation_info = base64.b64encode(b'AAAAAAA').decode('utf-8') rsp = self.api_put(get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': validation_info, }, expected_status=400) # Python 2 and 3 differ in the error contents you'll get when # attempting to load non-JSON data. if six.PY3: expected_error = ('Could not parse field: Expecting value: line 1 ' 'column 1 (char 0)') else: expected_error = \ 'Could not parse field: No JSON object could be decoded' self.assertEqual( rsp, { 'stat': 'fail', 'err': { 'code': INVALID_FORM_DATA.code, 'msg': INVALID_FORM_DATA.msg, }, 'fields': { 'validation_info': [expected_error], }, })
def test_get_with_site(self): """Testing the GET review-requests/<id>/draft/diffs/<revision>/ 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_item_url(review_request, diffset.revision, self.local_site_name), expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual(rsp['diff']['id'], diffset.id) self.assertEqual(rsp['diff']['name'], diffset.name)
def test_put_finalize_again(self): """Testing the PUT <URL> API with finalize_commit_series=1 when the diff is already finalized """ with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) self.create_diffcommit(diffset=diffset) diffset.finalize_commit_series( cumulative_diff=self.DEFAULT_GIT_FILEDIFF_DATA_DIFF, validation_info=None, validate=False, save=True) cumulative_diff = SimpleUploadedFile('diff', b'', content_type='text/x-patch') rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info({}), }, expected_status=400) self.assertEqual( rsp, { 'stat': 'fail', 'err': { 'code': INVALID_ATTRIBUTE.code, 'msg': INVALID_ATTRIBUTE.msg, }, 'reason': 'This diff is already finalized.', })
def test_put_finalize_invalid_validation_info_base64(self): """Testing the PUT <URL> API with finalize_commit_series=1 when validation_info is invalid base64 """ with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) self.create_diffcommit(diffset=diffset) cumulative_diff = SimpleUploadedFile('diff', b'', content_type='text/x-patch') rsp = self.api_put(get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': 'foo', }, expected_status=400) self.assertEqual( rsp, { 'stat': 'fail', 'err': { 'code': INVALID_FORM_DATA.code, 'msg': INVALID_FORM_DATA.msg, }, 'fields': { 'validation_info': [ 'Could not parse field: Incorrect padding', ], }, })
def test_put_finalize_missing_fields(self): """Testing the PUT <URL> API with finalize_commit_series=1 with missing request fields """ with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) diffset = self.create_diffset(review_request=review_request, draft=True) self.create_diffcommit(diffset=diffset) rsp = self.api_put(get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, }, expected_status=400) self.assertEqual( rsp, { 'stat': 'fail', 'err': { 'code': INVALID_FORM_DATA.code, 'msg': INVALID_FORM_DATA.msg, }, 'fields': { 'cumulative_diff': [ 'This field is required when finalize_commit_series is ' 'set.', ], 'validation_info': [ 'This field is required when finalize_commit_series is ' 'set.', ], }, })
def setup_http_not_allowed_item_test(self, user): review_request = self.create_review_request(create_repository=True, publish=True) return get_draft_diff_item_url(review_request, 1)
def test_put_finalize_adds_default_reviewers_first_time_only(self): """Testing the PUT <URL> API with finalize_commit_series=1 doesn't add default reviewers a second time """ def _get_file_exists(repository, path, revision, base_commit_id=None, request=None): self.assertEqual(path, 'README') self.assertEqual(revision, '94bdd3e') return True self.spy_on(Repository.get_file_exists, call_fake=_get_file_exists) with override_feature_check(dvcs_feature.feature_id, enabled=True): review_request = self.create_review_request( create_repository=True, create_with_history=True, submitter=self.user) # Create the state needed for the default reviewer. 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) # Create the initial diffset. This should prevent a default # reviewer from being applied, since we're not publishing the first # diff on a review request. self.create_diffset(review_request=review_request) # Create the state needed for the diff to post. diffset = self.create_diffset(review_request=review_request, draft=True) commit = self.create_diffcommit(diffset=diffset) filediff = FileDiff.objects.get() cumulative_diff = SimpleUploadedFile('diff', filediff.diff, content_type='text/x-patch') validation_info = update_validation_info( {}, commit_id=commit.commit_id, parent_id=commit.parent_id, filediffs=[filediff]) # Post the diff. rsp = self.api_put( get_draft_diff_item_url(review_request, diffset.revision), { 'finalize_commit_series': True, 'cumulative_diff': cumulative_diff, 'validation_info': serialize_validation_info(validation_info), }, expected_mimetype=diff_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.compare_item(rsp['diff'], diffset) draft = review_request.get_draft() self.assertEqual(list(draft.target_groups.all()), [])