def setup_responses(self): application_forms = [ ApplicationForm(1, True, datetime(2019, 4, 12)), ApplicationForm(2, False, datetime(2019, 4, 12)) ] db.session.add_all(application_forms) candidate1 = AppUser(email='*****@*****.**', firstname='candidate', lastname='1', user_title='Mr', password='******', organisation_id=1) candidate2 = AppUser(email='*****@*****.**', firstname='candidate', lastname='2', user_title='Ms', password='******', organisation_id=1) candidate3 = AppUser(email='*****@*****.**', firstname='candidate', lastname='3', user_title='Mr', password='******', organisation_id=1) event_admin = AppUser(email='*****@*****.**',firstname='event_admin', lastname='1', user_title='Ms', password='******', organisation_id=1) users = [candidate1, candidate2, candidate3, event_admin] for user in users: user.verify() db.session.add_all(users) event_role = EventRole('admin', 4, 1) db.session.add(event_role) responses = [ Response(1, 1, True, datetime(2019, 4, 10)), Response(1, 2, True, datetime(2019, 4, 9), True, datetime(2019, 4, 11)), Response(2, 3, True) ] db.session.add_all(responses) db.session.commit()
def setup_responses(self): application_forms = [ ApplicationForm(1, True, datetime(2019, 4, 12)), ApplicationForm(2, False, datetime(2019, 4, 12)) ] db.session.add_all(application_forms) candidate1 = AppUser('*****@*****.**', 'candidate', '1', 'Mr', 1, 1, 'M', 'UWC', 'CS', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') candidate2 = AppUser('*****@*****.**', 'candidate', '2', 'Ms', 1, 1, 'F', 'RU', 'Chem', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') candidate3 = AppUser('*****@*****.**', 'candidate', '3', 'Mr', 1, 1, 'M', 'UFH', 'Phys', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') event_admin = AppUser('*****@*****.**', 'event_admin', '1', 'Ms', 1, 1, 'F', 'NWU', 'Math', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') users = [candidate1, candidate2, candidate3, event_admin] for user in users: user.verify() db.session.add_all(users) event_role = EventRole('admin', 4, 1) db.session.add(event_role) responses = [ Response(1, 1, True, datetime(2019, 4, 10)), Response(1, 2, True, datetime(2019, 4, 9), True, datetime(2019, 4, 11)), Response(2, 3, True) ] db.session.add_all(responses) db.session.commit()
def seed_static_data(self): inactive_user = self.add_user('*****@*****.**', 'inactive', post_create_fn=lambda u: u.deactivate()) deleted_user = self.add_user('*****@*****.**', 'deleted', post_create_fn=lambda u: u.delete()) event_admin = self.add_user('*****@*****.**', 'event', 'admin') self.add_user('*****@*****.**', 'applicant') self.add_user('*****@*****.**', 'notstarted') self.add_user('*****@*****.**', 'applicant') db.session.commit() self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png', 'deeplearningindaba') country = Country('South Africa') db.session.add(country) user_category = UserCategory('Post Doc') db.session.add(user_category) event = Event('Indaba 2019', 'Deep Learning Indaba', datetime(2019, 8, 25), datetime(2019, 8, 31), 'COOLER', 1, '*****@*****.**', 'indaba.deeplearning', datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now()) db.session.add(event) db.session.commit() email_templates = [ EmailTemplate('application-not-submitted', None, ''), EmailTemplate('application-not-started', None, '') ] db.session.add_all(email_templates) db.session.commit() event_role = EventRole('admin', event_admin.id, event.id) db.session.add(event_role) application_form = ApplicationForm(1, True, datetime(2019, 4, 12)) db.session.add(application_form) db.session.commit() responses = [ Response(application_form.id, self.test_users[0].id, True), Response(application_form.id, self.test_users[1].id, False), Response(application_form.id, self.test_users[3].id, True, datetime.now(), True, datetime.now()), Response(application_form.id, self.test_users[4].id, False), ] db.session.add_all(responses) db.session.commit()
def seed_static_data(self): country = Country('South Africa') db.session.add(country) user_category = UserCategory('Post Doc') db.session.add(user_category) inactive_user = AppUser('*****@*****.**', 'inactive', '1', 'Mr', 1, 1, 'Male', 'Wits', 'Computer Science', 'None', 1, datetime(1991, 3, 27), 'English', 'abc') inactive_user.deactivate() deleted_user = AppUser('*****@*****.**', 'deleted', '1', 'Mr', 1, 1, 'Male', 'Wits', 'Computer Science', 'None', 1, datetime(1991, 3, 27), 'English', 'abc') deleted_user.delete() users = [ AppUser('*****@*****.**', 'event', 'admin', 'Mr', 1, 1, 'Male', 'Wits', 'Computer Science', 'None', 1, datetime(1991, 3, 27), 'English', 'abc'), AppUser('*****@*****.**', 'applicant', '1', 'Mr', 1, 1, 'Male', 'Wits', 'Computer Science', 'None', 1, datetime(1991, 3, 27), 'English', 'abc'), inactive_user, deleted_user, AppUser('*****@*****.**', 'notstarted', '1', 'Mr', 1, 1, 'Male', 'Wits', 'Computer Science', 'None', 1, datetime(1991, 3, 27), 'English', 'abc'), AppUser('*****@*****.**', 'applicant', '2', 'Mr', 1, 1, 'Male', 'Wits', 'Computer Science', 'None', 1, datetime(1991, 3, 27), 'English', 'abc') ] for user in users: user.verify() db.session.add_all(users) event = Event('Indaba 2019', 'Deep Learning Indaba', datetime(2019, 8, 25), datetime(2019, 8, 31)) db.session.add(event) event_role = EventRole('admin', 1, 1) db.session.add(event_role) application_form = ApplicationForm(1, True, datetime(2019, 4, 12)) db.session.add(application_form) responses = [ Response(1, 1, True), Response(1, 2, False), Response(1, 4, True, datetime.now(), True, datetime.now()), Response(1, 6, False), ] db.session.add_all(responses) db.session.commit()
def seed_static_data(self): self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png', 'deeplearningindaba') test_country = Country('Indaba Land') db.session.add(test_country) db.session.commit() test_category = UserCategory('Category1') db.session.add(test_category) db.session.commit() response = self.app.post('/api/v1/user', data=self.user_data_dict) self.test_user1 = json.loads(response.data) other_user_data = self.user_data_dict.copy() other_user_data['email'] = '*****@*****.**' response = self.app.post('/api/v1/user', data=other_user_data) self.test_user2 = json.loads(response.data) self.test_event = Event('Test Event', 'Event Description', datetime.now() + timedelta(days=30), datetime.now() + timedelta(days=60), 'KONNET', 1, '*****@*****.**', 'indaba.deeplearning', datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now()) db.session.add(self.test_event) db.session.commit() self.test_form = ApplicationForm(self.test_event.id, True, datetime.now() + timedelta(days=60)) db.session.add(self.test_form) db.session.commit() self.test_response = Response(self.test_form.id, self.test_user1['id']) db.session.add(self.test_response) db.session.commit() self.test_response2 = Response(self.test_form.id, self.test_user2['id']) self.test_response2.submit_response() db.session.add(self.test_response2) db.session.commit() self.user_role1 = EventRole('admin', self.test_user1['id'], self.test_event.id) db.session.add(self.user_role1) db.session.commit() db.session.flush()
def post(self): args = self.post_req_parser.parse_args() user_id = g.current_user['id'] is_submitted = args['is_submitted'] application_form_id = args['application_form_id'] language = args['language'] if len(language) != 2: language = 'en' # Fallback to English if language doesn't look like an ISO 639-1 code application_form = application_form_repository.get_by_id( application_form_id) if application_form is None: return errors.FORM_NOT_FOUND_BY_ID user = user_repository.get_by_id(user_id) responses = response_repository.get_all_for_user_application( user_id, application_form_id) if not application_form.nominations and len(responses) > 0: return errors.RESPONSE_ALREADY_SUBMITTED response = Response(application_form_id, user_id, language) if is_submitted: response.submit() response_repository.save(response) answers = [] for answer_args in args['answers']: answer = Answer(response.id, answer_args['question_id'], answer_args['value']) answers.append(answer) response_repository.save_answers(answers) try: if response.is_submitted: LOGGER.info( 'Sending confirmation email for response with ID : {id}'. format(id=response.id)) user = user_repository.get_by_id(user_id) response = response_repository.get_by_id_and_user_id( response.id, user_id) self.send_confirmation(user, response) except: LOGGER.warn( 'Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully' .format(id=response.id)) finally: return response, 201
def post(self): # Save a new response for the logged-in user. req_parser = reqparse.RequestParser() req_parser.add_argument('is_submitted', type=bool, required=True) req_parser.add_argument('application_form_id', type=int, required=True) req_parser.add_argument('answers', type=list, required=True, location='json') args = req_parser.parse_args() user_id = g.current_user['id'] try: response = Response(args['application_form_id'], user_id) response.is_submitted = args['is_submitted'] if args['is_submitted']: response.submitted_timestamp = datetime.datetime.now() db.session.add(response) db.session.commit() for answer_args in args['answers']: answer = Answer(response.id, answer_args['question_id'], answer_args['value']) db.session.add(answer) db.session.commit() try: if response.is_submitted: LOGGER.info( 'Sending confirmation email for response with ID : {id}' .format(id=response.id)) user = db.session.query(AppUser).filter( AppUser.id == g.current_user['id']).first() self.send_confirmation(user, response) except: LOGGER.warn( 'Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully' .format(id=response.id)) finally: return response, 201 # 201 is 'CREATED' status code except SQLAlchemyError as e: LOGGER.error("Database error encountered: {}".format(e)) return errors.DB_NOT_AVAILABLE except: LOGGER.error("Encountered unknown error: {}".format( traceback.format_exc())) return errors.DB_NOT_AVAILABLE
def test_get_events_withdrawn(self): self.seed_static_data() test_response = Response(self.test_form.id, self.test_user.id, is_submitted=True, is_withdrawn=True) db.session.add(test_response) db.session.commit() response = self.app.post('/api/v1/authenticate', data={ 'email': '*****@*****.**', 'password': '******' }) self.assertEqual(response.status_code, 200) data = json.loads(response.data) response = self.app.get('/api/v1/events', headers={'Authorization': data["token"]}) data = json.loads(response.data) self.assertEqual(len(data), 1) self.assertEqual(data[0]["id"], 1) self.assertEqual(data[0]["description"], 'Event Description') self.assertEqual(data[0]["status"], 'Application withdrawn')
def test_get_events_applied(self): self.seed_static_data() test_response = Response(self.test_form.id, self.test_user.id, is_submitted=True) db.session.add(test_response) db.session.commit() response = self.app.post('/api/v1/authenticate', data={ 'email': '*****@*****.**', 'password': '******' }) assert response.status_code == 200 data = json.loads(response.data) response = self.app.get('/api/v1/events', headers={'Authorization': data["token"]}) data = json.loads(response.data) assert len(data) == 1 assert data[0]["id"] == 1 assert data[0]["description"] == 'Event Description' assert data[0]["status"] == 'Applied'
def setup_static_data(self): self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png') self.add_organisation('Deep Learning IndabaX', 'blah.png', 'blah_big.png') db.session.add(UserCategory('Postdoc')) db.session.add(Country('South Africa')) db.session.commit() events = [ Event('Indaba', 'Indaba Event', datetime.now(), datetime.now(), 'ADAMOPTIM', 1, '*****@*****.**','indaba.deeplearning', datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now()), Event('Indaba2', 'Indaba Event 2', datetime.now(), datetime.now(), 'HACFTET', 2, '*****@*****.**','indaba.deeplearning', datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now()) ] db.session.add_all(events) application_forms = [ ApplicationForm(1, True, datetime.now()), ApplicationForm(2, False, datetime.now()) ] db.session.add_all(application_forms) candidate1 = AppUser(email='*****@*****.**', firstname='candidate', lastname='1', user_title='Mr',password= '******', organisation_id=1) candidate2 = AppUser(email='*****@*****.**', firstname='candidate', lastname='2', user_title='Ms', password='******', organisation_id=1) system_admin = AppUser(email='*****@*****.**', firstname='system_admin', lastname='1', user_title='Mr',password='******', organisation_id=1, is_admin=True) event_admin = AppUser(email='*****@*****.**', firstname='event_admin', lastname='1', user_title='Ms', password='******',organisation_id=1) reviewer = AppUser(email='*****@*****.**', firstname='reviewer', lastname='1', user_title='Ms', password='******', organisation_id=1) users = [candidate1, candidate2, system_admin, event_admin, reviewer] for user in users: user.verify() db.session.add_all(users) event_roles = [ EventRole('admin', 4, 1), EventRole('reviwer', 5, 1) ] db.session.add_all(event_roles) db.session.commit() responses = [ Response(1, 1, True, datetime(2019, 4, 10)), Response(2, 2, True, datetime(2019, 4, 9)) ] db.session.add_all(responses) db.session.commit()
def setup_static_data(self): db.session.add(UserCategory('Postdoc')) db.session.add(Country('South Africa')) events = [ Event('Indaba', 'Indaba Event', datetime.now(), datetime.now()), Event('Indaba2', 'Indaba Event 2', datetime.now(), datetime.now()) ] db.session.add_all(events) application_forms = [ ApplicationForm(1, True, datetime.now()), ApplicationForm(2, False, datetime.now()) ] db.session.add_all(application_forms) candidate1 = AppUser('*****@*****.**', 'candidate', '1', 'Mr', 1, 1, 'M', 'UWC', 'CS', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') candidate2 = AppUser('*****@*****.**', 'candidate', '2', 'Ms', 1, 1, 'F', 'RU', 'Chem', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') system_admin = AppUser('*****@*****.**', 'system_admin', '1', 'Mr', 1, 1, 'M', 'UFH', 'Phys', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc', True) event_admin = AppUser('*****@*****.**', 'event_admin', '1', 'Ms', 1, 1, 'F', 'NWU', 'Math', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') reviewer = AppUser('*****@*****.**', 'reviewer', '1', 'Ms', 1, 1, 'F', 'NWU', 'Math', 'NA', 1, datetime(1984, 12, 12), 'Eng', 'abc') users = [candidate1, candidate2, system_admin, event_admin, reviewer] for user in users: user.verify() db.session.add_all(users) event_roles = [EventRole('admin', 4, 1), EventRole('reviwer', 5, 1)] db.session.add_all(event_roles) responses = [ Response(1, 1, True, datetime(2019, 4, 10)), Response(2, 2, True, datetime(2019, 4, 9)) ] db.session.add_all(responses) db.session.commit()
def _seed_data(self): self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png') other_user_data = self.add_user('*****@*****.**') test_event = self.add_event() self.test_form = ApplicationForm(test_event.id, True, date(2019, 3, 24)) self.add_to_db(self.test_form) self.test_response = Response(self.test_form.id, other_user_data.id) self.add_to_db(self.test_response) self.headers = self.get_auth_header_for("*****@*****.**") db.session.flush()
def add_response(self, application_form_id, user_id, is_submitted=False, is_withdrawn=False, language='en'): response = Response(application_form_id, user_id, language) if is_submitted: response.submit() if is_withdrawn: response.withdraw() db.session.add(response) db.session.commit() return response
def _seed_data(self): # Add a user test_country = Country('Indaba Land') _add_object_to_db(test_country) test_category = UserCategory('Category1') _add_object_to_db(test_category) other_user_data = self.user_data_dict.copy() other_user_data['email'] = '*****@*****.**' response = self.app.post('/api/v1/user', data=other_user_data) self.other_user_data = json.loads(response.data) response = self.app.post('/api/v1/user', data=self.user_data_dict) self.user_data = json.loads(response.data) # Add application form data self.test_event = Event('Test Event', 'Event Description', date( 2019, 2, 24), date(2019, 3, 24)) _add_object_to_db(self.test_event) self.test_form = ApplicationForm( self.test_event.id, True, date(2019, 3, 24)) _add_object_to_db(self.test_form) test_section = Section( self.test_form.id, 'Test Section', 'Test Description', 1) _add_object_to_db(test_section) self.test_question = Question(self.test_form.id, test_section.id, 'Test Question Description', 'Test question placeholder', 1, 'Test Type', None) _add_object_to_db(self.test_question) self.test_question2 = Question( self.test_form.id, test_section.id, 'Test Question 2', 'Enter something', 2, 'short-text', None) _add_object_to_db(self.test_question2) self.test_response = Response( self.test_form.id, self.other_user_data['id']) _add_object_to_db(self.test_response) self.test_answer1 = Answer( self.test_response.id, self.test_question.id, 'My Answer') _add_object_to_db(self.test_answer1) db.session.flush()
def test_reference_application_closed(self): self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png') other_user_data = self.add_user('*****@*****.**') test_event = self.add_event() test_event.add_event_role('admin', 1) test_event.set_application_close(datetime.now()) self.add_to_db(test_event) self.test_form = self.create_application_form(test_event.id, True, False) self.add_to_db(self.test_form) self.test_response = Response(self.test_form.id, other_user_data.id) self.add_to_db(self.test_response) self.headers = self.get_auth_header_for("*****@*****.**") db.session.flush() reference_req = ReferenceRequest(1, 'Mr', 'John', 'Snow', 'Supervisor', '*****@*****.**') reference_request_repository.create(reference_req) REFERENCE_DETAIL = { 'token': reference_req.token, 'uploaded_document': 'DOCT-UPLOAD-78999', } response = self.app.post('/api/v1/reference', data=REFERENCE_DETAIL, headers=self.headers) self.assertEqual(response.status_code, 403) response = self.app.put('/api/v1/reference', data=REFERENCE_DETAIL, headers=self.headers) self.assertEqual(response.status_code, 403)
class EventsStatsAPITest(ApiTestCase): user_data_dict = { 'email': '*****@*****.**', 'firstname': 'Some', 'lastname': 'Thing', 'user_title': 'Mr', 'password': '******', 'policy_agreed': True } def seed_static_data(self): self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png', 'deeplearningindaba') test_country = Country('Indaba Land') db.session.add(test_country) db.session.commit() test_category = UserCategory('Category1') db.session.add(test_category) db.session.commit() response = self.app.post('/api/v1/user', data=self.user_data_dict) self.test_user1 = json.loads(response.data) other_user_data = self.user_data_dict.copy() other_user_data['email'] = '*****@*****.**' response = self.app.post('/api/v1/user', data=other_user_data) self.test_user2 = json.loads(response.data) self.test_event = self.add_event('Test Event', 'Event Description', datetime.now() + timedelta(days=30), datetime.now() + timedelta(days=60), 'KONNET', 1, '*****@*****.**', 'indaba.deeplearning', datetime.now(), datetime.now() + timedelta(days=30)) db.session.add(self.test_event) db.session.commit() self.test_form = self.create_application_form(self.test_event.id, True, False) db.session.add(self.test_form) db.session.commit() self.test_response = Response(self.test_form.id, self.test_user1['id']) db.session.add(self.test_response) db.session.commit() self.test_response2 = Response(self.test_form.id, self.test_user2['id']) self.test_response2.submit_response() db.session.add(self.test_response2) db.session.commit() self.user_role1 = EventRole('admin', self.test_user1['id'], self.test_event.id) db.session.add(self.user_role1) db.session.commit() db.session.flush() def test_get_stats_forbidden(self): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user2['token']}, query_string={'event_id': self.test_event.id}) self.assertEqual(response.status_code, 403) def test_event_id_required(self): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user2['token']}, query_string={'someparam': self.test_event.id}) self.assertEqual(response.status_code, 400) def test_event_id_missing(self): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user2['token']}, query_string={'event_id': self.test_event.id + 100}) self.assertEqual(response.status_code, 404) def test_event_stats_accurate(self): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user1['token']}, query_string={'event_id': self.test_event.id}) data = json.loads(response.data) self.assertEqual(data['num_users'], 2) self.assertEqual(data['num_responses'], 2) self.assertEqual(data['num_submitted_responses'], 1)
def _seed_data(self): self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png') self.first_user_data = self.add_user('*****@*****.**', 'First', 'User', 'Mx') self.other_user_data = self.add_user('*****@*****.**') test_event = self.add_event() test_event.add_event_role('admin', 1) self.test_event_data = copy.deepcopy(test_event.__dict__) self.add_to_db(test_event) nomination_event = self.add_event(key="AWARD_NOMINATIONS_ONLY") nomination_event.add_event_role('admin', 1) self.test_nomination_event_data = copy.deepcopy( nomination_event.__dict__) self.add_to_db(nomination_event) self.test_form = self.create_application_form(test_event.id, True, False) self.add_to_db(self.test_form) self.test_nomination_form = self.create_application_form( nomination_event.id, True, True) sections = [ Section(test_event.id, 'Nomination Capacity', 'Nomination Details', 1), Section(nomination_event.id, 'Nominee Information', 'Details of person being nominated', 1) ] sections[1].key = 'nominee_section' db.session.add_all(sections) db.session.commit() questions = [ Question(test_event.id, sections[0].id, 'Nomination Capacity', 'Enter 50 to 150 words', 1, 'long_text', ''), Question(test_event.id, sections[0].id, 'some details', 'Enter 50 to 150 words', 2, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'title', 'Enter 50 to 150 words', 1, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'firstname', 'Enter 50 to 150 words', 2, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'lastname', 'Enter 50 to 150 words', 3, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'email', 'Enter 50 to 150 words', 4, 'long_text', ''), ] questions[0].key = 'nominating_capacity' questions[2].key = 'nomination_title' questions[3].key = 'nomination_firstname' questions[4].key = 'nomination_lastname' questions[5].key = 'nomination_email' db.session.add_all(questions) db.session.commit() self.test_response1 = Response( # Self nomination self.test_form.id, self.first_user_data.id) self.add_to_db(self.test_response1) answers = [ Answer(self.test_response1.id, questions[0].id, 'self'), Answer(self.test_response1.id, questions[1].id, 'Blah') ] db.session.add_all(answers) db.session.commit() self.test_response2 = Response( # Nominating other self.test_form.id, self.other_user_data.id) self.add_to_db(self.test_response2) answers = [ Answer(self.test_response2.id, questions[0].id, 'other'), Answer(self.test_response2.id, questions[1].id, 'Blah'), Answer(self.test_response2.id, questions[2].id, 'Mx'), Answer(self.test_response2.id, questions[3].id, 'Skittles'), Answer(self.test_response2.id, questions[4].id, 'Cat'), Answer(self.test_response2.id, questions[5].id, '*****@*****.**'), ] db.session.add_all(answers) db.session.commit() self.first_headers = self.get_auth_header_for("*****@*****.**") self.other_headers = self.get_auth_header_for("*****@*****.**") db.session.flush()
class EventsStatsAPITest(ApiTestCase): user_data_dict = { 'email': '*****@*****.**', 'firstname': 'Some', 'lastname': 'Thing', 'user_title': 'Mr', 'nationality_country_id': 1, 'residence_country_id': 1, 'user_ethnicity': 'None', 'user_gender': 'Male', 'affiliation': 'University', 'department': 'Computer Science', 'user_disability': 'None', 'user_category_id': 1, 'user_primaryLanguage': 'Zulu', 'user_dateOfBirth': datetime(1984, 12, 12).strftime('%Y-%m-%dT%H:%M:%S.%fZ'), 'password': '******' } def seed_static_data(self): test_country = Country('Indaba Land') db.session.add(test_country) db.session.commit() test_category = UserCategory('Category1') db.session.add(test_category) db.session.commit() response = self.app.post('/api/v1/user', data=self.user_data_dict) self.test_user1 = json.loads(response.data) other_user_data = self.user_data_dict.copy() other_user_data['email'] = '*****@*****.**' response = self.app.post('/api/v1/user', data=other_user_data) self.test_user2 = json.loads(response.data) self.test_event = Event('Test Event', 'Event Description', datetime.now() + timedelta(days=30), datetime.now() + timedelta(days=60)) db.session.add(self.test_event) db.session.commit() self.test_form = ApplicationForm(self.test_event.id, True, datetime.now() + timedelta(days=60)) db.session.add(self.test_form) db.session.commit() self.test_response = Response(self.test_form.id, self.test_user1['id']) db.session.add(self.test_response) db.session.commit() self.test_response2 = Response(self.test_form.id, self.test_user2['id']) self.test_response2.submit_response() db.session.add(self.test_response2) db.session.commit() self.user_role1 = EventRole('admin', self.test_user1['id'], self.test_event.id) db.session.add(self.user_role1) db.session.commit() db.session.flush() def test_get_stats_forbidden(self): with app.app_context(): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user2['token']}, query_string={'event_id': self.test_event.id}) self.assertEqual(response.status_code, 403) def test_event_id_required(self): with app.app_context(): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user2['token']}, query_string={'someparam': self.test_event.id}) self.assertEqual(response.status_code, 400) def test_event_id_missing(self): with app.app_context(): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user2['token']}, query_string={'event_id': self.test_event.id + 100}) self.assertEqual(response.status_code, 404) def test_event_stats_accurate(self): with app.app_context(): self.seed_static_data() response = self.app.get( '/api/v1/eventstats', headers={'Authorization': self.test_user1['token']}, query_string={'event_id': self.test_event.id}) data = json.loads(response.data) self.assertEqual(data['num_users'], 2) self.assertEqual(data['num_responses'], 2) self.assertEqual(data['num_submitted_responses'], 1)
def _seed_data(self): organisation = self.add_organisation( 'Deep Learning Indaba', 'Baobab', 'blah.png', 'blah_big.png', 'deeplearningindba', 'https://www.deeplearningindaba.com', '*****@*****.**') email_templates = [ EmailTemplate('withdrawal', None, ''), EmailTemplate('confirmation-response', None, '{question_answer_summary}') ] db.session.add_all(email_templates) db.session.commit() # Add a user test_country = Country('Indaba Land') _add_object_to_db(test_country) test_category = UserCategory('Category1') _add_object_to_db(test_category) other_user_data = self.user_data_dict.copy() other_user_data['email'] = '*****@*****.**' response = self.app.post('/api/v1/user', data=other_user_data) self.other_user_data = json.loads(response.data) response = self.app.post('/api/v1/user', data=self.user_data_dict) self.user_data = json.loads(response.data) # Add application form data self.test_event = Event('Test Event', 'Event Description', date(2019, 2, 24), date(2019, 3, 24), 'NAGSOLVER', 1, '*****@*****.**', 'indaba.deeplearning', datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now()) _add_object_to_db(self.test_event) self.test_form = ApplicationForm(self.test_event.id, True, date(2019, 3, 24)) _add_object_to_db(self.test_form) test_section = Section(self.test_form.id, 'Test Section', 'Test Description', 1) _add_object_to_db(test_section) self.test_question = Question(self.test_form.id, test_section.id, 'Test Question Description', 'Test question placeholder', 1, 'Test Type', None) _add_object_to_db(self.test_question) self.test_question2 = Question(self.test_form.id, test_section.id, 'Test Question 2', 'Enter something', 2, 'short-text', None) _add_object_to_db(self.test_question2) self.test_response = Response(self.test_form.id, self.other_user_data['id']) _add_object_to_db(self.test_response) self.test_answer1 = Answer(self.test_response.id, self.test_question.id, 'My Answer') _add_object_to_db(self.test_answer1) db.session.flush()
def _seed_data(self): """Create dummy data for testing""" organisation = self.add_organisation( 'Deep Learning Indaba', 'Baobab', 'blah.png', 'blah_big.png', 'deeplearningindba', 'https://www.deeplearningindaba.com', '*****@*****.**') email_templates = [ EmailTemplate('withdrawal', None, ''), EmailTemplate('confirmation-response', None, '{question_answer_summary}') ] db.session.add_all(email_templates) db.session.commit() # Add country test_country = Country('Indaba Land') self.add_to_db(test_country) # Add category test_category = UserCategory('Category1') self.add_to_db(test_category) # Add users to database other_user_data = self.user_data_dict.copy() other_user_data['email'] = self.other_user_email response = self.app.post('/api/v1/user', data=other_user_data) self.other_user_data = json.loads(response.data) response = self.app.post('/api/v1/user', data=self.user_data_dict) self.user_data = json.loads(response.data) self.add_n_users(self.num_dummy_users) # Add application form data self.test_event = self.add_event('Test Event', 'Event Description', date(2019, 2, 24), date(2019, 3, 24), 'NAGSOLVER') self.test_form = self.create_application_form(self.test_event.id, True) self.test_section = Section(self.test_form.id, 'Test Section', 'Test Description', 1) self.test_section.key = 'test_section' self.add_to_db(self.test_section) self.test_question = Question(self.test_form.id, self.test_section.id, 'Test Question Description', 'Test question placeholder', 1, 'Test Type', None) self.add_to_db(self.test_question) self.test_question2 = Question(self.test_form.id, self.test_section.id, 'Test Question 2', 'Enter something', 2, 'short-text', None) self.add_to_db(self.test_question2) # responses self.test_response = Response(self.test_form.id, self.other_user_data['id']) self.add_to_db(self.test_response) self.test_answer1 = Answer(self.test_response.id, self.test_question.id, 'My Answer') self.add_to_db(self.test_answer1) self.responses = [] for user in self.test_users: response = Response(self.test_form.id, user.id) self.add_to_db(response) answer1 = Answer( response.id, self.test_question.id, "{}'s Answer for question 1".format(user.firstname)) answer2 = Answer( response.id, self.test_question2.id, "{}'s Answer for question 2".format(user.firstname)) self.add_to_db(answer1) self.add_to_db(answer2) self.responses.append(response) # add nomination application form self.test_nomination_form = self.create_application_form( self.test_event.id, True, True) self.test_nomination_response = Response(self.test_nomination_form.id, self.other_user_data['id']) self.add_to_db(self.test_nomination_response) db.session.flush()