Skip to content

Commit

Permalink
Fix api key default value (#82)
Browse files Browse the repository at this point in the history
* add failing spec

* fix adding api_key_default_value to headers

* update changelog

* bump rubocop to fix rake issue

* add geckodriver to fix issues with new selenium-webdriver

* fix specs

* change gem versions to keep ruby 2.0.0 compatibility
  • Loading branch information
Andrzej Piątyszek authored and dblock committed May 30, 2017
1 parent 34a4d36 commit f5ad4bc
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2015-08-19 12:23:54 -0400 using RuboCop version 0.33.0.
# on 2015-08-19 12:23:54 -0400 using RuboCop version 0.38.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
sudo: false

addons:
firefox: "latest"
before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- wget https://github.com/mozilla/geckodriver/releases/download/v0.16.1/geckodriver-v0.16.1-linux64.tar.gz
- mkdir geckodriver
- tar -xzf geckodriver-v0.16.1-linux64.tar.gz -C geckodriver
- export PATH=$PATH:$PWD/geckodriver
- gem install bundler

language: ruby

Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Next Release
============
### Next Release

* Your contribution here.
* [#82](https://github.com/ruby-grape/grape-swagger-rails/pull/82): Fixed api_key_default_value - [@konto-andrzeja](https://github.com/konto-andrzeja).

### 0.3.0 (September 22, 2016)

Expand Down
20 changes: 16 additions & 4 deletions app/views/grape_swagger_rails/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<%=raw "headers.header_#{index} = new SwaggerClient.ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header');" %>
<% end %>

<% if GrapeSwaggerRails.options.api_key_default_value.try(:strip).present? &&
GrapeSwaggerRails.options.headers['Authorization'].blank? %>
<% headers_count = GrapeSwaggerRails.options.headers.count %>
<%= raw "headers.header_#{headers_count} = getApiKeyAuthorization();" %>
<% end %>

window.swaggerUi = new SwaggerUi({
url: options.app_url + options.url,
dom_id: "swagger-ui-container",
Expand All @@ -28,7 +34,6 @@
console.log(swaggerUi);
}
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
addApiKeyAuthorization();
},
onFailure: function(data) {
if('console' in window) {
Expand All @@ -41,7 +46,7 @@
apisSorter: "alpha"
});

function addApiKeyAuthorization() {
function getApiKeyAuthorization() {
var key = $('#input_apiKey')[0].value;

if (key && key.trim() != "") {
Expand All @@ -50,9 +55,16 @@
} else if (options.api_auth == 'bearer') {
key = "Bearer " + key
}
window.swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization(options.api_key_name, key, options.api_key_type));
return new SwaggerClient.ApiKeyAuthorization(options.api_key_name, key, options.api_key_type);
}
}

function addApiKeyAuthorization() {
var apiKeyAuthorization = getApiKeyAuthorization();
if (apiKeyAuthorization) {
window.swaggerUi.api.clientAuthorizations.add("key", apiKeyAuthorization);
}
}
}

$('#input_apiKey').change(addApiKeyAuthorization);

Expand Down
5 changes: 3 additions & 2 deletions grape-swagger-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'railties', '>= 3.2.12'

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'bundler', '~> 1.15'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'git'
spec.add_development_dependency 'rspec-rails'
Expand All @@ -34,7 +34,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'sprockets'
spec.add_development_dependency 'rack', '~> 1.6'
spec.add_development_dependency 'rack-cors'
spec.add_development_dependency 'rubocop', '0.33.0'
spec.add_development_dependency 'rubocop', '0.38.0'
spec.add_development_dependency 'mime-types', '< 3.0'
spec.add_development_dependency 'rack-no_animations'
spec.add_development_dependency 'nokogiri', '< 1.7.0'
end
2 changes: 1 addition & 1 deletion lib/grape-swagger-rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GrapeSwaggerRails
VERSION = '0.3.0'
VERSION = '0.3.0'.freeze
end
4 changes: 2 additions & 2 deletions lib/tasks/swagger_ui.rake
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace :swagger_ui do
'object-assign-pollyfill.js',
'swagger-oauth.js',
'base64.js'
]
].freeze
javascript_files = Dir["#{root}/app/assets/javascripts/grape_swagger_rails/*.js"].map { |f|
f.split('/').last
} - ['application.js']
Expand All @@ -61,7 +61,7 @@ namespace :swagger_ui do
CSS_FILES = [
'reset.css',
'screen.css'
]
].freeze
css_files = Dir["#{root}/app/assets/stylesheets/grape_swagger_rails/*.css"].map { |f|
f.split('/').last
} - ['application.css']
Expand Down
19 changes: 17 additions & 2 deletions spec/features/swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

it 'evaluates config options correctly' do
visit '/swagger'
page_options_json = page.evaluate_script("$('html').data('swagger-options')").to_json
expect(page_options_json).to eq(@options.marshal_dump.to_json)
page_options = page.evaluate_script("$('html').data('swagger-options')").symbolize_keys
expect(page_options).to eq(@options.marshal_dump)
end

context '#headers' do
Expand Down Expand Up @@ -49,6 +49,21 @@
expect(page).to have_css 'span.hljs-string', text: 'Another Value'
end
end
context '#api_key_default_value' do
before do
GrapeSwaggerRails.options.api_auth = 'bearer'
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
GrapeSwaggerRails.options.api_key_default_value = 'token'
visit '/swagger'
end
it 'adds an Authorization header' do
headers = page.evaluate_script('swaggerUi.api.clientAuthorizations')['authz']
last_header = headers.fetch("header_#{headers.length - 1}", {})
expect(last_header.slice('name', 'value'))
.to eq('name' => 'Authorization', 'value' => 'Bearer token')
end
end
context '#api_auth:basic' do
before do
GrapeSwaggerRails.options.api_auth = 'basic'
Expand Down

0 comments on commit f5ad4bc

Please sign in to comment.