def _test_model(self, name): """ Tests - 1. When just one item, check if next and prev urls are empty 2. When one more item added, limit results to 1 and see if next is not empty 3. start from position 2 and see if prev is not empty """ if name == 'event': path = get_path('page') else: path = get_path(1, name + 's', 'page') data = self._json_from_url(path) self.assertEqual(data['next'], '') self.assertEqual(data['previous'], '') # add second service with app.test_request_context(): create_event(name='TestEvent2') create_services(1) data = self._json_from_url(path + '?limit=1') self.assertIn('start=2', data['next']) self.assertEqual(data['previous'], '') # check from start=2 data = self._json_from_url(path + '?start=2') self.assertIn('limit=1', data['previous']) self.assertEqual(data['next'], '')
def test_media_successful_uploads(self): """ Test successful uploads of relative and direct links, both types of media """ self._create_set() self._update_json('event', 'background_url', '/bg.png') self._create_file('bg.png') self._update_json('speakers', 'photo', '/spkr.png', 1) self._create_file('spkr.png') self._update_json('sponsors', 'logo', 'http://google.com/favicon.ico', 1) # import data = self._make_zip_from_dir() event_dic = self._do_succesful_import(data) # checks resp = self.app.get(event_dic['background_url']) self.assertEqual(resp.status_code, 200) # speaker photo = self._get_event_value( get_path(2, 'speakers', 2), 'photo' ) resp = self.app.get(photo) self.assertEqual(resp.status_code, 200) self.assertIn('http://', photo) # sponsor logo = self._get_event_value( get_path(2, 'sponsors', 2), 'logo' ) self.assertIn('sponsors', logo) self.assertNotEqual(logo, 'http://google.com/favicon.ico') resp = self.app.get(logo) self.assertEqual(resp.status_code, 200)
def _test_import_success(self): # first export resp = self._do_successful_export(1) file = resp.data # import upload_path = get_path('import', 'json') resp = self._upload(file, upload_path, 'event.zip') self.assertEqual(resp.status_code, 200) self.assertIn('task_url', resp.data) task_url = json.loads(resp.data)['task_url'] # wait for done while True: resp = self.app.get(task_url) if 'SUCCESS' in resp.data: self.assertIn('result', resp.data) dic = json.loads(resp.data)['result'] break logging.info(resp.data) time.sleep(2) # check internals self.assertEqual(dic['id'], 2) self.assertEqual(dic['name'], 'TestEvent') self.assertIn('fb.com', json.dumps(dic['social_links']), dic) # get to check final resp = self.app.get(get_path(2)) self.assertEqual(resp.status_code, 200) self.assertIn('TestEvent', resp.data)
def _test_model(self, name, data): """ 1. Test getting JWT token with wrong credentials and getting 401 2. Get JWT token with right credentials 3. Send a sample successful POST request """ path = get_path() if name == 'event' else get_path(1, name + 's') # get access token response = self._send_login_request('wrong_password') self.assertEqual(response.status_code, 401) response = self._send_login_request('test') self.assertEqual(response.status_code, 200) token = json.loads(response.data)['access_token'] # send a post request response = self.app.post( path, data=json.dumps(data), headers={ 'content-type': 'application/json', 'Authorization': 'JWT %s' % token } ) self.assertNotEqual(response.status_code, 401) self.assertEqual(response.status_code, 201) self.assertIn('Test' + str(name).title(), response.data)
def _test_model(self, name, data, fields=[]): """ Sets a random value to each of the :fields in :data and makes sure POST request failed """ path = get_path() if name == 'event' else get_path(1, name + 's') self._login_user() for field in fields: data_copy = data.copy() data_copy[field] = 'r@nd0m_g00d_for_n0thing_v@lue' response = self.post_request(path, data_copy) self.assertEqual(response.status_code, 400)
def _test_model(self, name, data, fields=[]): """ Sets a random value to each of the :fields in :data and makes sure POST request failed """ path = get_path() if name == "event" else get_path(1, name + "s") self._login_user() for field in fields: data_copy = data.copy() data_copy[field] = "r@nd0m_g00d_for_n0thing_v@lue" response = self.post_request(path, data_copy) self.assertEqual(response.status_code, 400)
def _test_model(self, name, data, api_model, path=None): # strip data data = data.copy() for i in api_model: if not api_model[i].required: data.pop(i, None) # test if not path: path = get_path() if name == 'event' else get_path(1, name + 's') self._login_user() response = self.post_request(path, data) self.assertEqual(201, response.status_code, msg=response.data) self.assertIn('Test' + name[0].upper() + name[1:], response.data)
def _test_model(self, name, data): path = get_path() if name == 'event' else get_path(1, name + 's') response = self.app.post( path, data=json.dumps(data), headers={ 'content-type': 'application/json', 'Authorization': 'Basic %s' % base64.b64encode('[email protected]:test') } ) self.assertNotEqual(response.status_code, 401) self.assertEqual(response.status_code, 201) self.assertIn('Test' + str(name).title(), response.data)
def _test_import_success(self): # first export path = get_path(1, 'export', 'json') resp = self.app.get(path) file = resp.data self.assertEqual(resp.status_code, 200) # import upload_path = get_path('import', 'json') resp = self._upload(file, upload_path, 'event.zip') self.assertEqual(resp.status_code, 200) # check internals dic = json.loads(resp.data) self.assertEqual(dic['id'], 2) self.assertEqual(dic['name'], 'TestEvent')
def test_event_api(self): # Non existing event event_id = 1 path = get_path(event_id) response = self.app.get(path) self.assertEqual(response.status_code, 404) self.assertIn('does not exist', response.data)
def _test_model(self, name, data): """ Tests - 1. Without login, try to do a PUT request and catch 401 error 2. Login and match 200 response code and make sure that data changed """ path = get_path(1) if name == 'event' else get_path(1, name + 's', 1) response = self._put(path, data) self.assertEqual(401, response.status_code, msg=response.data) # login and send the request again self._login_user() response = self._put(path, data) self.assertEqual(200, response.status_code, msg=response.data) # surrounded by quotes for strict checking self.assertIn('"Test%s"' % str(name).title(), response.data)
def _test_model(self, name, data, fields=[]): """ Sets a random value to each of the :fields in :data and makes sure PUT request failed. At last check if original value had prevailed """ path = get_path(1) if name == 'event' else get_path(1, name + 's', 1) self._login_user() for field in fields: data_copy = data.copy() data_copy[field] = 'r@nd0m_g00d_for_n0thing_v@lue' response = self._put(path, data_copy) self.assertEqual(response.status_code, 400) # make sure field is not updated response = self.app.get(path) self.assertIn('Test%s_1' % str(name).title(), response.data) self.assertNotIn('"Test%s"' % str(name).title(), response.data)
def test_session_api_extended(self): self._login_user() path = get_path(1, 'tracks') self.post_request(path, POST_TRACK_DATA) path = get_path(1, 'microlocations') self.post_request(path, POST_MICROLOCATION_DATA) path = get_path(1, 'speakers') self.post_request(path, POST_SPEAKER_DATA) # create session json data = POST_SESSION_DATA.copy() data['track_id'] = 1 data['microlocation_id'] = 1 data['speaker_ids'] = [1] resp = self.post_request(get_path(1, 'sessions'), data) self.assertEqual(resp.status_code, 201) for i in ['TestTrack', 'TestSpeaker', 'TestMicrolocation']: self.assertIn(i, resp.data, i)
def _test_import_ots(self): dir_path = 'samples/ots16' shutil.make_archive(dir_path, 'zip', dir_path) file = open(dir_path + '.zip', 'r').read() os.remove(dir_path + '.zip') upload_path = get_path('import', 'json') resp = self._upload(file, upload_path, 'event.zip') self.assertEqual(resp.status_code, 200) self.assertIn('Open Tech Summit', resp.data)
def test_api(self): path = get_path('page') response = self.app.get(path) self.assertEqual(response.status_code, 404, msg=response.data) with app.test_request_context(): create_event() response = self.app.get(path) self.assertEqual(response.status_code, 200) self.assertIn('TestEvent', response.data)
def test_session(self): session = POST_SESSION_DATA.copy() SESSION_FORM['comments']['require'] = 1 session['comments'] = None form_str = json.dumps(SESSION_FORM, separators=(',', ':')) with app.test_request_context(): update_or_create(CustomForms, event_id=1, session_form=form_str) path = get_path(1, 'sessions') resp = self.post(path, session) self.assertEqual(resp.status_code, 400)
def test_speaker(self): speaker = POST_SPEAKER_DATA.copy() SPEAKER_FORM['github']['require'] = 1 speaker['github'] = None form_str = json.dumps(SPEAKER_FORM, separators=(',', ':')) with app.test_request_context(): update_or_create(CustomForms, event_id=1, speaker_form=form_str) path = get_path(1, 'speakers') resp = self.post(path, speaker) self.assertEqual(resp.status_code, 400)
def _test_import_success(self): # first export path = get_path(1, 'export', 'json') resp = self.app.get(path) file = resp.data self.assertEqual(resp.status_code, 200) # import upload_path = get_path('import', 'json') resp = self._upload(file, upload_path, 'event.zip') self.assertEqual(resp.status_code, 200) # check internals dic = json.loads(resp.data) self.assertEqual(dic['id'], 2) self.assertEqual(dic['name'], 'TestEvent') self.assertIn('fb.com', json.dumps(dic['social_links']), dic) # get to check final resp = self.app.get(get_path(2)) self.assertEqual(resp.status_code, 200) self.assertIn('TestEvent', resp.data)
def _test_model(self, name, data, path=None, exclude=[]): if not path: path = get_path(1) if name == 'event' else get_path(1, name + 's', 1) self._login_user() data_copy = data.copy() # loop over keys for i in data_copy: if i in exclude: continue # get old resp_old = self.app.get(path) self.assertEqual(resp_old.status_code, 200) # update resp = self._put(path, {i: data_copy[i]}) self.assertEqual(200, resp.status_code, msg='Key: %s\nMsg: %s' % (i, resp.data)) # check persistence status = self._test_change_json(resp_old.data, resp.data, i) if status: self.assertTrue(0, msg='Key %s changed in %s' % (status, i))
def _test_model(self, name, data, checks=[]): """ Tests - 1. Without login, try to do a POST request and catch 401 error 2. Login and match 201 response code and correct response data Param: checks - list of strings to assert in successful response data """ path = get_path() if name == 'event' else get_path(1, name + 's') response = self.post_request(path, data) self.assertEqual(401, response.status_code, msg=response.data) # login and send the request again self._login_user() response = self.post_request(path, data) self.assertEqual(201, response.status_code, msg=response.data) self.assertIn('location', response.headers) self.assertIn('Test' + str(name).title(), response.data) for string in checks: self.assertIn(string, response.data, msg=string) return response
def test_event_api(self): self.app = Setup.create_app() with app.test_request_context(): register(self.app, u'*****@*****.**', u'test') login(self.app, u'*****@*****.**', u'test') # Non existing event event_id = 1 path = get_path(event_id) response = self.app.get(path) self.assertEqual(response.status_code, 404) self.assertIn('does not exist', response.data)
def _test_model(self, name, data, path=None, *args): """ Tests - 1. Without login, try to do a PUT request and catch 401 error 2. Login and match 200 response code and make sure that data changed """ if not path: path = get_path(1) if name == 'event' else get_path(1, name + 's', 1) response = self._put(path, data) self.assertEqual(401, response.status_code, msg=response.data) # login and send the request again self._login_user() response = self._put(path, data) self.assertEqual(200, response.status_code, msg=response.data) # surrounded by quotes for strict checking. # before PUT, name is TestX_1, after PUT: TestX self.assertIn('"Test%s"' % (name[0].upper() + name[1:]), response.data) # check more things for _ in args: self.assertIn(_, response.data, response.data)
def _test_model(self, name, data): """ Logs in a user and creates model. Tests for 200 status on deletion and for the deleted object. Tests that the deleted object no longer exists. """ self._login_user() path = get_path() if name == 'event' else get_path(1, name + 's') response = self.app.post(path, data=json.dumps(data), headers={'Content-Type': 'application/json'}) self.assertEqual(response.status_code, 201) path = get_path(1) if name == 'event' else get_path(1, name + 's', 1) response = self.app.delete(path) self.assertEqual(response.status_code, 200) self.assertIn('Test' + str(name).title(), response.data) response = self.app.get(path) self.assertEqual(response.status_code, 404) self.assertIn('does not exist', response.data)
def test_event_queries(self): path = get_path() resp = self._post(path, POST_EVENT_DATA) # check no return resp = self.app.get(path + '?state=r@nd0m') self.assertEqual(len(resp.data), 3, msg=resp.data) # check case-insensitive search resp = self.app.get(path + '?contains=test') self.assertEqual(resp.status_code, 200) self.assertEqual(len(json.loads(resp.data)), 2) # check queryset forwarding resp = self.app.get(path + '?contains=test&state=r@nd0m') self.assertEqual(len(resp.data), 3)
def test_export_success(self): path = get_path(1, 'export', 'json') resp = self.app.get(path) self.assertEqual(resp.status_code, 200) self.assertIn('event1.zip', resp.headers['Content-Disposition']) size = len(resp.data) with app.test_request_context(): create_services(1, '2') create_services(1, '3') # check if size increased resp = self.app.get(path) self.assertEqual(resp.status_code, 200) self.assertTrue(len(resp.data) > size)
def test_event_queries(self): path = get_path() resp = self._post(path, POST_EVENT_DATA) self.assertEqual(resp.status_code, 201) # check no return resp = self.app.get(path + '?state=r@nd0m') self.assertEqual(len(resp.data), 3, msg=resp.data) # check case-insensitive search resp = self.app.get(path + '?contains=test') self.assertEqual(resp.status_code, 200) self.assertEqual(len(json.loads(resp.data)), 2) # check queryset forwarding resp = self.app.get(path + '?contains=test&state=r@nd0m') self.assertEqual(len(resp.data), 3)
def test_session_type_api(self): self._login_user() # Create a session type path = get_path(1, 'sessions', 'types') data = POST_SESSION_TYPE_DATA response = self.app.post( path, data=json.dumps(data), headers={'Content-Type': 'application/json'} ) self.assertEqual(response.status_code, 201) # Delete the session type path = get_path(1, 'sessions', 'types', 1) response = self.app.delete(path) self.assertEqual(response.status_code, 200) self.assertIn('TestSessionType', response.data) # Test if it's in session types path = get_path(1, 'sessions', 'types') response = self.app.get(path) self.assertEqual(response.status_code, 200) self.assertNotIn('TestSessionType', response.data)
def _test_model(self, name, data): """ Logs in a user and creates model. Tests for 200 status on deletion and for the deleted object. Tests that the deleted object no longer exists. """ self._login_user() path = get_path() if name == 'event' else get_path(1, name + 's') response = self.app.post( path, data=json.dumps(data), headers={'Content-Type': 'application/json'} ) self.assertEqual(response.status_code, 201) path = get_path(1) if name == 'event' else get_path(1, name + 's', 1) response = self.app.delete(path) self.assertEqual(response.status_code, 200) self.assertIn('Test' + str(name).title(), response.data) response = self.app.get(path) self.assertEqual(response.status_code, 404) self.assertIn('does not exist', response.data)
def test_export_settings_marshal(self): """ test if export settings are marshalled by default properly Also check when settings are all False, nothing is exported """ resp = self._put(get_path(1), {'logo': 'https://placehold.it/350x150'}) self.assertIn('placehold', resp.data, resp.data) self._create_set(1, {}) dr = 'static/temp/test_event_import' data = open(dr + '/event', 'r').read() obj = json.loads(data) self.assertIn('placehold', obj['logo']) if os.path.isdir(dr + '/images'): self.assertFalse(1, 'Image Dir Exists')
def test_social_link_api(self): self._login_user() # Create a social link path = get_path(1, 'links') data = POST_SOCIAL_LINK_DATA response = self.app.post( path, data=json.dumps(data), headers={'Content-Type': 'application/json'} ) self.assertEqual(response.status_code, 201) # Delete the social link path = get_path(1, 'links', 1) response = self.app.delete(path) self.assertEqual(response.status_code, 200) self.assertIn('TestSocialLink', response.data) # Test if it's in event-links path = get_path(1, 'links') response = self.app.get(path) self.assertEqual(response.status_code, 200) self.assertNotIn('TestSocialLink', response.data)
def test_event_location_queries(self): path = get_path() resp = self._post(path, POST_EVENT_DATA) # check location smart queries resp = self.app.get(path + '?location=SomeBuilding,Berlin') self.assertIn('Berlin', resp.data) # add another event data = POST_EVENT_DATA.copy() data['location_name'] = 'SomeBuilding' self._post(path, data) # get resp = self.app.get(path + '?location=SomeBuilding,Berlin') self.assertEqual(resp.status_code, 200) self.assertIn('SomeBuilding', resp.data) self.assertIn('Berlin', resp.data)
def test_event_time_queries(self): path = get_path() # Event is of Apr 16 resp = self.app.get(path + '?end_time_lt=2015-12-31T23:59:59') self.assertEqual(len(resp.data), 3, msg=resp.data) resp = self.app.get(path + '?start_time_gt=2015-12-31T23:59:59') self.assertIn('TestEvent0', resp.data) # add one more event of May 16 resp = self._post(path, POST_EVENT_DATA) # test resp = self.app.get(path + '?start_time_lt=2016-05-31T23:59:59') self.assertEqual(len(json.loads(resp.data)), 2, msg=resp.data) resp = self.app.get(path + '?end_time_gt=2016-05-01T00:00:00') self.assertIn('"TestEvent"', resp.data) self.assertNotIn('TestEvent0', resp.data)
def _test_model(self, name, model): # get path if name == 'event': path = get_path(1) elif name == 'user': path = '/api/v2/users/1' else: path = get_path(1, name + 's', 1) # check if exists resp = self.app.get(path) self.assertEqual(resp.status_code, 200) # delete virtually with app.test_request_context(): item = model.query.get(1) item.in_trash = True save_to_db(item) # get item resp = self.app.get(path) self.assertEqual(resp.status_code, 404) self.assertNotIn('Test', resp.data) # get item list and check empty resp = self.app.get(path[:-2]) self.assertEqual(resp.status_code, 200) self.assertNotIn('Test', resp.data)
def _test_model(self, name): """ Tests the 404 response, then add item and test the success response """ path = get_path(1, name + 's', 'page') response = self.app.get(path, follow_redirects=True) # check for 404 in no data self.assertEqual(response.status_code, 404) # add single data with app.test_request_context(): create_services(1) response = self.app.get(path, follow_redirects=True) self.assertEqual(response.status_code, 200) self.assertIn('Test' + str(name).title(), response.data)
def test_export_media(self): """ test successful export of media (and more) """ resp = self._put(get_path(1), {'logo': 'https://placehold.it/350x150'}) self.assertIn('placehold', resp.data, resp.data) self._create_set() dr = 'static/temp/test_event_import' data = open(dr + '/event', 'r').read() self.assertIn('images/logo', data) obj = json.loads(data) logo_data = open(dr + obj['logo'], 'r').read() self.assertTrue(len(logo_data) > 10) # test meta.json data = open(dr + '/meta', 'r').read() self.assertIn('http', data)
def test_event_time_queries(self): with app.test_request_context(): login(self.app, u'*****@*****.**', u'test') path = get_path() # Event is of Apr 16 resp = self.app.get(path + '?end_time_lt=2015-12-31T23:59:59') self.assertEqual(len(resp.data), 3, msg=resp.data) resp = self.app.get(path + '?start_time_gt=2015-12-31T23:59:59') self.assertIn('TestEvent0', resp.data) # add one more event of May 16 resp = self._post(path, POST_EVENT_DATA) # test resp = self.app.get(path + '?start_time_lt=2016-05-31T23:59:59') self.assertEqual(len(json.loads(resp.data)), 2, msg=resp.data) resp = self.app.get(path + '?end_time_gt=2016-05-01T00:00:00') self.assertIn('"TestEvent"', resp.data) self.assertNotIn('TestEvent0', resp.data)
def test_sponsor_api(self): path = get_path(1, 'sponsors', 1) self._test_path(path)
def test_sponsor_types_api(self): path = get_path(1, 'sponsors', 'types') self._test_path(path, 'TestSponsorType1_1', 'TestSponsorType2_1')
def test_event_api_filters(self): path = get_path() + '?location_name=r@nd0m' resp = self.app.get(path) self.assertTrue(len(resp.data) < 4, resp.data) self.assertNotIn('TestEvent', resp.data)
def test_speaker_api(self): path = get_path(1, 'speakers') self._test_path(path, 'TestSpeaker1_1', 'TestSpeaker2_1')
def test_sponsor_api(self): path = get_path(1, 'sponsors') self._test_path(path, 'TestSponsor1_1', 'TestSponsor2_1')
def test_session_api(self): path = get_path(1, 'sessions') self._test_path(path, 'TestSession1_1', 'TestSession2_1')
def test_microlocation_api(self): path = get_path(1, 'microlocations') self._test_path(path, 'TestMicrolocation1_1', 'TestMicrolocation2_1')
def test_track_api(self): path = get_path(1, 'tracks') self._test_path(path, 'TestTrack1_1', 'TestTrack2_1')
def test_event_api(self): path = get_path() self._test_path(path, 'TestEvent_1', 'TestEvent_2')
def test_speaker_api(self): path = get_path(1, 'speakers', 1) self._test_path(path)