<s> from django . core . urlresolvers import resolve <EOL> from django . http import HttpRequest <EOL> from django . http import QueryDict <EOL> from django . test import TestCase <EOL> from django . test import Client <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth import authenticate , login , logout <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . conf . urls . static import static , settings <EOL> import json <EOL> from registrar . models import Course <EOL> from registrar . models import Teacher <EOL> from registrar . models import Policy <EOL> from teacher . views import policy <EOL> TEST_USER_EMAIL = "<STR_LIT>" <EOL> TEST_USER_USERNAME = "<STR_LIT>" <EOL> TEST_USER_PASSWORD = "<STR_LIT>" <EOL> TEST_USER_EMAIL2 = "<STR_LIT>" <EOL> TEST_USER_USERNAME2 = "<STR_LIT>" <EOL> TEST_USER_PASSWORD2 = "<STR_LIT>" <EOL> class PolicyTestCase ( TestCase ) : <EOL> def tearDown ( self ) : <EOL> policies = Policy . objects . all ( ) <EOL> for policy in policies : <EOL> policy . delete ( ) <EOL> courses = Course . objects . all ( ) <EOL> for course in courses : <EOL> course . delete ( ) <EOL> User . objects . all ( ) . delete ( ) <EOL> def setUp ( self ) : <EOL> User . objects . create_user ( <EOL> email = TEST_USER_EMAIL2 , <EOL> username = TEST_USER_USERNAME2 , <EOL> password = TEST_USER_PASSWORD2 <EOL> ) <EOL> user = User . objects . get ( email = TEST_USER_EMAIL2 ) <EOL> teacher = Teacher . objects . create ( user = user ) <EOL> User . objects . create_user ( <EOL> email = TEST_USER_EMAIL , <EOL> username = TEST_USER_USERNAME , <EOL> password = TEST_USER_PASSWORD , <EOL> ) <EOL> user = User . objects . get ( email = TEST_USER_EMAIL ) <EOL> teacher = Teacher . objects . create ( user = user ) <EOL> Course . objects . create ( <EOL> id = <NUM_LIT:1> , <EOL> title = "<STR_LIT>" , <EOL> sub_title = "<STR_LIT>" , <EOL> category = "<STR_LIT>" , <EOL> teacher = teacher , <EOL> ) <EOL> def get_logged_in_client ( self ) : <EOL> client = Client ( ) <EOL> client . login ( <EOL> username = TEST_USER_USERNAME , <EOL> password = TEST_USER_PASSWORD <EOL> ) <EOL> return client <EOL> def get_logged_in_trudy_client ( self ) : <EOL> client = Client ( ) <EOL> client . login ( <EOL> username = TEST_USER_USERNAME2 , <EOL> password = TEST_USER_PASSWORD2 <EOL> ) <EOL> return client <EOL> def test_url_resolves_to_policy_page_view ( self ) : <EOL> found = resolve ( '<STR_LIT>' ) <EOL> self . assertEqual ( found . func , policy . policy_page ) <EOL> def test_policy_page_without_pdf_file ( self ) : <EOL> client = self . get_logged_in_client ( ) <EOL> response = client . post ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( b'<STR_LIT>' , response . content ) <EOL> self . assertIn ( b'<STR_LIT>' , response . content ) <EOL> def test_policy_modal ( self ) : <EOL> client = self . get_logged_in_client ( ) <EOL> response = client . post ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( b'<STR_LIT>' , response . content ) <EOL> def test_policy_page_with_pdf_file ( self ) : <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> client = self . get_logged_in_client ( ) <EOL> file_path = settings . MEDIA_ROOT + '<STR_LIT>' <EOL> with open ( file_path , '<STR_LIT:rb>' ) as fp : <EOL> self . assertTrue ( fp is not None ) <EOL> response = client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:file>' : fp , <EOL> } , ** kwargs ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> response = client . post ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertIn ( b'<STR_LIT>' , response . content ) <EOL> self . assertIn ( b'<STR_LIT>' , response . content ) <EOL> self . assertIn ( b'<STR_LIT>' , response . content ) <EOL> try : <EOL> Policy . objects . get ( policy_id = <NUM_LIT:1> ) . delete ( ) <EOL> except Policy . DoesNotExist : <EOL> pass <EOL> try : <EOL> Policy . objects . get ( policy_id = <NUM_LIT:2> ) . delete ( ) <EOL> except Policy . DoesNotExist : <EOL> pass <EOL> try : <EOL> Policy . objects . get ( policy_id = <NUM_LIT:3> ) . delete ( ) <EOL> except Policy . DoesNotExist : <EOL> pass <EOL> def test_save_policy ( self ) : <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> client = self . get_logged_in_client ( ) <EOL> file_path = settings . MEDIA_ROOT + '<STR_LIT>' <EOL> with open ( file_path , '<STR_LIT:rb>' ) as fp : <EOL> self . assertTrue ( fp is not None ) <EOL> response = client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:file>' : fp , <EOL> } , ** kwargs ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> json_string = response . content . decode ( encoding = '<STR_LIT>' ) <EOL> array = json . loads ( json_string ) <EOL> self . assertEqual ( array [ '<STR_LIT:message>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( array [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> try : <EOL> Policy . objects . get ( policy_id = <NUM_LIT:1> ) . delete ( ) <EOL> except Policy . DoesNotExist : <EOL> pass <EOL> try : <EOL> Policy . objects . get ( policy_id = <NUM_LIT:2> ) . delete ( ) <EOL> except Policy . DoesNotExist : <EOL> pass <EOL> try : <EOL> Policy . objects . get ( policy_id = <NUM_LIT:3> ) . delete ( ) <EOL> except Policy . DoesNotExist : <EOL> pass <EOL> def test_delete_policy_with_submission_and_correct_user ( self ) : <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> client = self . get_logged_in_client ( ) <EOL> file_path = settings . MEDIA_ROOT + '<STR_LIT>' <EOL> with open ( file_path , '<STR_LIT:rb>' ) as fp : <EOL> self . assertTrue ( fp is not None ) <EOL> response = client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:file>' : fp , <EOL> } , ** kwargs ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> json_string = response . content . decode ( encoding = '<STR_LIT>' ) <EOL> array = json . loads ( json_string ) <EOL> self . assertEqual ( array [ '<STR_LIT:message>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( array [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> response = client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } , ** kwargs ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> json_string = response . content . decode ( encoding = '<STR_LIT>' ) <EOL> array = json . loads ( json_string ) <EOL> self . assertEqual ( array [ '<STR_LIT:message>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( array [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> def test_delete_policy_with_submission_and_correct_user ( self ) : <EOL> kwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> client = self . get_logged_in_client ( ) <EOL> file_path = settings . MEDIA_ROOT + '<STR_LIT>' <EOL> with open ( file_path , '<STR_LIT:rb>' ) as fp : <EOL> self . assertTrue ( fp is not None ) <EOL> response = client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT:file>' : fp , <EOL> } , ** kwargs ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> json_string = response . content . decode ( encoding = '<STR_LIT>' ) <EOL> array = json . loads ( json_string ) <EOL> self . assertEqual ( array [ '<STR_LIT:message>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( array [ '<STR_LIT:status>' ] , '<STR_LIT:success>' ) <EOL> client . logout ( ) <EOL> client = self . get_logged_in_trudy_client ( ) <EOL> response = client . post ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } , ** kwargs ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> json_string = response . content . decode ( encoding = '<STR_LIT>' ) <EOL> array = json . loads ( json_string ) <EOL> self . assertEqual ( array [ '<STR_LIT:message>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( array [ '<STR_LIT:status>' ] , '<STR_LIT>' ) </s>
