<s> from os import path <EOL> import unittest <EOL> import mocker <EOL> from PIL import Image <EOL> import StringIO <EOL> from django . core . urlresolvers import reverse <EOL> from django_webtest import WebTest <EOL> from django . conf import settings <EOL> from django_factory_boy import auth <EOL> from django . core . files . uploadedfile import InMemoryUploadedFile <EOL> from . import doubles <EOL> import pkg_resources <EOL> PACKTOOLS_VERSION = pkg_resources . get_distribution ( '<STR_LIT>' ) . version <EOL> def _get_test_xml_abspath ( filename ) : <EOL> folder_path = settings . PROJECT_PATH <EOL> folder_path = settings . PROJECT_PATH . split ( '<STR_LIT:/>' ) <EOL> folder_path . extend ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , filename ] ) <EOL> return '<STR_LIT:/>' . join ( folder_path ) <EOL> def get_temporary_text_file ( ) : <EOL> io = StringIO . StringIO ( ) <EOL> io . write ( '<STR_LIT:foo>' ) <EOL> text_file = InMemoryUploadedFile ( io , field_name = '<STR_LIT:file>' , name = '<STR_LIT>' , content_type = '<STR_LIT:text>' , size = io . len , charset = '<STR_LIT:utf8>' ) <EOL> text_file . seek ( <NUM_LIT:0> ) <EOL> return text_file <EOL> def get_temporary_image_file ( ) : <EOL> io = StringIO . StringIO ( ) <EOL> size = ( <NUM_LIT:200> , <NUM_LIT:200> ) <EOL> color = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> image = Image . new ( "<STR_LIT>" , size , color ) <EOL> image . save ( io , format = '<STR_LIT>' ) <EOL> image_file = InMemoryUploadedFile ( io , field_name = '<STR_LIT:file>' , name = '<STR_LIT>' , content_type = '<STR_LIT>' , size = io . len , charset = None ) <EOL> image_file . seek ( <NUM_LIT:0> ) <EOL> return image_file <EOL> def get_temporary_xml_file ( xml_abs_path ) : <EOL> fp = open ( xml_abs_path ) <EOL> io = StringIO . StringIO ( fp . read ( ) ) <EOL> fp . close ( ) <EOL> xml_file = InMemoryUploadedFile ( io , field_name = '<STR_LIT:file>' , name = '<STR_LIT>' , content_type = '<STR_LIT>' , size = io . len , charset = '<STR_LIT:utf8>' ) <EOL> xml_file . seek ( <NUM_LIT:0> ) <EOL> return xml_file <EOL> class ValidatorTests ( WebTest , mocker . MockerTestCase ) : <EOL> def test_access_unauthenticated_user ( self ) : <EOL> response = self . app . get ( <EOL> reverse ( '<STR_LIT>' , ) , <EOL> ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> def test_access_authenticated_user ( self ) : <EOL> self . user = auth . UserF ( is_active = True ) <EOL> response = self . app . get ( <EOL> reverse ( '<STR_LIT>' , ) , <EOL> user = self . user <EOL> ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> def test_link_is_present_at_homepage ( self ) : <EOL> response = self . app . get ( <EOL> reverse ( '<STR_LIT:index>' , ) , <EOL> ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> response = response . follow ( ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> expected_url = reverse ( '<STR_LIT>' , ) <EOL> response . mustcontain ( '<STR_LIT>' % expected_url ) <EOL> def test_submit_empty_form_is_not_valid ( self ) : <EOL> page = self . app . get ( <EOL> reverse ( '<STR_LIT>' , ) , <EOL> ) <EOL> form = page . forms [ '<STR_LIT>' ] <EOL> response = form . submit ( ) <EOL> self . assertTemplateUsed ( response , '<STR_LIT>' ) <EOL> self . assertFalse ( response . context [ '<STR_LIT>' ] . is_valid ( ) ) <EOL> form_errors = response . context [ '<STR_LIT>' ] . errors <EOL> expected_errors = { '<STR_LIT:file>' : [ u'<STR_LIT>' ] } <EOL> self . assertEqual ( form_errors , expected_errors ) <EOL> self . assertFalse ( hasattr ( response . context , '<STR_LIT>' ) ) <EOL> def test_submit_text_file_then_form_not_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> test_file = get_temporary_text_file ( ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' , ) , <EOL> { <EOL> '<STR_LIT:file>' : test_file <EOL> } <EOL> ) <EOL> form = response . context [ '<STR_LIT>' ] <EOL> self . assertFalse ( response . context [ '<STR_LIT>' ] . is_valid ( ) ) <EOL> expected_errors = { <EOL> '<STR_LIT:file>' : [ u'<STR_LIT>' ] <EOL> } <EOL> self . assertEqual ( form . errors , expected_errors ) <EOL> def test_submit_image_file_then_form_not_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> test_file = get_temporary_image_file ( ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' , ) , <EOL> { <EOL> '<STR_LIT:file>' : test_file <EOL> } <EOL> ) <EOL> form = response . context [ '<STR_LIT>' ] <EOL> self . assertFalse ( response . context [ '<STR_LIT>' ] . is_valid ( ) ) <EOL> expected_errors = { '<STR_LIT:file>' : [ u'<STR_LIT>' ] } <EOL> self . assertEqual ( form . errors , expected_errors ) <EOL> def test_submit_valid_xml_file_then_get_annotations_form_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> stub_analyze_xml = doubles . make_stub_analyze_xml ( '<STR_LIT>' ) <EOL> mock_utils = self . mocker . replace ( '<STR_LIT>' ) <EOL> mock_utils . analyze_xml <EOL> self . mocker . result ( stub_analyze_xml ) <EOL> self . mocker . replay ( ) <EOL> test_file_path = _get_test_xml_abspath ( '<STR_LIT>' ) <EOL> test_file = get_temporary_xml_file ( test_file_path ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' , ) , <EOL> { <EOL> '<STR_LIT:file>' : test_file <EOL> } <EOL> ) <EOL> form = response . context [ '<STR_LIT>' ] <EOL> xml_exception = response . context [ '<STR_LIT>' ] <EOL> results = response . context [ '<STR_LIT>' ] <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> self . assertTemplateUsed ( '<STR_LIT>' ) <EOL> self . assertEqual ( results . keys ( ) , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) </s>
