Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
upadhyeammit committed Aug 25, 2023
1 parent cf02cf8 commit 3d36968
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions tests/test_api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
from base64 import b64encode
from ros.api.main import app
from ros.lib.feature_flags import FLAG_INVENTORY_GROUPS
from ros.lib.models import db, PerformanceProfile, System
from tests.helpers.db_helper import db_get_host, db_get_record
from pathlib import Path
Expand Down Expand Up @@ -104,7 +105,8 @@ def test_system_os_filter(auth_token, db_setup, db_create_account, db_create_sys
assert response.json["data"][0]["os"] == "RHEL 8.4"


def test_system_groups(auth_token, db_setup, db_create_account, db_create_system, db_create_performance_profile):
def test_system_groups(
auth_token, db_setup, db_create_account, db_create_system, db_create_performance_profile, mocker):
with app.test_client() as client:
response_all_systems = client.get(
'/api/ros/v1/systems',
Expand All @@ -115,32 +117,39 @@ def test_system_groups(auth_token, db_setup, db_create_account, db_create_system

system = db_get_record(System)
system.groups = [{
"id": "fd11209a-1ca7-49b4-ae27-0f8a365b95b8",
"id": "12345678-fe1b-4191-8408-cbadbd47f7a3",
"name": "ros-test-3"
}]
db.session.commit()

with app.test_client() as client:
mock_enable_rbac(mocker)
mock_rbac(get_rbac_mock_file("mock_rbac_returns_groups_including_example_group.json"), mocker)

mock_unleash_hbi_flag_enabled(mocker)
response_all_systems = client.get(
'/api/ros/v1/systems',
headers={"x-rh-identity": auth_token}
)
assert response_all_systems.status_code == 200
assert response_all_systems.json["data"][0]["groups"] == [{
"id": "fd11209a-1ca7-49b4-ae27-0f8a365b95b8",
"id": "12345678-fe1b-4191-8408-cbadbd47f7a3",
"name": "ros-test-3"
}]


def test_system_group_filter(auth_token, db_setup, db_create_account, db_create_system, db_create_performance_profile):
def test_system_group_filter(
auth_token, db_setup, db_create_account, db_create_system, db_create_performance_profile, mocker):
system = db_get_record(System)
system.groups = [{
"id": "fd11209a-1ca7-49b4-ae27-0f8a365b95b8",
"id": "12345678-fe1b-4191-8408-cbadbd47f7a3",
"name": "ros-group-test"
}]
db.session.commit()

with app.test_client() as client:
mock_enable_rbac(mocker)
mock_rbac(get_rbac_mock_file("mock_rbac_returns_groups_including_example_group.json"), mocker)
response = client.get(
'/api/ros/v1/systems?group_name=ros-group-test',
headers={"x-rh-identity": auth_token}
Expand Down Expand Up @@ -427,6 +436,14 @@ def mock_rbac(json_data, mocker):
mocker.patch('ros.lib.rbac_interface.query_rbac', return_value=json_data)


def mock_unleash_hbi_flag_enabled(mocker):
mocker.patch('ros.lib.feature_flags.FLAG_FALLBACK_VALUES', return_value={FLAG_INVENTORY_GROUPS: True})


def mock_unleash_hbi_flag_disabled(mocker):
mocker.patch('ros.lib.feature_flags.FLAG_FALLBACK_VALUES', return_value={FLAG_INVENTORY_GROUPS: False})


def test_systems_rbac_returns_groups_including_example_group(
auth_token,
db_setup,
Expand All @@ -441,6 +458,7 @@ def test_systems_rbac_returns_groups_including_example_group(
While filtering based on groups we check if group id we get from are there in the System's group field
It is expected that we also return systems which are in no groups"""
with app.test_client() as client:
mock_unleash_hbi_flag_enabled(mocker)
mock_enable_rbac(mocker)
mock_rbac(get_rbac_mock_file("mock_rbac_returns_groups_including_example_group.json"), mocker)
response = client.get('/api/ros/v1/systems', headers={"x-rh-identity": auth_token})
Expand All @@ -463,6 +481,7 @@ def test_systems_rbac_returns_emtpy_group(
This is the situation when user has no groups created however groups as features in enabled from inventory
In this case we can only return the systems which are not included in any of the groups"""
with app.test_client() as client:
mock_unleash_hbi_flag_enabled(mocker)
mock_enable_rbac(mocker)
mock_rbac(get_rbac_mock_file("mock_rbac_returns_emtpy_group.json"), mocker)
response = client.get('/api/ros/v1/systems', headers={"x-rh-identity": auth_token})
Expand All @@ -485,8 +504,9 @@ def test_systems_mock_rbac_returns_no_groups(
This is the use case where for some reason RBAC does not have group.id included(i.e inventory groups disabled)
In this case we return all the systems because we can't find groups as feature enabled"""
with app.test_client() as client:
mock_unleash_hbi_flag_enabled(mocker)
mock_enable_rbac(mocker)
mock_rbac(get_rbac_mock_file("mock_rbac_returns_no_groups.json"), mocker)
response = client.get('/api/ros/v1/systems', headers={"x-rh-identity": auth_token})
assert response.status_code == 200
assert response.json["meta"]["count"] == 3
assert response.json["meta"]["count"] == 1

0 comments on commit 3d36968

Please sign in to comment.