Skip to content

Commit

Permalink
added check_modules as per #58
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnaglodha committed Sep 2, 2024
1 parent ffb2115 commit 08f7d01
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/geoserverx/_async/gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ def response_recognise(self, r) -> GSResponse:
elif r == 409:
resp = GSResponseEnum._409.value
return GSResponse.model_validate(resp)

# check if certain module/plugin exists in geoserver
async def check_modules(self,name)-> Union[bool, GSResponse]:
Client = self.http_client
response = await Client.get("about/status.json")
if response.status_code != 200:
return False
modules = [item["name"].lower() for item in response.json()['statuss']['status']]
return name.lower() in modules
# Get all workspaces
async def get_all_workspaces(self) -> Union[WorkspacesModel, GSResponse]:
Client = self.http_client
Expand Down Expand Up @@ -312,6 +319,9 @@ async def get_all_layer_groups(self,workspace: Optional[str] = None) -> Union[La
# Get all geofence rules
async def get_all_geofence_rules(self) -> Union[RulesResponse, GSResponse]:
Client = self.http_client
# Check if geofence plugin exists
if not self.check_modules('geofence'):
return GSResponse(code=404, response="Plugin not found")
responses = await Client.get("geofence/rules/",headers={'Accept': "application/json"})
if responses.status_code == 200:
return RulesResponse.model_validate(responses.json())
Expand All @@ -322,6 +332,9 @@ async def get_all_geofence_rules(self) -> Union[RulesResponse, GSResponse]:
# Get geofence rule by id
async def get_geofence_rule(self,id:int) -> Union[Rule, GSResponse]:
Client = self.http_client
# Check if geofence plugin exists
if not await self.check_modules('geofence'):
return GSResponse(code=404, response="Plugin not found")
responses = await Client.get(f"geofence/rules/id/{id}", headers={'Accept': "application/json"})
if responses.status_code == 200:
return Rule.model_validate(responses.json())
Expand All @@ -335,7 +348,9 @@ async def create_geofence(
self, rule:Rule
) -> GSResponse:
PostingRule = NewRule(Rule=rule)
print(PostingRule.model_dump_json())
# Check if geofence plugin exists
if not await self.check_modules('geofence'):
return GSResponse(code=404, response="Plugin not found")
Client = self.http_client
responses = await Client.post(
"geofence/rules",
Expand Down
25 changes: 23 additions & 2 deletions src/geoserverx/_sync/gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ def inner_function(*args, **kwargs):
return GSResponse(code=504, response="Timeout Error in connection")

return inner_function

# check if certain module/plugin exists in geoserver
@exception_handler
def check_modules(self,name)-> Union[bool, GSResponse]:
Client = self.http_client
response = Client.get("about/status.json")
if response.status_code != 200:
return False
modules = [item["name"].lower() for item in response.json()['statuss']['status']]
return name.lower() in modules


# Get all workspaces
@exception_handler
Expand Down Expand Up @@ -439,6 +450,10 @@ def get_all_layer_groups(self,workspace: Optional[str] = None) -> Union[LayerGro
@exception_handler
def get_all_geofence_rules(self) -> Union[RulesResponse, GSResponse]:
Client = self.http_client
# Check if geofence plugin exists
if not self.check_modules('geofence'):
return GSResponse(code=404, response="Plugin not found")

responses = Client.get("geofence/rules/", headers={'Accept': "application/json"})
if responses.status_code == 200:
return RulesResponse.model_validate(responses.json())
Expand All @@ -450,6 +465,9 @@ def get_all_geofence_rules(self) -> Union[RulesResponse, GSResponse]:
@exception_handler
def get_geofence_rule(self,id:int) -> Union[GetRule, GSResponse]:
Client = self.http_client
# Check if geofence plugin exists
if not self.check_modules('geofence'):
return GSResponse(code=404, response="Plugin not found")
responses = Client.get(f"geofence/rules/id/{id}", headers={'Accept': "application/json"})
if responses.status_code == 200:
return Rule.model_validate(responses.json())
Expand All @@ -463,12 +481,15 @@ def create_geofence(
self, rule:Rule
) -> GSResponse:
PostingRule = NewRule(Rule=rule)
print(PostingRule.model_dump_json())
# Check if geofence plugin exists
if not self.check_modules('geofence'):
return GSResponse(code=404, response="Plugin not found")
Client = self.http_client
responses = Client.post(
"geofence/rules",
content=PostingRule.model_dump_json(),
headers=self.head,
)
results = self.response_recognise(responses.status_code)
return results
return results

4 changes: 2 additions & 2 deletions src/geoserverx/models/geofence.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Attribute(BaseModel):


class LayerDetails(BaseModel):
layerType: str
layerType: Optional[str] = None
defaultStyle: Optional[str] = None
cqlFilterRead: Optional[str] = None
cqlFilterWrite: Optional[str] = None
Expand All @@ -25,7 +25,7 @@ class Rule(BaseModel):
userName: Optional[str] = None
roleName: str
addressRange: Optional[str] = None
workspace: str
workspace: Optional[str] = None
layer: Optional[str] = None
service: Optional[Literal["GWC", "WMS", "WCS", "WFS"]] = None
request: Optional[str] = None
Expand Down
30 changes: 30 additions & 0 deletions tests/_async/test_gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ async def test_get_all_layer_groups_NetworkError(create_a_client, respx_mock):
async def test_all_geofence_rules_validation(
create_a_client, respx_mock, bad_all_geofence_rules_connection
):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(
return_value=httpx.Response(404, json=bad_all_geofence_rules_connection)
)
Expand All @@ -480,6 +485,11 @@ async def test_all_geofence_rules_validation(
async def test_all_geofence_rules_success(
create_a_client, respx_mock, good_all_geofence_rules_connection
):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(
return_value=httpx.Response(200, json=good_all_geofence_rules_connection)
)
Expand All @@ -489,6 +499,11 @@ async def test_all_geofence_rules_success(

@pytest.mark.asyncio
async def test_all_geofence_rules_NetworkError(create_a_client, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(side_effect=httpx.ConnectError)
with pytest.raises(httpx.ConnectError):
response = await create_a_client.get_all_geofence_rules()
Expand All @@ -499,6 +514,11 @@ async def test_all_geofence_rules_NetworkError(create_a_client, respx_mock):
async def test_create_geofence_validation(
create_a_client, invalid_new_geofence_rule_connection, respx_mock
):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.post(f"{baseUrl}geofence/rules").mock(
return_value=httpx.Response(404, json=invalid_new_geofence_rule_connection)
)
Expand All @@ -516,6 +536,11 @@ async def test_create_geofence_validation(
async def test_create_geofence_success(
create_a_client, good_all_geofence_rules_connection, respx_mock
):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.post(f"{baseUrl}geofence/rules").mock(
return_value=httpx.Response(201, json=good_all_geofence_rules_connection)
)
Expand All @@ -531,6 +556,11 @@ async def test_create_geofence_success(

@pytest_mark.anyio
async def test_create_geofence_ConnectError(create_a_client, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.post(f"{baseUrl}geofence/rules").mock(
side_effect=httpx.ConnectError
)
Expand Down
32 changes: 31 additions & 1 deletion tests/_sync/test_gsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ def test_get_all_layer_groups_ConnectError(client: SyncGeoServerX, respx_mock):

# Test - all_geofence_rules
def test_all_geofence_rules_validation(client: SyncGeoServerX, bad_all_geofence_rules_connection, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(
return_value=httpx.Response(404, json=bad_all_geofence_rules_connection)
)
Expand All @@ -415,6 +420,11 @@ def test_all_geofence_rules_validation(client: SyncGeoServerX, bad_all_geofence_


def test_all_geofence_rules_success(client: SyncGeoServerX, good_all_geofence_rules_connection, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(
return_value=httpx.Response(200, json=good_all_geofence_rules_connection)
)
Expand All @@ -423,6 +433,11 @@ def test_all_geofence_rules_success(client: SyncGeoServerX, good_all_geofence_ru


def test_all_geofence_rules_ConnectError(client: SyncGeoServerX, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(side_effect=httpx.ConnectError)
response = client.get_all_geofence_rules()
assert response.response == "Error in connecting to Geoserver"
Expand All @@ -431,7 +446,12 @@ def test_all_geofence_rules_ConnectError(client: SyncGeoServerX, respx_mock):
# Test - create_geofence
def test_create_geofence_validation(
client: SyncGeoServerX, invalid_new_geofence_rule_connection, respx_mock
):
):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.post(f"{baseUrl}geofence/rules").mock(
return_value=httpx.Response(404, json=invalid_new_geofence_rule_connection)
)
Expand All @@ -452,6 +472,11 @@ def test_create_geofence_validation(
def test_create_geofence_success(
client: SyncGeoServerX, good_all_geofence_rules_connection, respx_mock
):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.post(f"{baseUrl}geofence/rules").mock(
return_value=httpx.Response(201, json=good_all_geofence_rules_connection)
)
Expand All @@ -470,6 +495,11 @@ def test_create_geofence_success(


def test_create_geofence_ConnectError(client: SyncGeoServerX, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.post(f"{baseUrl}geofence/rules").mock(side_effect=httpx.ConnectError)
response = client.create_geofence(
rule=Rule(
Expand Down
15 changes: 15 additions & 0 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ def test_get_all_layer_groups_NetworkError(respx_mock):

# Test - all_geofence_rules
def test_all_geofence_rules_validation(bad_all_geofence_rules_connection, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(
return_value=httpx.Response(404, json=bad_all_geofence_rules_connection)
)
Expand All @@ -397,6 +402,11 @@ def test_all_geofence_rules_validation(bad_all_geofence_rules_connection, respx_


def test_all_geofence_rules_success(good_all_geofence_rules_connection, respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(
return_value=httpx.Response(200, json=good_all_geofence_rules_connection)
)
Expand All @@ -405,6 +415,11 @@ def test_all_geofence_rules_success(good_all_geofence_rules_connection, respx_mo


def test_all_geofence_rules_NetworkError(respx_mock):
respx_mock.get(f"{baseUrl}about/status.json").mock(
return_value=httpx.Response(200, json={
'statuss': {'status': [{'name': 'geofence'}]}
})
)
respx_mock.get(f"{baseUrl}geofence/rules/", headers={'Accept': "application/json"}).mock(side_effect=httpx.ConnectError)
result = runner.invoke(app, ["geofence-rules"])
assert "Error in connecting to Geoserver" in result.stdout

0 comments on commit 08f7d01

Please sign in to comment.