Skip to content

Commit

Permalink
Merge pull request #108 from xmera-circle/develop
Browse files Browse the repository at this point in the history
fix_for_failing_redmine_api_key_test
  • Loading branch information
mikitex70 authored Mar 9, 2022
2 parents b3502d6 + d1177ef commit 13658fd
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% flash[:warning] = l(:drawio_warning_api_needs_to_be_enabled) %>
1 change: 1 addition & 0 deletions config/locales/da.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ da:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ de:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: Um drawio Diagramme speichern zu können, muss die REST API unter Administration -> Konfiguration -> API aktiviert werden.
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ en:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
2 changes: 1 addition & 1 deletion config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ it:
drawio_dlg_page: "Pagina iniziale"
drawio_dlg_hiligh: "Colore degli hyperlink"
drawio_dlg_zoom: "Controlli zoom"

drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ ja:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
1 change: 1 addition & 0 deletions config/locales/ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ ko:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ ru:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
1 change: 1 addition & 0 deletions config/locales/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ zh-TW:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
1 change: 1 addition & 0 deletions config/locales/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ zh:
drawio_dlg_page: "Initial page"
drawio_dlg_hiligh: "Hyperlinks color"
drawio_dlg_zoom: "Zoom controls"
drawio_warning_api_needs_to_be_enabled: For saving drawio diagrams you need to enable the REST API in Administration -> Configuration -> API.
15 changes: 14 additions & 1 deletion lib/redmine_drawio/hooks/view_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

module RedmineDrawio

class ViewLayoutsBaseBodyTop < Redmine::Hook::ViewListener
def view_layouts_base_body_top(context = {})
return unless User.current.admin? && !Setting.rest_api_enabled?

context[:controller].send(:render_to_string, { partial: 'redmine_drawio/hooks/api_not_enabled_warning' })
end
end

class ViewLayoutsBaseHtmlHeadHook < Redmine::Hook::ViewListener

# This method will add the necessary CSS and JS scripts to the page header.
Expand Down Expand Up @@ -63,7 +71,7 @@ def view_layouts_base_html_head(context={})
var Drawio = {
settings: {
redmineUrl: '#{redmine_url}',
hashCode : '#{Base64.encode64(User.current.api_key).gsub(/\n/, '').reverse!}',
hashCode : '#{hash_code}',
drawioUrl : '#{drawio_url}',
DMSF : #{dmsf_enabled? context},
isEasyRedmine: #{easyredmine?}
Expand Down Expand Up @@ -132,6 +140,11 @@ def mathjax_url
url
end

def hash_code
return '' unless Setting.rest_api_enabled?

Base64.encode64(User.current.api_key).gsub(/\n/, '').reverse!
end
end

end
59 changes: 59 additions & 0 deletions test/integration/view_hooks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

# Copyright (C) 2022 Liane Hampe <[email protected]>, xmera.

require File.expand_path('test_helper', File.dirname(__dir__))
require File.expand_path('authenticate_user', File.dirname(__dir__))
require File.expand_path('load_fixtures', File.dirname(__dir__))
require File.expand_path('with_drawio_settings', File.dirname(__dir__))

class ViewHooksTest < ActionDispatch::IntegrationTest
include Redmine::I18n
include RedmineDrawio::AuthenticateUser
include RedmineDrawio::LoadFixtures
include RedmineDrawio::WithDrawioSettings

fixtures :users, :email_addresses, :roles

def setup
@hook = RedmineDrawio::ViewLayoutsBaseHtmlHeadHook.instance
end

def teardown
Setting.rest_api_enabled = nil
end

test 'render warning_api_needs_to_be_enabled when api is disabled' do
render_view_hooks(user: 'admin', password: 'admin')
assert_select '#flash_warning', text: l(:drawio_warning_api_needs_to_be_enabled)
end

test 'do not render warning_api_needs_to_be_enabled when api is enabled' do
render_view_hooks(user: 'admin', password: 'admin', rest_api_enabled: '1')
assert_select '#flash_warning', 0
end

test 'do not render warning_api_needs_to_be_enabled for non admin user' do
render_view_hooks(user: 'jsmith', password: 'jsmith', rest_api_enabled: '1')
assert_select '#flash_warning', 0
end

test 'do not render hash code when api is disabled' do
render_view_hooks(user: 'admin', password: 'admin')
assert @hook.send(:hash_code).blank?
end

test 'render hash code when api is enabled' do
render_view_hooks(user: 'admin', password: 'admin', rest_api_enabled: '1')
assert @hook.send(:hash_code).present?
end

private

def render_view_hooks(user:, password:, rest_api_enabled: '0')
Setting.rest_api_enabled = rest_api_enabled
log_user(user, password)
get '/'
assert_response :success
end
end

0 comments on commit 13658fd

Please sign in to comment.