def setUp(self): self.app = create_app('TESTING_CONFIG') self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client() self.basedir = os.path.abspath(os.path.dirname(__file__)) db.create_all() test_password = '******' Organization.insert_org() UserScope.insert_scopes() User.insert_user(password=test_password) self.client = self.app.test_client(use_cookies=False) # set the vars for the connection self.cmisUrl = \ 'https://alfresco.oceanobservatories.org/alfresco/s/api/cmis' self.cmisUsername = '******' self.cmisPassword = '******' self.cmisId = 'c161bc66-4f7e-4a4f-b5f2-aac9fbf1d3cd' # cmis is tested elsewhere from cmislib.model import CmisClient client = CmisClient(self.cmisUrl, self.cmisUsername, self.cmisPassword) repo = client.getRepository(self.cmisId)
def setUp(self): self.app = create_app('TESTING_CONFIG') self.app_context = self.app.app_context() self.app_context.push() from sqlalchemy.orm.mapper import configure_mappers configure_mappers() db.create_all() test_username = '******' test_password = '******' Organization.insert_org() User.insert_user(username=test_username, password=test_password, email='*****@*****.**') OperatorEventType.insert_operator_event_types() self.client = self.app.test_client(use_cookies=False) UserScope.insert_scopes() admin = User.query.filter_by(user_name='admin').first() scope = UserScope.query.filter_by(scope_name='user_admin').first() admin.scopes.append(scope) db.session.add(admin) db.session.commit() joe = User.insert_user(username='******', password='******', email='*****@*****.**') bob = User.insert_user(username='******', password='******', email='*****@*****.**')
def test_organization(self): content_type = 'application/json' # Create Organization data organization = Organization() organization.organization_name = 'Hyperion' db.session.add(organization) db.session.commit() # get all organizations response = self.client.get('/organization', content_type=content_type) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals( data, {'organizations': [{ 'id': 1, 'organization_name': 'Hyperion' }]}) # Get organization by id response = self.client.get('/organization/1', content_type=content_type) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data, {'id': 1, 'organization_name': 'Hyperion'}) # Get non-existant organization (bad id value); expect failure response = self.client.get('/organization/999', content_type=content_type) self.assertEquals(response.status_code, 204)
def setUp(self): self.app = create_app('TESTING_CONFIG') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client(use_cookies=False) Organization.insert_org()
def test_organization(self): organization = Organization() organization.organization_name = 'HPN' organization.organization_long_name = 'Hyperion' db.session.add(organization) db.session.commit() response = self.client.get('/organization', content_type='application/json') self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals( data, { 'organizations': [{ 'id': 1, 'organization_name': 'HPN', 'organization_long_name': 'Hyperion', 'image_url': None }] }) response = self.client.get('/organization/1', content_type='application/json') self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals( data, { 'id': 1, 'organization_name': 'HPN', 'organization_long_name': 'Hyperion', 'image_url': None })
def setUp(self): self.app = create_app('TESTING_CONFIG') self.app_context = self.app.app_context() self.app_context.push() from sqlalchemy.orm.mapper import configure_mappers configure_mappers() db.create_all() test_username = '******' test_password = '******' Organization.insert_org() User.insert_user(username=test_username, password=test_password, email='*****@*****.**') OperatorEventType.insert_operator_event_types() self.client = self.app.test_client(use_cookies=False) UserScope.insert_scopes() admin = User.query.filter_by(user_name='admin').first() scope = UserScope.query.filter_by(scope_name='asset_manager').first() admin.scopes.append(scope) db.session.add(admin) db.session.commit() joe = User.insert_user(username='******', password='******', email='*****@*****.**') bob = User.insert_user(username='******', password='******', email='*****@*****.**')
def setUp(self): self.app = create_app('TESTING_CONFIG') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client(use_cookies=False) Organization.insert_org() User.insert_user(username=test_username, password=test_password) UserScope.insert_scopes()
def setUp(self): self.app = create_app("TESTING_CONFIG") self.app_context = self.app.app_context() self.app_context.push() db.create_all() test_password = "******" Organization.insert_org() User.insert_user(password=test_password, email="test@localhost") self.client = self.app.test_client(use_cookies=False)
def setUp(self): self.app = create_app('TESTING_CONFIG') self.app_context = self.app.app_context() self.app_context.push() db.create_all() test_username = '******' test_password = '******' Organization.insert_org() User.insert_user(username=test_username, password=test_password) self.client = self.app.test_client(use_cookies=False) UserScope.insert_scopes() admin = User.query.filter_by(user_name='admin').first() scope = UserScope.query.filter_by(scope_name='user_admin').first() admin.scopes.append(scope) db.session.add(admin) db.session.commit()
def test_organization(self): organization = Organization() organization.organization_name = 'HPN' organization.organization_long_name = 'Hyperion' db.session.add(organization) db.session.commit() response = self.client.get('/organization', content_type='application/json') self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data, {'organizations':[{'id':1, 'organization_name' : 'HPN', 'organization_long_name':'Hyperion', 'image_url':None}]}) response = self.client.get('/organization/1', content_type='application/json') self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data, {'id':1, 'organization_name':'HPN', 'organization_long_name' : 'Hyperion', 'image_url':None})
def setUp(self): self.app = create_app('TESTING_CONFIG') self.app_context = self.app.app_context() self.app_context.push() db.create_all() test_password = '******' Organization.insert_org() UserScope.insert_scopes() User.insert_user(password=test_password) self.client = self.app.test_client(use_cookies=False) self.basedir = os.path.abspath(os.path.dirname(__file__)) with open(self.basedir + '/mock_data/event_post.json', 'r') as f: doc = json.load(f) self.event_json_in = doc with open(self.basedir + '/mock_results/event_from.json', 'r') as f: doc = json.load(f) self.event_from_json = doc
def deploy(password, bulkload): from flask.ext.migrate import upgrade from ooiservices.app.models import User, UserScope, UserScopeLink, Array, Organization from ooiservices.app.models import PlatformDeployment, InstrumentDeployment, Stream, StreamParameterLink from sh import psql #Create the local database app.logger.info('Creating DEV and TEST Databases') psql('-c', 'create database ooiuidev;', '-U', 'postgres') psql('ooiuidev', '-c', 'create schema ooiui') psql('ooiuidev', '-c', 'create extension postgis') #Create the local test database psql('-c', 'create database ooiuitest;', '-U', 'postgres') psql('ooiuitest', '-c', 'create schema ooiui') psql('ooiuitest', '-c', 'create extension postgis') from sqlalchemy.orm.mapper import configure_mappers configure_mappers() db.create_all() if bulkload: with open('db/ooiui_schema_data.sql') as f: psql('ooiuidev', _in=f) app.logger.info('Bulk test data loaded.') # migrate database to latest revision #upgrade() if not os.getenv('TRAVIS'): Organization.insert_org() UserScope.insert_scopes() app.logger.info('Insert default user, name: admin') User.insert_user(password=password) admin = User.query.first() admin.scopes.append( UserScope.query.filter_by(scope_name='user_admin').first()) admin.scopes.append( UserScope.query.filter_by(scope_name='redmine').first()) db.session.add(admin) db.session.commit() if bulkload: with open('db/ooiui_schema_data_notifications.sql') as f: psql('ooiuidev', _in=f) app.logger.info('Bulk test data loaded for notifications.')
def setUp(self): self.app = create_app("TESTING_CONFIG") self.app_context = self.app.app_context() self.app_context.push() db.create_all() test_username = "******" test_password = "******" Organization.insert_org() User.insert_user(username=test_username, password=test_password) self.client = self.app.test_client(use_cookies=False) UserScope.insert_scopes() admin = User.query.filter_by(user_name="admin").first() scope = UserScope.query.filter_by(scope_name="user_admin").first() cc_scope = UserScope.query.filter_by(scope_name="command_control").first() admin.scopes.append(scope) admin.scopes.append(cc_scope) db.session.add(admin) db.session.commit() self.headers = self.get_api_headers("admin", "test")
def deploy(password, bulkload): from flask.ext.migrate import upgrade from ooiservices.app.models import User, UserScope, UserScopeLink, Array, Organization from ooiservices.app.models import PlatformDeployment, InstrumentDeployment, Stream, StreamParameterLink from sh import psql #Create the local database app.logger.info('Creating DEV and TEST Databases') psql('-c', 'create database ooiuidev;', '-U', 'postgres') psql('ooiuidev', '-c', 'create schema ooiui') psql('ooiuidev', '-c', 'create extension postgis') #Create the local test database psql('-c', 'create database ooiuitest;', '-U', 'postgres') psql('ooiuitest', '-c', 'create schema ooiui') psql('ooiuitest', '-c', 'create extension postgis') from sqlalchemy.orm.mapper import configure_mappers configure_mappers() db.create_all() if bulkload: with open('db/ooiui_schema_data.sql') as f: psql('ooiuidev', _in=f) app.logger.info('Bulk test data loaded.') # migrate database to latest revision #upgrade() if not os.getenv('TRAVIS'): Organization.insert_org() UserScope.insert_scopes() app.logger.info('Insert default user, name: admin') User.insert_user(password=password) admin = User.query.first() admin.scopes.append(UserScope.query.filter_by(scope_name='user_admin').first()) admin.scopes.append(UserScope.query.filter_by(scope_name='redmine').first()) db.session.add(admin) db.session.commit() if bulkload: with open('db/ooiui_schema_data_notifications.sql') as f: psql('ooiuidev', _in=f) app.logger.info('Bulk test data loaded for notifications.')
def test_operator_framework(self): # Create a new watch headers = self.get_api_headers('admin', 'test') org = Organization(organization_name='Hello') org.users db.session.add(org) db.session.commit() org_id = org.id # no watches, get watch (/watch/open), expect 204 (no watches currently open) response = self.client.get('/watch/open', headers=headers) self.assertEquals(response.status_code, 204) response = self.client.post('/watch', headers=headers) self.assertEquals(response.status_code, 201) data = json.loads(response.data) self.assertIn('id', data) watch_id = data['id'] # Test [POST] post watch when watch already open; expect failure (405) response = self.client.post('/watch', headers=headers) self.assertEquals(response.status_code, 405) # Verify watch exists response = self.client.get('/watch/user', headers=headers) self.assertEquals(response.status_code, 200) data = json.loads(response.data) watch = data['watches'][0] self.assertEquals(watch['id'], watch_id) # Get open watches response = self.client.get('/watch/open', headers=headers) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data['id'], watch_id) #Test [GET] /operator_event/<int:operator_event_id> - 'main.get_operator_event' response = self.client.get('/operator_event/1') self.assertEquals(response.status_code, 204) data = { 'operator_event_type_id': 1, 'event_title': 'Event Title', 'event_comment': 'Event Comment' } # Create a new operator event response = self.client.get('/operator_event', headers=headers) self.assertEquals(response.status_code, 204) response = self.client.post('/operator_event', data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 201) data = json.loads(response.data) data_event_1 = response.data[:] self.assertIn('id', data) event_id = data['id'] #Test [GET] /operator_event/<int:operator_event_id> - 'main.get_operator_event' response = self.client.get('/operator_event/%s' % event_id, headers=headers) self.assertEquals(response.status_code, 200) # Get events for the current watch response = self.client.get('/operator_event?watch_id=%s' % watch_id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data['operator_events'][0]['id'], event_id) # Close the current watch response = self.client.put('/watch/%s' % watch_id, headers=headers, data='{}') self.assertEquals(response.status_code, 201) # watch is closed, get watch (/watch/open), expect 204 (for no watches currently open) response = self.client.get('/watch/open', headers=headers) self.assertEquals(response.status_code, 204) # Try to create an event with no open watches (careful with data here!!) response = self.client.post('/operator_event', data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 400) #Test [GET] /watch - 'main.get_watches' response = self.client.get('/watch', headers=headers) self.assertEquals(response.status_code, 200) response = self.client.get('/watch?organization_id=%s' % org_id) self.assertEquals(response.status_code, 204) # Test [GET] non existent watch_id (999) response = self.client.get('/watch/999') self.assertEquals(response.status_code, 204) #Test [GET] /operator_event_type - 'main.get_operator_event_types' response = self.client.get('/operator_event_type') self.assertEquals(response.status_code, 200) #Test [GET] /operator_event/<int:operator_event_id> - 'main.get_operator_event' response = self.client.get('/operator_event/1', headers=headers) self.assertEquals(response.status_code, 200) #Test [POST] /operator_event - 'main.post_operator_event' # Post operator_event with data={} response = self.client.post('/operator_event', data='{}', headers=headers) self.assertEquals(response.status_code, 400)
def test_update_user_event_notification(self): verbose = False #self.verbose root = self.root if verbose: print '\n' content_type = 'application/json' headers = self.get_api_headers('admin', 'test') #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Add a second user ('foo', password 'test') #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test_username = '******' test_password = '******' test_email = '*****@*****.**' Organization.insert_org() User.insert_user(username=test_username, password=test_password, email=test_email) self.client = self.app.test_client(use_cookies=False) UserScope.insert_scopes() foo = User.query.filter_by(user_name='foo').first() scope = UserScope.query.filter_by(scope_name='user_admin').first() foo.scopes.append(scope) scope = UserScope.query.filter_by( scope_name='redmine').first() # added foo.scopes.append(scope) db.session.add(foo) db.session.commit() response = self.client.get(url_for('main.get_user', id=1), headers=headers) self.assertTrue(response.status_code == 200) response = self.client.get(url_for('main.get_user', id=2), headers=headers) self.assertTrue(response.status_code == 200) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Create alert and an alarm #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ref_def = "CE01ISSP-XX099-01-CTDPFJ999" # Create an alarm with user_event_notification - uses definition 1 and user_id 1 test_alarm = self.create_alert_alarm_definition(ref_def, event_type='alarm', uframe_id=2, severity=1) # Create an alarm without user_event_notification - uses definition 1 and user_id 1 bad_alarm = self.create_alert_alarm_definition_wo_notification( ref_def, event_type='alarm', uframe_filter_id=2, severity=1) notification = self.create_user_event_notification(bad_alarm.id, 2) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # GET alarm definition by SystemEventDefinition id #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - response = self.client.get(url_for('main.get_alert_alarm_def', id=test_alarm.id), headers=headers) self.assertEquals(response.status_code, 200) alarm_definition = json.loads(response.data) self.assertTrue(alarm_definition is not None) response = self.client.get(url_for('main.get_alert_alarm_def', id=bad_alarm.id), headers=headers) self.assertEquals(response.status_code, 200) bad_alarm_definition = json.loads(response.data) self.assertTrue(bad_alarm_definition is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Get user_event_notifications (1) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - response = self.client.get( url_for('main.get_user_event_notifications'), headers=headers) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertTrue(data is not None) notifications = data['notifications'] self.assertTrue(notifications is not None) self.assertEquals(len(notifications), 2) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Get user_event_notification by id=1 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - url = url_for('main.get_user_event_notification', id=1) response = self.client.get(url, content_type=content_type, headers=headers) self.assertEquals(response.status_code, 200) notification = json.loads(response.data) self.assertTrue(len(notification) > 0) self.assertEquals(len(notification), 8) """ Error messages for the following tests: 1. bad_notification: {} 2. 'Invalid ID, user_event_notification record not found.' 3. 'Inconsistent ID, user_event_notification id provided in data does not match id provided.' 4. 'Inconsistent User ID, user_id provided in data does not match id.' 5. 'IntegrityError creating user_event_notification.' 6. (no error) 7. 'Insufficient data, or bad data format.' """ #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (1) Get user_event_notification by id=5 (doesn't exist) response: {} #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - url = url_for('main.get_user_event_notification', id=5) response = self.client.get(url, content_type=content_type, headers=headers) self.assertEquals(response.status_code, 200) bad_notification = json.loads(response.data) self.assertTrue(bad_notification is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (2) (Negative) Update event_event_notification; # error: 'Invalid ID, user_event_notification record not found.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = { 'user_id': 1, 'system_event_definition_id': test_alarm.id, 'use_email': False, 'use_log': False, 'use_phone': False, 'use_redmine': False, 'use_sms': False, 'id': 50 } bad_stuff = json.dumps(test) response = self.client.put(url_for( 'main.update_user_event_notification', id=50), headers=headers, data=bad_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (3) (Negative) Update event_event_notification # error: 'Inconsistent ID, user_event_notification id provided in data does not match id provided.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = { 'user_id': 999, 'system_event_definition_id': 1, 'use_email': True, 'use_log': True, 'use_phone': True, 'use_redmine': True, 'use_sms': True, 'id': 1 } good_stuff = json.dumps(test) response = self.client.put(url_for( 'main.update_user_event_notification', id=800), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (4) (Negative) Update user_event_notification, with invalid user_id # error: 'Inconsistent User ID, user_id provided in data does not match id.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = { 'user_id': 1, 'system_event_definition_id': 2, 'use_email': True, 'use_log': True, 'use_phone': True, 'use_redmine': True, 'use_sms': True, 'id': 2 } good_stuff = json.dumps(test) response = self.client.put(url_for( 'main.update_user_event_notification', id=2), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (5) # (Negative) Update event_event_notification # error: 'IntegrityError creating user_event_notification.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = { 'user_id': 2, 'system_event_definition_id': bad_alarm.id, 'use_email': False, 'use_log': 'log', 'use_phone': False, 'use_redmine': False, 'use_sms': False, 'id': 2 } bad_stuff = json.dumps(test) response = self.client.put(url_for( 'main.update_user_event_notification', id=2), headers=headers, data=bad_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (6) (Positive) Update event_event_notification #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = { 'user_id': 1, 'system_event_definition_id': 1, 'use_email': True, 'use_log': True, 'use_phone': True, 'use_redmine': True, 'use_sms': True, 'id': 1 } good_stuff = json.dumps(test) response = self.client.put(url_for( 'main.update_user_event_notification', id=1), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 201) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue(len(notify_data) > 0) notify = UserEventNotification.query.get(1) for attribute in UserEventNotification.__table__.columns._data: self.assertTrue(attribute in notify_data) if attribute != 'user_id' or attribute != 'id': self.assertEquals(getattr(notify, attribute), True) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (Negative) Update event_event_notification - expect failure, invalid user_id # error 'Insufficient data, or bad data format.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - data = {'user_id': 10} good_stuff = json.dumps(data) response = self.client.put(url_for( 'main.update_user_event_notification', id=1), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 409) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) if verbose: print '\n'
def test_update_user_event_notification(self): verbose = False #self.verbose root = self.root if verbose: print '\n' content_type = 'application/json' headers = self.get_api_headers('admin', 'test') #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Add a second user ('foo', password 'test') #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test_username = '******' test_password = '******' test_email = '*****@*****.**' Organization.insert_org() User.insert_user(username=test_username, password=test_password, email=test_email) self.client = self.app.test_client(use_cookies=False) UserScope.insert_scopes() foo = User.query.filter_by(user_name='foo').first() scope = UserScope.query.filter_by(scope_name='user_admin').first() foo.scopes.append(scope) scope = UserScope.query.filter_by(scope_name='redmine').first() # added foo.scopes.append(scope) db.session.add(foo) db.session.commit() response = self.client.get(url_for('main.get_user',id=1), headers=headers) self.assertTrue(response.status_code == 200) response = self.client.get(url_for('main.get_user',id=2), headers=headers) self.assertTrue(response.status_code == 200) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Create alert and an alarm #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ref_def = "CE01ISSP-XX099-01-CTDPFJ999" # Create an alarm with user_event_notification - uses definition 1 and user_id 1 test_alarm = self.create_alert_alarm_definition(ref_def, event_type='alarm', uframe_id=2, severity=1) # Create an alarm without user_event_notification - uses definition 1 and user_id 1 bad_alarm = self.create_alert_alarm_definition_wo_notification(ref_def, event_type='alarm', uframe_filter_id=2, severity=1) notification = self.create_user_event_notification(bad_alarm.id, 2) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # GET alarm definition by SystemEventDefinition id #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - response = self.client.get(url_for('main.get_alert_alarm_def', id=test_alarm.id), headers=headers) self.assertEquals(response.status_code, 200) alarm_definition = json.loads(response.data) self.assertTrue(alarm_definition is not None) response = self.client.get(url_for('main.get_alert_alarm_def', id=bad_alarm.id), headers=headers) self.assertEquals(response.status_code, 200) bad_alarm_definition = json.loads(response.data) self.assertTrue(bad_alarm_definition is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Get user_event_notifications (1) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - response = self.client.get(url_for('main.get_user_event_notifications'), headers=headers) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertTrue(data is not None) notifications = data['notifications'] self.assertTrue(notifications is not None) self.assertEquals(len(notifications), 2) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Get user_event_notification by id=1 #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - url = url_for('main.get_user_event_notification', id=1) response = self.client.get(url, content_type=content_type, headers=headers) self.assertEquals(response.status_code, 200) notification = json.loads(response.data) self.assertTrue(len(notification) > 0) self.assertEquals(len(notification), 8) """ Error messages for the following tests: 1. bad_notification: {} 2. 'Invalid ID, user_event_notification record not found.' 3. 'Inconsistent ID, user_event_notification id provided in data does not match id provided.' 4. 'Inconsistent User ID, user_id provided in data does not match id.' 5. 'IntegrityError creating user_event_notification.' 6. (no error) 7. 'Insufficient data, or bad data format.' """ #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (1) Get user_event_notification by id=5 (doesn't exist) response: {} #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - url = url_for('main.get_user_event_notification', id=5) response = self.client.get(url, content_type=content_type, headers=headers) self.assertEquals(response.status_code, 200) bad_notification = json.loads(response.data) self.assertTrue(bad_notification is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (2) (Negative) Update event_event_notification; # error: 'Invalid ID, user_event_notification record not found.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = {'user_id': 1, 'system_event_definition_id': test_alarm.id, 'use_email': False, 'use_log': False, 'use_phone': False, 'use_redmine': False, 'use_sms': False, 'id': 50} bad_stuff = json.dumps(test) response = self.client.put(url_for('main.update_user_event_notification', id=50), headers=headers, data=bad_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (3) (Negative) Update event_event_notification # error: 'Inconsistent ID, user_event_notification id provided in data does not match id provided.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = {'user_id': 999, 'system_event_definition_id': 1, 'use_email': True, 'use_log': True, 'use_phone': True, 'use_redmine': True, 'use_sms': True, 'id': 1} good_stuff = json.dumps(test) response = self.client.put(url_for('main.update_user_event_notification', id=800), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (4) (Negative) Update user_event_notification, with invalid user_id # error: 'Inconsistent User ID, user_id provided in data does not match id.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = {'user_id': 1, 'system_event_definition_id': 2, 'use_email': True, 'use_log': True, 'use_phone': True, 'use_redmine': True, 'use_sms': True, 'id': 2} good_stuff = json.dumps(test) response = self.client.put(url_for('main.update_user_event_notification', id=2), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (5) # (Negative) Update event_event_notification # error: 'IntegrityError creating user_event_notification.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = {'user_id': 2, 'system_event_definition_id': bad_alarm.id, 'use_email': False, 'use_log': 'log', 'use_phone': False, 'use_redmine': False, 'use_sms': False, 'id': 2} bad_stuff = json.dumps(test) response = self.client.put(url_for('main.update_user_event_notification', id=2), headers=headers, data=bad_stuff) self.assertEquals(response.status_code, 400) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (6) (Positive) Update event_event_notification #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - test = {'user_id': 1, 'system_event_definition_id': 1, 'use_email': True, 'use_log': True, 'use_phone': True, 'use_redmine': True, 'use_sms': True, 'id': 1} good_stuff = json.dumps(test) response = self.client.put(url_for('main.update_user_event_notification', id=1), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 201) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue(len(notify_data) > 0) notify = UserEventNotification.query.get(1) for attribute in UserEventNotification.__table__.columns._data: self.assertTrue(attribute in notify_data) if attribute != 'user_id' or attribute != 'id': self.assertEquals(getattr(notify,attribute), True) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # (Negative) Update event_event_notification - expect failure, invalid user_id # error 'Insufficient data, or bad data format.' #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - data = {'user_id': 10} good_stuff = json.dumps(data) response = self.client.put(url_for('main.update_user_event_notification', id=1), headers=headers, data=good_stuff) self.assertEquals(response.status_code, 409) notify_data = json.loads(response.data) self.assertTrue(notify_data is not None) self.assertTrue('message' in notify_data) self.assertTrue(notify_data['message'] is not None) if verbose: print '\n'