Skip to content
Merged
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
10 changes: 10 additions & 0 deletions ckanext/activityinfo/assets/css/activityinfo.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
.btn {
margin: 7px;
}

.fa-activity-info-ico {
/* Use kobo official image */
background-image: url(/img/logo-activity-info.png);
background-size: 18px 18px;
height: 18px;
width: 18px;
content: "";
display: inline-block;
}
26 changes: 21 additions & 5 deletions ckanext/activityinfo/blueprints/activity_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@

@activityinfo_bp.route('/')
def index():
""" Home page
If the user has an API key, redirect to databases
If not, show a page to enter the API key
"""
extra_vars = {
'api_key': get_user_token(current_user.name),
}
if extra_vars['api_key']:
return toolkit.redirect_to('activity_info.databases')
return toolkit.render('activity_info/index.html', extra_vars)


@activityinfo_bp.route('/api-key')
def api_key():
""" Create or update the current ActivityInfo API key for the logged-in user.
"""
extra_vars = {
'api_key': get_user_token(current_user.name),
}
Expand All @@ -30,7 +46,7 @@ def databases():
message = f"Could not retrieve ActivityInfo databases: {e}"
log.error(message)
toolkit.h.flash_error(message)
return toolkit.redirect_to('activity_info.index')
return toolkit.redirect_to('activity_info.api_key')

log.info(f"Retrieved {ai_databases}")
# add the ActivityInfo URL to each database
Expand Down Expand Up @@ -110,7 +126,7 @@ def update_api_key():
message = 'Missing ActivityInfo API key.'
log.error(message)
toolkit.h.flash_error(message)
return toolkit.redirect_to('activity_info.index')
return toolkit.redirect_to('activity_info.api_key')

plugin_extras = get_activity_info_user_plugin_extras(toolkit.c.user) or {}
activity_info_extras = plugin_extras.get('activity_info', {})
Expand All @@ -125,7 +141,7 @@ def update_api_key():
}
)
toolkit.h.flash_success('ActivityInfo API key updated successfully.')
return toolkit.redirect_to('activity_info.index')
return toolkit.redirect_to('activity_info.api_key')


@activityinfo_bp.route('/remove-api-key', methods=['POST'])
Expand All @@ -134,7 +150,7 @@ def remove_api_key():
plugin_extras = get_activity_info_user_plugin_extras(toolkit.c.user) or {}
if not plugin_extras or 'activity_info' not in plugin_extras:
toolkit.h.flash_error('No ActivityInfo API key found to remove.')
return toolkit.redirect_to('activity_info.index')
return toolkit.redirect_to('activity_info.api_key')

activity_info_extras = plugin_extras.get('activity_info', {})
activity_info_extras.pop('api_key', None)
Expand All @@ -148,4 +164,4 @@ def remove_api_key():
}
)
toolkit.h.flash_success('ActivityInfo API key removed successfully.')
return toolkit.redirect_to('activity_info.index')
return toolkit.redirect_to('activity_info.api_key')
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions ckanext/activityinfo/templates/activity_info/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ <h2 class="module-heading"><i class="fa fa-line-chart"></i> Activity Info</h2>
<a href="{% url_for 'activity_info.databases' %}" title="My databases">My databases</a>
</li>
<li class="nav-item">
<a href="{% url_for 'activity_info.index' %}" title="Change my ActivityInfo API key">Change my ActivityInfo API key</a>
<a href="{% url_for 'activity_info.api_key' %}" title="Change my ActivityInfo API key">Change my ActivityInfo API key</a>
</li>
{% else %}
<li class="nav-item">
<a href="{% url_for 'activity_info.index' %}" title="Set my ActivityInfo API key">Set my ActivityInfo API key</a>
<a href="{% url_for 'activity_info.api_key' %}" title="Set my ActivityInfo API key">Set my ActivityInfo API key</a>
</li>
{% endif %}
</ul>
Expand Down
12 changes: 12 additions & 0 deletions ckanext/activityinfo/templates/user/read_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% ckan_extends %}

{% block content_primary_nav %}
{{ super() }}

{{ h.build_nav_icon(
'activity_info.index', 'Activity Info',
icon='activity-info-ico'
)
}}

{% endblock %}
29 changes: 23 additions & 6 deletions ckanext/activityinfo/tests/test_home.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest import mock
from types import SimpleNamespace
import pytest
from ckantoolkit.tests import factories as ckan_factories
Expand All @@ -22,13 +23,29 @@ def test_regular_user(self, app, setup_data):

resp = app.get("/activity-info", headers=environ)
assert resp.status_code == 200
# CKAN users without a registered ActivityInfo token, will be asked to add one
# This must be redirected to the /api-key URL
assert "Add API key" in resp.body

def test_activityinfo_user(self, app, setup_data):
environ = {"Authorization": setup_data.activityinfo_user["token"]}

resp = app.get("/activity-info", headers=environ)
assert resp.status_code == 200
# Activity info users will see the ActivityInfo UI
assert "Update API key" in resp.body
fake_databases = [
{
"databaseId": "0001",
"label": "Database label 01",
"description": "",
"ownerId": "999999999",
"billingAccountId": 8888888888888888,
"suspended": False,
"publishedTemplate": False,
"languages": []
},
]

with mock.patch(
"ckanext.activityinfo.data.base.ActivityInfoClient.get_databases",
return_value=fake_databases,
):
resp = app.get("/activity-info", headers=environ)
assert resp.status_code == 200
# Activity info users will be redirected to see their databases
assert "Activity Info databases" in resp.body
34 changes: 34 additions & 0 deletions ckanext/activityinfo/tests/test_home_api_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from types import SimpleNamespace
import pytest
from ckantoolkit.tests import factories as ckan_factories
from ckanext.activityinfo.tests import factories


@pytest.fixture
def setup_data():
"""test setup data"""
obj = SimpleNamespace()
# Create CKAN users
obj.activityinfo_user = factories.ActivityInfoUser()
obj.regular_user = ckan_factories.UserWithToken()

return obj


@pytest.mark.usefixtures("clean_db")
class TestActivityInfoUIApiKey:
def test_regular_user(self, app, setup_data):
environ = {"Authorization": setup_data.regular_user["token"]}

resp = app.get("/activity-info/api-key", headers=environ)
assert resp.status_code == 200
# CKAN users without a registered ActivityInfo token, will be asked to add one
assert "Add API key" in resp.body

def test_activityinfo_user(self, app, setup_data):
environ = {"Authorization": setup_data.activityinfo_user["token"]}

resp = app.get("/activity-info/api-key", headers=environ)
assert resp.status_code == 200
# Activity info users will be redirected to see their databases
assert "Update API key" in resp.body