def test_search_respects_groups(self): other_group = Group(org=self.factory.org, name="Other Group") db.session.add(other_group) ds = self.factory.create_data_source(group=other_group) q1 = self.factory.create_query(description="Testing search", data_source=ds) q2 = self.factory.create_query(description="Testing searching") q3 = self.factory.create_query(description="Testing sea rch") queries = list(Query.search("Testing", [self.factory.default_group.id])) self.assertNotIn(q1, queries) self.assertIn(q2, queries) self.assertIn(q3, queries) queries = list( Query.search("Testing", [other_group.id, self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries) self.assertIn(q3, queries) queries = list(Query.search("Testing", [other_group.id])) self.assertIn(q1, queries) self.assertNotIn(q2, queries) self.assertNotIn(q3, queries)
def test_recent_for_user(self): q1 = self.factory.create_query() q2 = self.factory.create_query() db.session.flush() e = Event(org=self.factory.org, user=self.factory.user, action="edit", object_type="query", object_id=q1.id) db.session.add(e) recent = Query.recent([self.factory.default_group.id], user_id=self.factory.user.id) self.assertIn(q1, recent) self.assertNotIn(q2, recent) recent = Query.recent([self.factory.default_group.id], user_id=self.factory.user.id + 1) self.assertNotIn(q1, recent) self.assertNotIn(q2, recent)
def test_search_query_parser_emails(self): q1 = self.factory.create_query(name="*****@*****.**") q2 = self.factory.create_query(name="*****@*****.**") queries = list(Query.search('example', [self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries) queries = list(Query.search('com', [self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries) queries = list(Query.search('johndoe', [self.factory.default_group.id])) self.assertNotIn(q1, queries) self.assertIn(q2, queries)
def test_search_query_parser_hyphen(self): q1 = self.factory.create_query(name="Testing search") q2 = self.factory.create_query(name="Testing-search") queries = list(Query.search('testing search', [self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries)
def post(self): # 忽视mimetype类型,强制为Json类型 req = request.get_json(True) # 判断请求参数 require_fields(req, ('options', 'name', 'query_id')) #### 根据哪个query_id创建的alert query = Query.get_by_id_and_org(req['query_id'], self.current_org) ## 权限系统?????????????? require_access(query.groups, self.current_user, view_only) # query_rel 是 relationShip, 传对方的行对象,需要提供 # 但是不用提供外键对应的列 alert = Alert(name=req['name'], query_rel=query, user=self.current_user, rearm=req.get('rearm'), options=req['options']) session.add(alert) session.flush() session.commit() self.record_event({ 'action': 'create', 'timestamp': int(time.time()), 'object_id': alert.id, 'object_type': 'alert' }) ### RESETFUL 规范, 不能只是响应个200,就完事了, 最好把创建好的东西返回给前端 return serialize_alert(alert)
def test_all_tags(self): self.create_tagged_query(tags=['tag1']) self.create_tagged_query(tags=['tag1', 'tag2']) self.create_tagged_query(tags=['tag1', 'tag2', 'tag3']) self.assertEqual(list(Query.all_tags(self.factory.user)), [('tag1', 3), ('tag2', 2), ('tag3', 1)])
def test_updates_existing_queries(self): query1 = self.factory.create_query(query_text=self.query) query2 = self.factory.create_query(query_text=self.query) query3 = self.factory.create_query(query_text=self.query) query_result = QueryResult.store_result(self.data_source.org_id, self.data_source, self.query_hash, self.query, self.data, self.runtime, self.utcnow) Query.update_latest_result(query_result) self.assertEqual(query1.latest_query_data, query_result) self.assertEqual(query2.latest_query_data, query_result) self.assertEqual(query3.latest_query_data, query_result)
def test_doesnt_update_queries_with_different_hash(self): query1 = self.factory.create_query(query_text=self.query) query2 = self.factory.create_query(query_text=self.query) query3 = self.factory.create_query(query_text=self.query + "123") query_result = QueryResult.store_result(self.data_source.org_id, self.data_source, self.query_hash, self.query, self.data, self.runtime, self.utcnow) Query.update_latest_result(query_result) self.assertEqual(query1.latest_query_data, query_result) self.assertEqual(query2.latest_query_data, query_result) self.assertNotEqual(query3.latest_query_data, query_result)
def test_search_query_parser_negation(self): q1 = self.factory.create_query(name="Testing") q2 = self.factory.create_query(name="search") queries = list( Query.search("testing -search", [self.factory.default_group.id])) self.assertIn(q1, queries) self.assertNotIn(q2, queries)
def test_logs_create_method(self): q = Query(name='Query', description='', query_text='', user=self.factory.user, data_source=self.factory.data_source, org=self.factory.org) change = Change.last_change(q) self.assertIsNotNone(change) self.assertEqual(q.user, change.user)
def test_logs_create_method(self): q = Query.create(name='Query', description='', query='', user=self.factory.user, data_source=self.factory.data_source, org=self.factory.org) change = Change.last_change(q) self.assertIsNotNone(change) self.assertEqual(q.user, change.user)
def test_search_query_parser_parenthesis(self): q1 = self.factory.create_query(name="Testing search") q2 = self.factory.create_query(name="Testing searching") q3 = self.factory.create_query(name="Testing finding") queries = list(Query.search('(testing search) or finding', [self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries) self.assertIn(q3, queries)
def test_returns_only_users_queries(self): q = self.factory.create_query(user=self.factory.user) q2 = self.factory.create_query(user=self.factory.create_user()) queries = Query.by_user(self.factory.user) # not using self.assertIn/NotIn because otherwise this fails :O self.assertTrue(q in list(queries)) self.assertFalse(q2 in list(queries))
def test_returns_only_queries_from_groups_the_user_is_member_in(self): q = self.factory.create_query() q2 = self.factory.create_query(data_source=self.factory.create_data_source(group=self.factory.create_group())) queries = Query.by_user(self.factory.user) # not using self.assertIn/NotIn because otherwise this fails :O self.assertTrue(q in queries) self.assertFalse(q2 in queries)
def test_search_finds_in_name(self): q1 = self.factory.create_query(name="Testing seåřċħ") q2 = self.factory.create_query(name="Testing seåřċħing") q3 = self.factory.create_query(name="Testing seå řċħ") queries = list(Query.search("seåřċħ", [self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries) self.assertNotIn(q3, queries)
def create_object(factory): obj = Query(name='Query', description='', query_text='SELECT 1', user=factory.user, data_source=factory.data_source, org=factory.org) return obj
def obj(self): obj = Query(name='Query', description='', query_text='SELECT 1', user=self.factory.user, data_source=self.factory.data_source, org=self.factory.org) return obj
def test_all_tags(self): self.create_tagged_query(tags=["tag1"]) self.create_tagged_query(tags=["tag1", "tag2"]) self.create_tagged_query(tags=["tag1", "tag2", "tag3"]) self.assertEqual( list(Query.all_tags(self.factory.user)), [("tag1", 3), ("tag2", 2), ("tag3", 1)], )
def test_returns_drafts_if_asked_to(self): q = self.factory.create_query(is_draft=True) q2 = self.factory.create_query(is_draft=False) queries = Query.by_user(self.factory.user, True) # not using self.assertIn/NotIn because otherwise this fails :O self.assertTrue(q in queries) self.assertFalse(q2 in queries)
def test_search_finds_in_name(self): q1 = self.factory.create_query(name=u"Testing seåřċħ") q2 = self.factory.create_query(name=u"Testing seåřċħing") q3 = self.factory.create_query(name=u"Testing seå řċħ") queries = list(Query.search(u"seåřċħ", [self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries) self.assertNotIn(q3, queries)
def test_returns_drafts_by_the_user(self): q = self.factory.create_query(is_draft=True) q2 = self.factory.create_query(is_draft=True, user=self.factory.create_user()) queries = Query.by_user(self.factory.user) # not using self.assertIn/NotIn because otherwise this fails :O self.assertTrue(q in queries) self.assertFalse(q2 in queries)
def test_all_tags(self): self.create_tagged_query(tags=['tag1']) self.create_tagged_query(tags=['tag1', 'tag2']) self.create_tagged_query(tags=['tag1', 'tag2', 'tag3']) self.assertEqual( list(Query.all_tags(self.factory.user)), [('tag1', 3), ('tag2', 2), ('tag3', 1)] )
def test_search_by_id_returns_query(self): q1 = self.factory.create_query(description="Testing search") q2 = self.factory.create_query(description="Testing searching") q3 = self.factory.create_query(description="Testing sea rch") db.session.flush() queries = Query.search(str(q3.id), [self.factory.default_group.id]) self.assertIn(q3, queries) self.assertNotIn(q1, queries) self.assertNotIn(q2, queries)
def test_search_finds_in_multi_byte_name_and_description(self): q1 = self.factory.create_query(name="日本語の名前テスト") q2 = self.factory.create_query(description=u"日本語の説明文テスト") q3 = self.factory.create_query(description=u"Testing search") queries = Query.search(u"テスト", [self.factory.default_group.id], multi_byte_search=True) self.assertIn(q1, queries) self.assertIn(q2, queries) self.assertNotIn(q3, queries)
def test_search_finds_in_description(self): q1 = self.factory.create_query(description=u"Testing seåřċħ") q2 = self.factory.create_query(description=u"Testing seåřċħing") q3 = self.factory.create_query(description=u"Testing seå řċħ") queries = Query.search(u"seåřċħ", [self.factory.default_group.id]) self.assertIn(q1, queries) self.assertIn(q2, queries) self.assertNotIn(q3, queries)
def create_object(factory): obj = Query( name="Query", description="", query_text="SELECT 1", user=factory.user, data_source=factory.data_source, org=factory.org, ) return obj
def test_returns_each_query_only_once(self): other_group = self.factory.create_group() second_group = self.factory.create_group() ds = self.factory.create_data_source(group=other_group) ds.add_group(second_group, False) q1 = self.factory.create_query(description="Testing search", data_source=ds) db.session.flush() queries = list(Query.search("Testing", [self.factory.default_group.id, other_group.id, second_group.id])) self.assertEqual(1, len(queries))
def obj(self): obj = Query( name="Query", description="", query_text="SELECT 1", user=self.factory.user, data_source=self.factory.data_source, org=self.factory.org, ) return obj
def test_respects_groups(self): q1 = self.factory.create_query() ds = self.factory.create_data_source(group=self.factory.create_group()) q2 = self.factory.create_query(data_source=ds) db.session.flush() Event(org=self.factory.org, user=self.factory.user, action="edit", object_type="query", object_id=q1.id) Event(org=self.factory.org, user=self.factory.user, action="edit", object_type="query", object_id=q2.id) recent = Query.recent([self.factory.default_group.id]) self.assertIn(q1, recent) self.assertNotIn(q2, recent)
def test_search_respects_groups(self): other_group = Group(org=self.factory.org, name="Other Group") db.session.add(other_group) ds = self.factory.create_data_source(group=other_group) q1 = self.factory.create_query(description="Testing search", data_source=ds) q2 = self.factory.create_query(description="Testing searching") q3 = self.factory.create_query(description="Testing sea rch") queries = list(Query.search("Testing", [self.factory.default_group.id])) self.assertNotIn(q1, queries) self.assertIn(q2, queries) self.assertIn(q3, queries) queries = list(Query.search("Testing", [other_group.id, self.factory.default_group.id])) self.assertIn(q1, queries) self.assertIn(q2, queries) self.assertIn(q3, queries) queries = list(Query.search("Testing", [other_group.id])) self.assertIn(q1, queries) self.assertNotIn(q2, queries) self.assertNotIn(q3, queries)
def test_recent_excludes_drafts(self): q1 = self.factory.create_query() q2 = self.factory.create_query(is_draft=True) db.session.add_all([ Event(org=self.factory.org, user=self.factory.user, action="edit", object_type="query", object_id=q1.id), Event(org=self.factory.org, user=self.factory.user, action="edit", object_type="query", object_id=q2.id) ]) recent = Query.recent([self.factory.default_group.id]) self.assertIn(q1, recent) self.assertNotIn(q2, recent)
def test_search_is_case_insensitive(self): q = self.factory.create_query(name="Testing search") self.assertIn(q, Query.search('testing', [self.factory.default_group.id]))
def test_search_by_number(self): q = self.factory.create_query(description="Testing search 12345") db.session.flush() queries = Query.search('12345', [self.factory.default_group.id]) self.assertIn(q, queries)