-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add library roles support for roles API #124
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||||||||
from __future__ import annotations | ||||||||||||
|
||||||||||||
import logging | ||||||||||||
import re | ||||||||||||
from copy import deepcopy | ||||||||||||
from enum import Enum | ||||||||||||
from typing import Any, Dict, List, Tuple | ||||||||||||
|
@@ -44,6 +45,19 @@ | |||||||||||
logger = logging.getLogger(__name__) | ||||||||||||
|
||||||||||||
|
||||||||||||
def are_all_library_ids(course_ids: List[str] | None) -> bool: | ||||||||||||
""" | ||||||||||||
Verify if all given ids are library ids | ||||||||||||
""" | ||||||||||||
if not course_ids: | ||||||||||||
return False | ||||||||||||
|
||||||||||||
for course_id in course_ids: | ||||||||||||
if not re.match(cs.LIBRARY_ID_REGX, course_id): | ||||||||||||
return False | ||||||||||||
return True | ||||||||||||
Comment on lines
+55
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
|
||||||||||||
def validate_course_access_role(course_access_role: dict) -> None: | ||||||||||||
""" | ||||||||||||
Validate the course access role. | ||||||||||||
|
@@ -60,6 +74,11 @@ def check_broken_rule(broken_rule: bool, code: FXExceptionCodes, message: str) - | |||||||||||
org = course_access_role['org'].strip() | ||||||||||||
course_id = course_access_role['course_id'] | ||||||||||||
role = course_access_role['role'] | ||||||||||||
check_broken_rule( | ||||||||||||
role == cs.COURSE_ACCESS_ROLES_LIBRARY_USER and course_id and not are_all_library_ids([course_id]), | ||||||||||||
FXExceptionCodes.ROLE_INVALID_ENTRY, | ||||||||||||
f'role {role} is only valid with library_id', | ||||||||||||
) | ||||||||||||
Comment on lines
+77
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. validation order is a bit important in this function. Please move this after |
||||||||||||
|
||||||||||||
check_broken_rule( | ||||||||||||
role not in cs.COURSE_ACCESS_ROLES_ALL, | ||||||||||||
|
@@ -1155,6 +1174,9 @@ def add_course_access_roles( # pylint: disable=too-many-arguments, too-many-bra | |||||||||||
if (tenant_wide and course_ids) or (not tenant_wide and not course_ids): | ||||||||||||
raise ValueError('Conflict between tenant_wide and course_ids') | ||||||||||||
|
||||||||||||
if role == cs.COURSE_ACCESS_ROLES_LIBRARY_USER and course_ids and not are_all_library_ids(course_ids): | ||||||||||||
raise ValueError(f'Role: {role} is only valid for library ids.') | ||||||||||||
|
||||||||||||
if not user_keys: | ||||||||||||
raise ValueError('No users provided!') | ||||||||||||
|
||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||
"""Mock modulestore""" | ||||||
from opaque_keys.edx.keys import CourseKey | ||||||
|
||||||
|
||||||
class DummyModuleStore: # pylint: disable=too-few-public-methods | ||||||
"""DUmmy module store class""" | ||||||
def __init__(self): | ||||||
self.ids = ['library-v1:org1+11', 'library-v1:org1+22'] | ||||||
|
||||||
def get_library_keys(self): | ||||||
"""mock modulestore library keys method""" | ||||||
return [CourseKey.from_string(id) for id in self.ids] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mocking is our enemy in tests. Please keep close to the real result as much as possible
Suggested change
|
||||||
|
||||||
|
||||||
def modulestore(): | ||||||
"""mocked modulestore""" | ||||||
return DummyModuleStore() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this was bad, then realized it's great! thank you!