def _create(self, miss_checks, db_checks, **kwargs): """ :param miss_checks: [miss1, miss2] :param db_checks: [(table, exist, query, error)] """ if self.json_args is None: raises.BadRequest(message.no_body()) data = self.table_cls.from_dict(self.json_args) for miss in miss_checks: miss_data = data.__getattribute__(miss) if miss_data is None or miss_data == '': raises.BadRequest(message.missing(miss)) for k, v in kwargs.iteritems(): data.__setattr__(k, v) for table, exist, query, error in db_checks: check = yield self._eval_db_find_one(query(data), table) if (exist and not check) or (not exist and check): code, msg = error(data) raises.CodeTBD(code, msg) if self.table != 'results': data.creation_date = datetime.now() _id = yield self._eval_db(self.table, 'insert', data.format(), check_keys=False) if 'name' in self.json_args: resource = data.name else: resource = _id self.finish_request(self._create_response(resource))
class TestCaseCreate(TestCaseBase): @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_noBody(self): return None @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noProject(self): self.project = 'noProject' return self.req_d @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_emptyName(self): req_empty = tcm.TestcaseCreateRequest('') return req_empty @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_noneName(self): req_none = tcm.TestcaseCreateRequest(None) return req_none @executor.create(httplib.OK, '_assert_success') def test_success(self): return self.req_d def _assert_success(self, body): self.assert_create_body(body, self.req_d, self.project) @executor.create(httplib.FORBIDDEN, message.exist_base) def test_alreadyExist(self): self.create_d() return self.req_d
def _update(self, query, db_keys): if self.json_args is None: raises.BadRequest(message.no_body()) # check old data exist from_data = yield self._eval_db_find_one(query) if from_data is None: raises.NotFound(message.not_found(self.table, query)) data = self.table_cls.from_dict(from_data) # check new data exist equal, new_query = self._update_query(db_keys, data) if not equal: to_data = yield self._eval_db_find_one(new_query) if to_data is not None: raises.Forbidden(message.exist(self.table, new_query)) # we merge the whole document """ edit_request = self._update_requests(data) """ Updating the DB """ yield self._eval_db(self.table, 'update', query, edit_request, check_keys=False) edit_request['_id'] = str(data._id) self.finish_request(edit_request)
class TestProjectCreate(TestProjectBase): @executor.create(httplib.BAD_REQUEST, message.not_login()) def test_notlogin(self): return self.req_d @executor.mock_valid_lfid() @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_withoutBody(self): return None @executor.mock_valid_lfid() @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_emptyName(self): return project_models.ProjectCreateRequest('') @executor.mock_valid_lfid() @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_noneName(self): return project_models.ProjectCreateRequest(None) @executor.mock_valid_lfid() @executor.create(httplib.OK, 'assert_create_body') def test_success(self): return self.req_d @executor.mock_valid_lfid() @executor.create(httplib.FORBIDDEN, message.exist_base) def test_alreadyExist(self): self.create_d() return self.req_d
class TestProjectUpdate(TestProjectBase): def setUp(self): super(TestProjectUpdate, self).setUp() _, d_body = self.create_d() _, get_res = self.get(self.req_d.name) self.index_d = get_res._id self.create_e() @executor.update(httplib.BAD_REQUEST, message.no_body()) def test_withoutBody(self): return None, 'noBody' @executor.update(httplib.NOT_FOUND, message.not_found_base) def test_notFound(self): return self.req_e, 'notFound' @executor.update(httplib.FORBIDDEN, message.exist_base) def test_newNameExist(self): return self.req_e, self.req_d.name @executor.update(httplib.FORBIDDEN, message.no_update()) def test_noUpdate(self): return self.req_d, self.req_d.name @executor.update(httplib.OK, '_assert_update') def test_success(self): req = project_models.ProjectUpdateRequest('newName', 'new description') return req, self.req_d.name def _assert_update(self, req, body): self.assertEqual(self.index_d, body._id) self.assert_body(body, req) _, new_body = self.get(req.name) self.assertEqual(self.index_d, new_body._id) self.assert_body(new_body, req)
class TestResultCreate(TestResultBase): @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_nobody(self): return None @executor.create(httplib.BAD_REQUEST, message.missing('pod_name')) def test_podNotProvided(self): req = self.req_d req.pod_name = None return req @executor.create(httplib.BAD_REQUEST, message.missing('project_name')) def test_projectNotProvided(self): req = self.req_d req.project_name = None return req @executor.create(httplib.BAD_REQUEST, message.missing('case_name')) def test_testcaseNotProvided(self): req = self.req_d req.case_name = None return req @executor.create(httplib.BAD_REQUEST, message.invalid_value('criteria', ['PASS', 'FAIL'])) def test_invalid_criteria(self): req = self.req_d req.criteria = 'invalid' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noPod(self): req = self.req_d req.pod_name = 'notExistPod' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noProject(self): req = self.req_d req.project_name = 'notExistProject' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noTestcase(self): req = self.req_d req.case_name = 'notExistTestcase' return req @executor.create(httplib.OK, 'assert_href') def test_success(self): return self.req_d @executor.create(httplib.OK, 'assert_href') def test_key_with_doc(self): req = copy.deepcopy(self.req_d) req.details = {'1.name': 'dot_name'} return req
class TestProjectUpdate(TestProjectBase): @executor.mock_valid_lfid() def setUp(self): super(TestProjectUpdate, self).setUp() _, d_body = self.create_d() _, get_res = self.get(self.req_d.name) self.index_d = get_res._id self.create_e() @executor.update(httplib.BAD_REQUEST, message.not_login()) def test_notlogin(self): req = project_models.ProjectUpdateRequest('apex', 'apex test') return req, self.req_d.name @executor.update(httplib.BAD_REQUEST, message.no_body()) def test_withoutBody(self): return None, 'noBody' @executor.mock_valid_lfid() @executor.update(httplib.NOT_FOUND, message.not_found_base) def test_notFound(self): return self.req_e, 'notFound' @executor.mock_valid_lfid() @executor.update(httplib.FORBIDDEN, message.exist_base) def test_newNameExist(self): return self.req_e, self.req_d.name @executor.mock_valid_lfid() @executor.update(httplib.FORBIDDEN, message.no_update()) def test_noUpdate(self): return self.req_d, self.req_d.name @executor.mock_valid_lfid() @executor.update(httplib.UNAUTHORIZED, message.tied_with_resource()) def test_updateNotAllowed(self): self.create_help('/api/v1/projects/%s/cases', self.testcase_d, self.req_d.name) req = project_models.ProjectUpdateRequest('apex', 'apex test') return req, self.req_d.name @executor.mock_valid_lfid() @executor.update(httplib.OK, '_assert_update') def test_success(self): req = project_models.ProjectUpdateRequest('apex', 'apex test') return req, self.req_d.name def _assert_update(self, req, body): self.assertEqual(self.index_d, body._id) self.assert_body(body, req) _, new_body = self.get(req.name) self.assertEqual(self.index_d, new_body._id) self.assert_body(new_body, req)
class TestCaseUpdate(TestCaseBase): def setUp(self): super(TestCaseUpdate, self).setUp() self.create_d() @executor.update(httplib.BAD_REQUEST, message.no_body()) def test_noBody(self): return None, 'noBody' @executor.update(httplib.NOT_FOUND, message.not_found_base) def test_notFound(self): update = tcm.TestcaseUpdateRequest(description='update description') return update, 'notFound' @executor.update(httplib.FORBIDDEN, message.exist_base) def test_newNameExist(self): self.create_e() return self.update_req, self.req_d.name @executor.update(httplib.FORBIDDEN, message.no_permission()) def test_unauthorized(self): update_req_e = tcm.TestcaseUpdateRequest(project_name="newProject", **self.req_e.format()) return update_req_e, self.req_d.name @executor.update(httplib.FORBIDDEN, message.no_update()) def test_noUpdate(self): update = tcm.TestcaseUpdateRequest(project_name=self.project, **self.req_d.format()) return update, self.req_d.name @executor.update(httplib.OK, '_update_success') def test_success(self): return self.update_req, self.req_d.name @executor.update(httplib.OK, '_update_success') def test_with_dollar(self): self.update_req.description = {'2. change': 'dollar change'} return self.update_req, self.req_d.name def _update_success(self, request, body): self.assert_update_body(body, request) _, new_body = self.get(request.name) self.assert_update_body(new_body, request)
class TestPodCreate(TestPodBase): @executor.create(httplib.BAD_REQUEST, message.not_login()) def test_notlogin(self): return self.req_d @executor.mock_invalid_lfid() @executor.create(httplib.BAD_REQUEST, message.not_lfid()) def test_invalidLfid(self): return self.req_d @executor.mock_valid_lfid() @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_withoutBody(self): return None @executor.mock_valid_lfid() @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_emptyName(self): return pod_models.PodCreateRequest('') @executor.mock_valid_lfid() @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_noneName(self): return pod_models.PodCreateRequest(None) @executor.mock_valid_lfid() @executor.create(httplib.OK, 'assert_create_body') def test_success(self): return self.req_d @executor.mock_valid_lfid() @executor.create(httplib.FORBIDDEN, message.exist_base) def test_alreadyExist(self): fake_pymongo.pods.insert(self.pod_d.format()) return self.req_d @executor.mock_valid_lfid() @executor.create(httplib.FORBIDDEN, message.exist_base) def test_alreadyExistCaseInsensitive(self): fake_pymongo.pods.insert(self.pod_d.format()) self.req_d.name = self.req_d.name.upper() return self.req_d
class DeployResultCreate(DeployResultBase): @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_nobody(self): return None @executor.create(httplib.BAD_REQUEST, message.missing('pod_name')) def test_podNotProvided(self): req = self.req_d req.pod_name = None return req @executor.create(httplib.BAD_REQUEST, message.missing('installer')) def test_installerNotProvided(self): req = self.req_d req.installer = None return req @executor.create(httplib.BAD_REQUEST, message.missing('scenario')) def test_testcaseNotProvided(self): req = self.req_d req.scenario = None return req @executor.create(httplib.BAD_REQUEST, message.invalid_value('criteria', ['PASS', 'FAIL'])) def test_invalid_criteria(self): req = self.req_d req.criteria = 'invalid' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noPod(self): req = self.req_d req.pod_name = 'notExistPod' return req @executor.create(httplib.OK, 'assert_href') def test_success(self): return self.req_d
class TestCaseUpdate(TestCaseBase): def setUp(self): super(TestCaseUpdate, self).setUp() self.create_d() @executor.update(httplib.BAD_REQUEST, message.no_body()) def test_noBody(self): return None, 'noBody' @executor.update(httplib.NOT_FOUND, message.not_found_base) def test_notFound(self): return self.update_e, 'notFound' @executor.update(httplib.FORBIDDEN, message.exist_base) def test_newNameExist(self): self.create_e() return self.update_e, self.req_d.name @executor.update(httplib.FORBIDDEN, message.no_update()) def test_noUpdate(self): return self.update_d, self.req_d.name @executor.update(httplib.OK, '_update_success') def test_success(self): return self.update_e, self.req_d.name @executor.update(httplib.OK, '_update_success') def test_with_dollar(self): update = copy.deepcopy(self.update_d) update.description = {'2. change': 'dollar change'} return update, self.req_d.name def _update_success(self, request, body): self.assert_update_body(self.req_d, body, request) _, new_body = self.get(request.name) self.assert_update_body(self.req_d, new_body, request)
def test_nobody(self): (code, body) = self.create(None) self.assertEqual(code, httplib.BAD_REQUEST) self.assertIn(message.no_body(), body)
class TestResultCreate(TestResultBase): @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_nobody(self): return None @executor.create(httplib.BAD_REQUEST, message.missing('pod_name')) def test_podNotProvided(self): req = self.req_d req.pod_name = None return req @executor.create(httplib.BAD_REQUEST, message.missing('project_name')) def test_projectNotProvided(self): req = self.req_d req.project_name = None return req @executor.create(httplib.BAD_REQUEST, message.missing('case_name')) def test_testcaseNotProvided(self): req = self.req_d req.case_name = None return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noPod(self): req = self.req_d req.pod_name = 'notExistPod' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noProject(self): req = self.req_d req.project_name = 'notExistProject' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noTestcase(self): req = self.req_d req.case_name = 'notExistTestcase' return req @executor.create(httplib.OK, 'assert_href') def test_success(self): return self.req_d @executor.create(httplib.OK, 'assert_href') def test_key_with_doc(self): req = copy.deepcopy(self.req_d) req.details = {'1.name': 'dot_name'} return req @executor.create(httplib.OK, '_assert_no_ti') def test_no_ti(self): req = result_models.ResultCreateRequest(pod_name=self.pod, project_name=self.project, case_name=self.case, installer=self.installer, version=self.version, start_date=self.start_date, stop_date=self.stop_date, details=self.details.format(), build_tag=self.build_tag, scenario=self.scenario, criteria=self.criteria) self.actual_req = req return req def _assert_no_ti(self, body): _id = body.href.split('/')[-1] code, body = self.get(_id) self.assert_res(body, self.actual_req)
def wrap(self, *args, **kwargs): if self.json_args is None: raises.BadRequest(message.no_body()) ret = yield gen.coroutine(xstep)(self, *args, **kwargs) raise gen.Return(ret)
class TestResultCreate(TestResultBase): @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_nobody(self): return None @executor.create(httplib.BAD_REQUEST, message.missing('pod_name')) def test_podNotProvided(self): req = self.req_d req.pod_name = None return req @executor.create(httplib.BAD_REQUEST, message.missing('project_name')) def test_projectNotProvided(self): req = self.req_d req.project_name = None return req @executor.create(httplib.BAD_REQUEST, message.missing('case_name')) def test_testcaseNotProvided(self): req = self.req_d req.case_name = None return req @executor.create(httplib.BAD_REQUEST, message.invalid_value('criteria', ['PASS', 'FAIL'])) def test_invalid_criteria(self): req = self.req_d req.criteria = 'invalid' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noPod(self): req = self.req_d req.pod_name = 'notExistPod' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noProject(self): req = self.req_d req.project_name = 'notExistProject' return req @executor.create(httplib.FORBIDDEN, message.not_found_base) def test_noTestcase(self): req = self.req_d req.case_name = 'notExistTestcase' return req @executor.create(httplib.OK, 'assert_href') def test_success(self): return self.req_d @executor.create(httplib.OK, 'assert_href') def test_key_with_doc(self): req = copy.deepcopy(self.req_d) req.details = {'1.name': 'dot_name'} return req @executor.create(httplib.OK, '_assert_no_ti') def test_no_ti(self): req = copy.deepcopy(self.req_d) req.trust_indicator = rm.TI(0) self.actual_req = req return req def _assert_no_ti(self, body): _id = body.href.split('/')[-1] code, body = self.get(_id) self.assert_res(body, self.actual_req)