def test_it_with_a_group(self, factories): group = factories.Group() context = GroupContext(group=group) assert context.group == group assert context.__acl__() == group.__acl__()
def group_search_controller(self, group, pyramid_request): # Set the slug in the URL to the slug of the group. # Otherwise GroupSearchController will redirect the request to the # correct URL. pyramid_request.matchdict["slug"] = group.slug return activity.GroupSearchController(GroupContext(group), pyramid_request)
def test_read_noslug_redirects(pyramid_request, factories): group = factories.Group() with pytest.raises(HTTPMovedPermanently) as exc: views.read_noslug(GroupContext(group), pyramid_request) assert exc.value.location == f"/g/{group.pubid}/{group.slug}"
def test_delete_deletes_group(self, group, delete_group_service, pyramid_request, routes): view = GroupEditViews(GroupContext(group), pyramid_request) view.delete() delete_group_service.delete.assert_called_once_with(group)
def test_read_renders_form_if_group_has_no_creator(self, pyramid_request, group): group.creator = None view = GroupEditViews(GroupContext(group), pyramid_request) response = view.read() assert response["form"] == self._expected_form(group)
def controller(self, request, group, pyramid_request, query): test_group = group if "test_group" in request.fixturenames: test_group = request.getfixturevalue("test_group") context = GroupContext(test_group) controller = activity.GroupSearchController(context, pyramid_request) return controller
def test_group_found(self, annotation_context, group_context, user_context): assert not predicates.group_found(sentinel.identity, user_context) assert not predicates.group_found(sentinel.identity, GroupContext(group=None)) # Annotations have a group assert predicates.group_found(sentinel.identity, annotation_context) assert predicates.group_found(sentinel.identity, group_context)
def test_it_without_a_group(self, set_permissions, pyramid_request, principal, has_upsert): set_permissions("acct:adminuser@foo", principals=[principal]) context = GroupContext(group=None) assert context.group is None assert bool(pyramid_request.has_permission("upsert", context)) == has_upsert
def test_update_proxies_to_update_service_on_success( self, factories, pyramid_request, user_service, list_organizations_service, handle_form_submission, group_update_service, group, GroupScope, ): fetched_user = factories.User() user_service.fetch.return_value = fetched_user updated_org = factories.Organization() list_organizations_service.organizations.return_value.append( updated_org) def call_on_success( # pylint:disable=unused-argument request, form, on_success, on_failure): return on_success({ "creator": fetched_user.username, "description": "New description", "group_type": "open", "name": "Updated group", "organization": updated_org.pubid, "scopes": ["http://somewhereelse.com", "http://www.gladiolus.org"], "members": [], "enforce_scope": False, }) handle_form_submission.side_effect = call_on_success view = GroupEditViews(GroupContext(group), pyramid_request) response = view.update() group_update_service.update.assert_called_once_with( group, organization=updated_org, creator=fetched_user, description="New description", name="Updated group", scopes=[ GroupScope(scope=scope) for scope in ["http://somewhereelse.com", "http://www.gladiolus.org"] ], enforce_scope=False, ) assert response["form"] == self._expected_form(group)
def test_post_sets_group_properties( self, form_validating_to, pyramid_request, group ): controller = views.GroupEditController(GroupContext(group), pyramid_request) controller.form = form_validating_to( {"name": "New name", "description": "New description"} ) controller.post() assert group.name == "New name" assert group.description == "New description"
def test_get_reads_group_properties(self, pyramid_request, group): pyramid_request.create_form.return_value = FakeForm() result = views.GroupEditController(GroupContext(group), pyramid_request).get() assert result == { "form": { "name": group.name, "description": group.description, }, "group_path": f"/g/{group.pubid}/{group.slug}", }
def test_read_renders_form(self, pyramid_request, factories, group): factories.Annotation(groupid=group.pubid) factories.Annotation(groupid=group.pubid) view = GroupEditViews(GroupContext(group), pyramid_request) response = view.read() assert response["form"] == self._expected_form(group) assert response["pubid"] == group.pubid assert response["group_name"] == group.name assert response["member_count"] == len(group.members) assert response["annotation_count"] == 2
def test_read_lists_organizations_in_groups_authority( self, pyramid_request, group, organization, AdminGroupSchema, list_organizations_service, ): GroupEditViews(GroupContext(group), pyramid_request) list_organizations_service.organizations.assert_called_with( group.authority) schema = AdminGroupSchema.return_value (_, call_kwargs) = schema.bind.call_args assert call_kwargs["organizations"] == { organization.pubid: organization }
def test_it_binds_schema( self, pyramid_request, group, user_service, organization, AdminGroupSchema, ): GroupEditViews(GroupContext(group), pyramid_request) schema = AdminGroupSchema.return_value schema.bind.assert_called_with( request=pyramid_request, group=group, user_svc=user_service, organizations={organization.pubid: organization}, )
def test_update_updates_group_members_on_success( self, factories, pyramid_request, group_create_service, user_service, group_members_service, handle_form_submission, list_organizations_service, ): group = factories.RestrictedGroup( pubid="testgroup", organization=factories.Organization()) list_organizations_service.organizations.return_value = [ group.organization ] fetched_user = factories.User() user_service.fetch.return_value = fetched_user def call_on_success(request, form, on_success, on_failure): return on_success({ "authority": pyramid_request.default_authority, "creator": fetched_user.username, "description": "a desc", "group_type": "restricted", "name": "a name", "members": ["phil", "sue"], "organization": group.organization.pubid, "scopes": ["http://www.example.com"], "enforce_scope": group.enforce_scope, }) handle_form_submission.side_effect = call_on_success view = GroupEditViews(GroupContext(group), pyramid_request) view.update() group_members_service.update_members.assert_any_call( group, [fetched_user.userid, fetched_user.userid])
id=root_annotation_id)) # Create the annotation and enable relationship loading so we can access # the group, even though we've not added this to the session yet annotation = models.Annotation(**data) request.db.enable_relationship_loading(annotation) group = annotation.group if not group: raise schemas.ValidationError( "group: " + _(f"Invalid group id {annotation.groupid}")) # The user must have permission to create an annotation in the group # they've asked to create one in. if not request.has_permission(Permission.Group.WRITE, context=GroupContext(group)): raise schemas.ValidationError( "group: " + _("You may not create annotations in the specified group!")) _validate_group_scope(group, data["target_uri"]) annotation.created = annotation.updated = datetime.utcnow() annotation.document = update_document_metadata( request.db, annotation.target_uri, document_data["document_meta_dicts"], document_data["document_uri_dicts"], created=annotation.created, updated=annotation.updated, )
def group_context(self, factories): return GroupContext(group=factories.Group.build())
def context(factories): return GroupContext(group=factories.Group(creator=factories.User()))