"""
Common functions for organisations.
"""
import time
from pprint import pprint
from django.urls import reverse
from selenium.webdriver.common.by import By
from accounts.models import User
from organisations.models import Organisation
from organisations.views.admin import rbac_get_basic_and_advanced
from tests.test_manager import CobaltTestManagerIntegration
[docs]
def add_club(
manager: CobaltTestManagerIntegration,
user: User = None,
view_data=None,
reverse_result=False,
):
"""Common function to try to add a club
Args:
manager: test_manager.Manager object for interacting with system
user: User object
view_data: dic thing for the view
reverse_result: swap True for False - we want this to fail
"""
url = reverse("organisations:admin_add_club")
# log user in
manager.login_test_client(user)
response = manager.client.post(url, view_data)
ok = response.status_code == 302
if reverse_result:
ok = not ok
desc = f"{user.first_name} adds {view_data['name']} as a club but doesn't have permission. This should fail. We should get a redirect if it works."
test_name = (
f"{user.first_name} adds {view_data['name']}. Should fail - Redirect"
)
output = f"Checking we didn't redirected. {ok}"
else:
desc = f"{user.first_name} adds {view_data['name']} as a club. We have to use real club names as this connects to the MPC. We should get a redirect if it works."
test_name = f"{user.first_name} adds {view_data['name']} - Redirect"
output = f"Checking we got redirected. {ok}"
manager.save_results(
status=ok, test_name=test_name, output=output, test_description=desc
)
ok = Organisation.objects.filter(org_id=view_data["org_id"]).exists()
if reverse_result:
ok = not ok
test_name = f"{user.first_name} adds {view_data['name']}. Should fail - Check doesn't exist"
desc = "Check that an org with this id doesn't exists. This test is expected to fail. Invalid permissions."
output = f"Shouldn't find org with org_id={view_data['org_id']}. {ok}."
else:
test_name = f"{user.first_name} adds {view_data['name']} - Check exists"
desc = "Check that an org with this id exists. Invalid test if it existed before we tried to add it."
output = f"Should find org with org_id={view_data['org_id']}. {ok}."
manager.save_results(
status=ok,
test_name=test_name,
output=output,
test_description=desc,
)
[docs]
def confirm_club_rbac_status(
manager: CobaltTestManagerIntegration,
club_org_id: int,
expected_status: str,
test_name: str,
test_description: str,
reverse_result=False,
):
"""Common function to test the rbac status of a club
Args:
manager: test_manager.Manager object for interacting with system
club_org_id: org id of club
expected_status: what the status should be
test_description: long description of test
test_name: name of test
reverse_result: if failure is a good outcome
"""
club = Organisation.objects.filter(org_id=club_org_id).first()
basic, advanced = rbac_get_basic_and_advanced(club)
if basic and advanced:
rbac_state = "Both basic and advanced"
elif basic:
rbac_state = "Basic"
elif advanced:
rbac_state = "Advanced"
else:
rbac_state = "Not Set Up"
ok = expected_status == rbac_state
if reverse_result:
ok = not ok
manager.save_results(
status=ok,
test_name=test_name,
output=f"{club} checked for RBAC state to be: '{expected_status}'. Actual state is: '{rbac_state}'",
test_description=test_description,
)
[docs]
def set_rbac_status_as_user(
manager: CobaltTestManagerIntegration,
user: User,
club_org_id: int,
new_status: str,
test_name: str,
test_description: str,
reverse_result: bool,
):
"""Common function to change the rbac state of a club and check the outcome. Sometimes we expect this to fail.
Args:
manager: test_manager.Manager object for interacting with system
user: User object
club_org_id: org id of club
new_status: status to change to
test_description: long description of test
test_name: name of test
reverse_result: for tests that should fail
"""
# log user in
manager.login_test_client(user)
# Get club
club = Organisation.objects.filter(org_id=club_org_id).first()
if new_status == "Advanced":
url_string = "organisations:admin_club_rbac_convert_basic_to_advanced"
elif new_status == "Basic":
url_string = "organisations:admin_club_rbac_convert_advanced_to_basic"
url = reverse(url_string, kwargs={"club_id": club.id})
# run - don't bother checking what comes back
manager.client.post(url)
# see what the status is now
confirm_club_rbac_status(
manager, club_org_id, new_status, test_name, test_description, reverse_result
)
[docs]
def change_rbac_status_as_user(
manager: CobaltTestManagerIntegration,
user: User,
club_org_id: int,
new_status: str,
test_name: str,
test_description: str,
reverse_result: bool,
):
"""Common function to change the rbac state of a club and check the outcome. Sometimes we expect this to fail.
Args:
manager: test_manager.Manager object for interacting with system
user: User object
club_org_id: org id of club
new_status: status to change to
test_description: long description of test
test_name: name of test
reverse_result: for tests that should fail
"""
# log user in
manager.login_test_client(user)
# Get club
club = Organisation.objects.filter(org_id=club_org_id).first()
if new_status == "Basic":
url_string = "organisations:admin_club_rbac_convert_advanced_to_basic"
elif new_status == "Advanced":
url_string = "organisations:admin_club_rbac_convert_basic_to_advanced"
url = reverse(url_string, kwargs={"club_id": club.id})
# run - don't bother checking what comes back
manager.client.post(url)
# see what the status is now
confirm_club_rbac_status(
manager, club_org_id, new_status, test_name, test_description, reverse_result
)
[docs]
def login_and_go_to_club_menu(
manager: CobaltTestManagerIntegration,
org_id: int,
user: User,
test_description: str,
test_name: str,
reverse_result: bool,
):
"""Login and got to the club menu
Initial Selenium State: Doesn't matter
Final Selenium State: On Club Menu logged in as User (if allowed)
Args:
manager: test_manager.Manager object for interacting with system
org_id: club to check for
user: User to use for test
test_description: long description of test
test_name: name of test
reverse_result: for tests that should fail
"""
# Login as user
manager.login_selenium_user(user)
# Go to club menu
club = Organisation.objects.filter(org_id=org_id).first()
url = manager.base_url + reverse(
"organisations:club_menu", kwargs={"club_id": club.id}
)
manager.driver.get(url)
club_name = manager.driver.find_elements(By.ID, "t_club_name")
if club_name:
title = club_name[0].text
ok = True
else:
title = "Not found"
ok = False
real_ok = ok != reverse_result
manager.save_results(
status=real_ok,
test_name=test_name,
output=f"Logged in as {user.first_name}. Went to club menu and looked for club name. Got: {title}. "
f"Outcome:{ok}. Expected outcome: {not reverse_result}.",
test_description=test_description,
)
[docs]
def access_finance_for_club(
manager: CobaltTestManagerIntegration,
club: Organisation,
user: User,
test_name: str,
test_description: str,
reverse_result=False,
):
"""Common function to move to the Access tab
Initial Selenium State: Doesn't matter
Final Selenium State: On Organisation Statement logged in as User
Args:
manager: test_manager.Manager object for interacting with system
club: club to check for
user: User to use for test
test_description: long description of test
test_name: name of test
reverse_result: for tests that should fail
"""
# Get URL for payments statement for this org
url = reverse("payments:statement_org", kwargs={"org_id": club.id})
# login the user
manager.login_test_client(user)
# Get page
response = manager.client.get(url)
# Look for Org in context to see if we got there
ret = "org" in response.context
ok = not ret if reverse_result else ret
manager.save_results(
status=ok,
test_name=test_name,
output=f"Accessed Org Statement for {club} as {user.first_name}. Successful: {ret}. Was this expected: {ok}",
test_description=test_description,
)