From 15accdc2192b7aad12a86d2d1e0e6d7215c8fe1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Mon, 31 Oct 2016 12:53:06 +0100 Subject: [PATCH 1/9] Add helper methods for mapping account stores and enabling email verification --- spec/dummy/config/stormpath.yml | 2 +- spec/features/email_verification_spec.rb | 16 +++++++++++----- spec/support/config_spec_helpers.rb | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/spec/dummy/config/stormpath.yml b/spec/dummy/config/stormpath.yml index ae865da..cd9cedd 100644 --- a/spec/dummy/config/stormpath.yml +++ b/spec/dummy/config/stormpath.yml @@ -53,7 +53,7 @@ stormpath: enabled: true uri: "/register" nextUri: "/" - autoLogin: false + autoLogin: true form: fields: givenName: diff --git a/spec/features/email_verification_spec.rb b/spec/features/email_verification_spec.rb index 8b7cf5d..183e78c 100644 --- a/spec/features/email_verification_spec.rb +++ b/spec/features/email_verification_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' describe 'the email verification feature', type: :feature, vcr: true do - let(:verify_email_config) { configuration.web.verify_email } + let(:application) do + Stormpath::Rails::Client.client.applications.create(name: 'rails integration app') + end let(:test_dir_with_verification) do - Stormpath::Rails::Client.client.directories.get( - ENV.fetch('STORMPATH_SDK_TEST_DIRECTORY_WITH_VERIFICATION_URL') - ) + Stormpath::Rails::Client.client.directories.create(name: 'rails test dir with verification') end let(:account) { test_dir_with_verification.accounts.create(account_attrs) } @@ -16,12 +16,18 @@ let(:sptoken) { account.email_verification_token.token } before do + enable_email_verification_for(test_dir_with_verification) + map_account_store(application, test_dir_with_verification, 0, true, false) account enable_email_verification Rails.application.reload_routes! end - after { account.delete } + after do + application.delete + test_dir_with_verification.delete + account.delete + end describe 'GET /verify' do describe 'with no sptoken' do diff --git a/spec/support/config_spec_helpers.rb b/spec/support/config_spec_helpers.rb index 9b97833..2335a65 100644 --- a/spec/support/config_spec_helpers.rb +++ b/spec/support/config_spec_helpers.rb @@ -74,4 +74,21 @@ def reload_form_class Stormpath::Rails.send(:remove_const, 'RegistrationForm') if defined?(Stormpath::Rails::RegistrationForm) load('stormpath/rails/registration_form.rb') end + + def map_account_store(app, store, index, default_account_store, default_group_store) + Stormpath::Rails::Client.client.account_store_mappings.create( + application: app, + account_store: store, + list_index: index, + is_default_account_store: default_account_store, + is_default_group_store: default_group_store + ) + end + + def enable_email_verification_for(directory) + directory.account_creation_policy.verification_email_status = 'ENABLED' + directory.account_creation_policy.verification_success_email_status = 'ENABLED' + directory.account_creation_policy.welcome_email_status = 'ENABLED' + directory.account_creation_policy.save + end end From bb1b78111bfcb72bc1e6ea63bcde7013f481b0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Mon, 31 Oct 2016 16:26:11 +0100 Subject: [PATCH 2/9] Remove all uses of the base env variables in code --- Gemfile | 1 + app/services/stormpath/rails/token_cookie_setter.rb | 2 +- spec/dummy/config/stormpath.yml | 2 +- spec/features/email_verification_spec.rb | 7 ++++--- spec/services/token_cookie_setter_spec.rb | 2 +- spec/spec_helper.rb | 6 ------ stormpath-rails.gemspec | 2 +- 7 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index f7c25da..d43f97e 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ gemspec gem 'sqlite3', '~> 1.3' gem 'sass-rails' +gem 'stormpath-sdk', path: './../ruby' group :development do gem 'pry' diff --git a/app/services/stormpath/rails/token_cookie_setter.rb b/app/services/stormpath/rails/token_cookie_setter.rb index 7e11676..fdf8294 100644 --- a/app/services/stormpath/rails/token_cookie_setter.rb +++ b/app/services/stormpath/rails/token_cookie_setter.rb @@ -58,7 +58,7 @@ def cookie_data end def expires - Time.zone.at(JWT.decode(token, ENV['STORMPATH_API_KEY_SECRET']).first['exp']) + Time.zone.at(JWT.decode(token, Stormpath::Rails::Client.client.data_store.api_key.secret).first['exp']) end def http_only diff --git a/spec/dummy/config/stormpath.yml b/spec/dummy/config/stormpath.yml index cd9cedd..ae865da 100644 --- a/spec/dummy/config/stormpath.yml +++ b/spec/dummy/config/stormpath.yml @@ -53,7 +53,7 @@ stormpath: enabled: true uri: "/register" nextUri: "/" - autoLogin: true + autoLogin: false form: fields: givenName: diff --git a/spec/features/email_verification_spec.rb b/spec/features/email_verification_spec.rb index 183e78c..c7595b5 100644 --- a/spec/features/email_verification_spec.rb +++ b/spec/features/email_verification_spec.rb @@ -17,10 +17,10 @@ before do enable_email_verification_for(test_dir_with_verification) - map_account_store(application, test_dir_with_verification, 0, true, false) - account + map_account_store(application, test_dir_with_verification, 0, true, true) enable_email_verification Rails.application.reload_routes! + account end after do @@ -84,7 +84,8 @@ allow(configuration.web.register).to receive(:auto_login).and_return(true) end - it 'redirects to root and sets cookies' do + xit 'redirects to root and sets cookies' do + # TODO: important to fix visit "verify?sptoken=#{sptoken}" expect(current_path).to eq('/') diff --git a/spec/services/token_cookie_setter_spec.rb b/spec/services/token_cookie_setter_spec.rb index 0eb8e2a..10204c5 100644 --- a/spec/services/token_cookie_setter_spec.rb +++ b/spec/services/token_cookie_setter_spec.rb @@ -26,7 +26,7 @@ end def expiration_from_token(token) - Time.zone.at(JWT.decode(token, ENV['STORMPATH_API_KEY_SECRET']).first['exp']) + Time.zone.at(JWT.decode(token, Stormpath::Rails::Client.client.data_store.api_key.secret).first['exp']) end describe 'default setup' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0b5d048..f32f53a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -41,12 +41,6 @@ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes' c.hook_into :webmock c.configure_rspec_metadata! - c.ignore_request do |request| - request.uri == Stormpath::Rails.config.application.href - end - c.ignore_request do |request| - request.uri == ENV['STORMPATH_SDK_TEST_DIRECTORY_WITH_VERIFICATION_URL'] - end end RSpec.configure do |config| diff --git a/stormpath-rails.gemspec b/stormpath-rails.gemspec index 23b6c01..b9bdf11 100644 --- a/stormpath-rails.gemspec +++ b/stormpath-rails.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_dependency 'stormpath-sdk', '>= 1.1.5' + #spec.add_dependency 'stormpath-sdk', '>= 1.1.5' spec.add_dependency 'virtus' spec.add_dependency 'rails', '>= 3.1' spec.add_dependency 'recursive-open-struct' From 508cbf1663505d451b60914f83a1760da1d0bc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Tue, 8 Nov 2016 09:44:57 +0100 Subject: [PATCH 3/9] Enable spec for verifying account and autologin --- spec/features/email_verification_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/features/email_verification_spec.rb b/spec/features/email_verification_spec.rb index c7595b5..1704813 100644 --- a/spec/features/email_verification_spec.rb +++ b/spec/features/email_verification_spec.rb @@ -84,8 +84,7 @@ allow(configuration.web.register).to receive(:auto_login).and_return(true) end - xit 'redirects to root and sets cookies' do - # TODO: important to fix + it 'redirects to root and sets cookies' do visit "verify?sptoken=#{sptoken}" expect(current_path).to eq('/') From 033ba5380e74db572efd1ec1f7dcd6f4097c61fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Tue, 8 Nov 2016 11:06:55 +0100 Subject: [PATCH 4/9] Fix email verification specs --- Gemfile | 1 - spec/features/email_verification_spec.rb | 11 ++--------- spec/support/stormpath_testing_helpers.rb | 4 ++++ stormpath-rails.gemspec | 2 +- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index d43f97e..f7c25da 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,6 @@ gemspec gem 'sqlite3', '~> 1.3' gem 'sass-rails' -gem 'stormpath-sdk', path: './../ruby' group :development do gem 'pry' diff --git a/spec/features/email_verification_spec.rb b/spec/features/email_verification_spec.rb index 1704813..53b4b74 100644 --- a/spec/features/email_verification_spec.rb +++ b/spec/features/email_verification_spec.rb @@ -1,30 +1,23 @@ require 'spec_helper' describe 'the email verification feature', type: :feature, vcr: true do - let(:application) do - Stormpath::Rails::Client.client.applications.create(name: 'rails integration app') - end - + let(:application) { test_application } let(:test_dir_with_verification) do Stormpath::Rails::Client.client.directories.create(name: 'rails test dir with verification') end - let(:account) { test_dir_with_verification.accounts.create(account_attrs) } - let(:account_attrs) { FactoryGirl.attributes_for(:account) } - let(:sptoken) { account.email_verification_token.token } before do enable_email_verification_for(test_dir_with_verification) - map_account_store(application, test_dir_with_verification, 0, true, true) + map_account_store(application, test_dir_with_verification, 2, false, false) enable_email_verification Rails.application.reload_routes! account end after do - application.delete test_dir_with_verification.delete account.delete end diff --git a/spec/support/stormpath_testing_helpers.rb b/spec/support/stormpath_testing_helpers.rb index 805d0dd..f143701 100644 --- a/spec/support/stormpath_testing_helpers.rb +++ b/spec/support/stormpath_testing_helpers.rb @@ -18,6 +18,10 @@ def delete_test_account def delete_account(email) Stormpath::Rails::Client.application.accounts.search(email: email).first.delete end + + def test_application + Stormpath::Rails::Client.client.applications.get(ENV['STORMPATH_APPLICATION_URL']) + end end end end diff --git a/stormpath-rails.gemspec b/stormpath-rails.gemspec index b9bdf11..88c8a94 100644 --- a/stormpath-rails.gemspec +++ b/stormpath-rails.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - #spec.add_dependency 'stormpath-sdk', '>= 1.1.5' + spec.add_dependency 'stormpath-sdk', '>= 1.3.1' spec.add_dependency 'virtus' spec.add_dependency 'rails', '>= 3.1' spec.add_dependency 'recursive-open-struct' From 8d77646d2933dca9c9534dae00ff0fd497a2a6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Tue, 8 Nov 2016 11:14:31 +0100 Subject: [PATCH 5/9] Set new application url in .travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3555950..19cebac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ after_success: - if [ "$CURRENT_HASH" = "$RELEASE_HASH" ]; then DEPLOY_DOCS=true; fi env: global: - - STORMPATH_APPLICATION_URL=https://api.stormpath.com/v1/applications/4BiS1Hc1LuBEbszdDrOwyz + - STORMPATH_APPLICATION_URL=https://api.stormpath.com/v1/applications/4xz3y2Hrid4aqp5YwbNTvk - secure: VMjIMI42vIPTMPTr0fnzSPiIjzuSAnT7iNWjhAXP9WsdaCmxwHp1vOlry4QuX8DzpKHv2MQubeUN/UA227Nk1xn+CVu9mujWOqvvjmL9m20wMJvwT4ctn7zG+FJK76id9TEyx0mCTlH4ZrRoDMGfM9yzhpsg8FtSebBDdHxePaM= - secure: IhR6H9qxmxCDNbLK0ebYuIXQRsGA/JhD6In4V/hnSMJ8lPi2kwRn6eKclNCHGNjcy6QF1V5vddKIfKOkFFZvIyP26reygTX1g5Mfa8SqTGKh3DAW4WP+T+yaE4z4UBDK1zZpbV0Zbkw/HC0xeD8UPnjRzERX1LVZp1qeEjhQrks= - secure: a5woUmOQPRW6FBQBaxKJATfggUD/BVTBfeRaS07u1SQOpMoGJZLY0m29PVx4fHwRD1E7ho31YIeH8wk1vMMrimIHSdE1B4pm4n8bUTi/gDFwavXq9KgTdH8f6Eli37nAXZum78m4NgL9+OlrKeJKCcROdzniZPvFaLrnOwBGzVs= From 3c8c86bfc56843123ce750bb9020e3ee3f6b7c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Thu, 10 Nov 2016 10:27:35 +0100 Subject: [PATCH 6/9] Finish refactoring base env names --- .travis.yml | 7 +-- Gemfile | 2 - README.md | 18 +++--- .../local_account_resolution.rb | 2 +- .../stormpath_account_resolution.rb | 2 +- docs/configuration.rst | 12 ++-- docs/quickstart.rst | 10 +-- lib/stormpath/rails.rb | 1 + lib/stormpath/rails/client.rb | 15 +++-- lib/stormpath/rails/env_names_warning.rb | 62 +++++++++++++++++++ spec/dummy/config/stormpath.yml | 2 +- spec/factories.rb | 2 +- spec/requests/email_verification/get_spec.rb | 12 ++-- spec/requests/email_verification/post_spec.rb | 10 +-- spec/requests/id_site_login/get_spec.rb | 4 +- spec/spec_helper.rb | 26 +------- spec/support/stormpath_testing_helpers.rb | 2 +- 17 files changed, 112 insertions(+), 77 deletions(-) create mode 100644 lib/stormpath/rails/env_names_warning.rb diff --git a/.travis.yml b/.travis.yml index 19cebac..72bd621 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,6 @@ after_success: - if [ "$CURRENT_HASH" = "$RELEASE_HASH" ]; then DEPLOY_DOCS=true; fi env: global: - - STORMPATH_APPLICATION_URL=https://api.stormpath.com/v1/applications/4xz3y2Hrid4aqp5YwbNTvk - - secure: VMjIMI42vIPTMPTr0fnzSPiIjzuSAnT7iNWjhAXP9WsdaCmxwHp1vOlry4QuX8DzpKHv2MQubeUN/UA227Nk1xn+CVu9mujWOqvvjmL9m20wMJvwT4ctn7zG+FJK76id9TEyx0mCTlH4ZrRoDMGfM9yzhpsg8FtSebBDdHxePaM= - - secure: IhR6H9qxmxCDNbLK0ebYuIXQRsGA/JhD6In4V/hnSMJ8lPi2kwRn6eKclNCHGNjcy6QF1V5vddKIfKOkFFZvIyP26reygTX1g5Mfa8SqTGKh3DAW4WP+T+yaE4z4UBDK1zZpbV0Zbkw/HC0xeD8UPnjRzERX1LVZp1qeEjhQrks= - - secure: a5woUmOQPRW6FBQBaxKJATfggUD/BVTBfeRaS07u1SQOpMoGJZLY0m29PVx4fHwRD1E7ho31YIeH8wk1vMMrimIHSdE1B4pm4n8bUTi/gDFwavXq9KgTdH8f6Eli37nAXZum78m4NgL9+OlrKeJKCcROdzniZPvFaLrnOwBGzVs= + - STORMPATH_APPLICATION_HREF=https://api.stormpath.com/v1/applications/4xz3y2Hrid4aqp5YwbNTvk + - secure: fuZU/DCNpezx1qrhpt/b+eeZRF83AiAwJnUNgA0D2fH4KGleN+BGfz5wEy6oOu5QKDWWAWepHDuwUjTP6QL504QId8+QZbycr52tFM0NXDSzpzhGkoaAEIALjuJ+DBto032yr8hoFjM+P5M1057p2jZKyr5CMkB66j4g3oWwA5k= + - secure: F0+m4wWKgqZLWpouKwn3BQB1/DXkbwmKEE6E+XFSEJB3rIsf8J4stP6OslqGmkdOzbx/4Dl/V3EXMmWk6R8hnQ5rmr60FGN6FBXUTA+s/9ZULl1I3+J/PsV5azFUwuP+87hbS9QQTVxbG7S3Gpmuy3JCSjySQ4vfXo8fZPz6Plk= diff --git a/Gemfile b/Gemfile index f7c25da..3f9e423 100644 --- a/Gemfile +++ b/Gemfile @@ -18,8 +18,6 @@ group :test do gem 'webmock' gem 'vcr', '3.0.1' gem 'ammeter', git: 'https://github.com/alexrothenberg/ammeter' - gem 'simplecov', require: false - gem 'coveralls', require: false gem 'json_matchers' gem 'match_json', '0.0.5' gem 'capybara' diff --git a/README.md b/README.md index afa28ec..96eead1 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ [![Build Status](https://travis-ci.org/stormpath/stormpath-rails.svg?branch=master)](https://travis-ci.org/stormpath/stormpath-rails) -[![Coverage Status](https://coveralls.io/repos/stormpath/stormpath-rails/badge.svg?branch=master&service=github)](https://coveralls.io/github/stormpath/stormpath-rails?branch=master) [![Code Climate](https://codeclimate.com/github/stormpath/stormpath-rails/badges/gpa.svg)](https://codeclimate.com/github/stormpath/stormpath-rails) # Stormpath Rails @@ -38,16 +37,16 @@ rails generate stormpath:install Create a Stormpath account if you haven't already, and be sure to set up the following environment variables: - - STORMPATH_API_KEY_ID - - STORMPATH_API_KEY_SECRET + - STORMPATH_CLIENT_APIKEY_ID + - STORMPATH_CLIENT_APIKEY_SECRET Environment variables should be set up in you .bashrc file (or .zshrc if you use myzsh). Example setup: ```sh -export STORMPATH_API_KEY_ID=6U4HZMHGVHN0U765BGW -export STORMPATH_API_KEY_SECRET=0e0TuVZKYiPiLTDLNnswEwpPpa5nPv +export STORMPATH_CLIENT_APIKEY_ID=6U4HZMHGVHN0U765BGW +export STORMPATH_CLIENT_APIKEY_SECRET=0e0TuVZKYiPiLTDLNnswEwpPpa5nPv ``` Alternatively you can use gems such as [Dotenv](https://github.com/bkeepers/dotenv) or [Figaro](https://github.com/laserlemon/figaro) to preload environment variables. @@ -91,7 +90,7 @@ You can use embedded ruby (ERB) in the configuration file: ```yaml stormpath: application: - href: <%= ENV['STORMPATH_APPLICATION_URL'] %> + href: <%= ENV['STORMPATH_APPLICATION_HREF'] %> ``` ## Usage @@ -298,12 +297,9 @@ If you wish to contribute to the gem, please follow these steps: 1. Create a Stormpath Application. 2. Export the following env variables: - - STORMPATH_API_KEY_ID - - STORMPATH_API_KEY_SECRET + - STORMPATH_CLIENT_APIKEY_ID + - STORMPATH_CLIENT_APIKEY_SECRET 3. Create a Directory and associate it to the app. Make it the default account and group store for the app. -4. Create a Directory With a Verification Workflow and associate it to the app. -5. Export the following env variable: - - STORMPATH_SDK_TEST_DIRECTORY_WITH_VERIFICATION_URL ### Specs diff --git a/app/services/stormpath/rails/account_from_access_token/local_account_resolution.rb b/app/services/stormpath/rails/account_from_access_token/local_account_resolution.rb index e90a502..6932022 100644 --- a/app/services/stormpath/rails/account_from_access_token/local_account_resolution.rb +++ b/app/services/stormpath/rails/account_from_access_token/local_account_resolution.rb @@ -22,7 +22,7 @@ def account_href def jwt_data begin - @jwt_data ||= JWT.decode(access_token, ENV['STORMPATH_API_KEY_SECRET']) + @jwt_data ||= JWT.decode(access_token, Stormpath::Rails::Client.client.data_store.api_key.secret) rescue JWT::ExpiredSignature raise Stormpath::Oauth::Error, :jwt_expired end diff --git a/app/services/stormpath/rails/account_from_access_token/stormpath_account_resolution.rb b/app/services/stormpath/rails/account_from_access_token/stormpath_account_resolution.rb index f7d4a3c..b720e39 100644 --- a/app/services/stormpath/rails/account_from_access_token/stormpath_account_resolution.rb +++ b/app/services/stormpath/rails/account_from_access_token/stormpath_account_resolution.rb @@ -19,7 +19,7 @@ def validate_jwt_is_access_token end def jwt_data - @jwt_data ||= JWT.decode(access_token, ENV['STORMPATH_API_KEY_SECRET']) + @jwt_data ||= JWT.decode(access_token, Stormpath::Rails::Client.client.data_store.api_key.secret) end end end diff --git a/docs/configuration.rst b/docs/configuration.rst index a09c0fd..c792c0e 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -23,9 +23,9 @@ by running the following commands in the shell: .. code-block:: bash - export STORMPATH_API_KEY_ID=YOUR_ID_HERE - export STORMPATH_API_KEY_SECRET=YOUR_SECRET_HERE - export STORMPATH_APPLICATION_URL=YOUR_APP_HREF + export STORMPATH_CLIENT_APIKEY_ID=YOUR_ID_HERE + export STORMPATH_CLIENT_APIKEY_SECRET=YOUR_SECRET_HERE + export STORMPATH_APPLICATION_HREF=YOUR_APP_HREF or by using any text editor and adding the environment variables to .bashrc (or .zshrc if you're using ohmyzsh) @@ -35,9 +35,9 @@ or by using any text editor and adding the environment variables to .bashrc (or .. code-block:: bash - set STORMPATH_API_KEY_ID=YOUR_ID_HERE - set STORMPATH_API_KEY_SECRET=YOUR_SECRET_HERE - set STORMPATH_APPLICATION_URL=YOUR_APP_HREF + set STORMPATH_CLIENT_APIKEY_ID=YOUR_ID_HERE + set STORMPATH_CLIENT_APIKEY_SECRET=YOUR_SECRET_HERE + set STORMPATH_APPLICATION_HREF=YOUR_APP_HREF The examples above show you the 3 mandatory settings you need to configure to make stormpath-rails work. These settings can be configured via environment diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 6c6c4b8..cb9bfdf 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -30,8 +30,8 @@ prompt you to download your key pair. Once you've downloaded your `apiKey.properties` file, save it and be sure to set up the following environment variables: - - STORMPATH_API_KEY_ID - - STORMPATH_API_KEY_SECRET + - STORMPATH_CLIENT_APIKEY_ID + - STORMPATH_CLIENT_APIKEY_SECRET Environment variables should be set up in you .bashrc file (or .zshrc if you use myzsh). @@ -39,8 +39,8 @@ Example setup: .. code-block:: sh - export STORMPATH_API_KEY_ID=6U4HZMHGVHN0U765BGW - export STORMPATH_API_KEY_SECRET=0e0TuVZKYiPiLTDLNnswEwpPpa5nPv + export STORMPATH_CLIENT_APIKEY_ID=6U4HZMHGVHN0U765BGW + export STORMPATH_CLIENT_APIKEY_SECRET=0e0TuVZKYiPiLTDLNnswEwpPpa5nPv Find Your Stormpath Application ------------------------------- @@ -73,7 +73,7 @@ Now that you have your application HREF, make sure to set up another environment .. code-block:: sh - export STORMPATH_APPLICATION_URL=https://api.stormpath.com/v1/applications/24kkU5XOz4tQlZ7sBtPUN6 + export STORMPATH_APPLICATION_HREF=https://api.stormpath.com/v1/applications/24kkU5XOz4tQlZ7sBtPUN6 You're ready to bundle Stormpath Rails gem into your project! diff --git a/lib/stormpath/rails.rb b/lib/stormpath/rails.rb index 54d4fc1..6652364 100644 --- a/lib/stormpath/rails.rb +++ b/lib/stormpath/rails.rb @@ -23,5 +23,6 @@ module Rails autoload :NoGithubAuthorizationError, 'stormpath/rails/errors/no_github_authorization_error' autoload :FacebookAuthCodeExchange, 'stormpath/rails/facebook_auth_code_exchange' autoload :GithubAuthCodeExchange, 'stormpath/rails/github_auth_code_exchange' + autoload :EnvNamesWarning, 'stormpath/rails/env_names_warning' end end diff --git a/lib/stormpath/rails/client.rb b/lib/stormpath/rails/client.rb index 42c332f..819f536 100644 --- a/lib/stormpath/rails/client.rb +++ b/lib/stormpath/rails/client.rb @@ -24,12 +24,15 @@ def self.application end def self.client - self.connection ||= Stormpath::Client.new( - api_key: { - id: ENV['STORMPATH_API_KEY_ID'], - secret: ENV['STORMPATH_API_KEY_SECRET'] - } - ) + self.connection ||= Stormpath::Client.new(api_key: locate_api_key) + end + + def self.locate_api_key + EnvNamesWarning.check_env_variable_names + { + id: ENV['STORMPATH_CLIENT_APIKEY_ID'] || ENV['STORMPATH_API_KEY_ID'], + secret: ENV['STORMPATH_CLIENT_APIKEY_SECRET'] || ENV['STORMPATH_API_KEY_SECRET'] + } end end end diff --git a/lib/stormpath/rails/env_names_warning.rb b/lib/stormpath/rails/env_names_warning.rb new file mode 100644 index 0000000..7813460 --- /dev/null +++ b/lib/stormpath/rails/env_names_warning.rb @@ -0,0 +1,62 @@ +module Stormpath + module Rails + module EnvNamesWarning + TEST_ENV_VARS = { + required: { + STORMPATH_CLIENT_APIKEY_ID: 'The id from your Stormpath API Key', + STORMPATH_CLIENT_APIKEY_SECRET: 'The secret from your Stormpath API Key', + STORMPATH_APPLICATION_HREF: 'The href to your application' + }, + deprecated: { + STORMPATH_API_KEY_ID: 'The id from your Stormpath API Key', + STORMPATH_API_KEY_SECRET: 'The secret from your Stormpath API Key', + STORMPATH_APPLICATION_URL: 'The url to your application' + } + }.freeze + + def self.test_missing_deprecated_env_vars + TEST_ENV_VARS[:deprecated].reject do |var, _| + ENV[var.to_s] + end + end + + def self.test_missing_required_env_vars + TEST_ENV_VARS[:required].reject do |var, _| + ENV[var.to_s] + end + end + + def self.env_vars_not_set? + !test_missing_deprecated_env_vars.empty? && !test_missing_required_env_vars.empty? + end + + def self.check_env_variable_names + unless Stormpath::Rails::EnvNamesWarning.test_missing_required_env_vars.empty? + warn_message = "\n\n" + 40.times { warn_message << '*' } + warn_message << 'STORMPATH RAILS' + 52.times { warn_message << '*' } + warn_message << "\n\n" + warn_message << TEST_ENV_VARS[:deprecated].map do |var, _| + "\t#{var} is deprecated since the new version of the gem." + end.join("\n") + warn_message << "\n\tPlease update your environment variables to use the new names:\n" + warn_message << "\n\t\texport STORMPATH_CLIENT_APIKEY_ID=your_api_key_id" + warn_message << "\n\t\texport STORMPATH_CLIENT_APIKEY_SECRET=your_api_key_secret" + warn_message << "\n\t\texport STORMPATH_APPLICATION_HREF=href_to_application\n\n" + 110.times { warn_message << '*' } + warn "#{warn_message}\n\n" unless Stormpath::Rails::EnvNamesWarning.env_vars_not_set? + end + + if Stormpath::Rails::EnvNamesWarning.env_vars_not_set? + set_up_message = "In order to use the stormpath-rails gem you need to set the following environment variables:\n\t" + set_up_message << Stormpath::Rails::EnvNamesWarning.test_missing_required_env_vars.map do |var, message| + "#{var} : #{message}" + end.join("\n\t") + set_up_message << "\nBe sure to configure these before trying to run your application.\n\n" + raise set_up_message + end + end + end + end +end diff --git a/spec/dummy/config/stormpath.yml b/spec/dummy/config/stormpath.yml index ae865da..f89d7b7 100644 --- a/spec/dummy/config/stormpath.yml +++ b/spec/dummy/config/stormpath.yml @@ -1,6 +1,6 @@ stormpath: application: - href: <%= ENV['STORMPATH_APPLICATION_URL'] %> + href: <%= ENV['STORMPATH_APPLICATION_HREF'] || ENV['STORMPATH_APPLICATION_URL'] %> name: null web: diff --git a/spec/factories.rb b/spec/factories.rb index 0064b6e..bd1c14d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -9,7 +9,7 @@ end factory :account_without_username, class: Stormpath::Resource::Account do - sequence(:email) { |n| "dev#{n}@example.com" } + sequence(:email) { |n| "dev#{n}@testmail.stormpath.com" } password 'Password1337' given_name { Faker::Name.first_name } surname { Faker::Name.last_name } diff --git a/spec/requests/email_verification/get_spec.rb b/spec/requests/email_verification/get_spec.rb index 2b22911..4d94562 100644 --- a/spec/requests/email_verification/get_spec.rb +++ b/spec/requests/email_verification/get_spec.rb @@ -2,20 +2,17 @@ describe 'Email Verification GET', type: :request, vcr: true do let(:response_body) { JSON.parse(response.body) } - + let(:application) { test_application } let(:test_dir_with_verification) do - Stormpath::Rails::Client.client.directories.get( - ENV.fetch('STORMPATH_SDK_TEST_DIRECTORY_WITH_VERIFICATION_URL') - ) + Stormpath::Rails::Client.client.directories.create(name: 'rails test dir with verification') end - let(:account) { test_dir_with_verification.accounts.create(account_attrs) } - let(:account_attrs) { FactoryGirl.attributes_for(:account) } - let(:sptoken) { account.email_verification_token.token } before do + enable_email_verification_for(test_dir_with_verification) + map_account_store(application, test_dir_with_verification, 2, false, false) account enable_email_verification Rails.application.reload_routes! @@ -23,6 +20,7 @@ after do account.delete + test_dir_with_verification.delete end context 'application/json' do diff --git a/spec/requests/email_verification/post_spec.rb b/spec/requests/email_verification/post_spec.rb index 98b62de..97898cb 100644 --- a/spec/requests/email_verification/post_spec.rb +++ b/spec/requests/email_verification/post_spec.rb @@ -1,17 +1,16 @@ require 'spec_helper' describe 'Email Verification POST', type: :request, vcr: true do + let(:application) { test_application } let(:test_dir_with_verification) do - Stormpath::Rails::Client.client.directories.get( - ENV.fetch('STORMPATH_SDK_TEST_DIRECTORY_WITH_VERIFICATION_URL') - ) + Stormpath::Rails::Client.client.directories.create(name: 'rails test dir with verification') end - let(:account) { test_dir_with_verification.accounts.create(account_attrs) } - let(:account_attrs) { FactoryGirl.attributes_for(:account) } before do + enable_email_verification_for(test_dir_with_verification) + map_account_store(application, test_dir_with_verification, 2, false, false) account enable_email_verification Rails.application.reload_routes! @@ -19,6 +18,7 @@ after do account.delete + test_dir_with_verification.delete end context 'application/json' do diff --git a/spec/requests/id_site_login/get_spec.rb b/spec/requests/id_site_login/get_spec.rb index 0b494fc..51063e9 100644 --- a/spec/requests/id_site_login/get_spec.rb +++ b/spec/requests/id_site_login/get_spec.rb @@ -9,8 +9,8 @@ let(:path) { '' } let(:tenant_name) { application.tenant.name } let(:tenant_domain) { "https://#{tenant_name}.id.stormpath.io" } - let(:api_key_secret) { ENV['STORMPATH_API_KEY_SECRET'] } - let(:aud) { ENV['STORMPATH_API_KEY_ID'] } + let(:api_key_secret) { Stormpath::Rails::Client.client.data_store.api_key.secret } + let(:aud) { Stormpath::Rails::Client.client.data_store.api_key.id } let(:account) { application.accounts.create(account_attrs) } let(:account_attrs) { FactoryGirl.attributes_for(:account) } let(:jwt_response) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d98d7b2..7a32b5f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,5 @@ -TEST_ENV_REQUIRED_VARS = [ - :STORMPATH_API_KEY_ID, - :STORMPATH_API_KEY_SECRET, - :STORMPATH_SDK_TEST_DIRECTORY_WITH_VERIFICATION_URL -].freeze - $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) ENV['RAILS_ENV'] ||= 'test' -require 'simplecov' -SimpleCov.start - -require 'coveralls' -Coveralls.wear! - require 'webmock/rspec' require 'vcr' require 'pry' @@ -47,24 +35,14 @@ RSpec.configure do |config| config.use_transactional_fixtures = true config.include FactoryGirl::Syntax::Methods - config.include Stormpath::Testing::Helpers, type: :request - config.include Stormpath::Testing::Helpers, type: :feature - config.include Stormpath::Testing::Helpers, type: :service + config.include Stormpath::Testing::Helpers config.include Rails.application.routes.url_helpers, type: :service config.include MatchJson::Matchers config.include Capybara::DSL, type: :feature config.include ConfigSpecHelpers config.include Stormpath::Social::Helpers - RSpec::Matchers.alias_matcher :match_json, :include_json - config.before(:suite) do - missing_env_vars = TEST_ENV_REQUIRED_VARS.reject { |var| ENV[var.to_s] } - if missing_env_vars.any? - raise "Missing the following ENV vars to run the specs: #{missing_env_vars.join(', ')}" - end - end - config.before(:each) do Timecop.freeze( VCR.current_cassette && VCR.current_cassette.originally_recorded_at || Time.zone.now @@ -79,4 +57,4 @@ Capybara::RackTest::Driver.new(app, headers: { 'HTTP_ACCEPT' => 'text/html' }) end -Rails.application.routes.default_url_options[:host]= 'localhost:3000' +Rails.application.routes.default_url_options[:host] = 'localhost:3000' diff --git a/spec/support/stormpath_testing_helpers.rb b/spec/support/stormpath_testing_helpers.rb index e7c72a6..8a7cf6f 100644 --- a/spec/support/stormpath_testing_helpers.rb +++ b/spec/support/stormpath_testing_helpers.rb @@ -20,7 +20,7 @@ def delete_account(email) end def test_application - Stormpath::Rails::Client.client.applications.get(ENV['STORMPATH_APPLICATION_URL']) + Stormpath::Rails::Client.client.applications.get(ENV['STORMPATH_APPLICATION_HREF'] || ENV['STORMPATH_APPLICATION_URL']) end def default_domain From a08827d319aef07c1ea0ef270191e28586c14ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Thu, 10 Nov 2016 10:49:47 +0100 Subject: [PATCH 7/9] Use factories for directory creation in specs --- spec/factories.rb | 5 +++++ spec/features/email_verification_spec.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/factories.rb b/spec/factories.rb index bd1c14d..9b774bd 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -18,4 +18,9 @@ factory :unverified_account, parent: :account do status 'UNVERIFIED' end + + factory :directory, class: Stormpath::Resource::Directory do + sequence(:name) { |n| "rails-#{n}-#{Faker::Lorem.word}-directory" } + description 'rails test directory' + end end diff --git a/spec/features/email_verification_spec.rb b/spec/features/email_verification_spec.rb index 53b4b74..b36b773 100644 --- a/spec/features/email_verification_spec.rb +++ b/spec/features/email_verification_spec.rb @@ -3,7 +3,7 @@ describe 'the email verification feature', type: :feature, vcr: true do let(:application) { test_application } let(:test_dir_with_verification) do - Stormpath::Rails::Client.client.directories.create(name: 'rails test dir with verification') + Stormpath::Rails::Client.client.directories.create(FactoryGirl.attributes_for(:directory)) end let(:account) { test_dir_with_verification.accounts.create(account_attrs) } let(:account_attrs) { FactoryGirl.attributes_for(:account) } From 4dcd0488a832891501893bde5482e599feefe04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Mon, 14 Nov 2016 10:31:42 +0100 Subject: [PATCH 8/9] Use attributes from FactoryGirl while creating directories with verification in email verification specs --- spec/requests/email_verification/get_spec.rb | 2 +- spec/requests/email_verification/post_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/requests/email_verification/get_spec.rb b/spec/requests/email_verification/get_spec.rb index 4d94562..b6503f2 100644 --- a/spec/requests/email_verification/get_spec.rb +++ b/spec/requests/email_verification/get_spec.rb @@ -4,7 +4,7 @@ let(:response_body) { JSON.parse(response.body) } let(:application) { test_application } let(:test_dir_with_verification) do - Stormpath::Rails::Client.client.directories.create(name: 'rails test dir with verification') + Stormpath::Rails::Client.client.directories.create(FactoryGirl.attributes_for(:directory)) end let(:account) { test_dir_with_verification.accounts.create(account_attrs) } let(:account_attrs) { FactoryGirl.attributes_for(:account) } diff --git a/spec/requests/email_verification/post_spec.rb b/spec/requests/email_verification/post_spec.rb index 97898cb..569e9bc 100644 --- a/spec/requests/email_verification/post_spec.rb +++ b/spec/requests/email_verification/post_spec.rb @@ -3,7 +3,7 @@ describe 'Email Verification POST', type: :request, vcr: true do let(:application) { test_application } let(:test_dir_with_verification) do - Stormpath::Rails::Client.client.directories.create(name: 'rails test dir with verification') + Stormpath::Rails::Client.client.directories.create(FactoryGirl.attributes_for(:directory)) end let(:account) { test_dir_with_verification.accounts.create(account_attrs) } let(:account_attrs) { FactoryGirl.attributes_for(:account) } From 200cc58805ab780fa343255b5095d24fda8ed129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C4=86ilimkovi=C4=87?= Date: Wed, 23 Nov 2016 15:24:49 +0100 Subject: [PATCH 9/9] Refactor client.rb and checking environment variable names --- lib/stormpath/rails.rb | 1 + lib/stormpath/rails/api_key.rb | 87 ++++++++++++++++++++++++++++++++++ lib/stormpath/rails/client.rb | 10 ++-- 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 lib/stormpath/rails/api_key.rb diff --git a/lib/stormpath/rails.rb b/lib/stormpath/rails.rb index 6652364..67041a5 100644 --- a/lib/stormpath/rails.rb +++ b/lib/stormpath/rails.rb @@ -15,6 +15,7 @@ module Rails autoload :Controller, 'stormpath/rails/controller' autoload :Version, 'stormpath/rails/version' autoload :Social, 'stormpath/rails/social' + autoload :ApiKey, 'stormpath/rails/api_key' autoload :ContentTypeNegotiator, 'stormpath/rails/content_type_negotiator' autoload :RoutingConstraint, 'stormpath/rails/routing_constraint' autoload :InvalidSptokenError, 'stormpath/rails/errors/invalid_sptoken_error' diff --git a/lib/stormpath/rails/api_key.rb b/lib/stormpath/rails/api_key.rb new file mode 100644 index 0000000..e4c3e18 --- /dev/null +++ b/lib/stormpath/rails/api_key.rb @@ -0,0 +1,87 @@ +module Stormpath + module Rails + class ApiKey + TEST_ENV_VARS = { + required: { + STORMPATH_CLIENT_APIKEY_ID: 'The id from your Stormpath API Key', + STORMPATH_CLIENT_APIKEY_SECRET: 'The secret from your Stormpath API Key', + STORMPATH_APPLICATION_HREF: 'The href to your application' + }, + deprecated: { + STORMPATH_API_KEY_ID: 'The id from your Stormpath API Key', + STORMPATH_API_KEY_SECRET: 'The secret from your Stormpath API Key', + STORMPATH_APPLICATION_URL: 'The url to your application' + } + }.freeze + + def credentials + check_env_variable_names + credentials_from_env_variables + end + + private + + def check_env_variable_names + unless test_missing_required_env_vars.empty? + show_deprecation_warning unless env_vars_not_set? + end + + raise set_up_message if env_vars_not_set? + end + + def credentials_from_env_variables + { + id: ENV['STORMPATH_CLIENT_APIKEY_ID'] || ENV['STORMPATH_API_KEY_ID'], + secret: ENV['STORMPATH_CLIENT_APIKEY_SECRET'] || ENV['STORMPATH_API_KEY_SECRET'] + } + end + + def test_missing_deprecated_env_vars + TEST_ENV_VARS[:deprecated].reject do |var, _| + ENV[var.to_s] + end + end + + def test_missing_required_env_vars + TEST_ENV_VARS[:required].reject do |var, _| + ENV[var.to_s] + end + end + + def env_vars_not_set? + !test_missing_deprecated_env_vars.empty? && !test_missing_required_env_vars.empty? + end + + def show_deprecation_warning + warn deprecation_warning + end + + def deprecation_warning + warn_message = "\n\n" + 40.times { warn_message << '*' } + warn_message << 'STORMPATH RAILS' + 52.times { warn_message << '*' } + warn_message << "\n\n" + warn_message << TEST_ENV_VARS[:deprecated].map do |var, _| + "\t#{var} is deprecated since the new version of the gem." + end.join("\n") + warn_message << "\n\tPlease update your environment variables to use the new names:\n" + warn_message << "\n\t\texport STORMPATH_CLIENT_APIKEY_ID=your_api_key_id" + warn_message << "\n\t\texport STORMPATH_CLIENT_APIKEY_SECRET=your_api_key_secret" + warn_message << "\n\t\texport STORMPATH_APPLICATION_HREF=href_to_application\n\n" + 110.times { warn_message << '*' } + warn_message << "\n\n" + warn_message + end + + def set_up_message + set_up_message = "In order to use the stormpath-rails gem you need to set the following environment variables:\n\t" + set_up_message << test_missing_required_env_vars.map do |var, message| + "#{var} : #{message}" + end.join("\n\t") + set_up_message << "\nBe sure to configure these before trying to run your application.\n\n" + set_up_message + end + end + end +end diff --git a/lib/stormpath/rails/client.rb b/lib/stormpath/rails/client.rb index 819f536..bbf569a 100644 --- a/lib/stormpath/rails/client.rb +++ b/lib/stormpath/rails/client.rb @@ -24,15 +24,11 @@ def self.application end def self.client - self.connection ||= Stormpath::Client.new(api_key: locate_api_key) + self.connection ||= Stormpath::Client.new(api_key: api_key.credentials) end - def self.locate_api_key - EnvNamesWarning.check_env_variable_names - { - id: ENV['STORMPATH_CLIENT_APIKEY_ID'] || ENV['STORMPATH_API_KEY_ID'], - secret: ENV['STORMPATH_CLIENT_APIKEY_SECRET'] || ENV['STORMPATH_API_KEY_SECRET'] - } + def self.api_key + Stormpath::Rails::ApiKey.new end end end