fix tests, security and post-migration

This commit is contained in:
Marc Durepos 2024-09-18 15:40:45 -04:00
parent 1ab1d00cab
commit 75ed4642c6
4 changed files with 62 additions and 34 deletions

View file

@ -2,5 +2,22 @@
access. Everything is calculated from there. Inverse functions deal with
sports.team.staff records instead of having a separate table for storing
access rights."""
from odoo import api, SUPERUSER_ID, Command
def migrate(cr, version):
cr.execute("DROP TABLE sports_team_res_users_rel")
env = api.Environment(cr, SUPERUSER_ID, {})
rules = (
env.ref("bemade_sports_clinic.restrict_staff_access_to_teams")
+ env.ref("bemade_sports_clinic.restrict_staff_access_to_team_injuries")
+ env.ref("bemade_sports_clinic.restrict_staff_access_to_team_players")
)
rules.write({
"groups": [Command.link(env.ref("base.group_user").id)]
})
env.ref("bemade_sports_clinic.group_sports_clinic_user").write({
"implied_ids": [Command.link(env.ref("base.group_partner_manager").id)]
})
cr.execute("DROP TABLE sports_team_res_users_rel")
# Check the groups are OK

View file

@ -1,12 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data noupdate="1">
<data>
<record id="module_category_sports_clinic_management" model="ir.module.category">
<field name="name">Sports Medicine Clinic Management</field>
</record>
<record id="group_sports_clinic_user" model="res.groups">
<field name="name">Internal User</field>
<field name="category_id" ref="module_category_sports_clinic_management"/>
<field name="implied_ids" eval="[
Command.set(
[
ref('base.group_user'),
ref('base.group_partner_manager'),
]
)
]"/>
</record>
<record id="group_sports_clinic_treatment_professional" model="res.groups">
<field name="name">Treatment Professional</field>

View file

@ -3,6 +3,7 @@ from odoo import fields, Command
from datetime import timedelta
@tagged("-at_install", "post_install")
class TestPatient(TransactionCase):
@classmethod

View file

@ -1,9 +1,14 @@
from odoo.tests import TransactionCase, Form
from odoo.tests import TransactionCase, Form, tagged
from odoo.fields import Date
from datetime import timedelta
from odoo.exceptions import AccessError
from odoo import Command
import logging
_logger = logging.getLogger(__name__)
@tagged("-at_install", "post_install")
class TestRights(TransactionCase):
@classmethod
def setUpClass(cls):
@ -12,18 +17,14 @@ class TestRights(TransactionCase):
cls.admin_user = cls.env["res.users"].create(
{
"name": "Admin User",
"login": "admin",
"password": "admin",
"login": "sports_admin",
"password": "sports_admin",
"groups_id": [
(
6,
0,
[
cls.env.ref(
"bemade_sports_clinic.group_sports_clinic_admin"
),
],
)
Command.set(
cls.env.ref(
"bemade_sports_clinic.group_sports_clinic_admin"
).id,
),
],
}
)
@ -34,18 +35,17 @@ class TestRights(TransactionCase):
"login": "treatment_professional",
"password": "treatment_professional",
"groups_id": [
(
6,
0,
[
cls.env.ref(
"bemade_sports_clinic.group_sports_clinic_treatment_professional"
).id
],
)
Command.set(
cls.env.ref(
"bemade_sports_clinic.group_sports_clinic_treatment_professional"
).id,
),
],
}
)
_logger.info(
f"Treatment Pro Groups: {cls.treatment_professional_user.groups_id.name}"
)
def test_treatment_pro_has_access_only_to_staffed_teams(self):
"""A treatment professional should only have access to teams and,
@ -61,16 +61,18 @@ class TestRights(TransactionCase):
Form(
self.env["sports.patient"]
.with_user(self.treatment_professional_user)
.browse(patients.ids)
.browse(patients[0].id)
)
def test_treatment_pro_can_remove_patient_from_team(self):
team, patients = self._generate_team_with_patient(self.admin_user)
self.env['sports.team.staff'].create({
"team_id": team.id,
"partner_id": self.treatment_professional_user.id,
"role": "head_therapist",
})
self.env["sports.team.staff"].with_user(self.admin_user).create(
{
"team_id": team.id,
"partner_id": self.treatment_professional_user.partner_id.id,
"role": "head_therapist",
}
)
# Test removing the patient since we are team staff
# Should not throw an error...
with Form(team.with_user(self.treatment_professional_user)) as team:
@ -81,7 +83,7 @@ class TestRights(TransactionCase):
user = user or self.env.user
team = (
self.env["sports.team"]
.with_user(self.user)
.with_user(user)
.create(
{
"name": "Test Team",
@ -90,22 +92,22 @@ class TestRights(TransactionCase):
)
patients = (
self.env["sports.patient"]
.with_user(self.user)
.with_user(user)
.create(
[
{
"first_name": "Test",
"last_name": "Patient One",
"date_of_birth": Date.today() - timedelta(days=-365 * 18),
"team_ids": [6, 0, team.ids],
"team_ids": [(6, 0, team.ids)],
},
{
"first_name": "Test",
"last_name": "Patient Two",
"date_of_birth": Date.today() - timedelta(days=-365 * 18),
"team_ids": [6, 0, team.ids],
"team_ids": [(6, 0, team.ids)],
},
]
)
)
return team, patients
return team, patients