def test_logout(self): url = '/logout/' request_with_all = TestRequest.blank( url, method='GET', referrer='http://localhost/content/list/', headers=self.mk_session()[1]) request_without_ref = TestRequest.blank(url, method='GET', headers=self.mk_session()[1]) request_with_invalid_ref = TestRequest.blank( url, method='GET', referrer='http://example.com/page/', headers=self.mk_session()[1]) for request in (request_with_all, request_without_ref, request_with_invalid_ref): resp = self.app.request(request) self.assertEqual(resp.status_int, 302) if request == request_with_all: self.assertEqual(resp.location, request.referrer) else: self.assertEqual(resp.location, 'http://localhost/') # check that the session is no longer authenticated session = Session(request, id=request.cookies['beaker.session.id'], use_cookies=False) self.assertNotIn('auth.userid', session) self.app.reset()
def test_redirect_to_login(self): url = '/login/hub/?%s' % urlencode({'_LOCALE_': 'urd_IN'}) request_with_all = TestRequest.blank( url, method='GET', referrer='http://localhost/content/list/') request_without_ref = TestRequest.blank(url, method='GET') request_with_invalid_ref = TestRequest.blank( url, method='GET', referrer='http://example.com/page/') for request in (request_with_all, request_without_ref, request_with_invalid_ref): resp = self.app.request(request) self.assertEqual(resp.status_int, 302) parts = urlparse(resp.location) params = parse_qs(parts.query) self.assertIn('service', params) self.assertIn('_LOCALE_', params) self.assertEqual(params['_LOCALE_'][0], request.params['_LOCALE_']) callback_url = params['service'][0] parts = urlparse(callback_url) self.assertEqual(parts[:3], ('http', 'localhost', '/login/')) if request is request_with_all: params = parse_qs(parts.query) self.assertIn('url', params) self.assertEqual( params['url'][0], request.referrer) else: self.assertFalse(parts.query)
def test_redirect_to_login(self): url = '/login/hub/?%s' % urlencode({'_LOCALE_': 'urd_IN'}) request_with_all = TestRequest.blank( url, method='GET', referrer='http://localhost/content/list/') request_without_ref = TestRequest.blank(url, method='GET') request_with_invalid_ref = TestRequest.blank( url, method='GET', referrer='http://example.com/page/') for request in (request_with_all, request_without_ref, request_with_invalid_ref): resp = self.app.request(request) self.assertEqual(resp.status_int, 302) parts = urlparse(resp.location) params = parse_qs(parts.query) self.assertIn('service', params) self.assertIn('_LOCALE_', params) self.assertEqual(params['_LOCALE_'][0], request.params['_LOCALE_']) callback_url = params['service'][0] parts = urlparse(callback_url) self.assertEqual(parts[:3], ('http', 'localhost', '/login/')) if request is request_with_all: params = parse_qs(parts.query) self.assertIn('url', params) self.assertEqual(params['url'][0], request.referrer) else: self.assertFalse(parts.query)
def test_locale_negotiator(self): request = TestRequest.blank('', {}, headers={}) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), 'en') request = TestRequest.blank('', {}, headers={ 'Accept-Language': 'es', }) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), 'es') request = TestRequest.blank( '', {}, headers={ 'Accept-Language': 'de', # german is not supported }) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), None) request = TestRequest.blank('', {}, headers={ 'Accept-Language': 'de, es', }) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), 'es')
def test_logout(self): url = '/logout/' request_with_all = TestRequest.blank( url, method='GET', referrer='http://localhost/content/list/', headers=self.mk_session()[1]) request_without_ref = TestRequest.blank( url, method='GET', headers=self.mk_session()[1]) request_with_invalid_ref = TestRequest.blank( url, method='GET', referrer='http://example.com/page/', headers=self.mk_session()[1]) for request in (request_with_all, request_without_ref, request_with_invalid_ref): resp = self.app.request(request) self.assertEqual(resp.status_int, 302) if request == request_with_all: self.assertEqual(resp.location, request.referrer) else: self.assertEqual(resp.location, 'http://localhost/') # check that the session is no longer authenticated session = Session( request, id=request.cookies['beaker.session.id'], use_cookies=False) self.assertNotIn('auth.userid', session) self.app.reset()
def test_12_github_webhook_negative(self): request = TestRequest.blank('/jobs/%s/github-webhook' % self.test_state['job_id']) request.method = 'POST' request.body = 'unexpected content' _response = self.app.do_request(request, 400, True) request = TestRequest.blank('/jobs/%s/github-webhook' % self.test_state['job_id']) request.method = 'POST' request.body = json.dumps({'ref': 'refs/heads/wrongbranch'}) _response = self.app.do_request(request, 400, True)
def test_login(self): ticket = 'iamaticket' redirect_url = 'http://localhost/content/list/' user_data = { 'uuid': 'imauuid', 'username': '******', 'app_data': { 'display_name': 'foobar' } } responses.add(responses.GET, re.compile(r'.*/sso/validate.*'), body=json.dumps(user_data), status=200, content_type='application/json') request_with_url = TestRequest.blank('/login/?%s' % urlencode({ 'ticket': ticket, 'url': redirect_url })) request_without_url = TestRequest.blank('/login/?%s' % urlencode({'ticket': ticket})) for request in (request_with_url, request_without_url): resp = self.app.request(request) self.assertEqual(resp.status_int, 302) if request == request_with_url: self.assertEqual(resp.location, redirect_url) else: self.assertEqual(resp.location, 'http://localhost/') # check that session contains user data self.assertIn('beaker.session.id', self.app.cookies) session = Session(request, id=self.app.cookies['beaker.session.id'], use_cookies=False) self.assertEqual(session['auth.userid'], user_data['uuid']) self.assertEqual(session[USER_DATA_SESSION_KEY], user_data) self.app.reset() responses.reset() responses.add(responses.GET, re.compile(r'.*/sso/validate.*'), body=json.dumps('no\n'), status=200, content_type='application/json') resp = self.app.request(request_with_url) self.assertEqual(resp.status_int, 302) self.assertEqual(resp.location, redirect_url) self.assertNotIn('beaker.session.id', self.app.cookies)
def test_login(self): ticket = 'iamaticket' redirect_url = 'http://localhost/content/list/' user_data = { 'uuid': 'imauuid', 'username': '******', 'app_data': {'display_name': 'foobar'} } responses.add( responses.GET, re.compile(r'.*/sso/validate.*'), body=json.dumps(user_data), status=200, content_type='application/json') request_with_url = TestRequest.blank( '/login/?%s' % urlencode({'ticket': ticket, 'url': redirect_url})) request_without_url = TestRequest.blank( '/login/?%s' % urlencode({'ticket': ticket})) for request in (request_with_url, request_without_url): resp = self.app.request(request) self.assertEqual(resp.status_int, 302) if request == request_with_url: self.assertEqual(resp.location, redirect_url) else: self.assertEqual(resp.location, 'http://localhost/') # check that session contains user data self.assertIn('beaker.session.id', self.app.cookies) session = Session( request, id=self.app.cookies['beaker.session.id'], use_cookies=False) self.assertEqual(session['auth.userid'], user_data['uuid']) self.assertEqual(session[USER_DATA_SESSION_KEY], user_data) self.app.reset() responses.reset() responses.add( responses.GET, re.compile(r'.*/sso/validate.*'), body=json.dumps('no\n'), status=200, content_type='application/json') resp = self.app.request(request_with_url) self.assertEqual(resp.status_int, 302) self.assertEqual(resp.location, redirect_url) self.assertNotIn('beaker.session.id', self.app.cookies)
def test_04_update_build_state(self): request = TestRequest.blank('/jobs/%s/builds/%s/state' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/json') request.method = 'PUT' request.body = json.dumps({"status": "pending", "config": {"name": "test"}}) response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" assert result.has_key('state'), "state went missing" assert result['state'].has_key('status'), "status went missing" assert result['state']['status'] == 'pending', "wrong status" assert result['state'].has_key('config'), "config went missing" assert result['state']['config'].has_key('name'), "config/name went missing" assert result['state']['config']['name'] == 'test', "wrong name" response = self.app.request('/jobs/%s/builds/%s/state' % (self.test_state['job_id'], self.test_state['build_number'])) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" assert result.has_key('state'), "state went missing" assert result['state'].has_key('status'), "status went missing" assert result['state']['status'] == 'pending', "wrong status" assert result['state'].has_key('config'), "config went missing" assert result['state']['config'].has_key('name'), "config/name went missing" assert result['state']['config']['name'] == 'test', "wrong name"
def test_04_update_job(self): job_id = self.test_state.get('job_id') if job_id is None: raise SkipTest( "Skipping test for single job config update, no recorded state" ) new_job_config = json.dumps({ 'build_tasks': ['something_else'], 'job_id': 'test_job' }) request = TestRequest.blank('/jobs/%s' % job_id, content_type='application/json') request.method = 'PUT' request.body = new_job_config response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('config'), "config entry went missing" assert result['config']['build_tasks'] == ['something_else' ], "Wrong command" response = self.app.request('/jobs') result = json.loads(response.body) assert result.has_key('jobs'), "Jobs entry went missing" assert len(result['jobs']) == 1, "Invalid job count"
def _gen_request(self, method, url, params='', headers=None, extra_environ=None, status=None, upload_files=None, expect_errors=False, content_type=None, body=None): """ Do a generic request. """ environ = self._make_environ(extra_environ) # @@: Should this be all non-strings? if isinstance(params, (list, tuple, dict)): params = urllib.urlencode(params) if hasattr(params, 'items'): params = urllib.urlencode(params.items()) if upload_files: params = cgi.parse_qsl(params, keep_blank_values=True) content_type, params = self.encode_multipart( params, upload_files) environ['CONTENT_TYPE'] = content_type elif params: environ.setdefault('CONTENT_TYPE', 'application/x-www-form-urlencoded') if '?' in url: url, environ['QUERY_STRING'] = url.split('?', 1) else: environ['QUERY_STRING'] = '' if content_type is not None: environ['CONTENT_TYPE'] = content_type environ['CONTENT_LENGTH'] = str(len(params)) environ['REQUEST_METHOD'] = method environ['wsgi.input'] = StringIO(params) req = TestRequest.blank(url, environ) if headers: req.headers.update(headers) if body: req.body = body return self.do_request(req, status=status, expect_errors=expect_errors)
def wapp_post(wapp, resourcetype, data, test_session_id): """ We build the request as a blank and copy it to allow restrest to work """ URL = get_url(resourcetype, id_=None, method="POST") headerd = get_cookie_hdr(test_session_id) ### data_as_json = json.dumps(data) err.write("\n"+"URL: %s" % URL) err.write("\n"+"body: %s" % data_as_json[:100]) err.write("\n"+"hdrs: %s" % headerd) req = TestRequest.blank(URL, method="POST", body=data_as_json, headers=headerd) req.content_type = "application/json; charset=utf-8" for k, v in wapp.extra_environ.items(): req.environ.setdefault(k, v) reqcp = req.copy() resp = wapp.do_request(req, status="*", expect_errors=True) capture_conversation(reqcp, resp) ### return resp
def set_user_cookie(self, user_id): request = TestRequest.blank('', {}) request.registry = self.testapp.app.registry remember_headers = remember(request, user_id) cookie_value = remember_headers[0][1].split('"')[1] self.testapp.set_cookie('auth_tkt', cookie_value) return request
def set_user_cookie(self, user_id): request = TestRequest.blank('', {}) request.registry = self.testapp.app.registry remember_headers = remember(request, user_id) cookie_value = remember_headers[0][1].split('"')[1] self.testapp.cookies['auth_tkt'] = cookie_value return request
def test_02_trigger_build(self): request = TestRequest.blank('/jobs/%s/builds' % self.test_state['job_id']) request.method = 'POST' response = self.app.do_request(request, 201, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" self.test_state['build_number'] = result['build_number']
def test_08_update_workspace(self): request = TestRequest.blank('/jobs/%s/builds/%s/workspace' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/octet-stream') request.method = 'PUT' request.body = 'updated_test_content' _ = self.app.do_request(request, 204, False) response = self.app.request('/jobs/%s/builds/%s/workspace' % (self.test_state['job_id'], self.test_state['build_number'])) assert response.body == 'updated_test_content', "Wrong data"
def test_05_delete_artifact(self): request = TestRequest.blank( '/jobs/%s/builds/%s/artifacts/%s' % (self.test_state['job_id'], self.test_state['build_number'], self.test_state['artifact_id']), content_type='application/octet-stream') request.method = 'DELETE' _ = self.app.do_request(request, 204, False)
def test_signature_mangled(self): app.get('/') orig_cookie = app.cookies['manhattan'] # truncate the last 4 chars, which will blow the sig bad_cookie = orig_cookie[:-4] bad_request = TestRequest.blank('/', cookies={'manhattan': bad_cookie}) with self.assertRaises(BadSignature): app.request(bad_request)
def test_signature_mangled(self): app.get('/') orig_cookie = app.cookies['manhattan'] # truncate the last 4 chars, which will blow the sig bad_cookie = orig_cookie[:-4] bad_request = TestRequest.blank('/', cookies={'manhattan': bad_cookie}) app.request(bad_request) new_cookie = app.cookies['manhattan'] self.assertNotEqual(bad_cookie, new_cookie)
def test_05_update_with_wrong_assignee(self): task_id = self.test_state.get("id") if task_id is None: raise SkipTest("Skipping test for single task update, no recorded state") new_task_data = json.dumps({"command": "something_else", "assignee": "my-id-not-same"}) request = TestRequest.blank("/tasks/%s" % task_id, content_type="application/json") request.method = "PUT" request.body = new_task_data response = self.app.do_request(request, 409, False)
def _app_options(self, url, headers=None, status=None, expect_errors=False): """ To be injected into TestApp if it doesn't have an options method available """ req = TestRequest.blank(path=url, method='OPTIONS', headers=headers, environ=self.extra_environ) return self.do_request(req, status, expect_errors)
def test_05_update_with_wrong_assignee(self): task_id = self.test_state.get('id') if task_id is None: raise SkipTest("Skipping test for single task update, no recorded state") new_task_data = json.dumps({'command': 'something_else', 'assignee': 'my-id-not-same'}) request = TestRequest.blank('/tasks/%s' % task_id, content_type='application/json') request.method = 'PUT' request.body = new_task_data response = self.app.do_request(request, 409, False)
def test_00_setup(self): test_job_data_file = os.path.join(os.path.dirname(__file__), 'test-frontend-jobs-builds_job-config.json') test_job_data = json.load(file(test_job_data_file, 'rb')) request = TestRequest.blank('/jobs/%s' % str(test_job_data['job_id']), content_type='application/json') request.method = 'PUT' request.body = json.dumps(test_job_data) response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('config'), "config entry went missing" self.test_state['job_id'] = str(result['job_id']) request = TestRequest.blank('/jobs/%s/builds' % self.test_state['job_id']) request.method = 'POST' response = self.app.do_request(request, 201, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" self.test_state['build_number'] = str(result['build_number'])
def test_05_console_log(self): response = self.app.request('/jobs/%s/builds/%s/console' % (self.test_state['job_id'], self.test_state['build_number'])) assert response.body == '', "console log was not empty" request = TestRequest.blank('/jobs/%s/builds/%s/console' % (self.test_state['job_id'], self.test_state['build_number']), content_type='text/plain') request.method = 'POST' request.body = 'line1\n' _ = self.app.do_request(request, 204, False) response = self.app.request('/jobs/%s/builds/%s/console' % (self.test_state['job_id'], self.test_state['build_number'])) assert response.body == 'line1\n', "Wrong content" request = TestRequest.blank('/jobs/%s/builds/%s/console' % (self.test_state['job_id'], self.test_state['build_number']), content_type='text/plain') request.method = 'POST' request.body = 'line2\n' _ = self.app.do_request(request, 204, False) response = self.app.request('/jobs/%s/builds/%s/console' % (self.test_state['job_id'], self.test_state['build_number'])) assert response.body == 'line1\nline2\n', "Wrong content"
def test_01_create_artifact(self): request = TestRequest.blank('/jobs/%s/builds/%s/artifacts' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/octet-stream') request.method = 'POST' request.body = 'test_content' response = self.app.do_request(request, 201, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" assert result.has_key('artifact_id'), "Artifact ID went missing" self.test_state['artifact_id'] = str(result['artifact_id'])
def test_02_post_task(self): task_data = json.dumps({ 'command': 'something' }) request = TestRequest.blank('/tasks', content_type='application/json') request.method = 'POST' request.body = task_data response = self.app.do_request(request, 201, False) result = json.loads(response.body) assert result.has_key('id'), "ID entry went missing" assert result.has_key('data'), "data entry went missing" self.test_state['id'] = str(result['id'])
def test_02_post_task(self): task_data = json.dumps({"command": "something"}) request = TestRequest.blank("/tasks", content_type="application/json") request.method = "POST" request.body = task_data response = self.app.do_request(request, 201, False) result = json.loads(response.body) assert result.has_key("id"), "ID entry went missing" assert result.has_key("data"), "data entry went missing" self.test_state["id"] = str(result["id"])
def test_00_setup(self): test_job_data_file = os.path.join(os.path.dirname(__file__), 'test-frontend-jobs-builds_job-config.json') test_job_data = json.load(file(test_job_data_file, 'rb')) request = TestRequest.blank('/jobs/%s' % str(test_job_data['job_id']), content_type='application/json') request.method = 'PUT' request.body = json.dumps(test_job_data) response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('config'), "config entry went missing" self.test_state['job_id'] = str(result['job_id'])
def test_07_delete(self): task_id = self.test_state.get('id') if task_id is None: raise SkipTest("Skipping test for single task status, no recorded state") request = TestRequest.blank('/tasks/%s' % task_id) request.method = 'DELETE' response = self.app.do_request(request, 204, False) response = self.app.request('/tasks') result = json.loads(response.body) assert result.has_key('tasks'), "Tasks entry went missing" assert len(result['tasks']) == 0, "Invalid task count"
def test_05_delete_job(self): job_id = self.test_state.get('job_id') if job_id is None: raise SkipTest("Skipping test for single job deletion, no recorded state") request = TestRequest.blank('/jobs/%s' % job_id) request.method = 'DELETE' response = self.app.do_request(request, 204, False) response = self.app.request('/jobs') result = json.loads(response.body) assert result.has_key('jobs'), "Jobs entry went missing" assert len(result['jobs']) == 0, "Invalid job count"
def test_05_delete_job(self): job_id = self.test_state.get('job_id') if job_id is None: raise SkipTest( "Skipping test for single job deletion, no recorded state") request = TestRequest.blank('/jobs/%s' % job_id) request.method = 'DELETE' response = self.app.do_request(request, 204, False) response = self.app.request('/jobs') result = json.loads(response.body) assert result.has_key('jobs'), "Jobs entry went missing" assert len(result['jobs']) == 0, "Invalid job count"
def test_01_create_artifact(self): request = TestRequest.blank( '/jobs/%s/builds/%s/artifacts' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/octet-stream') request.method = 'POST' request.body = 'test_content' response = self.app.do_request(request, 201, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" assert result.has_key('artifact_id'), "Artifact ID went missing" self.test_state['artifact_id'] = str(result['artifact_id'])
def test_04_update_artifact(self): request = TestRequest.blank('/jobs/%s/builds/%s/artifacts/%s' % (self.test_state['job_id'], self.test_state['build_number'], self.test_state['artifact_id']), content_type='application/octet-stream') request.method = 'PUT' request.body = 'test_content_modified' response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" assert result.has_key('artifact_id'), "Artifact ID went missing" assert result['artifact_id'] == self.test_state['artifact_id'], "Artifact ID mismatch" response = self.app.request('/jobs/%s/builds/%s/artifacts/%s' % (self.test_state['job_id'], self.test_state['build_number'], self.test_state['artifact_id'])) assert response.body == 'test_content_modified', "Wrong data"
def test_locale_negotiator(self): request = TestRequest.blank('', {}, headers={}) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), 'en') request = TestRequest.blank('', {}, headers={ 'Accept-Language': 'es', }) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), 'es') request = TestRequest.blank('', {}, headers={ 'Accept-Language': 'de', # german is not supported }) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), None) request = TestRequest.blank('', {}, headers={ 'Accept-Language': 'de, es', }) request.registry = self.testapp.app.registry self.assertEqual(locale_negotiator(request), 'es')
def wapp_delete(wapp, resourcetype, id_, test_session_id): """ """ URL = get_url(resourcetype, id_=id_, method="DELETE") headerd = get_cookie_hdr(test_session_id) req = TestRequest.blank(URL, method="DELETE", headers=headerd) for k, v in wapp.extra_environ.items(): req.environ.setdefault(k, v) reqcp = req.copy() resp = wapp.do_request(req, status="*", expect_errors=True) capture_conversation(reqcp, resp) return resp
def test_04_update(self): task_id = self.test_state.get("id") if task_id is None: raise SkipTest("Skipping test for single task update, no recorded state") new_task_data = json.dumps({"command": "something_else", "assignee": "my-id"}) request = TestRequest.blank("/tasks/%s" % task_id, content_type="application/json") request.method = "PUT" request.body = new_task_data response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key("id"), "ID entry went missing" assert result.has_key("data"), "data entry went missing" assert result["data"]["command"] == "something_else", "Wrong command" assert result["data"]["assignee"] == "my-id", "Wrong assignee"
def test_04_update(self): task_id = self.test_state.get('id') if task_id is None: raise SkipTest("Skipping test for single task update, no recorded state") new_task_data = json.dumps({'command': 'something_else', 'assignee': 'my-id'}) request = TestRequest.blank('/tasks/%s' % task_id, content_type='application/json') request.method = 'PUT' request.body = new_task_data response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('id'), "ID entry went missing" assert result.has_key('data'), "data entry went missing" assert result['data']['command'] == 'something_else', "Wrong command" assert result['data']['assignee'] == 'my-id', "Wrong assignee"
def test_read_max_on_auth_fail(self): ''' KerberosAuthMiddleware's ``read_max_on_auth_fail`` should allow customizing reading of request bodies of unauthenticated requests. ''' body = b'body of unauthenticated request' for read_max in (0, 5, 100, _DEFAULT_READ_MAX, float('inf')): # When we drop Py2, we can use `with self.subTest(read_max=read_max):` here. app = TestApp(KerberosAuthMiddleware(index, read_max_on_auth_fail=read_max)) req = TestRequest.blank('/', method='POST', body=body) resp = app.do_request(req, status=401) if read_max < len(body): expect_read = 0 else: expect_read = min(read_max, len(body)) self.assertEqual(req.body_file.input.tell(), expect_read)
def test_02_create_job(self): job_data = json.dumps({ 'build_tasks': ['something'], 'job_id': 'test_job' }) request = TestRequest.blank('/jobs/test_job', content_type='application/json') request.method = 'PUT' request.body = job_data response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('config'), "config entry went missing" self.test_state['job_id'] = str(result['job_id']) assert result['config']['build_tasks'] == ['something'], "wrong config values" response = self.app.request('/jobs') result = json.loads(response.body) assert result.has_key('jobs'), "Jobs entry went missing" assert len(result['jobs']) == 1, "Invalid job count"
def wapp_get(wapp, resourcetype, id_, test_session_id, URL=None): """ """ ## bit specific exceotion here todo:: fix this whole wappget approach if URL is None: headerd = get_cookie_hdr(test_session_id) URL = get_url(resourcetype, id_=id_, method="GET") else: headerd = get_cookie_hdr(test_session_id) ### req = TestRequest.blank(URL, method="GET", headers=headerd) for k, v in wapp.extra_environ.items(): req.environ.setdefault(k, v) reqcp = req.copy() resp = wapp.do_request(req, status="*", expect_errors=True) capture_conversation(reqcp, resp) ### return resp
def wapp_put(wapp, resourcetype, data, test_session_id, id_=None): headerd = get_cookie_hdr(test_session_id) URL = get_url(resourcetype, method="PUT", id_=id_) ### data_as_json = json.dumps(data) req = TestRequest.blank(URL, method="PUT", body=data_as_json, headers=headerd) req.content_type = "application/json; charset=utf-8" for k, v in wapp.extra_environ.items(): req.environ.setdefault(k, v) reqcp = req.copy() resp = wapp.do_request(req, status="*", expect_errors=True) capture_conversation(reqcp, resp) ### return resp
def test_04_update_job(self): job_id = self.test_state.get('job_id') if job_id is None: raise SkipTest("Skipping test for single job config update, no recorded state") new_job_config = json.dumps({'build_tasks': ['something_else'], 'job_id': 'test_job'}) request = TestRequest.blank('/jobs/%s' % job_id, content_type='application/json') request.method = 'PUT' request.body = new_job_config response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('config'), "config entry went missing" assert result['config']['build_tasks'] == ['something_else'], "Wrong command" response = self.app.request('/jobs') result = json.loads(response.body) assert result.has_key('jobs'), "Jobs entry went missing" assert len(result['jobs']) == 1, "Invalid job count"
def test_02_create_job(self): job_data = json.dumps({ 'build_tasks': ['something'], 'job_id': 'test_job' }) request = TestRequest.blank('/jobs/test_job', content_type='application/json') request.method = 'PUT' request.body = job_data response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('config'), "config entry went missing" self.test_state['job_id'] = str(result['job_id']) assert result['config']['build_tasks'] == ['something' ], "wrong config values" response = self.app.request('/jobs') result = json.loads(response.body) assert result.has_key('jobs'), "Jobs entry went missing" assert len(result['jobs']) == 1, "Invalid job count"
def test_04_update_artifact(self): request = TestRequest.blank( '/jobs/%s/builds/%s/artifacts/%s' % (self.test_state['job_id'], self.test_state['build_number'], self.test_state['artifact_id']), content_type='application/octet-stream') request.method = 'PUT' request.body = 'test_content_modified' response = self.app.do_request(request, 200, False) result = json.loads(response.body) assert result.has_key('job_id'), "ID entry went missing" assert result.has_key('build_number'), "build_number went missing" assert result.has_key('artifact_id'), "Artifact ID went missing" assert result['artifact_id'] == self.test_state[ 'artifact_id'], "Artifact ID mismatch" response = self.app.request( '/jobs/%s/builds/%s/artifacts/%s' % (self.test_state['job_id'], self.test_state['build_number'], self.test_state['artifact_id'])) assert response.body == 'test_content_modified', "Wrong data"
def request_response(request, testapp: TestApp) -> RequestResponse: req = request.param # type: Request test_request = TestRequest.blank(req.url, **req.kwargs) test_response = testapp.request(test_request, expect_errors=True) return RequestResponse(test_request, test_response, req.expected_status, None)
def test_10_delete_build(self): request = TestRequest.blank('/jobs/%s/builds/%s' % (self.test_state['job_id'], self.test_state['build_number'])) request.method = 'DELETE' _ = self.app.do_request(request, 204, False)
def test_06_set_workspace(self): request = TestRequest.blank('/jobs/%s/builds/%s/workspace' % (self.test_state['job_id'], self.test_state['build_number']), content_type='application/octet-stream') request.method = 'PUT' request.body = 'test_content' _ = self.app.do_request(request, 204, False)
def _build_request(): req = TestRequest.blank('/') req.headers = HEADERS return req