<s> from __future__ import unicode_literals <EOL> import os <EOL> from django . utils import six <EOL> from djblets . webapi . errors import INVALID_FORM_DATA <EOL> from reviewboard import scmtools <EOL> from reviewboard . attachments . models import FileAttachment <EOL> from reviewboard . diffviewer . models import DiffSet , FileDiff <EOL> from reviewboard . webapi . resources import resources <EOL> from reviewboard . webapi . tests . base import BaseWebAPITestCase <EOL> from reviewboard . webapi . tests . mimetypes import ( diff_item_mimetype , <EOL> filediff_item_mimetype , <EOL> filediff_list_mimetype ) <EOL> from reviewboard . webapi . tests . mixins import BasicTestsMetaclass <EOL> from reviewboard . webapi . tests . mixins_extra_data import ExtraDataItemMixin <EOL> from reviewboard . webapi . tests . urls import ( get_diff_list_url , <EOL> get_draft_filediff_item_url , <EOL> get_draft_filediff_list_url ) <EOL> @ six . add_metaclass ( BasicTestsMetaclass ) <EOL> class ResourceListTests ( BaseWebAPITestCase ) : <EOL> """<STR_LIT>""" <EOL> fixtures = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> sample_api_url = '<STR_LIT>' <EOL> resource = resources . draft_filediff <EOL> def compare_item ( self , item_rsp , filediff ) : <EOL> self . assertEqual ( item_rsp [ '<STR_LIT:id>' ] , filediff . pk ) <EOL> self . assertEqual ( item_rsp [ '<STR_LIT>' ] , filediff . source_file ) <EOL> self . assertEqual ( item_rsp [ '<STR_LIT>' ] , filediff . extra_data ) <EOL> def setup_http_not_allowed_list_test ( self , user ) : <EOL> review_request = self . create_review_request ( <EOL> create_repository = True , <EOL> submitter = user ) <EOL> diffset = self . create_diffset ( review_request , draft = True ) <EOL> return get_draft_filediff_list_url ( diffset , review_request ) <EOL> def setup_basic_get_test ( self , user , with_local_site , local_site_name , <EOL> populate_items ) : <EOL> review_request = self . create_review_request ( <EOL> create_repository = True , <EOL> with_local_site = with_local_site , <EOL> submitter = user ) <EOL> diffset = self . create_diffset ( review_request , draft = True ) <EOL> if populate_items : <EOL> items = [ self . create_filediff ( diffset ) ] <EOL> else : <EOL> items = [ ] <EOL> return ( get_draft_filediff_list_url ( diffset , review_request , <EOL> local_site_name ) , <EOL> filediff_list_mimetype , <EOL> items ) <EOL> def test_get_not_owner ( self ) : <EOL> """<STR_LIT>""" <EOL> review_request = self . create_review_request ( create_repository = True ) <EOL> self . assertNotEqual ( review_request . submitter , self . user ) <EOL> diffset = self . create_diffset ( review_request , draft = True ) <EOL> self . api_get ( <EOL> get_draft_filediff_list_url ( diffset , review_request ) , <EOL> expected_status = <NUM_LIT> ) <EOL> @ six . add_metaclass ( BasicTestsMetaclass ) <EOL> class ResourceItemTests ( ExtraDataItemMixin , BaseWebAPITestCase ) : <EOL> """<STR_LIT>""" <EOL> fixtures = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> sample_api_url = '<STR_LIT>' <EOL> resource = resources . draft_filediff <EOL> test_http_methods = ( '<STR_LIT>' , '<STR_LIT:GET>' , '<STR_LIT>' ) <EOL> def setup_http_not_allowed_item_test ( self , user ) : <EOL> review_request = self . create_review_request ( <EOL> create_repository = True , <EOL> submitter = user ) <EOL> diffset = self . create_diffset ( review_request , draft = True ) <EOL> filediff = self . create_filediff ( diffset ) <EOL> return get_draft_filediff_item_url ( filediff , review_request ) <EOL> def compare_item ( self , item_rsp , filediff ) : <EOL> self . assertEqual ( item_rsp [ '<STR_LIT:id>' ] , filediff . pk ) <EOL> self . assertEqual ( item_rsp [ '<STR_LIT>' ] , filediff . source_file ) <EOL> self . assertEqual ( item_rsp [ '<STR_LIT>' ] , filediff . extra_data ) <EOL> def setup_basic_get_test ( self , user , with_local_site , local_site_name ) : <EOL> review_request = self . create_review_request ( <EOL> create_repository = True , <EOL> with_local_site = with_local_site , <EOL> submitter = user ) <EOL> diffset = self . create_diffset ( review_request , draft = True ) <EOL> filediff = self . create_filediff ( diffset ) <EOL> return ( get_draft_filediff_item_url ( filediff , review_request , <EOL> local_site_name ) , <EOL> filediff_item_mimetype , <EOL> filediff ) <EOL> def test_get_not_owner ( self ) : <EOL> """<STR_LIT>""" <EOL> review_request = self . create_review_request ( create_repository = True ) <EOL> self . assertNotEqual ( review_request . submitter , self . user ) <EOL> diffset = self . create_diffset ( review_request , draft = True ) <EOL> filediff = self . create_filediff ( diffset ) <EOL> self . api_get ( <EOL> get_draft_filediff_item_url ( filediff , review_request ) , <EOL> expected_status = <NUM_LIT> ) <EOL> def setup_basic_put_test ( self , user , with_local_site , local_site_name , <EOL> put_valid_data ) : <EOL> review_request = self . create_review_request ( <EOL> submitter = user , <EOL> with_local_site = with_local_site , <EOL> create_repository = True ) <EOL> diffset = self . create_diffset ( review_request , draft = True ) <EOL> filediff = self . create_filediff ( diffset ) <EOL> return ( get_draft_filediff_item_url ( filediff , review_request , <EOL> local_site_name ) , <EOL> filediff_item_mimetype , <EOL> { } , <EOL> filediff , <EOL> [ ] ) <EOL> def check_put_result ( self , user , item_rsp , filediff ) : <EOL> filediff = FileDiff . objects . get ( pk = filediff . pk ) <EOL> self . compare_item ( item_rsp , filediff ) <EOL> def test_put_with_new_file_and_dest_attachment_file ( self ) : <EOL> """<STR_LIT>""" <EOL> review_request = self . create_review_request ( create_repository = True , <EOL> submitter = self . user ) <EOL> diff_filename = os . path . join ( os . path . dirname ( scmtools . __file__ ) , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> with open ( diff_filename , '<STR_LIT:r>' ) as f : <EOL> rsp = self . api_post ( <EOL> get_diff_list_url ( review_request ) , <EOL> { <EOL> '<STR_LIT:path>' : f , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> expected_mimetype = diff_item_mimetype ) <EOL> self . assertEqual ( rsp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> diffset = DiffSet . objects . get ( pk = rsp [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> filediffs = diffset . files . all ( ) <EOL> self . assertEqual ( len ( filediffs ) , <NUM_LIT:1> ) <EOL> filediff = filediffs [ <NUM_LIT:0> ] <EOL> self . assertEqual ( filediff . source_file , '<STR_LIT>' ) <EOL> with open ( self . _getTrophyFilename ( ) , '<STR_LIT:r>' ) as f : <EOL> rsp = self . api_put ( <EOL> get_draft_filediff_item_url ( filediff , review_request ) + <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : f , <EOL> } , <EOL> expected_mimetype = filediff_item_mimetype ) <EOL> self . assertEqual ( rsp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , rsp [ '<STR_LIT:file>' ] ) <EOL> attachment = FileAttachment . objects . get ( <EOL> pk = rsp [ '<STR_LIT:file>' ] [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> self . assertTrue ( attachment . is_from_diff ) <EOL> self . assertEqual ( attachment . orig_filename , '<STR_LIT>' ) <EOL> self . assertEqual ( attachment . added_in_filediff , filediff ) <EOL> self . assertEqual ( attachment . repo_path , None ) <EOL> self . assertEqual ( attachment . repo_revision , None ) <EOL> self . assertEqual ( attachment . repository , None ) <EOL> def test_put_with_modified_file_and_dest_attachment_file ( self ) : <EOL> """<STR_LIT>""" <EOL> review_request = self . create_review_request ( create_repository = True , <EOL> submitter = self . user ) <EOL> diff_filename = os . path . join ( os . path . dirname ( scmtools . __file__ ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> with open ( diff_filename , '<STR_LIT:r>' ) as f : <EOL> rsp = self . api_post ( <EOL> get_diff_list_url ( review_request ) , <EOL> { <EOL> '<STR_LIT:path>' : f , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> expected_mimetype = diff_item_mimetype ) <EOL> self . assertEqual ( rsp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> diffset = DiffSet . objects . get ( pk = rsp [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> filediffs = diffset . files . all ( ) <EOL> self . assertEqual ( len ( filediffs ) , <NUM_LIT:1> ) <EOL> filediff = filediffs [ <NUM_LIT:0> ] <EOL> self . assertEqual ( filediff . source_file , '<STR_LIT>' ) <EOL> with open ( self . _getTrophyFilename ( ) , '<STR_LIT:r>' ) as f : <EOL> rsp = self . api_put ( <EOL> get_draft_filediff_item_url ( filediff , review_request ) + <EOL> '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : f , <EOL> } , <EOL> expected_mimetype = filediff_item_mimetype ) <EOL> self . assertEqual ( rsp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , rsp [ '<STR_LIT:file>' ] ) <EOL> attachment = FileAttachment . objects . get ( <EOL> pk = rsp [ '<STR_LIT:file>' ] [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> self . assertTrue ( attachment . is_from_diff ) <EOL> self . assertEqual ( attachment . orig_filename , '<STR_LIT>' ) <EOL> self . assertEqual ( attachment . added_in_filediff , None ) <EOL> self . assertEqual ( attachment . repo_path , '<STR_LIT>' ) <EOL> self . assertEqual ( attachment . repo_revision , '<STR_LIT>' ) <EOL> self . assertEqual ( attachment . repository , review_request . repository ) <EOL> def test_put_second_dest_attachment_file_disallowed ( self ) : <EOL> """<STR_LIT>""" <EOL> review_request = self . create_review_request ( create_repository = True , <EOL> submitter = self . user ) <EOL> diff_filename = os . path . join ( os . path . dirname ( scmtools . __file__ ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> with open ( diff_filename , '<STR_LIT:r>' ) as f : <EOL> rsp = self . api_post ( <EOL> get_diff_list_url ( review_request ) , <EOL> { <EOL> '<STR_LIT:path>' : f , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> expected_mimetype = diff_item_mimetype ) <EOL> diffset = DiffSet . objects . get ( pk = rsp [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> filediff = diffset . files . all ( ) [ <NUM_LIT:0> ] <EOL> url = get_draft_filediff_item_url ( filediff , review_request ) <EOL> trophy_filename = self . _getTrophyFilename ( ) <EOL> with open ( trophy_filename , '<STR_LIT:r>' ) as f : <EOL> self . api_put ( <EOL> url , <EOL> { <EOL> '<STR_LIT>' : f , <EOL> } , <EOL> expected_mimetype = filediff_item_mimetype ) <EOL> with open ( trophy_filename , '<STR_LIT:r>' ) as f : <EOL> rsp = self . api_put ( <EOL> url , <EOL> { <EOL> '<STR_LIT>' : f , <EOL> } , <EOL> expected_status = <NUM_LIT> ) <EOL> self . assertEqual ( rsp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( rsp [ '<STR_LIT>' ] [ '<STR_LIT:code>' ] , INVALID_FORM_DATA . code ) <EOL> self . assertIn ( '<STR_LIT>' , rsp ) <EOL> self . assertIn ( '<STR_LIT>' , rsp [ '<STR_LIT>' ] ) </s>
