def test_good_token_but_non_member(self): non_member = User.objects.create(username='******') Token.objects.create(user=non_member, key='nonmemberkey') client = APIClient('nonmemberkey') response = client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment') self.assertEqual(response.status_code, 403) self.assertEqual(models.TestRun.objects.count(), 0)
def test_good_token_but_non_member(self): non_member = User.objects.create(username='******') Token.objects.create(user=non_member, key='nonmemberkey') client = APIClient('nonmemberkey') response = client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment') self.assertEqual(response.status_code, 403) self.assertEqual(models.TestRun.objects.count(), 0)
def test_good_token_for_member_with_submit_access(self): member = User.objects.create(username='******') self.group.add_user(member, 'submitter') Token.objects.create(user=member, key='memberkey') client = APIClient('memberkey') response = client.post('/api/submit/mygroup/myproject/1.0.16/myenvironment') self.assertEqual(response.status_code, 201) self.assertEqual(models.TestRun.objects.count(), 1)
def test_good_token_for_member_with_submit_access(self): member = User.objects.create(username='******') self.group.add_user(member, 'submitter') Token.objects.create(user=member, key='memberkey') client = APIClient('memberkey') response = client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment') self.assertEqual(response.status_code, 201) self.assertEqual(models.TestRun.objects.count(), 1)
class TestApiUpperCaseSlug(TestCase): def setUp(self): self.group = models.Group.objects.create(slug='MyGroup') self.project = self.group.projects.create(slug='MyProject') self.project.tokens.create(key='thekey') self.client = APIClient('thekey') def test_accepts_uppercase_in_slug(self): response = self.client.post('/api/submit/MyGroup/MyProject/1.0.0/MyEnvironment') self.assertEqual(response.status_code, 201)
class CiApiTest(TestCase): def setUp(self): self.group = core_models.Group.objects.create(slug='mygroup') self.project = self.group.projects.create(slug='myproject') self.project_admin_user = User.objects.create(username='******') self.group.add_admin(self.project_admin_user) self.project_submission_user = User.objects.create( username='******') self.group.add_user(self.project_submission_user, 'submitter') self.project_member_user = User.objects.create( username='******') self.group.add_user(self.project_member_user, 'member') self.build = self.project.builds.create(version='1') Token.objects.create(user=self.project_submission_user, key='thekey') Token.objects.create(user=self.project_member_user, key='memberkey') Token.objects.create(user=self.project_admin_user, key='adminkey') self.backend = models.Backend.objects.create( name='lava', implementation_type='fake') self.client = APIClient('thekey') self.restclient = RestAPIClient('thekey') self.memberclient = APIClient('memberkey') self.adminclient = APIClient('adminkey') def test_auth(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } self.client.token = 'invalid-token' r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(403, r.status_code) def test_creates_test_run(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter( target=self.project, environment='myenv', target_build=self.build, backend=self.backend, definition='foo: 1', ).count()) def test_invalid_backend_test_run(self): args = { 'backend': 'lava.foo', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_missing_definition_test_run(self): args = {'backend': 'lava'} r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_accepts_definition_as_file_upload(self): args = { 'backend': 'lava', 'definition': open(twoline_job_definition_file) } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) testjob = models.TestJob.objects.filter( target=self.project, environment='myenv', target_build=self.build, backend=self.backend, definition='bar: something\nfoo: 1', ).get() # when parsing back to yaml, it weirdly adds an extra linebreak at the end self.assertEqual('bar: something\nfoo: 1\n', testjob.show_definition) @patch("squad.ci.tasks.submit.delay") def test_schedules_submission(self, submit): args = { 'backend': 'lava', 'definition': 'foo: 1', } self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) job_id = models.TestJob.objects.last().id submit.assert_called_with(job_id) @patch("squad.ci.tasks.fetch.delay") def test_auth_on_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } self.client.token = 'invalid-token' r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(403, r.status_code) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter(target=self.project, environment='myenv', target_build=self.build, backend=self.backend, submitted=True, job_id=testjob_id).count()) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob_mising_id(self, fetch): args = {'backend': 'lava'} r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create(target=self.project, can_resubmit=True) r = self.client.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter_cant_resubmit(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create(target=self.project, can_resubmit=False) r = self.client.post('/api/resubmit/%s' % t.pk) self.assertEqual(403, r.status_code) impl.resubmit.assert_not_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter_token_auth(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create(target=self.project, can_resubmit=True) r = self.restclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter_auth_token_cant_resubmit( self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create(target=self.project, can_resubmit=False) r = self.restclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(403, r.status_code) impl.resubmit.assert_not_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_force_resubmit_submitter_token_auth(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create(target=self.project, can_resubmit=True) r = self.restclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_admin(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create(target=self.project, can_resubmit=True) r = self.adminclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) def test_disallowed_resubmit(self): t = self.backend.test_jobs.create(target=self.project, can_resubmit=True) r = self.memberclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(401, r.status_code) def test_resubmit_invalid_id(self): staff_user_password = "******" staff_user = User.objects.create_superuser( username="******", email="*****@*****.**", password=staff_user_password, is_staff=True) staff_user.save() client = Client() client.login(username=staff_user.username, password=staff_user_password) r = client.post('/api/resubmit/999') self.assertEqual(404, r.status_code)
class CiApiTest(TestCase): def setUp(self): self.group = core_models.Group.objects.create(slug='mygroup') self.project = self.group.projects.create(slug='myproject') self.project.tokens.create(key='thekey') self.backend = models.Backend.objects.create(name='lava') self.client = APIClient('thekey') def test_creates_test_run(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter( target=self.project, environment='myenv', build='1', backend=self.backend, definition='foo: 1', ).count()) def test_invalid_backend_test_run(self): args = { 'backend': 'lava.foo', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_missing_definition_test_run(self): args = {'backend': 'lava'} r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_accepts_definition_as_file_upload(self): args = {'backend': 'lava', 'definition': open(job_definition_file)} self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) @patch("squad.ci.tasks.submit.delay") def test_schedules_submission(self, submit): args = { 'backend': 'lava', 'definition': 'foo: 1', } self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) job_id = models.TestJob.objects.last().id submit.assert_called_with(job_id) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter(target=self.project, environment='myenv', build='1', backend=self.backend, job_id=testjob_id).count()) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob_mising_id(self, fetch): args = {'backend': 'lava'} r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create(target=self.project, can_resubmit=True) r = self.client.get('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) def test_resubmit_invalid_id(self): r = self.client.get('/api/resubmit/999') self.assertEqual(400, r.status_code)
class ApiTest(TestCase): def setUp(self): self.group = models.Group.objects.create(slug='mygroup') self.project = self.group.projects.create(slug='myproject') self.project.tokens.create(key='thekey') self.client = APIClient('thekey') def test_create_object_hierarchy(self): response = self.client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment') self.assertEqual(response.status_code, 201) self.project.builds.get(version='1.0.0') self.project.environments.get(slug='myenvironment') def test_create_test_run(self): test_runs = models.TestRun.objects.count() self.client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment') self.assertEqual(test_runs + 1, models.TestRun.objects.count()) def test_receives_tests_file(self): with open(tests_file) as f: self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', {'tests': f} ) self.assertIsNotNone(models.TestRun.objects.last().tests_file) self.assertNotEqual(0, models.Test.objects.count()) def test_receives_tests_file_as_POST_param(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', {'tests': '{"test1": "pass"}'} ) self.assertIsNotNone(models.TestRun.objects.last().tests_file) self.assertNotEqual(0, models.Test.objects.count()) def test_receives_metrics_file(self): with open(metrics_file) as f: self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', {'metrics': f} ) self.assertIsNotNone(models.TestRun.objects.last().metrics_file) self.assertNotEqual(0, models.Metric.objects.count()) def test_receives_metrics_file_as_POST_param(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', {'metrics': '{"metric1": 10}'} ) self.assertIsNotNone(models.TestRun.objects.last().metrics_file) self.assertNotEqual(0, models.Metric.objects.count()) def test_receives_log_file(self): with open(log_file) as f: self.client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment', {'log': f}) self.assertIsNotNone(models.TestRun.objects.last().log_file) def test_receives_log_file_as_POST_param(self): self.client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment', {'log': "THIS IS THE LOG"}) self.assertIsNotNone(models.TestRun.objects.last().log_file) def test_process_data_on_submission(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'tests': open(tests_file), 'metrics': open(metrics_file), } ) self.assertNotEqual(0, models.Test.objects.count()) self.assertNotEqual(0, models.Metric.objects.count()) self.assertNotEqual(0, models.Status.objects.count()) def test_receives_metadata_file(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'metadata': open(metadata_file), } ) t = models.TestRun.objects.last() self.assertEqual("2016-09-01T00:00:00+00:00", t.datetime.isoformat()) def test_receives_metadata_file_as_POST_param(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'metadata': '{"job_id": "123", "datetime": "2016-09-01T00:00:00+00:00"}', } ) t = models.TestRun.objects.last() self.assertEqual("2016-09-01T00:00:00+00:00", t.datetime.isoformat()) def test_receives_metadata_fields_as_POST_params(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { "build_url": "http://example.com/build/1", "datetime": "2016-09-01T00:00:00+00:00", "job_id": "123", "job_status": "Complete", "job_url": "http://example.com/build/1/jobs/1", "resubmit_url": "http://example.com/build/1/jobs/1/resubmit", } ) t = models.TestRun.objects.last() self.assertEqual("2016-09-01T00:00:00+00:00", t.datetime.isoformat()) self.assertIsNotNone(t.build_url) self.assertIsNotNone(t.job_id) self.assertIsNotNone(t.job_status) self.assertIsNotNone(t.job_url) self.assertIsNotNone(t.resubmit_url) def test_stores_metadata_file(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'metadata': open(metadata_file), } ) t = models.TestRun.objects.last() self.assertEqual(open(metadata_file).read(), t.metadata_file) def test_attachment(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'attachment': open(metadata_file), } ) t = models.TestRun.objects.last() attachment = t.attachments.first() self.assertEqual(open(metadata_file, mode='rb').read(), bytes(attachment.data)) def test_multiple_attachments(self): self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'attachment': [ open(metadata_file), open(log_file), ] } ) t = models.TestRun.objects.last() self.assertIsNotNone(t.attachments.get(filename=os.path.basename(metadata_file))) self.assertIsNotNone(t.attachments.get(filename=os.path.basename(log_file))) def test_unauthorized(self): client = Client() # regular client without auth support response = client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment') self.assertEqual(response.status_code, 401) self.assertEqual(models.TestRun.objects.count(), 0) def test_forbidden(self): self.client.token = 'wrongtoken' response = self.client.post('/api/submit/mygroup/myproject/1.0.0/myenv') self.assertEqual(response.status_code, 403) self.assertEqual(models.TestRun.objects.count(), 0) def test_auth_with_global_token(self): global_token = models.Token.objects.create() self.client.token = global_token.key response = self.client.post('/api/submit/mygroup/myproject/1.0.0/myenvironment') self.assertEqual(201, response.status_code) self.assertEqual(1, models.TestRun.objects.count()) def test_404_on_non_existing_group(self): response = self.client.post('/api/submit/mygrouppp/myproject/1.0.0/myenv') self.assertEqual(404, response.status_code) def test_404_on_non_existing_project(self): response = self.client.post('/api/submit/mygroup/myprojectttt/1.0.0/myenv') self.assertEqual(404, response.status_code) def test_invalid_metrics_json(self): response = self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'metrics': invalid_json(), } ) self.assertEqual(400, response.status_code) def test_invalid_tests_json(self): response = self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'tests': invalid_json(), } ) self.assertEqual(400, response.status_code) def test_invalid_metadata_json(self): response = self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'metadata': invalid_json(), } ) self.assertEqual(400, response.status_code) def test_reject_submission_without_job_id(self): response = self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'metadata': StringIO('{"datetime": "2016-09-01T00:00:00+00:00"}'), } ) self.assertEqual(400, response.status_code) def test_reject_submission_with_existing_job_id(self): def post(): return self.client.post( '/api/submit/mygroup/myproject/1.0.0/myenvironment', { 'metadata': open(metadata_file), } ) first = post() second = post() self.assertEqual(201, first.status_code) self.assertEqual(400, second.status_code)
class CiApiTest(TestCase): def setUp(self): self.group = core_models.Group.objects.create(slug='mygroup') self.usergroup = core_models.UserNamespace.objects.create(slug='~project-member-user') self.project = self.group.projects.create(slug='myproject') self.userproject = self.usergroup.projects.create(slug='userproject') self.project_admin_user = User.objects.create(username='******') self.group.add_admin(self.project_admin_user) self.project_privileged_user = User.objects.create(username='******') self.group.add_user(self.project_privileged_user, 'privileged') self.project_submitter_level_user = User.objects.create(username='******') self.group.add_user(self.project_submitter_level_user, 'submitter') self.project_member_user = User.objects.create(username='******') self.group.add_user(self.project_member_user, 'member') self.usergroup.add_user(self.project_member_user, 'privileged') self.build = self.project.builds.create(version='1') self.userbuild = self.userproject.builds.create(version='1') Token.objects.create(user=self.project_privileged_user, key='thekey') Token.objects.create(user=self.project_submitter_level_user, key='thesubmitterkey') Token.objects.create(user=self.project_member_user, key='memberkey') Token.objects.create(user=self.project_admin_user, key='adminkey') self.backend = models.Backend.objects.create(name='lava', implementation_type='fake') self.client = APIClient('thekey') self.submitter_client = APIClient('thesubmitterkey') self.restclient = RestAPIClient('thekey') self.memberclient = APIClient('memberkey') self.adminclient = APIClient('adminkey') def test_auth(self): self.client.token = 'invalid-token' r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv') self.assertEqual(403, r.status_code) self.assertEqual('User needs permission to submit test jobs.', r.json()['detail']) r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv') self.assertEqual(403, r.status_code) self.assertEqual('User needs permission to submit test jobs.', r.json()['detail']) def test_group_project_not_found(self): r = self.client.post('/api/submitjob/nonexistentgroup/myproject/1/myenv') self.assertEqual(404, r.status_code) self.assertEqual('No Group matches the given query.', r.json()['detail']) r = self.client.post('/api/submitjob/mygroup/nonexistentproject/1/myenv') self.assertEqual(404, r.status_code) self.assertEqual('No Project matches the given query.', r.json()['detail']) def test_creates_test_run(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } r = self.submitter_client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(403, r.status_code) r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) testjob_queryset = models.TestJob.objects.filter( target=self.project, environment='myenv', target_build=self.build, backend=self.backend, definition='foo: 1', ) self.assertEqual( 1, testjob_queryset.count() ) logentry_queryset = LogEntry.objects.filter( user_id=self.project_privileged_user.pk, object_id=testjob_queryset.last().pk ) self.assertEqual( 1, logentry_queryset.count() ) self.assertEqual( ADDITION, logentry_queryset.last().action_flag ) def test_submitjob_private_group(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } r = self.memberclient.post('/api/submitjob/~project-member-user/userproject/1/myenv', args) self.assertEqual(201, r.status_code) testjob_queryset = models.TestJob.objects.filter( target=self.userproject, environment='myenv', target_build=self.userbuild, backend=self.backend, definition='foo: 1', ) self.assertEqual( 1, testjob_queryset.count() ) logentry_queryset = LogEntry.objects.filter( user_id=self.project_member_user.pk, object_id=testjob_queryset.last().pk ) self.assertEqual( 1, logentry_queryset.count() ) self.assertEqual( ADDITION, logentry_queryset.last().action_flag ) def test_invalid_backend_test_run(self): args = { 'backend': 'lava.foo', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_missing_definition_test_run(self): args = { 'backend': 'lava' } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_accepts_definition_as_file_upload(self): args = { 'backend': 'lava', 'definition': open(twoline_job_definition_file) } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) testjob = models.TestJob.objects.filter( target=self.project, environment='myenv', target_build=self.build, backend=self.backend, definition='bar: something\nfoo: 1', ).get() # when parsing back to yaml, it weirdly adds an extra linebreak at the end self.assertEqual('bar: something\nfoo: 1\n', testjob.show_definition) @patch("squad.ci.tasks.submit.delay") def test_schedules_submission(self, submit): args = { 'backend': 'lava', 'definition': 'foo: 1', } r = self.submitter_client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(403, r.status_code) self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) job_id = models.TestJob.objects.last().id submit.assert_called_with(job_id) @patch("squad.ci.tasks.fetch.apply_async") def test_auth_on_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } self.client.token = 'invalid-token' r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(403, r.status_code) @patch("squad.ci.tasks.fetch.apply_async") def test_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) testjob_queryset = models.TestJob.objects.filter( target=self.project, environment='myenv', target_build=self.build, backend=self.backend, submitted=True, job_id=testjob_id ) self.assertEqual( 1, testjob_queryset.count() ) logentry_queryset = LogEntry.objects.filter( user_id=self.project_privileged_user.pk, object_id=testjob_queryset.last().pk ) self.assertEqual( 1, logentry_queryset.count() ) self.assertEqual( ADDITION, logentry_queryset.last().action_flag ) @patch("squad.ci.tasks.fetch.apply_async") def test_watch_testjob_private_group(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } r = self.memberclient.post('/api/watchjob/~project-member-user/userproject/1/myenv', args) self.assertEqual(201, r.status_code) testjob_queryset = models.TestJob.objects.filter( target=self.userproject, environment='myenv', target_build=self.userbuild, backend=self.backend, submitted=True, job_id=testjob_id ) self.assertEqual( 1, testjob_queryset.count() ) logentry_queryset = LogEntry.objects.filter( user_id=self.project_member_user.pk, object_id=testjob_queryset.last().pk ) self.assertEqual( 1, logentry_queryset.count() ) self.assertEqual( ADDITION, logentry_queryset.last().action_flag ) @patch("squad.ci.tasks.fetch.apply_async") def test_watch_testjob_mising_id(self, fetch): args = { 'backend': 'lava' } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.client.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter_cant_resubmit(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=False ) r = self.client.post('/api/resubmit/%s' % t.pk) self.assertEqual(403, r.status_code) impl.resubmit.assert_not_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter_token_auth(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.restclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_submitter_auth_token_cant_resubmit(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=False ) r = self.restclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(403, r.status_code) impl.resubmit.assert_not_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_force_resubmit_submitter_token_auth(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.restclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit_admin(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.adminclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) def test_disallowed_resubmit(self): t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.memberclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(401, r.status_code) def test_resubmit_invalid_id(self): staff_user_password = "******" staff_user = User.objects.create_superuser( username="******", email="*****@*****.**", password=staff_user_password, is_staff=True) staff_user.save() client = Client() client.login(username=staff_user.username, password=staff_user_password) r = client.post('/api/resubmit/999') self.assertEqual(404, r.status_code) @patch('squad.ci.models.TestJob.resubmit', side_effect=SubmissionIssue('BOOM')) def test_resubmit_error(self, resubmit): t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.adminclient.post('/api/resubmit/%s' % t.pk) self.assertEqual(500, r.status_code) self.assertEqual('BOOM', r.content.decode())
class CiApiTest(TestCase): def setUp(self): self.group = core_models.Group.objects.create(slug='mygroup') self.project = self.group.projects.create(slug='myproject') self.project_submission_user = User.objects.create(username='******') usergroup = self.group.user_groups.create() self.project_submission_user.groups.add(usergroup) Token.objects.create(user=self.project_submission_user, key='thekey') self.backend = models.Backend.objects.create(name='lava') self.client = APIClient('thekey') def test_auth(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } self.client.token = 'invalid-token' r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(401, r.status_code) def test_creates_test_run(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter( target=self.project, environment='myenv', build='1', backend=self.backend, definition='foo: 1', ).count() ) def test_invalid_backend_test_run(self): args = { 'backend': 'lava.foo', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_missing_definition_test_run(self): args = { 'backend': 'lava' } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_accepts_definition_as_file_upload(self): args = { 'backend': 'lava', 'definition': open(job_definition_file) } self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) @patch("squad.ci.tasks.submit.delay") def test_schedules_submission(self, submit): args = { 'backend': 'lava', 'definition': 'foo: 1', } self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) job_id = models.TestJob.objects.last().id submit.assert_called_with(job_id) @patch("squad.ci.tasks.fetch.delay") def test_auth_on_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } self.client.token = 'invalid-token' r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(401, r.status_code) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter( target=self.project, environment='myenv', build='1', backend=self.backend, submitted=True, job_id=testjob_id ).count() ) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob_mising_id(self, fetch): args = { 'backend': 'lava' } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) staff_user_password = "******" staff_user = User.objects.create_superuser( username="******", email="*****@*****.**", password=staff_user_password, is_staff=True) staff_user.save() client = Client() client.login(username=staff_user.username, password=staff_user_password) r = client.get('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) def test_disallowed_resubmit(self): t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.client.get('/api/resubmit/%s' % t.pk) self.assertEqual(401, r.status_code) def test_resubmit_invalid_id(self): staff_user_password = "******" staff_user = User.objects.create_superuser( username="******", email="*****@*****.**", password=staff_user_password, is_staff=True) staff_user.save() client = Client() client.login(username=staff_user.username, password=staff_user_password) r = client.get('/api/resubmit/999') self.assertEqual(400, r.status_code)
class CiApiTest(TestCase): def setUp(self): self.group = core_models.Group.objects.create(slug='mygroup') self.project = self.group.projects.create(slug='myproject') self.project_submission_user = User.objects.create(username='******') self.group.add_admin(self.project_submission_user) self.build = self.project.builds.create(version='1') Token.objects.create(user=self.project_submission_user, key='thekey') self.backend = models.Backend.objects.create(name='lava') self.client = APIClient('thekey') def test_auth(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } self.client.token = 'invalid-token' r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(403, r.status_code) def test_creates_test_run(self): args = { 'backend': 'lava', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter( target=self.project, environment='myenv', target_build=self.build, backend=self.backend, definition='foo: 1', ).count() ) def test_invalid_backend_test_run(self): args = { 'backend': 'lava.foo', 'definition': 'foo: 1', } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_missing_definition_test_run(self): args = { 'backend': 'lava' } r = self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) def test_accepts_definition_as_file_upload(self): args = { 'backend': 'lava', 'definition': open(job_definition_file) } self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) @patch("squad.ci.tasks.submit.delay") def test_schedules_submission(self, submit): args = { 'backend': 'lava', 'definition': 'foo: 1', } self.client.post('/api/submitjob/mygroup/myproject/1/myenv', args) job_id = models.TestJob.objects.last().id submit.assert_called_with(job_id) @patch("squad.ci.tasks.fetch.delay") def test_auth_on_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } self.client.token = 'invalid-token' r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(403, r.status_code) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob(self, fetch): testjob_id = 1234 args = { 'backend': 'lava', 'testjob_id': testjob_id, } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(201, r.status_code) self.assertEqual( 1, models.TestJob.objects.filter( target=self.project, environment='myenv', target_build=self.build, backend=self.backend, submitted=True, job_id=testjob_id ).count() ) @patch("squad.ci.tasks.fetch.delay") def test_watch_testjob_mising_id(self, fetch): args = { 'backend': 'lava' } r = self.client.post('/api/watchjob/mygroup/myproject/1/myenv', args) self.assertEqual(400, r.status_code) @patch('squad.ci.models.Backend.get_implementation') def test_resubmit(self, get_implementation): impl = MagicMock() impl.resubmit = MagicMock() get_implementation.return_value = impl t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) staff_user_password = "******" staff_user = User.objects.create_superuser( username="******", email="*****@*****.**", password=staff_user_password, is_staff=True) staff_user.save() client = Client() client.login(username=staff_user.username, password=staff_user_password) r = client.post('/api/resubmit/%s' % t.pk) self.assertEqual(201, r.status_code) impl.resubmit.assert_called() t.refresh_from_db() self.assertEqual(False, t.can_resubmit) def test_disallowed_resubmit(self): t = self.backend.test_jobs.create( target=self.project, can_resubmit=True ) r = self.client.post('/api/resubmit/%s' % t.pk) self.assertEqual(401, r.status_code) def test_resubmit_invalid_id(self): staff_user_password = "******" staff_user = User.objects.create_superuser( username="******", email="*****@*****.**", password=staff_user_password, is_staff=True) staff_user.save() client = Client() client.login(username=staff_user.username, password=staff_user_password) r = client.post('/api/resubmit/999') self.assertEqual(404, r.status_code)