Skip to content

Commit

Permalink
emails: improve templates and add tests
Browse files Browse the repository at this point in the history
* improves email templates
* adds tests
  • Loading branch information
ntarocco committed Jul 28, 2023
1 parent 16f2c35 commit 7d84ca8
Show file tree
Hide file tree
Showing 20 changed files with 515 additions and 87 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
requirements-level: [pypi]
db-service: [postgresql14]
search-service: [opensearch2]
cache-service: [redis]
mq-service: [rabbitmq]
node-version: [18]
include:
- db-service: postgresql14
Expand Down
13 changes: 5 additions & 8 deletions assets/js/invenio_app_rdm/overridableRegistry/mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import { CDSCommunitiesCarousel } from "../../components/communities_carousel/ov
import { CDSRecordsList } from "../../components/frontpage/overrides/RecordsList";
import { CDSRecordsResultsListItem } from "../../components/frontpage/overrides/RecordsResultsListItem";


export const overriddenComponents = {
"InvenioAppRdm.RecordsList.layout": CDSRecordsList,
"InvenioAppRdm.RecordsResultsListItem.layout": CDSRecordsResultsListItem,
"InvenioCommunities.CommunitiesCarousel.layout": CDSCommunitiesCarousel,
"InvenioCommunities.CarouselItem.layout": CDSCarouselItem,
};


"InvenioAppRdm.RecordsList.layout": CDSRecordsList,
"InvenioAppRdm.RecordsResultsListItem.layout": CDSRecordsResultsListItem,
"InvenioCommunities.CommunitiesCarousel.layout": CDSCommunitiesCarousel,
"InvenioCommunities.CarouselItem.layout": CDSCarouselItem,
};
4 changes: 1 addition & 3 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,10 @@ CELERY_BEAT_SCHEDULE = {
###############################################################################
# CDS-RDM configuration
###############################################################################

CDS_SERVICE_ELEMENT_URL = "https://cern.service-now.com/service-portal?id=service_element&name=CDS-Service"

# LDAP configuration
# AUTH/LDAP
CERN_LDAP_URL = "ldap://xldap.cern.ch"

CERN_AUTHORIZATION_SERVICE_API = "https://authorization-service-api-qa.web.cern.ch/api/v1.0/"
CERN_AUTHORIZATION_SERVICE_API_GROUP = "Group"

Expand Down
1 change: 0 additions & 1 deletion site/cds_rdm/assets/semantic-ui/js/cds_rdm/.gitkeep

This file was deleted.

14 changes: 5 additions & 9 deletions site/cds_rdm/ldap/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,7 @@ def update_invenio_users_from_ldap(remote_accounts, ldap_users_map, log_func):
db.session.commit()
log_func(
"user_updated",
dict(
user_id=invenio_user.user_id,
previous_department=invenio_user.data[
"remote_account_department"
],
new_department=ldap_user["remote_account_department"],
),
dict(user_id=invenio_user.user_id),
)

updated_count += 1
Expand All @@ -120,7 +114,8 @@ def update_invenio_users_from_ldap(remote_accounts, ldap_users_map, log_func):

def import_new_ldap_users(new_ldap_users, log_func):
"""Import any new LDAP user not in Invenio yet."""
importer = LdapUserImporter()
remote_account_client_id = current_app.config["CERN_APP_CREDENTIALS"]["consumer_key"]
importer = LdapUserImporter(remote_account_client_id)
added_count = 0
user_ids = []
for ldap_user in new_ldap_users:
Expand All @@ -139,13 +134,14 @@ def import_new_ldap_users(new_ldap_users, log_func):
)
continue
email = ldap_user["user_email"]
username = ldap_user["user_username"]
employee_id = ldap_user["remote_account_person_id"]

user_id = importer.import_user(ldap_user)
user_ids.append(user_id)
log_func(
"invenio_user_added",
dict(email=email, employee_id=employee_id),
dict(email=email, username=username, employee_id=employee_id),
)

added_count += 1
Expand Down
9 changes: 5 additions & 4 deletions site/cds_rdm/ldap/user_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ class LdapUserImporter:
]
"""

def __init__(self):
def __init__(self, remote_account_client_id):
"""Constructor."""
self.client_id = current_app.config["CERN_APP_CREDENTIALS"]["consumer_key"]
self.client_id = remote_account_client_id

def create_invenio_user(self, ldap_user):
"""Commit new user in db."""
email = ldap_user["user_email"]
user = User(email=email, active=True)
username = ldap_user["user_username"]
user = User(email=email, username=username, active=True)
db.session.add(user)
db.session.commit()
return user
Expand All @@ -47,7 +48,7 @@ def create_invenio_user_identity(self, user_id, ldap_user):
uid_number = ldap_user["user_identity_id"]
return UserIdentity(
id=uid_number,
method=current_app.config.get("OAUTH_REMOTE_APP_NAME"),
method=current_app.config["OAUTH_REMOTE_APP_NAME"],
id_user=user_id,
)

Expand Down
3 changes: 3 additions & 0 deletions site/cds_rdm/ldap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def serialize(ldap_user_data):
decoded_data[key] = value[0].decode("utf8")
serialized_data = dict(
user_email=decoded_data["mail"].lower(),
user_username=decoded_data["cn"],
user_profile_full_name=decoded_data["displayName"],
user_identity_id=decoded_data["uidNumber"],
cern_account_type=decoded_data["cernAccountType"],
Expand Down Expand Up @@ -104,6 +105,7 @@ def _get_full_user_info(self):
user_info = dict(
user_profile_full_name=self.user_profile.full_name,
user_email=self.user.email,
user_username=self.user.username,
user_identity_id=self.user_identity.id,
remote_account_id=self.remote_account.id,
remote_account_person_id=str(self.remote_account.extra_data["person_id"]),
Expand All @@ -116,4 +118,5 @@ def update(self, ldap_user):
ra = self.remote_account
ra.extra_data["department"] = ldap_user["remote_account_department"]
self.user.email = ldap_user["user_email"]
self.user.username = ldap_user["user_username"]
self.user_profile.full_name = ldap_user["user_profile_full_name"]
1 change: 0 additions & 1 deletion site/cds_rdm/templates/semantic-ui/cds_rdm/.gitkeep

This file was deleted.

File renamed without changes.
File renamed without changes.

This file was deleted.

2 changes: 1 addition & 1 deletion site/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if [[ ${keep_services} -eq 0 ]]; then
trap cleanup EXIT
fi

eval "$(docker-services-cli up --db ${DB:-postgresql} --search ${SEARCH:-opensearch} --mq ${MQ:-redis} --env)"
eval "$(docker-services-cli up --db ${DB:-postgresql} --search ${SEARCH:-opensearch} --cache ${CACHE:-redis} --mq ${MQ:-rabbitmq} --env)"
# Note: expansion of pytest_args looks like below to not cause an unbound
# variable error when 1) "nounset" and 2) the array is empty.
python -m pytest ${pytest_args[@]+"${pytest_args[@]}"}
Expand Down
1 change: 1 addition & 0 deletions site/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ install_requires =
[options.extras_require]
tests =
pytest-invenio>=2.1.0,<3.0.0
pytest-mock>=3

[options.entry_points]
flask.commands =
Expand Down
1 change: 0 additions & 1 deletion site/tests/.gitkeep

This file was deleted.

38 changes: 38 additions & 0 deletions site/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
#
# CDS-RDM is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""Pytest fixtures."""

import pytest

from invenio_app.factory import create_api

from cds_rdm.permissions import CDSCommunitiesPermissionPolicy


@pytest.fixture(scope="module")
def app_config(app_config):
"""Mimic an instance's configuration."""
app_config["REST_CSRF_ENABLED"] = True
app_config["DATACITE_ENABLED"] = True
app_config["DATACITE_PREFIX"] = "10.17181"
app_config["OAUTH_REMOTE_APP_NAME"] = "cern"
app_config["CERN_APP_CREDENTIALS"] = {
"consumer_key": "CHANGE ME",
"consumer_secret": "CHANGE ME",
}
app_config["CERN_LDAP_URL"] = "" # mock
app_config["COMMUNITIES_PERMISSION_POLICY"] = CDSCommunitiesPermissionPolicy
app_config["COMMUNITIES_ALLOW_RESTRICTED"] = True
app_config["CDS_GROUPS_ALLOW_CREATE_COMMUNITIES"] = ["group-allowed-create-communities"]
return app_config


@pytest.fixture(scope="module")
def create_app():
"""Create test app."""
return create_api
Loading

0 comments on commit 7d84ca8

Please sign in to comment.