Skip to content

Commit

Permalink
organisation: create default organisation
Browse files Browse the repository at this point in the history
* Creates a default organisation during setup.
* Associates default organisation to users.
* Adds a CLI command for creating organisations from file.
* Sets organisation as required in user JSON schema.

Co-Authored-by: Sébastien Délèze <sebastien.deleze@rero.ch>
  • Loading branch information
Sébastien Délèze committed May 20, 2020
1 parent dbd3371 commit b1d1d5c
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 26 deletions.
6 changes: 6 additions & 0 deletions data/organisations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"code": "rero",
"name": "RERO"
}
]
20 changes: 16 additions & 4 deletions data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"street": "Sonnenbergstr 39",
"postal_code": "6544",
"city": "Braggio",
"phone": "+41916658961"
"phone": "+41916658961",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/rero"
}
},
{
"full_name": "Elia Rossi",
Expand All @@ -19,7 +22,10 @@
"street": "Brunnacherstrasse 112",
"postal_code": "5330",
"city": "Zurzach",
"phone": "+41625037543"
"phone": "+41625037543",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/rero"
}
},
{
"full_name": "Emanuele Fiorentini",
Expand All @@ -30,7 +36,10 @@
"street": "Lichtmattstrasse 123",
"postal_code": "5736",
"city": "Burg",
"phone": "+41627676171"
"phone": "+41627676171",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/rero"
}
},
{
"full_name": "Jules Brochu",
Expand All @@ -41,6 +50,9 @@
"street": "Rasenstrasse 2",
"postal_code": "4577",
"city": "Hessigkofen",
"phone": "+41323666556"
"phone": "+41323666556",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/rero"
}
}
]
3 changes: 2 additions & 1 deletion scripts/setup
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pipenv run invenio fixtures deposits create
# Create configuration for RERODOC OAI harvesting
pipenv run invenio oaiharvester config create data/rerodoc_oai_sources.json

# Create users
# Create users and organisations
pipenv run invenio fixtures organisations import $(pipenv --where)/data/organisations.json
pipenv run invenio fixtures users import $(pipenv --where)/data/users.json

# Create document sample
Expand Down
2 changes: 2 additions & 0 deletions sonar/modules/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from jsonref import JsonLoader

from .deposits.cli import deposits
from .organisations.cli.organisations import organisations
from .users.cli import users


Expand All @@ -38,6 +39,7 @@ def fixtures():


fixtures.add_command(users)
fixtures.add_command(organisations)
fixtures.add_command(deposits)


Expand Down
64 changes: 64 additions & 0 deletions sonar/modules/organisations/cli/organisations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2019 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Documents CLI commands."""

import json

import click
from click.exceptions import ClickException
from flask.cli import with_appcontext
from invenio_db import db

from sonar.modules.organisations.api import OrganisationIndexer, \
OrganisationRecord


@click.group()
def organisations():
"""Organisations CLI commands."""


@organisations.command('import')
@click.argument('file', type=click.File('r'))
@with_appcontext
def import_organisations(file):
"""Import organisations from JSON file."""
click.secho('Importing organisations from {file}'.format(file=file.name))

indexer = OrganisationIndexer()

for record in json.load(file):
try:
# Check existence in DB
db_record = OrganisationRecord.get_record_by_pid(record['code'])

if db_record:
raise ClickException('Record already exists in DB')

# Register record to DB
db_record = OrganisationRecord.create(record)
db.session.commit()

indexer.index(db_record)
except Exception as error:
click.secho(
'Organisation {org} could not be imported: {error}'.format(
org=record, error=str(error)),
fg='red')

click.secho('Finished', fg='green')
6 changes: 5 additions & 1 deletion sonar/modules/users/jsonschemas/users/user-v1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@
}
}
}
}
},
"required": [
"$ref"
]
},
"roles": {
"title": "Role",
Expand Down Expand Up @@ -139,6 +142,7 @@
"full_name",
"email",
"roles",
"organisation",
"$schema"
]
}
14 changes: 10 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ def organisation_fixture(app, db):


@pytest.fixture()
def db_user_fixture(app, db):
def db_user_fixture(app, db, organisation_fixture):
"""Create user in database."""
data = {
'email': 'user@rero.ch',
'full_name': 'John Doe',
'roles': ['user']
'roles': ['user'],
'organisation': {
'$ref': 'https://sonar.ch/api/organisations/org'
}
}

user = UserRecord.create(data, dbcommit=True)
Expand All @@ -139,12 +142,15 @@ def db_user_fixture(app, db):


@pytest.fixture()
def db_moderator_fixture(app, db):
def db_moderator_fixture(app, db, organisation_fixture):
"""Create moderator in database."""
data = {
'email': 'moderator@rero.ch',
'full_name': 'John Doe',
'roles': ['moderator']
'roles': ['moderator'],
'organisation': {
'$ref': 'https://sonar.ch/api/organisations/org'
}
}

user = UserRecord.create(data, dbcommit=True)
Expand Down
48 changes: 48 additions & 0 deletions tests/ui/organisations/cli/test_organisations_cli_organisations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2019 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Test CLI for organisations."""

from click.testing import CliRunner

import sonar.modules.organisations.cli.organisations as Cli
from sonar.modules.organisations.api import OrganisationRecord


def test_import_organisations(app, script_info):
"""Test import organisations."""
runner = CliRunner()

datastore = app.extensions['security'].datastore
datastore.create_role(name='admin')

# Import ok
result = runner.invoke(Cli.import_organisations,
['./tests/ui/organisations/data/valid.json'],
obj=script_info)
organisation = OrganisationRecord.get_record_by_pid('test')
assert organisation
assert organisation['pid'] == 'test'

# Already existing
result = runner.invoke(Cli.import_organisations,
['./tests/ui/organisations/data/valid.json'],
obj=script_info)
assert result.output.find(
'Organisation {\'code\': \'test\', \'name\': \'Test\'} could not be '
'imported: Record already exists in DB'
)
6 changes: 6 additions & 0 deletions tests/ui/organisations/data/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"code": "test",
"name": "Test"
}
]
5 changes: 4 additions & 1 deletion tests/ui/users/data/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"password": "123456",
"phone": "+39324993597",
"postal_code": "11100",
"street": "Viale Rue Gran Paradiso, 44"
"street": "Viale Rue Gran Paradiso, 44",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/org"
}
}
]
5 changes: 4 additions & 1 deletion tests/ui/users/data/without_email.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"password": "123456",
"phone": "+39324993597",
"postal_code": "11100",
"street": "Viale Rue Gran Paradiso, 44"
"street": "Viale Rue Gran Paradiso, 44",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/org"
}
}
]
5 changes: 4 additions & 1 deletion tests/ui/users/data/without_roles.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"password": "123456",
"phone": "+39324993597",
"postal_code": "11100",
"street": "Viale Rue Gran Paradiso, 44"
"street": "Viale Rue Gran Paradiso, 44",
"organisation": {
"$ref": "https://sonar.ch/api/organisations/org"
}
}
]
7 changes: 5 additions & 2 deletions tests/ui/users/test_user_jsonresolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
from sonar.modules.users.api import UserRecord


def test_user_resolver(app):
def test_user_resolver(app, organisation_fixture):
"""Test user resolver."""
UserRecord.create({
'pid': '1',
'full_name': 'Jules Brochu',
'email': 'admin@test.com',
'roles': ['user']
'roles': ['user'],
'organisation': {
'$ref': 'https://sonar.ch/api/organisations/org'
}
})

record = DepositRecord.create({
Expand Down
Loading

0 comments on commit b1d1d5c

Please sign in to comment.