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)
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)