def _get_assessments_json(self, obj, assessments): """Get json representation for all assessments in result set.""" if not assessments: return [] with benchmark("get documents of related assessments"): evidence_json_map = self._get_evidences(assessments) with benchmark("get snapshots of related assessments"): snapshot_json_map = self._get_snapshots(obj, assessments) with benchmark("generate related_assessment json"): assessments_json = [] for assessment in assessments: single_json = assessment.log_json_base() single_json["audit"] = assessment.audit.log_json_base() single_json["verified"] = assessment.verified single_json["custom_attribute_values"] = [ cav.log_json_base() for cav in assessment.custom_attribute_values ] single_json["custom_attribute_definitions"] = [ cad.log_json_base() for cad in assessment.custom_attribute_definitions ] single_json["snapshots"] = snapshot_json_map[assessment.id] single_json["evidence"] = evidence_json_map[assessment.id] single_json["audit"]["viewLink"] = utils.view_url_for( assessment.audit) single_json["viewLink"] = utils.view_url_for(assessment) assessments_json.append(single_json) return assessments_json
def test_handle_one_comment(self, send_email_mock): """Test handling of mapped comment.""" with factories.single_commit(): person = factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product1") comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", ) comment.modified_by_id = person.id comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42) url = urljoin(get_url_root(), utils.view_url_for(obj)) people_mentions.handle_comment_mapped(obj, [comment]) expected_title = (u"[email protected] mentioned you " u"on a comment within Product1") expected_body = ( u"[email protected] mentioned you on a comment within Product1 " u"at 01/09/2018 23:31:42 PST:\n" u"One <a href=\"mailto:[email protected]\"></a>\n" ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_comment_imported(self, send_email_mock): """Test sending mention email after import an object with comments.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product4") obj_slug = obj.slug url = urljoin(get_url_root(), utils.view_url_for(obj)) first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict([("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment)]) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) expected_title = (u"[email protected] mentioned you on " u"a comment within Product4") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_comment_imported(self, send_email_mock): """Test sending mention email after import an object with comments.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product4") obj_slug = obj.slug url = urljoin(get_url_root(), utils.view_url_for(obj)) first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict( [ ("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment) ] ) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) expected_title = (u"[email protected] mentioned you on " u"a comment within Product4") body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product4 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_review_posted(self, model, send_email_mock): """Test mentions in request review comment {}.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.get_model_factory(model.__name__)() url = urljoin(get_url_root(), utils.view_url_for(obj)) with freeze_time("2018-01-10 07:31:42"): resp, _ = generate_review_object( obj, email_message= u"Test <a href=\"mailto:[email protected]\"></a>", ) self.assertEqual(201, resp.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within {title}").format(title=obj.title) expected_body = ( u"[email protected] mentioned you on a comment within {title} " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Review requested from</p>" u"<p>[email protected]</p>" u"<p>with a comment:" u" Test <a href=\"mailto:[email protected]\"></a></p>\n" ).format(title=obj.title) body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_handle_one_comment(self, send_email_mock): """Test handling of mapped comment.""" with factories.single_commit(): person = factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product1") comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", ) comment.modified_by_id = person.id comment.created_at = datetime.datetime(2018, 1, 10, 7, 31, 42) url = urljoin(get_url_root(), utils.view_url_for(obj)) people_mentions.handle_comment_mapped(obj, [comment]) expected_title = (u"[email protected] mentioned you " u"on a comment within Product1") expected_body = ( u"[email protected] mentioned you on a comment within Product1 " u"at 01/09/2018 23:31:42 PST:\n" u"One <a href=\"mailto:[email protected]\"></a>\n") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def publish_base_properties(obj): ret = {} self_url = url_for(obj) if self_url: ret['selfLink'] = self_url view_url = view_url_for(obj) if view_url: ret['viewLink'] = view_url return ret
def publish_base_properties(obj): """Return a dict with selfLink and viewLink for obj.""" ret = {} self_url = url_for(obj) if self_url: ret['selfLink'] = self_url view_url = view_url_for(obj) if view_url: ret['viewLink'] = view_url return ret
def _stub(obj): """Returns stub of object""" data = { "viewLink": utils.view_url_for(obj), "selfLink": utils.url_for(obj), "id": obj.id, "type": obj.type, } if hasattr(obj, "title"): data.update({"title": obj.title}) return data
def get(self, id): obj = self.get_object(id) if obj is None: return self.not_found_response() if 'Accept' in self.request.headers and\ 'text/html' not in self.request.headers['Accept']: return current_app.make_response(( 'text/html', 406, [('Content-Type', 'text/plain')])) if not permissions.is_allowed_read(self.model.__name__, obj.context_id): raise Forbidden() return redirect(view_url_for(obj))
def get(self, id): obj = self.get_object(id) if obj is None: return self.not_found_response() if 'Accept' in self.request.headers and\ 'text/html' not in self.request.headers['Accept']: return current_app.make_response( ('text/html', 406, [('Content-Type', 'text/plain')])) if not permissions.is_allowed_read(self.model.__name__, obj.id, obj.context_id): raise Forbidden() return redirect(view_url_for(obj))
def test_several_mentions_imported(self, send_email_mock): """Test sending mention email after import an object with comments with mentions of different persons.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product5") obj_slug = obj.slug first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" \ u"<a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict([("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment)]) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) obj = all_models.Product.query.filter_by(title="Product5").one() url = urljoin(get_url_root(), utils.view_url_for(obj)) expected_title = (u"[email protected] mentioned you on " u"a comment within Product5") first_body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) first_call = mock.call(u"*****@*****.**", expected_title, first_body) second_body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [(u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n")], "url": url, }) second_call = mock.call(u"*****@*****.**", expected_title, second_body) send_email_mock.assert_has_calls([second_call, first_call])
def test_several_mentions_imported(self, send_email_mock): """Test sending mention email after import an object with comments with mentions of different persons.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product5") obj_slug = obj.slug first_comment = u"One <a href=\"mailto:[email protected]\"></a>" second_comment = u"Two <a href=\"mailto:[email protected]\"></a>" \ u"<a href=\"mailto:[email protected]\"></a>" import_data = OrderedDict( [ ("object_type", "Product"), ("Code*", obj_slug), ("comments", first_comment + u";;" + second_comment) ] ) with freeze_time("2018-01-10 07:31:42"): response = self.import_data(import_data) self._check_csv_response(response, {}) obj = all_models.Product.query.filter_by(title="Product5").one() url = urljoin(get_url_root(), utils.view_url_for(obj)) expected_title = (u"[email protected] mentioned you on " u"a comment within Product5") first_body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [ (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + first_comment + u"\n"), (u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n"), ], "url": url, }) first_call = mock.call(u"*****@*****.**", expected_title, first_body) second_body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [( u"[email protected] mentioned you on a comment within Product5 " u"at 01/09/2018 23:31:42 PST:\n" + second_comment + u"\n" )], "url": url, }) second_call = mock.call(u"*****@*****.**", expected_title, second_body) send_email_mock.assert_has_calls([second_call, first_call])
def test_relation_comment_posted(self, send_email_mock): """Test sending mention email after posting a relationship to comment.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product3") obj_id = obj.id comment = factories.CommentFactory( description= u"One <a href=\"mailto:[email protected]\"></a>", ) comment_id = comment.id comment.created_at = datetime.datetime(2018, 07, 10, 8, 31, 42) comment.modified_by_id = author_person.id url = urljoin(get_url_root(), utils.view_url_for(obj)) author_person = all_models.Person.query.filter_by( email="*****@*****.**").one() api = api_helper.Api() api.set_user(author_person) response = api.post( all_models.Relationship, { "relationship": { "source": { "id": obj_id, "type": obj.type, }, "destination": { "id": comment_id, "type": comment.type }, "context": None }, }) self.assertEqual(response.status_code, 201) expected_title = (u"[email protected] mentioned you on " u"a comment within Product3") expected_body = ( u"[email protected] mentioned you on a comment within Product3 " u"at 07/10/2018 01:31:42 PDT:\n" u"One <a href=\"mailto:[email protected]\"></a>\n") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_relation_comment_posted(self, send_email_mock): """Test sending mention email after posting a relationship to comment.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") obj = factories.ProductFactory(title="Product3") obj_id = obj.id comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", ) comment_id = comment.id comment.created_at = datetime.datetime(2018, 07, 10, 8, 31, 42) comment.modified_by_id = author_person.id url = urljoin(get_url_root(), utils.view_url_for(obj)) author_person = all_models.Person.query.filter_by( email="*****@*****.**" ).one() api = api_helper.Api() api.set_user(author_person) response = api.post(all_models.Relationship, { "relationship": {"source": { "id": obj_id, "type": obj.type, }, "destination": { "id": comment_id, "type": comment.type }, "context": None}, }) self.assertEqual(response.status_code, 201) expected_title = (u"[email protected] mentioned you on " u"a comment within Product3") expected_body = ( u"[email protected] mentioned you on a comment within Product3 " u"at 07/10/2018 01:31:42 PDT:\n" u"One <a href=\"mailto:[email protected]\"></a>\n" ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def handle_comment_mapped(obj, comments): """Send mentions in the comments in the bg task. Args: obj: object for which comments were created, comments: A list of comment objects. """ comments_data = _fetch_comments_data(comments) models.background_task.create_task( name="send_mentions_bg", url=flask.url_for("send_mentions_bg"), parameters={ "comments_data": comments_data, "object_name": obj.title, "href": urljoin(get_url_root(), utils.view_url_for(obj)), }, queued_callback=send_mentions_bg, )
def test_proposal_put(self, send_email_mock): """Test sending mention email after a change of proposal.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") risk = factories.RiskFactory(title="Risk2") proposal = factories.ProposalFactory( instance=risk, content={"fields": { "title": "Risk3" }}, agenda=u'some agenda', proposed_by=author_person, ) url = urljoin(get_url_root(), utils.view_url_for(risk)) proposal_id = proposal.id proposal = all_models.Proposal.query.get(proposal_id) api = api_helper.Api() with freeze_time("2018-01-10 07:31:42"): data = { "status": proposal.STATES.APPLIED, "apply_reason": u'<a href=\"mailto:[email protected]\"></a>', } response = api.put(proposal, {"proposal": data}) self.assertEqual(200, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk3") expected_body = ( u"[email protected] mentioned you on a comment within Risk3 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal created by [email protected] has been applied" u" with a comment: " u'<a href="mailto:[email protected]"></a></p>\n') body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_proposal_posted(self, send_email_mock): """Test sending mention email after posting a proposal.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.RiskFactory(title="Risk1") obj_id = obj.id url = urljoin(get_url_root(), utils.view_url_for(obj)) obj = all_models.Risk.query.get(obj_id) obj_content = obj.log_json() obj_content["title"] = "Risk2" with freeze_time("2018-01-10 07:31:42"): api = api_helper.Api() response = api.post( all_models.Proposal, { "proposal": { "instance": { "id": obj_id, "type": obj.type, }, "full_instance_content": obj_content, "agenda": u'<a href=\"mailto:[email protected]\"></a', "context": None, } }) self.assertEqual(201, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk1") expected_body = ( u"[email protected] mentioned you on a comment within Risk1 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal has been created with comment: " u"<a href=\"mailto:[email protected]\"></a></p>\n") body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_proposal_put(self, send_email_mock): """Test sending mention email after a change of proposal.""" with factories.single_commit(): author_person = factories.PersonFactory(email="*****@*****.**") factories.PersonFactory(email="*****@*****.**") risk = factories.RiskFactory(title="Risk2") proposal = factories.ProposalFactory( instance=risk, content={"fields": {"title": "Risk3"}}, agenda=u'some agenda', proposed_by=author_person, ) url = urljoin(get_url_root(), utils.view_url_for(risk)) proposal_id = proposal.id proposal = all_models.Proposal.query.get(proposal_id) api = api_helper.Api() with freeze_time("2018-01-10 07:31:42"): data = { "status": proposal.STATES.APPLIED, "apply_reason": u'<a href=\"mailto:[email protected]\"></a>', } response = api.put(proposal, {"proposal": data}) self.assertEqual(200, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk3") expected_body = ( u"[email protected] mentioned you on a comment within Risk3 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal created by [email protected] has been applied" u" with a comment: " u'<a href="mailto:[email protected]"></a></p>\n' ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_proposal_posted(self, send_email_mock): """Test sending mention email after posting a proposal.""" with factories.single_commit(): factories.PersonFactory(email="*****@*****.**") obj = factories.RiskFactory(title="Risk1") obj_id = obj.id url = urljoin(get_url_root(), utils.view_url_for(obj)) obj = all_models.Risk.query.get(obj_id) obj_content = obj.log_json() obj_content["title"] = "Risk2" with freeze_time("2018-01-10 07:31:42"): api = api_helper.Api() response = api.post(all_models.Proposal, { "proposal": { "instance": { "id": obj_id, "type": obj.type, }, "full_instance_content": obj_content, "agenda": u'<a href=\"mailto:[email protected]\"></a', "context": None, } }) self.assertEqual(201, response.status_code) expected_title = (u"[email protected] mentioned you on " u"a comment within Risk1") expected_body = ( u"[email protected] mentioned you on a comment within Risk1 " u"at 01/09/2018 23:31:42 PST:\n" u"<p>Proposal has been created with comment: " u"<a href=\"mailto:[email protected]\"></a></p>\n" ) body = settings.EMAIL_MENTIONED_PERSON.render(person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def test_comment_people_mentioned(self, send_email_mock): """Test handling of mapped comment with mentioned people""" person = factories.PersonFactory(email="*****@*****.**") comment = factories.CommentFactory( description=u"One <a href=\"mailto:[email protected]\"></a>", modified_by_id=person.id, created_at=datetime.datetime(2018, 1, 10, 7, 31, 42)) assessment = factories.AssessmentFactory() response = self.api.put( assessment, { "actions": { "add_related": [{ "id": comment.id, "type": comment.__class__.__name__, }] } }) url = urljoin(get_url_root(), utils.view_url_for(assessment)) self.assert200(response) relationship = _get_relationship("Assessment", response.json["assessment"]["id"]) self.assertIsNotNone(relationship) expected_title = (u"[email protected] mentioned you " u"on a comment within {}".format(assessment.title)) expected_body = ( u"[email protected] mentioned you on a comment within {} " u"at 01/09/2018 23:31:42 PST:\n" u"One <a href=\"mailto:[email protected]\"></a>\n".format( assessment.title)) body = settings.EMAIL_MENTIONED_PERSON.render( person_mention={ "comments": [expected_body], "url": url, }) send_email_mock.assert_called_once_with(u"*****@*****.**", expected_title, body)
def handle_comment_mapped(obj, comments): """Send mentions in the comments in the bg task. Args: obj: object for which comments were created, comments: A list of comment objects. """ comments_data = _fetch_comments_data(comments) if obj.__class__.__name__ == "CycleTaskGroupObjectTask": url = calendar_utils.get_cycle_tasks_url_by_slug(obj.slug) else: url = urljoin(get_url_root(), utils.view_url_for(obj)) models.background_task.create_task( name="send_mentions_bg", url=flask.url_for("send_mentions_bg"), parameters={ "comments_data": comments_data, "object_name": obj.title, "href": url, }, queued_callback=send_mentions_bg, )
def get_ggrc_object_url(obj): """Builds and returns URL to GGRC object.""" return urlparse.urljoin( ggrc_utils.get_url_root(), ggrc_utils.view_url_for(obj) )
def _get_assessment_url(assessment): """Returns string URL for assessment view page.""" return urlparse.urljoin(utils.get_url_root(), utils.view_url_for(assessment))