<s> import os <EOL> import hashlib <EOL> import unittest <EOL> from django . core . files . base import File <EOL> from django . core . files . uploadedfile import InMemoryUploadedFile <EOL> from django . core . urlresolvers import reverse <EOL> from onadata . apps . main . models import MetaData <EOL> from onadata . apps . main . views import show , edit , download_metadata , download_media_data , delete_metadata <EOL> from test_base import TestBase <EOL> class TestFormMetadata ( TestBase ) : <EOL> def setUp ( self ) : <EOL> TestBase . setUp ( self ) <EOL> self . _create_user_and_login ( ) <EOL> self . _publish_transportation_form_and_submit_instance ( ) <EOL> self . url = reverse ( show , kwargs = { <EOL> '<STR_LIT:username>' : self . user . username , <EOL> '<STR_LIT>' : self . xform . id_string <EOL> } ) <EOL> self . edit_url = reverse ( edit , kwargs = { <EOL> '<STR_LIT:username>' : self . user . username , <EOL> '<STR_LIT>' : self . xform . id_string <EOL> } ) <EOL> def _add_metadata ( self , data_type = '<STR_LIT>' ) : <EOL> if data_type == '<STR_LIT>' : <EOL> name = '<STR_LIT>' <EOL> else : <EOL> name = '<STR_LIT>' <EOL> path = os . path . join ( self . this_directory , "<STR_LIT>" , <EOL> "<STR_LIT>" , name ) <EOL> with open ( path ) as doc_file : <EOL> self . post_data = { } <EOL> self . post_data [ data_type ] = doc_file <EOL> self . client . post ( self . edit_url , self . post_data ) <EOL> if data_type == '<STR_LIT>' : <EOL> self . doc = MetaData . objects . filter ( data_type = '<STR_LIT>' ) . reverse ( ) [ <NUM_LIT:0> ] <EOL> self . doc_url = reverse ( download_media_data , kwargs = { <EOL> '<STR_LIT:username>' : self . user . username , <EOL> '<STR_LIT>' : self . xform . id_string , <EOL> '<STR_LIT>' : self . doc . id } ) <EOL> else : <EOL> self . doc = MetaData . objects . all ( ) . reverse ( ) [ <NUM_LIT:0> ] <EOL> self . doc_url = reverse ( download_metadata , kwargs = { <EOL> '<STR_LIT:username>' : self . user . username , <EOL> '<STR_LIT>' : self . xform . id_string , <EOL> '<STR_LIT>' : self . doc . id } ) <EOL> return name <EOL> def test_adds_supporting_doc_on_submit ( self ) : <EOL> count = len ( MetaData . objects . filter ( xform = self . xform , <EOL> data_type = '<STR_LIT>' ) ) <EOL> self . _add_metadata ( ) <EOL> self . assertEquals ( count + <NUM_LIT:1> , len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) ) <EOL> def test_adds_supporting_media_on_submit ( self ) : <EOL> count = len ( MetaData . objects . filter ( xform = self . xform , <EOL> data_type = '<STR_LIT>' ) ) <EOL> self . _add_metadata ( data_type = '<STR_LIT>' ) <EOL> self . assertEquals ( count + <NUM_LIT:1> , len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) ) <EOL> def test_adds_mapbox_layer_on_submit ( self ) : <EOL> count = len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) <EOL> self . post_data = { } <EOL> self . post_data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . post_data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . client . post ( self . edit_url , self . post_data ) <EOL> self . assertEquals ( count + <NUM_LIT:1> , len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) ) <EOL> def test_shows_supporting_doc_after_submit ( self ) : <EOL> name = self . _add_metadata ( ) <EOL> response = self . client . get ( self . url ) <EOL> self . assertContains ( response , name ) <EOL> self . xform . shared = True <EOL> self . xform . save ( ) <EOL> response = self . anon . get ( self . url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertContains ( response , name ) <EOL> @ unittest . skip ( '<STR_LIT>' ) <EOL> def test_shows_supporting_media_after_submit ( self ) : <EOL> name = self . _add_metadata ( data_type = '<STR_LIT>' ) <EOL> response = self . client . get ( self . url ) <EOL> self . assertContains ( response , name ) <EOL> self . xform . shared = True <EOL> self . xform . save ( ) <EOL> response = self . anon . get ( self . url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertContains ( response , name ) <EOL> def test_shows_mapbox_layer_after_submit ( self ) : <EOL> self . post_data = { } <EOL> self . post_data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . post_data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response = self . client . post ( self . edit_url , self . post_data ) <EOL> response = self . client . get ( self . url ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> self . xform . shared = True <EOL> self . xform . save ( ) <EOL> response = self . anon . get ( self . url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertContains ( response , '<STR_LIT>' ) <EOL> def test_download_supporting_doc ( self ) : <EOL> self . _add_metadata ( ) <EOL> response = self . client . get ( self . doc_url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> fileName , ext = os . path . splitext ( response [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( ext , '<STR_LIT>' ) <EOL> def test_no_download_supporting_doc_for_anon ( self ) : <EOL> self . _add_metadata ( ) <EOL> response = self . anon . get ( self . doc_url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_download_supporting_media ( self ) : <EOL> self . _add_metadata ( data_type = '<STR_LIT>' ) <EOL> response = self . client . get ( self . doc_url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> fileName , ext = os . path . splitext ( response [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( ext , '<STR_LIT>' ) <EOL> def test_shared_download_supporting_doc_for_anon ( self ) : <EOL> self . _add_metadata ( ) <EOL> self . xform . shared = True <EOL> self . xform . save ( ) <EOL> response = self . anon . get ( self . doc_url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_shared_download_supporting_media_for_anon ( self ) : <EOL> self . _add_metadata ( data_type = '<STR_LIT>' ) <EOL> self . xform . shared = True <EOL> self . xform . save ( ) <EOL> response = self . anon . get ( self . doc_url ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_delete_supporting_doc ( self ) : <EOL> count = MetaData . objects . filter ( xform = self . xform , <EOL> data_type = '<STR_LIT>' ) . count ( ) <EOL> self . _add_metadata ( ) <EOL> self . assertEqual ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) . count ( ) , count + <NUM_LIT:1> ) <EOL> doc = MetaData . objects . filter ( data_type = '<STR_LIT>' ) . reverse ( ) [ <NUM_LIT:0> ] <EOL> self . delete_doc_url = reverse ( delete_metadata , kwargs = { <EOL> '<STR_LIT:username>' : self . user . username , <EOL> '<STR_LIT>' : self . xform . id_string , <EOL> '<STR_LIT>' : doc . id } ) <EOL> response = self . client . get ( self . delete_doc_url + '<STR_LIT>' ) <EOL> self . assertEqual ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) . count ( ) , count ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_delete_supporting_media ( self ) : <EOL> count = MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) . count ( ) <EOL> self . _add_metadata ( data_type = '<STR_LIT>' ) <EOL> self . assertEqual ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) . count ( ) , count + <NUM_LIT:1> ) <EOL> response = self . client . get ( self . doc_url + '<STR_LIT>' ) <EOL> self . assertEqual ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) . count ( ) , count ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . _add_metadata ( data_type = '<STR_LIT>' ) <EOL> response = self . anon . get ( self . doc_url + '<STR_LIT>' ) <EOL> self . assertEqual ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) . count ( ) , count + <NUM_LIT:1> ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def _add_mapbox_layer ( self ) : <EOL> self . count = len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) <EOL> post_data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> response = self . client . post ( self . edit_url , post_data ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) , self . count + <NUM_LIT:1> ) <EOL> def test_delete_mapbox_layer ( self ) : <EOL> self . _add_mapbox_layer ( ) <EOL> doc = MetaData . objects . filter ( data_type = '<STR_LIT>' ) . reverse ( ) [ <NUM_LIT:0> ] <EOL> self . delete_doc_url = reverse ( delete_metadata , kwargs = { <EOL> '<STR_LIT:username>' : self . user . username , <EOL> '<STR_LIT>' : self . xform . id_string , <EOL> '<STR_LIT>' : doc . id } ) <EOL> response = self . client . get ( self . delete_doc_url + '<STR_LIT>' ) <EOL> self . assertEqual ( len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) , self . count ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_anon_delete_mapbox_layer ( self ) : <EOL> self . _add_mapbox_layer ( ) <EOL> doc = MetaData . objects . filter ( data_type = '<STR_LIT>' ) . reverse ( ) [ <NUM_LIT:0> ] <EOL> self . delete_doc_url = reverse ( delete_metadata , kwargs = { <EOL> '<STR_LIT:username>' : self . user . username , <EOL> '<STR_LIT>' : self . xform . id_string , <EOL> '<STR_LIT>' : doc . id } ) <EOL> response = self . anon . get ( self . delete_doc_url + '<STR_LIT>' ) <EOL> self . assertEqual ( len ( MetaData . objects . filter ( <EOL> xform = self . xform , data_type = '<STR_LIT>' ) ) , self . count + <NUM_LIT:1> ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_user_source_edit_updates ( self ) : <EOL> desc = '<STR_LIT>' <EOL> response = self . client . post ( self . edit_url , { '<STR_LIT:source>' : desc } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( MetaData . source ( self . xform ) . data_value , desc ) <EOL> def test_upload_source_file ( self ) : <EOL> self . _add_metadata ( '<STR_LIT:source>' ) <EOL> self . assertNotEqual ( MetaData . source ( self . xform ) . data_file , None ) <EOL> def test_upload_source_file_set_value_to_name ( self ) : <EOL> name = self . _add_metadata ( '<STR_LIT:source>' ) <EOL> self . assertEqual ( MetaData . source ( self . xform ) . data_value , name ) <EOL> def test_upload_source_file_keep_name ( self ) : <EOL> desc = '<STR_LIT>' <EOL> response = self . client . post ( self . edit_url , { '<STR_LIT:source>' : desc } , <EOL> HTTP_X_REQUESTED_WITH = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . _add_metadata ( '<STR_LIT:source>' ) <EOL> self . assertNotEqual ( MetaData . source ( self . xform ) . data_file , None ) <EOL> self . assertEqual ( MetaData . source ( self . xform ) . data_value , desc ) <EOL> def test_media_file_hash ( self ) : <EOL> name = "<STR_LIT>" <EOL> media_file = os . path . join ( <EOL> self . this_directory , '<STR_LIT>' , '<STR_LIT>' , name ) <EOL> m = MetaData . objects . create ( <EOL> data_type = '<STR_LIT>' , xform = self . xform , data_value = name , <EOL> data_file = File ( open ( media_file ) , name ) , <EOL> data_file_type = '<STR_LIT>' ) <EOL> f = open ( media_file ) <EOL> media_hash = '<STR_LIT>' % hashlib . md5 ( f . read ( ) ) . hexdigest ( ) <EOL> f . close ( ) <EOL> meta_hash = m . hash <EOL> self . assertEqual ( meta_hash , media_hash ) <EOL> self . assertEqual ( m . file_hash , media_hash ) <EOL> def test_add_media_url ( self ) : <EOL> uri = '<STR_LIT>' <EOL> count = MetaData . objects . filter ( data_type = '<STR_LIT>' ) . count ( ) <EOL> self . client . post ( self . edit_url , { '<STR_LIT>' : uri } ) <EOL> self . assertEquals ( count + <NUM_LIT:1> , <EOL> len ( MetaData . objects . filter ( data_type = '<STR_LIT>' ) ) ) <EOL> def test_windows_csv_file_upload ( self ) : <EOL> count = MetaData . objects . filter ( data_type = '<STR_LIT>' ) . count ( ) <EOL> media_file = os . path . join ( <EOL> self . this_directory , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> f = InMemoryUploadedFile ( open ( media_file ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> <NUM_LIT> , <EOL> None ) <EOL> MetaData . media_upload ( self . xform , f ) <EOL> media_list = MetaData . objects . filter ( data_type = '<STR_LIT>' ) <EOL> new_count = media_list . count ( ) <EOL> self . assertEqual ( count + <NUM_LIT:1> , new_count ) <EOL> media = media_list . get ( data_value = '<STR_LIT>' ) <EOL> self . assertEqual ( media . data_file_type , '<STR_LIT>' ) </s>
