def test_with_valid_token(self, mock_controller): """Client auth token has required public read scope.""" document = mocks.document() docs = { "results": [document], "metadata": { "start": 0, "end": 1, "size": 50, "total": 1 }, } r_data = {"results": docs, "query": APIQuery()} mock_controller.search.return_value = r_data, HTTPStatus.OK, {} token = helpers.generate_token("1234", "*****@*****.**", "foouser", scope=[auth.scopes.READ_PUBLIC]) response = self.client.get("/", headers={"Authorization": token}) self.assertEqual(response.status_code, HTTPStatus.OK) data = json.loads(response.data) res = jsonschema.RefResolver( "file://%s/" % os.path.abspath(os.path.dirname(self.SCHEMA_PATH)), None, ) self.assertIsNone( jsonschema.validate(data, self.schema, resolver=res), "Response content is valid per schema", ) for field in get_required_fields(): self.assertIn(field, data["results"][0])
def test_include_fields(self, mock_index): """Request with specific fields included.""" extra_fields = ['title', 'abstract', 'authors'] params = MultiDict({'include': extra_fields}) data, code, headers = api.search(params) self.assertEqual(code, status.HTTP_200_OK, "Returns 200 OK") self.assertIn("results", data, "Results are returned") self.assertIn("query", data, "Query object is returned") expected_fields = api_domain.get_required_fields() + extra_fields self.assertEqual(set(data["query"].include_fields), set(expected_fields), "Requested fields are included")
def test_no_params(self, mock_index): """Request with no parameters.""" params = MultiDict({}) data, code, headers = api.search(params) self.assertEqual(code, status.HTTP_200_OK, "Returns 200 OK") self.assertIn("results", data, "Results are returned") self.assertIn("query", data, "Query object is returned") expected_fields = api_domain.get_required_fields() \ + api_domain.get_default_extra_fields() self.assertEqual(set(data["query"].include_fields), set(expected_fields), "Default set of fields is included")
def test_query_param(self, mock_index): """Request with a query string. Tests conjuncts and quoted phrases.""" params = MultiDict({"query": 'au:copernicus AND ti:"dark matter"'}) data, code, headers = api.search(params) self.assertEqual(code, HTTPStatus.OK, "Returns 200 OK") self.assertIn("results", data, "Results are returned") self.assertIn("query", data, "Query object is returned") expected_fields = ( api_domain.get_required_fields() + api_domain.get_default_extra_fields() ) self.assertEqual( set(data["query"].include_fields), set(expected_fields), "Default set of fields is included", )