Skip to content
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

fixtures: fix long description #321

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions invenio_communities/communities/records/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ def create(cls, record, **kwargs):
parameters.
:returns: A :class:`CommunitiesIdProvider` instance.
"""
try:
existing_pid = cls.get(record['id']).pid
except PIDDoesNotExistError:
pass
else:
raise PIDAlreadyExists(
existing_pid.pid_type,
existing_pid.pid_value
)
kwargs['pid_value'] = record['id']
kwargs['status'] = cls.default_status
kwargs['object_type'] = cls.object_type
Expand Down
9 changes: 9 additions & 0 deletions invenio_communities/fixtures/__init__py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
# Copyright (C) 2019 Northwestern University.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Data fixtures module."""
15 changes: 9 additions & 6 deletions invenio_communities/fixtures/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ def create_fake_community(faker):
"id": faker.unique.domain_word(),
"metadata": {
"title": faker.sentence(nb_words=5, variable_nb_words=True),
"description": faker.text(max_nb_chars=2000),
"type": random.choice(
["organization", "event", "topic", "project"]),
"curation_policy": faker.text(max_nb_chars=2000),
"page": faker.text(max_nb_chars=2000),
"description": faker.text(max_nb_chars=500),
"type": random.choice(["organization", "event", "topic", "project"]),
"curation_policy": faker.text(max_nb_chars=500),
"page": faker.text(max_nb_chars=250),
"website": "https://" + faker.domain_name(), # fake.url()
"funding": [
{
Expand All @@ -48,7 +47,11 @@ def create_fake_community(faker):
{
"name": "CERN",
"identifiers": [
{"identifier": "01ggx4157", "scheme": "ror"}],
{
"identifier": "01ggx4157",
"scheme": "ror"
}
],
}
],
},
Expand Down
66 changes: 66 additions & 0 deletions invenio_communities/fixtures/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 TU Wien.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Users fixtures module."""

import secrets
import string

import yaml
from flask import current_app
from flask_security.utils import hash_password
from invenio_access.models import ActionUsers
from invenio_access.proxies import current_access
from invenio_accounts.proxies import current_datastore
from invenio_db import db


class UsersFixture:
"""Users fixture."""

def __init__(self, search_path, filename):
"""Initialize the fixture."""
self._search_path = search_path
self._filename = filename

def load(self):
"""Load the fixture."""
with open(self._search_path.path(self._filename)) as fp:
data = yaml.load(fp)
for email, user_data in data.items():
self.create_user(email, user_data)

def create_user(self, email, entry):
"""Load a single user."""
# when the user's password is set in the configuration, then
# this overrides everything else
password = current_app.config.get(
"RDM_RECORDS_USER_FIXTURE_PASSWORDS", {}
).get(email)

if not password:
# for auto-generated passwords use letters, digits,
# and some punctuation marks
alphabet = string.ascii_letters + string.digits + "+,-_."
gen_passwd = "".join(secrets.choice(alphabet) for i in range(20))
password = entry.get("password") or gen_passwd

user_data = {
"email": email,
"active": entry.get("active", False),
"password": hash_password(password),
}
user = current_datastore.create_user(**user_data)

for role in entry.get("roles", []):
current_datastore.add_role_to_user(user, role)

for action in entry.get("allow", []):
action = current_access.actions[action]
db.session.add(ActionUsers.allow(action, user_id=user.id))

db.session.commit()
1 change: 1 addition & 0 deletions tests/communities/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@

def test_fake_demo_community_creation(app, db, location, es_clear):
"""Assert that demo community creation works without failing."""

faker = Faker()
create_demo_community(create_fake_community(faker))