<s> import json <EOL> from unittest . mock import patch <EOL> from django . test import Client as RequestClient <EOL> from django . test import override_settings <EOL> from django . test import TestCase <EOL> from django . test import TransactionTestCase <EOL> from orchestra . tests . helpers . notifications import MockMail <EOL> from orchestra . tests . helpers . fixtures import UserFactory <EOL> from orchestra . communication . tests . helpers . slack import MockSlacker <EOL> import logging <EOL> logging . disable ( logging . CRITICAL ) <EOL> class OrchestraTestHelpersMixin ( object ) : <EOL> def setUp ( self ) : <EOL> super ( ) . setUp ( ) <EOL> self . maxDiff = None <EOL> self . slack = MockSlacker ( ) <EOL> patcher = patch ( '<STR_LIT>' , <EOL> return_value = self . slack ) <EOL> patcher . start ( ) <EOL> self . addCleanup ( patcher . stop ) <EOL> self . addCleanup ( self . slack . clear ) <EOL> self . mail = MockMail ( ) <EOL> patcher = patch ( '<STR_LIT>' , <EOL> side_effect = self . mail . send_mail ) <EOL> patcher . start ( ) <EOL> self . addCleanup ( patcher . stop ) <EOL> self . addCleanup ( self . mail . clear ) <EOL> def ensure_response ( self , <EOL> response , <EOL> expected_json_payload , <EOL> expected_status_code ) : <EOL> self . assertEquals ( response . status_code , expected_status_code ) <EOL> returned = json . loads ( response . content . decode ( '<STR_LIT:utf-8>' ) ) <EOL> self . assertEquals ( returned , <EOL> expected_json_payload ) <EOL> def _submit_assignment ( self , client , task_id , data = None , <EOL> seconds = <NUM_LIT:1> , command = '<STR_LIT>' ) : <EOL> if data is None : <EOL> data = { '<STR_LIT:test>' : '<STR_LIT:test>' } <EOL> request = json . dumps ( <EOL> { '<STR_LIT>' : task_id , '<STR_LIT>' : data , '<STR_LIT>' : command } ) <EOL> return client . post ( <EOL> '<STR_LIT>' , <EOL> request , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> def assertModelInstanceExists ( self , <EOL> model_class , <EOL> lookup_attributes , <EOL> check_attributes = { } ) : <EOL> try : <EOL> model_instance = model_class . objects . get ( ** lookup_attributes ) <EOL> except model_class . DoesNotExist : <EOL> self . fail ( '<STR_LIT>' <EOL> . format ( str ( model_class ) ) ) <EOL> for attribute_name , expected_value in check_attributes . items ( ) : <EOL> self . assertEqual ( getattr ( model_instance , attribute_name ) , <EOL> expected_value ) <EOL> return model_instance <EOL> def assertModelInstanceNotExists ( self , model_class , lookup_attributes ) : <EOL> with self . assertRaises ( model_class . DoesNotExist ) : <EOL> model_class . objects . get ( ** lookup_attributes ) <EOL> class AuthenticatedUserMixin ( object ) : <EOL> def authenticate_user ( self ) : <EOL> request_client = RequestClient ( ) <EOL> password = '<STR_LIT:test>' <EOL> auth_user = UserFactory ( <EOL> username = '<STR_LIT>' , <EOL> email = '<STR_LIT>' , <EOL> first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> is_active = True , <EOL> password = password ) <EOL> self . assertTrue ( request_client . login ( <EOL> username = auth_user . username , password = password ) ) <EOL> return request_client , auth_user <EOL> @ override_settings ( SLACK_STAFFBOT_TOKEN = '<STR_LIT>' ) <EOL> class OrchestraTestCase ( OrchestraTestHelpersMixin , TestCase ) : <EOL> pass <EOL> @ override_settings ( SLACK_STAFFBOT_TOKEN = '<STR_LIT>' ) <EOL> class OrchestraTransactionTestCase ( OrchestraTestHelpersMixin , <EOL> TransactionTestCase ) : <EOL> pass <EOL> @ override_settings ( SLACK_STAFFBOT_TOKEN = '<STR_LIT>' ) <EOL> class OrchestraAuthenticatedTestCase ( OrchestraTestCase , <EOL> AuthenticatedUserMixin ) : <EOL> pass </s>
