def test_update_job(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login(AdminFactory()) response = self.put( url_for('api.job', id=task.id), { 'name': task.name, 'description': 'New description', 'task': task.task, 'crontab': task.crontab.to_json() }) self.assert200(response) self.assertEqual(response.json['id'], str(task.id)) self.assertEqual(response.json['name'], task.name) self.assertEqual(response.json['task'], task.task) self.assertEqual(response.json['description'], 'New description') self.assertIsNotNone(response.json['crontab']) self.assertIsNone(response.json['interval'])
def test_create_crontab_job(self): @job('a-job') def test_job(): pass data = { 'name': 'A crontab job', 'description': 'A simple crontab job doing nothing', 'task': 'a-job', 'crontab': { 'minute': '0', 'hour': '0' } } self.login(AdminFactory()) response = self.post(url_for('api.jobs'), data) self.assert201(response) self.assertEqual(response.json['name'], data['name']) self.assertEqual(response.json['description'], data['description']) self.assertEqual(response.json['task'], data['task']) self.assertEqual( response.json['crontab'], { 'minute': '0', 'hour': '0', 'day_of_week': '*', 'day_of_month': '*', 'month_of_year': '*', })
def test_update_job_change_type(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login(AdminFactory()) response = self.put( url_for('api.job', id=task.id), { 'name': task.name, 'description': task.description, 'task': task.task, 'interval': { 'every': 5, 'period': 'minutes', } }) self.assert200(response) self.assertEqual(response.json['id'], str(task.id)) self.assertEqual(response.json['name'], task.name) self.assertEqual(response.json['task'], task.task) self.assertEqual(response.json['description'], task.description) self.assertEqual(response.json['interval']['every'], 5) self.assertEqual(response.json['interval']['period'], 'minutes') self.assertIsNone(response.json['crontab'])
def test_set_home_reuses(self): ids = [r.id for r in VisibleReuseFactory.create_batch(3)] self.login(AdminFactory()) response = self.put(url_for('api.home_reuses'), ids) self.assert200(response) self.assertEqual(len(response.json), len(ids)) site = Site.objects.get(id=self.app.config['SITE_ID']) self.assertEqual([r.id for r in site.settings.home_reuses], ids)
def test_get_home_reuses(self): site = SiteFactory.create( id=self.app.config['SITE_ID'], settings__home_reuses=VisibleReuseFactory.create_batch(3) ) current_site.reload() self.login(AdminFactory()) response = self.get(url_for('api.home_reuses')) self.assert200(response) self.assertEqual(len(response.json), len(site.settings.home_reuses))
def test_validate_source(self): '''It should allow to validate a source if admin''' self.login(AdminFactory()) source = HarvestSourceFactory() data = {'state': VALIDATION_ACCEPTED} url = url_for('api.validate_harvest_source', ident=str(source.id)) response = self.post(url, data) self.assert200(response) source.reload() self.assertEqual(source.validation.state, VALIDATION_ACCEPTED) self.assertEqual(source.validation.by, self.user)
def test_reject_source(self): '''It should allow to reject a source if admin''' self.login(AdminFactory()) source = HarvestSourceFactory() data = {'state': VALIDATION_REFUSED, 'comment': 'Not valid'} url = url_for('api.validate_harvest_source', ident=str(source.id)) response = self.post(url, data) self.assert200(response) source.reload() self.assertEqual(source.validation.state, VALIDATION_REFUSED) self.assertEqual(source.validation.comment, 'Not valid') self.assertEqual(source.validation.by, self.user)
def test_pending_harvester_validations(self): source = HarvestSourceFactory() admin = AdminFactory() user = UserFactory() self.assertEqual(len(validate_harvester_notifications(user)), 0) notifications = validate_harvester_notifications(admin) self.assertEqual(len(notifications), 1) dt, details = notifications[0] self.assertEqualDates(dt, source.created_at) self.assertEqual(details['id'], source.id) self.assertEqual(details['name'], source.name)
def test_delete_job(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login(AdminFactory()) response = self.delete(url_for('api.job', id=task.id)) self.assert204(response) self.assertIsNone(PeriodicTask.objects(id=task.id).first())
def test_with_other_user_admin(self): Ownable, OwnableForm = self.factory() user = UserFactory() admin = AdminFactory() login_user(admin) form = OwnableForm(MultiDict({'owner': str(user.id)})) self.assertEqual(form.owner.data, user) form.validate() self.assertEqual(form.errors, {}) ownable = Ownable() form.populate_obj(ownable) self.assertEqual(ownable.owner, user)
def test_fail_on_create_with_both_crontab_and_interval(self): @job('a-job') def test_job(): pass data = { 'name': 'A mixed job', 'description': 'A simple crontab job doing nothing', 'task': 'a-job', 'crontab': { 'minute': '0', 'hour': '0' }, 'interval': { 'every': 5, 'period': 'minutes' } } self.login(AdminFactory()) response = self.post(url_for('api.jobs'), data) self.assertStatus(response, 400)
def test_admin_can_set_organization(self): Ownable, OwnableForm = self.factory() user = UserFactory() org = OrganizationFactory() ownable = Ownable(owner=user) login_user(AdminFactory()) form = OwnableForm(MultiDict({'organization': str(org.id)}), ownable) self.assertEqual(form.owner.data, user) self.assertEqual(form.organization.data, org) self.assertTrue(form.validate()) self.assertEqual(form.errors, {}) self.assertIsNone(form.owner.data) self.assertEqual(form.organization.data, org) form.populate_obj(ownable) self.assertIsNone(ownable.owner) self.assertEqual(ownable.organization, org)
def test_create_interval_job(self): @job('a-job') def test_job(): pass data = { 'name': 'An interval job', 'description': 'A simple interval job doing nothing', 'task': 'a-job', 'interval': { 'every': 5, 'period': 'minutes' } } self.login(AdminFactory()) response = self.post(url_for('api.jobs'), data) self.assert201(response) self.assertEqual(response.json['name'], data['name']) self.assertEqual(response.json['description'], data['description']) self.assertEqual(response.json['task'], data['task']) self.assertEqual(response.json['interval'], data['interval'])
def test_render_theme_config_form(self): '''It should render the site theme form''' self.login(AdminFactory()) response = self.get(url_for('admin.theme')) self.assert200(response)
def test_render_jobs_empty(self): '''It should render the site jobs page''' self.login(AdminFactory()) response = self.get(url_for('admin.jobs')) self.assert200(response)