def test_duplicate_post(self): (other, teammate, contact, captain, super_admin, team, classroom) = self.create() today_str = datetime.date.today().strftime(config.iso_date_format) report_params = { 'filename': 'Team_filewhack.{}.html'.format(today_str), 'team_id': team.uid, 'dataset_id': 'Dataset_first', 'template': 'template.html', } response = self.testapp.post_json( '/api/reports', report_params, headers={'Authorization': 'Bearer ' + self.valid_jwt}, ) first_uid = json.loads(response.body)['uid'] self.assertIsNotNone(Report.get_by_id(first_uid)) # Post a second report with the same filename and team. This should # update the old report. report_params['dataset_id'] = 'Dataset_second' response = self.testapp.post_json( '/api/reports', report_params, headers={'Authorization': 'Bearer ' + self.valid_jwt}, ) second_uid = json.loads(response.body)['uid'] self.assertEqual(first_uid, second_uid) self.assertEqual( Report.get_by_id(first_uid).dataset_id, 'Dataset_second')
def test_post_preview_setting(self): for x in range(1, 4): Team.create( id='00{}'.format(x), captain_id='User_cap', program_id=self.program.uid, ).put() def body(x): return { 'filename': 'file_00{}.html'.format(x), 'team_id': 'Team_00{}'.format(x), 'dataset_id': 'Dataset_00{}'.format(x), 'template': 'template.html', } headers = {'Authorization': 'Bearer ' + self.valid_jwt} preview_rsp = self.testapp.post_json('/api/reports', dict(body(1), preview=True), headers=headers) non_pre_rsp = self.testapp.post_json('/api/reports', dict(body(2), preview=False), headers=headers) default_rsp = self.testapp.post_json('/api/reports', body(3), headers=headers) preview_fetched = Report.get_by_id(json.loads(preview_rsp.body)['uid']) non_pre_fetched = Report.get_by_id(json.loads(non_pre_rsp.body)['uid']) default_fetched = Report.get_by_id(json.loads(default_rsp.body)['uid']) self.assertEqual(preview_fetched.preview, True) self.assertEqual(non_pre_fetched.preview, False) self.assertEqual(default_fetched.preview, True)
def test_post_team_dataset(self): today_str = datetime.date.today().strftime(config.iso_date_format) filename = 'Team_filewhack.{}.html'.format(today_str) dataset_id = 'Dataset_foo' template = 'template.html' (other, teammate, contact, captain, super_admin, team, classroom) = self.create() response = self.testapp.post_json( '/api/reports', { 'filename': filename, 'team_id': team.uid, 'dataset_id': dataset_id, 'template': template, }, headers={'Authorization': 'Bearer ' + self.valid_jwt}, ) fetched = Report.get_by_id(json.loads(response.body)['uid']) self.assertEqual(fetched.team_id, team.uid) self.assertIsNone(fetched.classroom_id) self.assertEqual(fetched.filename, filename) self.assertEqual(fetched.dataset_id, dataset_id) self.assertEqual(fetched.template, template)
def test_post_team_pdf(self): today_str = datetime.date.today().strftime(config.iso_date_format) filename = 'Team_filewhack.{}.pdf'.format(today_str) (other, teammate, contact, captain, super_admin, team, classroom, gcs_path, file_size) = self.create_for_post(filename) response = self.testapp.post( '/api/reports', { 'team_id': team.uid, 'filename': filename }, upload_files=[('file', filename)], headers=self.login_headers(super_admin), ) report_dict = json.loads(response.body) fetched = Report.get_by_id(report_dict['uid']) self.assertEqual(fetched.team_id, team.uid) self.assertEqual(fetched.classroom_id, None) self.assertEqual(fetched.filename, filename) self.assertEqual(str(fetched.gcs_path), gcs_path) self.assertEqual(fetched.size, file_size) self.assertEqual(fetched.content_type, 'application/pdf') os.unlink(filename) return (other, teammate, contact, captain, super_admin, team, classroom, report_dict)
def test_full_delete_by_captain(self): captain, contact, classroom, report1, report2 = \ self.create_for_delete() url = '/api/classrooms/{}'.format(classroom.uid) headers = self.login_headers(captain) # Delete the classroom. self.testapp.delete(url, headers=headers, status=204) # Expect the classroom and related reports are gone from the db. self.assertIsNone(Classroom.get_by_id(classroom.uid)) self.assertIsNone(Report.get_by_id(report1.uid)) self.assertIsNone(Report.get_by_id(report2.uid)) # Api should show a 404. self.testapp.get(url, headers=headers, status=404) self.testapp.delete(url, headers=headers, status=404)
def test_delete(self): (other, teammate, contact, captain, super_admin, team, classroom, classReport1, classReport2, teamReport) = self.create_reports() url = '/api/reports/{}'.format(classReport1.uid) headers = self.login_headers(super_admin) # Delete the report. self.testapp.delete(url, headers=headers, status=204) # Expect the report is gone from the db. self.assertIsNone(Report.get_by_id(classReport1.uid)) # Api should show a 404. self.testapp.get(url, headers=headers, status=404) self.testapp.delete(url, headers=headers, status=404)
def delete(self, id=None): # Replaces function of `requires_auth = True`. user = self.get_current_user() if user.user_type == 'public': return self.http_unauthorized() # Overrides ownership, only supers may delete reports. if id is None: # Collection has no DELETE self.error(405) self.response.headers['Allow'] = 'GET, HEAD, POST' elif Report.get_by_id(id): if self.get_current_user().super_admin: super(Reports, self).delete(id) else: self.error(403) else: self.error(404)
def test_delete_removes_related(self): user = User.create(name='foo', email='*****@*****.**') team = Team.create(name='Team Foo', captain_id=user.uid, program_id=self.demo_program.uid) team.put() survey = Survey.create(team_id=team.uid) survey.put() user.owned_teams = [team.uid] user.put() classroom1 = Classroom.create( name='Classroom One', code='trout viper', team_id=team.uid, contact_id='User_contact', num_students=22, grade_level='9-12', ) classroom1.put() classroom2 = Classroom.create( name='Classroom Two', code='trout viper', team_id=team.uid, contact_id='User_contact', num_students=22, grade_level='9-12', ) classroom2.put() report1 = Report.create( team_id=team.uid, classroom_id=classroom1.uid, filename='report1.pdf', gcs_path='/upload/abc', size=10, content_type='application/pdf', ) report1.put() report2 = Report.create( team_id=team.uid, classroom_id=classroom2.uid, filename='report2.pdf', gcs_path='/upload/def', size=10, content_type='application/pdf', ) report2.put() url = '/api/teams/{}'.format(team.uid) headers = self.login_headers(user) # Delete the team. self.testapp.delete(url, headers=headers, status=204) # Expect the survey, classrooms, and related reports are gone from the # db. self.assertIsNone(Survey.get_by_id(survey.uid)) self.assertIsNone(Classroom.get_by_id(classroom1.uid)) self.assertIsNone(Classroom.get_by_id(classroom2.uid)) self.assertIsNone(Report.get_by_id(report1.uid)) self.assertIsNone(Report.get_by_id(report2.uid))