def test_create_admin_email_missed(self): args = {'email': '*****@*****.**'} db_adapter = Mock() db_adapter.find_first_object.return_value = None am = AdminManager(db_adapter) self.assertEqual(am.create_admin(args), not_found("email does not exist in DB")) db_adapter.find_first_object.assert_called_once_with(UserEmail, ANY)
def test_update_admin_raise_exception(self, generate_update_items, validate_updated_args): self.assertEqual(1, 1) db_adapter = Mock() args = {'test': 'test'} db_adapter.update_object.side_effect = [Exception] am = AdminManager(db_adapter) self.assertEqual(am.update_admin(args), internal_server_error(ANY)) generate_update_items.assert_called_once_with(args) validate_updated_args.assert_called_once_with(args)
def test_validate_deleted_args_already_missed(self): db_adapter = Mock() db_adapter.find_first_object.return_value = None am = AdminManager(db_adapter) status, return_info = am.validate_deleted_args(1) self.assertFalse(status) self.assertEqual(return_info, ok()) db_adapter.find_first_object(AdminHackathonRel, ANY)
def test_validate_updated_args_ahl_not_found(self): args = {'id': 7} db_adapter = Mock() db_adapter.find_first_object.return_value = None am = AdminManager(db_adapter) status, return_info = am.validate_updated_args(args) self.assertFalse(status) self.assertEqual(return_info, not_found("ahl does not exist")) db_adapter.find_first_object.assert_called_once_with(AdminHackathonRel, ANY)
def test_validate_updated_args_common_logic(self): args = {'id': 7} ahl = AdminHackathonRel(id=7) db_adapter = Mock() db_adapter.find_first_object.return_value = ahl am = AdminManager(db_adapter) status, return_info = am.validate_updated_args(args) self.assertTrue(status) self.assertEqual(return_info, ahl) db_adapter.find_first_object.assert_called_once_with(AdminHackathonRel, ANY)
def test_update_admin_common_logic(self, validate_updated_args): db_adapter = Mock() args = {'role_type': 1, 'email': 'test'} db_adapter.update_object.return_value = 'passed' am = AdminManager(db_adapter) with mock.patch('hackathon.admin.admin_mgr.get_now') as get_now: get_now.return_value = "now" self.assertEqual(am.update_admin(args), ok('update hackathon admin success')) validate_updated_args.assert_called_once_with(args) db_adapter.update_object.assert_called_once_with(ANY, role_type=1, update_time='now')
def test_validate_request_token_not_found(self): token_value = "token_value" mock_db = Mock() mock_db.find_first_object_by.return_value = None am = AdminManager(mock_db) with app.test_request_context('/api', headers={"token": token_value}): self.assertTrue("token" in request.headers) self.assertFalse(am.validate_request()) mock_db.find_first_object_by.assert_called_once_with(AdminToken, token=token_value)
def test_validate_deleted_args_delete_creator(self): ahl = AdminHackathonRel(id=7, user_id=7, hackathon_id=7) hackathon = Hackathon(id=7, creator_id=7) db_adapter = Mock() db_adapter.find_first_object.side_effect = [ahl, hackathon] am = AdminManager(db_adapter) status, return_info = am.validate_deleted_args(1) self.assertFalse(status) self.assertEqual(return_info, precondition_failed("hackathon creator can not be deleted")) self.assertEqual(db_adapter.find_first_object.call_count, 2)
def test_validate_request_if_token_missing(self): am = AdminManager(None) '''more args for app.text_request_context: path='/', base_url=None, query_string=None, method='GET', input_stream=None, content_type=None, content_length=None, errors_stream=None, multithread=False, multiprocess=False, run_once=False, headers=None, data=None, environ_base=None, environ_overrides=None, charset='utf-8' ''' with app.test_request_context('/', headers=None): self.assertFalse("token" in request.headers) self.assertFalse(am.validate_request())
def test_get_hackid_by_adminid(self): admin_email_test = [AdminEmail(email='*****@*****.**')] admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=-1)] mock_db = Mock() mock_db.find_all_objects_by.return_value = admin_email_test mock_db.find_all_objects.return_value = admin_user_hackathon_rel am = AdminManager(mock_db) self.assertEqual(am.get_hack_id_by_admin_id(1), [-1L]) mock_db.find_all_objects_by.assert_called_once_with(AdminEmail, admin_id=1) mock_db.find_all_objects.assert_called_once_with(AdminUserHackathonRel, ANY)
def test_validate_request_token_expired(self): token_value = "token_value" token = AdminToken(token=token_value, admin=None, expire_date=datetime.utcnow() - timedelta(seconds=30)) mock_db = Mock() mock_db.find_first_object_by.return_value = token am = AdminManager(mock_db) with app.test_request_context('/', headers={"token": token_value}): self.assertTrue("token" in request.headers) self.assertFalse(am.validate_request()) mock_db.find_first_object_by.assert_called_once_with(AdminToken, token=token_value)
def test_create_admin_already_exist(self): args = {'email': '*****@*****.**'} user = User(id=7) user_email = UserEmail(id=7, user=user, user_id=7) db_adapter = Mock() db_adapter.find_first_object.side_effect = [user_email, "test"] am = AdminManager(db_adapter) with app.test_request_context('/', headers=None): g.hackathon = Hackathon(id=7) self.assertEqual(am.create_admin(args), ok()) self.assertEqual(db_adapter.find_first_object.call_count, 2)
def test_validate_admin_hackathon_request_havnt_authority(self): token_value = "token_value" admin_email_test = [AdminEmail(email='*****@*****.**')] admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=1L)] mock_db = Mock() mock_db.find_all_objects_by.return_value = admin_email_test mock_db.find_all_objects.return_value = admin_user_hackathon_rel am = AdminManager(mock_db) with app.test_request_context('/', headers={"token": token_value}): g.admin = AdminUser(id=1, name='testadmin') self.assertFalse(am.validate_admin_hackathon_request(2)) mock_db.find_all_objects_by.assert_called_once_with(AdminEmail, admin_id=1) mock_db.find_all_objects.assert_called_once_with(AdminUserHackathonRel, ANY)
def test_get_hackid_by_adminid(self): admin_email_test = [AdminEmail(email='*****@*****.**')] admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=-1)] mock_db = Mock() mock_db.find_all_objects_by.return_value = admin_email_test mock_db.find_all_objects.return_value = admin_user_hackathon_rel am = AdminManager(mock_db) self.assertEqual(am.get_hack_id_by_admin_id(1), [-1L]) mock_db.find_all_objects_by.assert_called_once_with(AdminEmail, admin_id=1) mock_db.find_all_objects.assert_called_once_with( AdminUserHackathonRel, ANY)
def test_create_admin_common_logic(self): args = {'email': '*****@*****.**', 'role_type': 1, 'remarks': 'test'} user = User(id=7) user_email = UserEmail(id=7, user=user, user_id=7) db_adapter = Mock() db_adapter.find_first_object.side_effect = [user_email, None] db_adapter.add_object.return_value = "success" am = AdminManager(db_adapter) with app.test_request_context('/', headers=None): g.hackathon = Hackathon(id=7) self.assertEqual(am.create_admin(args), ok()) self.assertEqual(db_adapter.find_first_object.call_count, 2) db_adapter.add_object.assert_called_once_with(ANY)
def test_validate_request_token_expired(self): token_value = "token_value" token = AdminToken(token=token_value, admin=None, expire_date=datetime.utcnow() - timedelta(seconds=30)) mock_db = Mock() mock_db.find_first_object_by.return_value = token am = AdminManager(mock_db) with app.test_request_context('/', headers={"token": token_value}): self.assertTrue("token" in request.headers) self.assertFalse(am.validate_request()) mock_db.find_first_object_by.assert_called_once_with( AdminToken, token=token_value)
def test_validate_admin_hackathon_request_havnt_authority(self): token_value = "token_value" admin_email_test = [AdminEmail(email='*****@*****.**')] admin_user_hackathon_rel = [AdminUserHackathonRel(hackathon_id=1L)] mock_db = Mock() mock_db.find_all_objects_by.return_value = admin_email_test mock_db.find_all_objects.return_value = admin_user_hackathon_rel am = AdminManager(mock_db) with app.test_request_context('/', headers={"token": token_value}): g.admin = AdminUser(id=1, name='testadmin') self.assertFalse(am.validate_admin_hackathon_request(2)) mock_db.find_all_objects_by.assert_called_once_with(AdminEmail, admin_id=1) mock_db.find_all_objects.assert_called_once_with( AdminUserHackathonRel, ANY)
def test_validate_admin_hackathon_request_token_missing(self): am = AdminManager(None) with app.test_request_context('/', headers=None): g.admin = AdminUser(id=1, name='testadmin') self.assertTrue(am.validate_admin_hackathon_request(1)) self.assertFalse('token' in request.headers)
def test_delete_admin_common_logic(self, validate_deleted_args): db_adapter = Mock() db_adapter.delete_all_objects.side_effect = 'passed' am = AdminManager(db_adapter) self.assertEqual(am.delete_admin(1), ok()) validate_deleted_args.assert_called_once_with(1)
def test_delete_admin_raise_exception(self, validate_deleted_args): db_adapter = Mock() db_adapter.delete_all_objects.side_effect = [Exception] am = AdminManager(db_adapter) self.assertEqual(am.delete_admin(1), internal_server_error(ANY)) validate_deleted_args.assert_called_once_with(1)