pillar/tests/test_api/test_search_queries_users.py
Tobias Johansson 6ae9a5ddeb Quick-Search: Added Quick-search in the topbar
Changed how and what we store in elastic to unify it with how we store
things in mongodb so we can have more generic javascript code
to render the data.

Elastic changes:
  Added:
  Node.project.url

  Altered to store id instead of url
  Node.picture

  Made Post searchable

./manage.py elastic reset_index
./manage.py elastic reindex

Thanks to Pablo and Sybren
2018-11-22 15:31:53 +01:00

286 lines
9.2 KiB
Python

import copy
from pillar.api.search import queries
from pillar.tests import AbstractPillarTest
SOURCE = {
"_source": {
"include": ["full_name", "objectID", "username"]
}
}
EMPTY_QUERY = {
"query": {
"bool": {
"must": [
{
"bool": {}
}
]
}
},
}
AGGREGATIONS = {
"aggs": {
"roles": {
"terms": {
"field": "roles"
}
}
}
}
PAGE_1 = {
"from": 0,
"size": 10
}
class TestSearchUsers(AbstractPillarTest):
def test_empty_query(self):
with self.app.app_context():
search = queries.create_user_search(query='', terms={}, page=0)
expected = {
**SOURCE,
**EMPTY_QUERY,
**AGGREGATIONS,
**PAGE_1
}
self.assertEquals(expected, search.to_dict())
def test_query(self):
with self.app.app_context():
search = queries.create_user_search(query='Jens', terms={}, page=0)
query = copy.deepcopy(EMPTY_QUERY)
query['query']['bool']['must'] = [
{
"bool": {
"should": [
{
"match": {
"username": "Jens"
}
},
{
"match": {
"full_name": "Jens"
}
},
{
"match": {
"email": {
"query": "Jens",
"boost": 1
}
}
},
{
"term": {
"username_exact": {
"value": "Jens",
"boost": 50
}
}
}
]
}
}
]
expected = {
**SOURCE,
**query,
**AGGREGATIONS,
**PAGE_1
}
self.assertEquals(expected, search.to_dict())
def test_email_query(self):
with self.app.app_context():
search = queries.create_user_search(query='mail@mail.com', terms={}, page=0)
query = copy.deepcopy(EMPTY_QUERY)
query['query']['bool']['must'] = [
{
"bool": {
"should": [
{
"term": {
"email_exact": {
"value": "mail@mail.com",
"boost": 50
}
}
},
{
"match": {
"username": "mail@mail.com"
}
},
{
"match": {
"full_name": "mail@mail.com"
}
},
{
"match": {
"email": {
"query": "mail@mail.com",
"boost": 25
}
}
},
{
"term": {
"username_exact": {
"value": "mail@mail.com",
"boost": 50
}
}
}
]
}
}
]
expected = {
**SOURCE,
**query,
**AGGREGATIONS,
**PAGE_1
}
self.assertEquals(expected, search.to_dict())
class TestSearchUsersAdmin(AbstractPillarTest):
def test_empty_query(self):
with self.app.app_context():
search = queries.create_user_admin_search(query='', terms={}, page=0)
expected = {
**EMPTY_QUERY,
**AGGREGATIONS,
**PAGE_1
}
self.assertEquals(expected, search.to_dict())
def test_query(self):
with self.app.app_context():
search = queries.create_user_admin_search(query='Jens', terms={}, page=0)
query = copy.deepcopy(EMPTY_QUERY)
query['query']['bool']['must'] = [
{
"bool": {
"should": [
{
"match": {
"username": "Jens"
}
},
{
"match": {
"full_name": "Jens"
}
},
{
"match": {
"email": {
"query": "Jens",
"boost": 1
}
}
},
{
"term": {
"username_exact": {
"value": "Jens",
"boost": 50
}
}
}
]
}
}
]
expected = {
**query,
**AGGREGATIONS,
**PAGE_1
}
self.assertEquals(expected, search.to_dict())
def test_terms(self):
with self.app.app_context():
search = queries.create_user_admin_search(query='', terms={'roles': 'Admin'}, page=0)
query = copy.deepcopy(EMPTY_QUERY)
query['query']['bool']['filter'] = [
{
"term": {
"roles": "Admin"
}
}
]
expected = {
**query,
**AGGREGATIONS,
**PAGE_1
}
self.assertEquals(expected, search.to_dict())
def test_object_id_query(self):
with self.app.app_context():
search = queries.create_user_admin_search(query='563aca02c379cf0005e8e17d', terms={}, page=0)
query = copy.deepcopy(EMPTY_QUERY)
query['query']['bool']['must'] = [
{
"bool": {
"should": [
{
"match": {
"username": "563aca02c379cf0005e8e17d"
}
},
{
"match": {
"full_name": "563aca02c379cf0005e8e17d"
}
},
{
"match": {
"email": {
"query": "563aca02c379cf0005e8e17d",
"boost": 1
}
}
},
{
"term": {
"username_exact": {
"value": "563aca02c379cf0005e8e17d",
"boost": 50
}
}
},
{
"term": {
"objectID": {
"value": "563aca02c379cf0005e8e17d",
"boost": 100
}
}
}
]
}
}
]
expected = {
**query,
**AGGREGATIONS,
**PAGE_1
}
self.assertEquals(expected, search.to_dict())