foreman_webhooks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +619 -0
  3. data/README.md +30 -0
  4. data/Rakefile +49 -0
  5. data/app/controllers/api/v2/webhook_templates_controller.rb +110 -0
  6. data/app/controllers/api/v2/webhooks_controller.rb +61 -0
  7. data/app/controllers/concerns/foreman_webhooks/controller/parameters/webhook.rb +34 -0
  8. data/app/controllers/concerns/foreman_webhooks/controller/parameters/webhook_template.rb +34 -0
  9. data/app/controllers/webhook_templates_controller.rb +5 -0
  10. data/app/controllers/webhooks_controller.rb +39 -0
  11. data/app/jobs/foreman_webhooks/deliver_webhook_job.rb +27 -0
  12. data/app/lib/foreman_webhooks/renderer/scope/webhook_template.rb +36 -0
  13. data/app/models/webhook.rb +141 -0
  14. data/app/models/webhook_template.rb +59 -0
  15. data/app/services/foreman_webhooks/webhook_service.rb +111 -0
  16. data/app/subscribers/foreman_webhooks/event_subscriber.rb +9 -0
  17. data/app/views/api/v2/webhook_templates/base.json.rabl +5 -0
  18. data/app/views/api/v2/webhook_templates/create.json.rabl +5 -0
  19. data/app/views/api/v2/webhook_templates/index.json.rabl +5 -0
  20. data/app/views/api/v2/webhook_templates/main.json.rabl +7 -0
  21. data/app/views/api/v2/webhook_templates/show.json.rabl +11 -0
  22. data/app/views/api/v2/webhook_templates/update.json.rabl +5 -0
  23. data/app/views/api/v2/webhooks/base.json.rabl +5 -0
  24. data/app/views/api/v2/webhooks/create.json.rabl +5 -0
  25. data/app/views/api/v2/webhooks/index.json.rabl +5 -0
  26. data/app/views/api/v2/webhooks/main.json.rabl +8 -0
  27. data/app/views/api/v2/webhooks/show.json.rabl +17 -0
  28. data/app/views/api/v2/webhooks/update.json.rabl +5 -0
  29. data/app/views/foreman_webhooks/webhook_templates/ansible_tower_-_host_in_inventory.erb +20 -0
  30. data/app/views/foreman_webhooks/webhook_templates/empty_payload.erb +6 -0
  31. data/app/views/foreman_webhooks/webhook_templates/webhook_template_-_payload_default.erb +11 -0
  32. data/app/views/webhook_templates/_alerts.html.erb +3 -0
  33. data/app/views/webhook_templates/_custom_tab_headers.html.erb +1 -0
  34. data/app/views/webhook_templates/_custom_tabs.html.erb +5 -0
  35. data/app/views/webhook_templates/edit.html.erb +3 -0
  36. data/app/views/webhook_templates/index.html.erb +29 -0
  37. data/app/views/webhook_templates/new.html.erb +3 -0
  38. data/app/views/webhooks/_form.html.erb +37 -0
  39. data/app/views/webhooks/_templates.html.erb +5 -0
  40. data/app/views/webhooks/edit.html.erb +3 -0
  41. data/app/views/webhooks/new.html.erb +3 -0
  42. data/config/routes.rb +45 -0
  43. data/db/migrate/20191016100128_create_webhook_targets.rb +13 -0
  44. data/db/migrate/20200831194208_rename_webhook_targets_to_webhooks.rb +7 -0
  45. data/db/migrate/20200831194514_add_template_to_webhooks.rb +7 -0
  46. data/db/migrate/20200907232758_rename_webhook_permissions.rb +22 -0
  47. data/db/migrate/20200908004234_add_columns_to_webhooks.rb +13 -0
  48. data/db/migrate/20201014115147_rename_ca_file_column.rb +7 -0
  49. data/db/migrate/20201109135301_add_http_headers.rb +8 -0
  50. data/db/seeds.d/95_webhook_templates.rb +7 -0
  51. data/lib/foreman_webhooks.rb +6 -0
  52. data/lib/foreman_webhooks/engine.rb +80 -0
  53. data/lib/foreman_webhooks/version.rb +5 -0
  54. data/lib/tasks/foreman_webhooks_tasks.rake +47 -0
  55. data/package.json +45 -0
  56. data/test/controllers/api/v2/webhook_templates_controller_test.rb +200 -0
  57. data/test/controllers/api/v2/webhooks_controller_test.rb +108 -0
  58. data/test/factories/webhook.rb +20 -0
  59. data/test/factories/webhook_target.rb +9 -0
  60. data/test/factories/webhook_template.rb +16 -0
  61. data/test/jobs/foreman_webhooks/deliver_webhook_job_test.rb +17 -0
  62. data/test/models/webhook_test.rb +38 -0
  63. data/test/test_plugin_helper.rb +8 -0
  64. data/test/unit/foreman_webhooks/webhook_service_test.rb +53 -0
  65. data/webpack/ForemanWebhooks/Routes/ForemanWebhooksRoutes.js +12 -0
  66. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/EmptyWebhooksTable/index.js +29 -0
  67. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhookDeleteModal.js +43 -0
  68. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/EnabledCell.js +16 -0
  69. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/Formatters/__tests__/__snapshots__/enabledCellFormatter.test.js.snap +7 -0
  70. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/Formatters/__tests__/enabledCellFormatter.test.js +7 -0
  71. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/Formatters/enabledCellFormatter.js +6 -0
  72. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/Formatters/index.js +1 -0
  73. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/__tests__/EnabledCell.test.js +14 -0
  74. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/Components/__tests__/__snapshots__/EnabledCell.test.js.snap +17 -0
  75. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/WebhooksTable.js +75 -0
  76. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/WebhooksTableSchema.js +41 -0
  77. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/__tests__/WebhooksTable.test.js +57 -0
  78. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/__tests__/__snapshots__/WebhooksTable.test.js.snap +115 -0
  79. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/Components/WebhooksTable/index.js +25 -0
  80. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/WebhooksIndexPage.js +87 -0
  81. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/__tests__/WebhooksIndexPage.fixtures.js +74 -0
  82. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/__tests__/WebhooksIndexPage.test.js +21 -0
  83. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/__tests__/__snapshots__/WebhooksIndexPage.test.js.snap +68 -0
  84. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksIndexPage/index.js +51 -0
  85. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksPageActions.js +51 -0
  86. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksPageHelpers.js +29 -0
  87. data/webpack/ForemanWebhooks/Routes/Webhooks/WebhooksPageSelectors.js +85 -0
  88. data/webpack/ForemanWebhooks/Routes/Webhooks/__tests__/WebhooksPageHelpers.test.js +20 -0
  89. data/webpack/ForemanWebhooks/Routes/Webhooks/__tests__/WebhooksPageSelectors.test.js +45 -0
  90. data/webpack/ForemanWebhooks/Routes/Webhooks/__tests__/__snapshots__/WebhooksPageSelectors.test.js.snap +50 -0
  91. data/webpack/ForemanWebhooks/Routes/Webhooks/constants.js +13 -0
  92. data/webpack/__mocks__/foremanReact/common/HOC.js +2 -0
  93. data/webpack/__mocks__/foremanReact/common/I18n.js +7 -0
  94. data/webpack/__mocks__/foremanReact/common/helpers.js +7 -0
  95. data/webpack/__mocks__/foremanReact/common/urlHelpers.js +1 -0
  96. data/webpack/__mocks__/foremanReact/components/ForemanModal/ForemanModalActions.js +2 -0
  97. data/webpack/__mocks__/foremanReact/components/ForemanModal/ForemanModalHooks.js +10 -0
  98. data/webpack/__mocks__/foremanReact/components/ForemanModal/index.js +18 -0
  99. data/webpack/__mocks__/foremanReact/components/Layout/LayoutActions.js +2 -0
  100. data/webpack/__mocks__/foremanReact/components/Pagination/PaginationWrapper.js +2 -0
  101. data/webpack/__mocks__/foremanReact/components/common/EmptyState.js +5 -0
  102. data/webpack/__mocks__/foremanReact/components/common/table.js +5 -0
  103. data/webpack/__mocks__/foremanReact/constants.js +24 -0
  104. data/webpack/__mocks__/foremanReact/redux/API/APISelectors.js +6 -0
  105. data/webpack/__mocks__/foremanReact/redux/API/index.js +10 -0
  106. data/webpack/__mocks__/foremanReact/redux/actions/toasts.js +8 -0
  107. data/webpack/__mocks__/foremanReact/routes/common/PageLayout/PageLayout.js +10 -0
  108. data/webpack/index.js +0 -0
  109. data/webpack/routes_index.js +4 -0
  110. metadata +195 -0
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateWebhookTargets < ActiveRecord::Migration[5.2]
4
+ def change
5
+ create_table :webhook_targets do |t|
6
+ t.string :name, null: false
7
+ t.string :target_url, null: false
8
+ t.string :events, null: false, array: true, index: true
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RenameWebhookTargetsToWebhooks < ActiveRecord::Migration[6.0]
4
+ def change
5
+ rename_table :webhook_targets, :webhooks
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddTemplateToWebhooks < ActiveRecord::Migration[6.0]
4
+ def change
5
+ add_reference :webhooks, :webhook_template, foreign_key: { to_table: :templates }
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RenameWebhookPermissions < ActiveRecord::Migration[6.0]
4
+ PERMISSIONS = {
5
+ 'view_webhook_targets' => 'view_webhooks',
6
+ 'create_webhook_targets' => 'create_webhooks',
7
+ 'edit_webhook_targets' => 'edit_webhooks',
8
+ 'destroy_webhook_targets' => 'destroy_webhooks'
9
+ }.freeze
10
+
11
+ def up
12
+ PERMISSIONS.each do |from, to|
13
+ Permission.unscoped.find_by(name: from)&.update_columns(name: to)
14
+ end
15
+ end
16
+
17
+ def down
18
+ PERMISSIONS.each do |from, to|
19
+ Permission.unscoped.find_by(name: to)&.update_columns(name: from)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddColumnsToWebhooks < ActiveRecord::Migration[6.0]
4
+ def change
5
+ add_column :webhooks, :http_method, :string, default: 'POST', null: false
6
+ add_column :webhooks, :http_content_type, :string, default: 'application/json', null: false
7
+ add_column :webhooks, :enabled, :boolean, default: true
8
+ add_column :webhooks, :verify_ssl, :boolean, default: true
9
+ add_column :webhooks, :ssl_ca_file, :text, null: true
10
+ add_column :webhooks, :user, :string, null: true
11
+ add_column :webhooks, :password, :text, null: true
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RenameCaFileColumn < ActiveRecord::Migration[6.0]
4
+ def change
5
+ rename_column :webhooks, :ssl_ca_file, :ssl_ca_certs
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddHttpHeaders < ActiveRecord::Migration[6.0]
4
+ def change
5
+ # HTTP headers stored as JSON: {"header_name": "value"}
6
+ add_column :webhooks, :http_headers, :text, null: true
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ WebhookTemplate.without_auditing do
4
+ SeedHelper.import_templates(
5
+ Dir[File.join("#{ForemanWebhooks::Engine.root}/app/views/foreman_webhooks/webhook_templates/**/*.erb")]
6
+ )
7
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'foreman_webhooks/engine'
4
+
5
+ module ForemanWebhooks
6
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanWebhooks
4
+ class Engine < ::Rails::Engine
5
+ engine_name 'foreman_webhooks'
6
+
7
+ config.autoload_paths += Dir["#{config.root}/app/jobs"]
8
+
9
+ # Add any db migrations
10
+ initializer 'foreman_webhooks.load_app_instance_data' do |app|
11
+ ForemanWebhooks::Engine.paths['db/migrate'].existent.each do |path|
12
+ app.config.paths['db/migrate'] << path
13
+ end
14
+ end
15
+
16
+ initializer 'foreman_webhooks.register_plugin', before: :finisher_hook do |_app|
17
+ Foreman::Plugin.register :foreman_webhooks do
18
+ requires_foreman '>= 2.3'
19
+
20
+ apipie_documented_controllers ["#{ForemanWebhooks::Engine.root}/app/controllers/api/v2/*.rb"]
21
+ ApipieDSL.configuration.sections += ['webhooks']
22
+ ApipieDSL.configuration.dsl_classes_matchers += [
23
+ "#{ForemanWebhooks::Engine.root}/app/lib/foreman_webhooks/renderer/**/*.rb"
24
+ ]
25
+
26
+ register_global_js_file 'routes'
27
+
28
+ # Add permissions
29
+ security_block :foreman_webhooks do
30
+ permission :view_webhooks, { webhooks: %i[index show auto_complete_search],
31
+ 'api/v2/webhooks': %i[index show] }, resource_type: 'Webhook'
32
+ permission :create_webhooks, { webhooks: %i[new create],
33
+ 'api/v2/webhooks': [:create] }, resource_type: 'Webhook'
34
+ permission :edit_webhooks, { webhooks: %i[edit update],
35
+ 'api/v2/webhooks': [:update] }, resource_type: 'Webhook'
36
+ permission :destroy_webhooks, { webhooks: [:destroy],
37
+ 'api/v2/webhooks': [:destroy] }, resource_type: 'Webhook'
38
+ permission :view_webhook_templates, { webhook_templates: %i[index show auto_complete_search preview export],
39
+ 'api/v2/webhook_templates': %i[index show export] },
40
+ resource_type: 'WebhookTemplate'
41
+ permission :create_webhook_templates, { webhook_templates: %i[new create clone_template],
42
+ 'api/v2/webhook_templates': %i[create clone import] },
43
+ resource_type: 'WebhookTemplate'
44
+ permission :edit_webhook_templates, { webhook_templates: %i[edit update],
45
+ 'api/v2/webhook_templates': %i[update import] },
46
+ resource_type: 'WebhookTemplate'
47
+ permission :destroy_webhook_templates, { webhook_templates: [:destroy],
48
+ 'api/v2/webhook_templates': [:destroy] },
49
+ resource_type: 'WebhookTemplate'
50
+ permission :lock_webhook_templates, { webhook_templates: %i[lock unlock],
51
+ 'api/v2/webhook_templates': %i[lock unlock] },
52
+ resource_type: 'WebhookTemplate'
53
+ end
54
+
55
+ # add menu entry
56
+ divider :admin_menu, caption: N_('Webhook'), parent: :administer_menu
57
+ menu :admin_menu, :webhooks, url: '/webhooks',
58
+ url_hash: { controller: :webhooks, action: :index },
59
+ caption: N_('Webhooks'),
60
+ parent: :administer_menu
61
+ menu :admin_menu, :webhook_templates, url_hash: { controller: :webhook_templates, action: :index },
62
+ caption: N_('Webhook Templates'),
63
+ parent: :administer_menu
64
+
65
+ # add helpers to safe-mode
66
+ allowed_template_helpers :payload
67
+
68
+ # subscribe to all events
69
+ subscribe(/.event.foreman$/, ::ForemanWebhooks::EventSubscriber)
70
+ end
71
+ end
72
+
73
+ # Include concerns in this config.to_prepare block
74
+ config.to_prepare do
75
+ # Host::Managed.send(:include, ForemanWebhooks::HostExtensions)
76
+ rescue StandardError => e
77
+ Rails.logger.warn "ForemanWebhooks: skipping engine hook (#{e})"
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanWebhooks
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake/testtask'
4
+
5
+ # Tasks
6
+ namespace :foreman_webhooks do
7
+ namespace :example do
8
+ desc 'Example Task'
9
+ task task: :environment do
10
+ # Task goes here
11
+ end
12
+ end
13
+ end
14
+
15
+ # Tests
16
+ namespace :test do
17
+ desc 'Test ForemanWebhooks'
18
+ Rake::TestTask.new(:foreman_webhooks) do |t|
19
+ test_dir = File.join(File.dirname(__FILE__), '../..', 'test')
20
+ t.libs << ['test', test_dir]
21
+ t.pattern = "#{test_dir}/**/*_test.rb"
22
+ t.verbose = true
23
+ t.warning = false
24
+ end
25
+ end
26
+
27
+ namespace :foreman_webhooks do
28
+ task :rubocop do
29
+ begin
30
+ require 'rubocop/rake_task'
31
+ RuboCop::RakeTask.new(:rubocop_foreman_webhooks) do |task|
32
+ task.patterns = ["#{ForemanWebhooks::Engine.root}/app/**/*.rb",
33
+ "#{ForemanWebhooks::Engine.root}/lib/**/*.rb",
34
+ "#{ForemanWebhooks::Engine.root}/test/**/*.rb"]
35
+ end
36
+ rescue StandardError
37
+ puts 'Rubocop not loaded.'
38
+ end
39
+
40
+ Rake::Task['rubocop_foreman_webhooks'].invoke
41
+ end
42
+ end
43
+
44
+ Rake::Task[:test].enhance ['test:foreman_webhooks']
45
+
46
+ load 'tasks/jenkins.rake'
47
+ Rake::Task['jenkins:unit'].enhance ['test:foreman_webhooks', 'foreman_webhooks:rubocop'] if Rake::Task.task_defined?(:'jenkins:unit')
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "foreman_webhooks",
3
+ "version": "0.0.1",
4
+ "description": "This plugins allows to configure Webhooks for Foreman.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "lint": "tfm-lint --plugin -d /webpack",
8
+ "test": "tfm-test --plugin",
9
+ "test:watch": "tfm-test --plugin --watchAll",
10
+ "test:current": "tfm-test --plugin --watch",
11
+ "publish-coverage": "tfm-publish-coverage",
12
+ "stories": "tfm-stories --plugin",
13
+ "stories:build": "tfm-build-stories --plugin",
14
+ "create-react-component": "yo react-domain"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/theforeman/foreman_webhooks.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://projects.theforeman.org/projects/webhooks/issues"
22
+ },
23
+ "peerDependencies": {
24
+ "@theforeman/vendor": ">= 4.15.1"
25
+ },
26
+ "devDependencies": {
27
+ "@babel/core": "^7.7.0",
28
+ "@theforeman/builder": "^4.15.1",
29
+ "@theforeman/stories": "^4.15.1",
30
+ "@theforeman/test": "^4.15.1",
31
+ "@theforeman/vendor-dev": "^4.15.1",
32
+ "@theforeman/eslint-plugin-foreman": "^4.15.1",
33
+ "babel-eslint": "^10.0.0",
34
+ "eslint": "^6.8.0",
35
+ "eslint-plugin-spellcheck": "^0.0.17",
36
+ "prettier": "^1.13.5",
37
+ "stylelint": "^9.3.0",
38
+ "stylelint-config-standard": "^18.0.0",
39
+ "surge": "^0.20.3"
40
+ },
41
+ "dependencies": {
42
+ "jed": "^1.1.1",
43
+ "react-intl": "^2.8.0"
44
+ }
45
+ }
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class Api::V2::WebhookTemplatesControllerTest < ActionController::TestCase
6
+ valid_attrs = { name: 'webhook_template_test', template: 'a,b,c' }
7
+
8
+ def setup
9
+ @webhook_template = FactoryBot.create(:webhook_template)
10
+ end
11
+
12
+ let(:webhook_template) { FactoryBot.create(:webhook_template) }
13
+
14
+ test 'should get index' do
15
+ get :index
16
+ assert_response :success
17
+ assert_not_nil assigns(:webhook_templates)
18
+ webhook_templates = ActiveSupport::JSON.decode(@response.body)
19
+ assert !webhook_templates.empty?
20
+ template = webhook_templates['results'].find { |h| h['id'] == @webhook_template.id }
21
+ assert_equal @webhook_template.name, template['name']
22
+ end
23
+
24
+ test 'should show individual record' do
25
+ get :show, params: { id: @webhook_template.to_param }
26
+ assert_response :success
27
+ show_response = ActiveSupport::JSON.decode(@response.body)
28
+ assert !show_response.empty?
29
+ assert_equal @webhook_template['template'], show_response['template']
30
+ end
31
+
32
+ test 'should create webhook_template' do
33
+ assert_difference('WebhookTemplate.unscoped.count') do
34
+ post :create, params: { webhook_template: valid_attrs }
35
+ end
36
+ assert_response :created
37
+ response = JSON.parse(@response.body)
38
+ assert response.key?('name')
39
+ assert response.key?('template')
40
+ assert_equal response['name'], valid_attrs[:name]
41
+ assert_equal response['template'], valid_attrs[:template]
42
+ end
43
+
44
+ test 'create with template length' do
45
+ valid_params = valid_attrs.merge(template: RFauxFactory.gen_alpha(5000))
46
+ assert_difference('WebhookTemplate.unscoped.count') do
47
+ post :create, params: { webhook_template: valid_params }
48
+ end
49
+ assert_response :created
50
+ response = JSON.parse(@response.body)
51
+ assert response.key?('template')
52
+ assert_equal response['template'], valid_params[:template]
53
+ end
54
+
55
+ test 'create with one character name' do
56
+ valid_params = valid_attrs.merge(name: RFauxFactory.gen_alpha(1))
57
+ assert_difference('WebhookTemplate.unscoped.count') do
58
+ post :create, params: { webhook_template: valid_params }
59
+ end
60
+ assert_response :created
61
+ response = JSON.parse(@response.body)
62
+ assert response.key?('name')
63
+ assert_equal response['name'], valid_params[:name]
64
+ end
65
+
66
+ test 'should create webhook_template with organization' do
67
+ organization_id = Organization.first.id
68
+ assert_difference('WebhookTemplate.unscoped.count') do
69
+ post :create, params: { webhook_template: valid_attrs.merge(organization_ids: [organization_id]) }
70
+ end
71
+ assert_response :created
72
+ response = JSON.parse(@response.body)
73
+ assert response.key?('organizations')
74
+ organization_ids = response['organizations'].map { |org| org['id'] }
75
+ assert_equal organization_ids.length, 1
76
+ assert_include organization_ids, organization_id
77
+ end
78
+
79
+ test 'should update name' do
80
+ new_name = 'new webhook_template name'
81
+ put :update, params: { id: @webhook_template.id, webhook_template: { name: new_name } }
82
+ assert_response :success
83
+ response = JSON.parse(@response.body)
84
+ assert response.key?('name')
85
+ assert_equal response['name'], new_name
86
+ end
87
+
88
+ test 'should update template' do
89
+ new_template = 'new webhook_template template'
90
+ put :update, params: { id: @webhook_template.id, webhook_template: { template: new_template } }
91
+ assert_response :success
92
+ response = JSON.parse(@response.body)
93
+ assert response.key?('template')
94
+ assert_equal response['template'], new_template
95
+ end
96
+
97
+ test 'should not create with invalid name' do
98
+ assert_difference('WebhookTemplate.unscoped.count', 0) do
99
+ post :create, params: { webhook_template: valid_attrs.merge(name: '') }
100
+ end
101
+ assert_response :unprocessable_entity
102
+ end
103
+
104
+ test 'should not create with invalid template' do
105
+ assert_difference('WebhookTemplate.unscoped.count', 0) do
106
+ post :create, params: { webhook_template: valid_attrs.merge(template: '') }
107
+ end
108
+ assert_response :unprocessable_entity
109
+ end
110
+
111
+ test 'should not update with invalid name' do
112
+ put :update, params: { id: @webhook_template.id, webhook_template: { name: '' } }
113
+ assert_response :unprocessable_entity
114
+ end
115
+
116
+ test 'should not update with invalid template' do
117
+ put :update, params: { id: @webhook_template.id, webhook_template: { template: '' } }
118
+ assert_response :unprocessable_entity
119
+ end
120
+
121
+ test 'search webhook_template' do
122
+ get :index, params: { search: @webhook_template.name, format: 'json' }
123
+ assert_response :success, "search webhook_template name: '#{@webhook_template.name}'" \
124
+ " failed with code: #{@response.code}"
125
+ response = JSON.parse(@response.body)
126
+ assert_equal response['results'].length, 1
127
+ assert_equal response['results'][0]['id'], @webhook_template.id
128
+ end
129
+
130
+ test 'search webhook_template by name and organization' do
131
+ org = Organization.first
132
+ @webhook_template.organizations = [org]
133
+ assert @webhook_template.save
134
+ get :index, params: { search: @webhook_template.name, organization_id: org.id, format: 'json' }
135
+ assert_response :success, "search webhook_template by name and organization failed with code: #{@response.code}"
136
+ response = JSON.parse(@response.body)
137
+ assert_equal response['results'].length, 1
138
+ assert_equal response['results'][0]['id'], @webhook_template.id
139
+ end
140
+
141
+ test "should created webhook_template with unwrapped 'template'" do
142
+ assert_difference('WebhookTemplate.unscoped.count') do
143
+ post :create, params: valid_attrs
144
+ end
145
+ assert_response :created
146
+ end
147
+
148
+ test 'should update webhook_template' do
149
+ put :update, params: { id: @webhook_template.to_param, webhook_template: valid_attrs }
150
+ assert_response :success
151
+ end
152
+
153
+ test 'should destroy webhook_template' do
154
+ assert_difference('WebhookTemplate.unscoped.count', -1) do
155
+ delete :destroy, params: { id: @webhook_template.to_param }
156
+ end
157
+ assert_response :success
158
+ end
159
+
160
+ test 'should add audit comment' do
161
+ WebhookTemplate.auditing_enabled = true
162
+ WebhookTemplate.any_instance.stubs(:valid?).returns(true)
163
+ webhook_template = FactoryBot.create(:webhook_template)
164
+ put :update, params: { id: webhook_template.to_param,
165
+ webhook_template: { audit_comment: 'aha', template: 'tmp' } }
166
+ assert_response :success
167
+ assert_equal 'aha', webhook_template.audits.last.comment
168
+ end
169
+
170
+ test 'should clone template' do
171
+ original_webhook_template = FactoryBot.create(:webhook_template)
172
+ post :clone, params: { id: original_webhook_template.to_param,
173
+ webhook_template: { name: 'MyClone' } }
174
+ assert_response :success
175
+ template = ActiveSupport::JSON.decode(@response.body)
176
+ assert_equal(template['name'], 'MyClone')
177
+ assert_equal(template['template'], original_webhook_template.template)
178
+ refute_equal(template['id'], original_webhook_template.id)
179
+ end
180
+
181
+ test 'export should export the erb of the template' do
182
+ get :export, params: { id: webhook_template.to_param }
183
+ assert_response :success
184
+ assert_equal 'text/plain', response.media_type
185
+ assert_equal webhook_template.to_erb, response.body
186
+ end
187
+
188
+ test 'clone name should not be blank' do
189
+ post :clone, params: { id: FactoryBot.create(:webhook_template).to_param,
190
+ webhook_template: { name: '' } }
191
+ assert_response :unprocessable_entity
192
+ end
193
+
194
+ test 'should import webhook template' do
195
+ webhook_template = FactoryBot.create(:webhook_template, template: 'a')
196
+ post :import, params: { webhook_template: { name: webhook_template.name, template: 'b' } }
197
+ assert_response :success
198
+ assert_equal 'b', WebhookTemplate.unscoped.find_by_name(webhook_template.name).template
199
+ end
200
+ end