stormpath-rails 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -4,3 +4,5 @@ rvm:
4
4
  gemfile:
5
5
  - Gemfile
6
6
  script: bundle exec rake spec
7
+ services:
8
+ - mongodb
data/Guardfile ADDED
@@ -0,0 +1,23 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
@@ -11,24 +11,29 @@ module Stormpath
11
11
  STORMPATH_FIELDS = [ :email, :password, :username, :given_name, :middle_name, :surname ]
12
12
 
13
13
  included do
14
- self.partial_updates = false
14
+ #AR specific workaround
15
+ self.partial_updates = false if self.respond_to?(:partial_updates)
16
+
17
+ #Mongoid specific declaration
18
+ #TODO index?
19
+ field(:stormpath_url, type: String) if self.respond_to?(:field)
15
20
 
16
21
  attr_accessor *STORMPATH_FIELDS
17
22
  attr_accessible *STORMPATH_FIELDS
18
23
 
19
24
  after_initialize do |user|
20
- return unless user.stormpath_url
25
+ return true unless user.stormpath_url
21
26
  begin
22
27
  account = Client.find_account(user.stormpath_url)
23
28
  (STORMPATH_FIELDS - [:password]).each { |field| self.send("#{field}=", account.send("get_#{field}")) }
24
29
  rescue ResourceError => error
25
- #somehow mark as data not loaded
30
+ Logger.new(STDERR).warn "Error loading Stormpath account (#{error})"
26
31
  end
27
32
  end
28
33
 
29
34
  before_create do
30
35
  begin
31
- account = Client.create_account!(Hash[*STORMPATH_FIELDS.map { |f| { f => self.send(f) } }.map(&:to_a).flatten])
36
+ account = Stormpath::Rails::Client.create_account!(Hash[*STORMPATH_FIELDS.map { |f| { f => self.send(f) } }.map(&:to_a).flatten])
32
37
  rescue ResourceError => error
33
38
  self.errors[:base] << error.to_s
34
39
  return false
@@ -37,26 +42,24 @@ module Stormpath
37
42
  end
38
43
 
39
44
  before_update do
45
+ return true unless self.stormpath_url
40
46
  begin
41
- account = Client.update_account!(self.stormpath_url, Hash[*STORMPATH_FIELDS.map { |f| { f => self.send(f) } }.map(&:to_a).flatten])
47
+ Client.update_account!(self.stormpath_url, Hash[*STORMPATH_FIELDS.map { |f| { f => self.send(f) } }.map(&:to_a).flatten])
42
48
  rescue ResourceError => error
43
49
  self.errors[:base] << error.to_s
44
- return nil
50
+ return false
45
51
  end
46
52
  end
47
53
 
48
54
  after_destroy do
55
+ return true unless self.stormpath_url
49
56
  begin
50
57
  account = Client.delete_account!(self.stormpath_url)
51
58
  rescue ResourceError => error
52
- self.errors[:base] << error.to_s
53
- return false
59
+ Logger.new(STDERR).warn "Error destroying Stormpath account (#{error})"
54
60
  end
55
61
  end
56
62
  end
57
-
58
- module ClassMethods
59
- end
60
63
  end
61
64
  end
62
65
  end
@@ -6,7 +6,9 @@ include Stormpath::Authentication
6
6
  module Stormpath
7
7
  module Rails
8
8
  class Client
9
- cattr_accessor :connection
9
+ class << self
10
+ attr_accessor :connection
11
+ end
10
12
 
11
13
  def self.authenticate_account(login, password)
12
14
  application = self.ds.get_resource Config[:application], ::Application
@@ -14,6 +16,16 @@ module Stormpath
14
16
  auth_result.get_account
15
17
  end
16
18
 
19
+ def self.send_password_reset_email(login_or_email)
20
+ application = self.ds.get_resource Config[:application], ::Application
21
+ application.send_password_reset_email login_or_email
22
+ end
23
+
24
+ def self.verify_password_reset_token(token)
25
+ application = self.ds.get_resource Config[:application], ::Application
26
+ application.verify_password_reset_token token
27
+ end
28
+
17
29
  def self.create_account!(attributes)
18
30
  account = self.ds.instantiate ::Account
19
31
  attributes.each { |field, value| account.send("set_#{field}", value) }
@@ -0,0 +1,14 @@
1
+ module Stormpath
2
+ module Rails
3
+ class Config
4
+ class << self
5
+ attr_accessor :vars
6
+ end
7
+
8
+ def self.[](name)
9
+ self.vars ||= YAML.load(ERB.new(File.read("#{::Rails.root}/config/stormpath.yml")).result)
10
+ self.vars["common"].update(self.vars[::Rails.env])[name.to_s]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  module Stormpath
2
2
  module Rails
3
- VERSION = "0.3.4"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -1,16 +1 @@
1
- require "stormpath/rails/version"
2
- require "stormpath/rails/client"
3
- require "stormpath/rails/account"
4
-
5
- module Stormpath
6
- module Rails
7
- class Config
8
- cattr_accessor :vars
9
-
10
- def self.[](name)
11
- self.vars ||= YAML.load(ERB.new(File.read("#{::Rails.root}/config/stormpath.yml")).result)
12
- self.vars["common"].update(self.vars[::Rails.env])[name.to_s]
13
- end
14
- end
15
- end
16
- end
1
+ Dir[File.join(File.dirname(__FILE__), "stormpath/**/*.rb")].sort.each {|f| require f}
@@ -0,0 +1,12 @@
1
+ common:
2
+ href: stormpath_url
3
+ application: application_url
4
+
5
+ development:
6
+ root: development_root
7
+
8
+ test:
9
+ root: test_root
10
+
11
+ production:
12
+ root: production_root
@@ -1,16 +1,7 @@
1
1
  require "spec_helper"
2
-
3
- module Rails
4
- module Generators
5
- class Base
6
- end
7
- end
8
- end
9
-
10
2
  require "generators/stormpath/rails/install/install_generator"
11
3
 
12
4
  describe Stormpath::Rails::Generators::InstallGenerator do
13
-
14
5
  it "creates a configuration file" do
15
6
  subject.should_receive(:create_file).with("config/stormpath.yml", kind_of(String))
16
7
  subject.create_initializer_file
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+ require "active_record"
3
+ require "stormpath-rails"
4
+
5
+ describe "ActiveRecord record" do
6
+ class ArEntity < ActiveRecord::Base
7
+ include Stormpath::Rails::Account
8
+ end
9
+
10
+ subject { ArEntity.new }
11
+
12
+ before(:each) do
13
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
14
+ ActiveRecord::Migration.verbose = false
15
+ ActiveRecord::Migration.create_table :ar_entities do |t|
16
+ t.string :id
17
+ t.string :stormpath_url
18
+ end
19
+ end
20
+
21
+ it_should_behave_like "stormpath account"
22
+
23
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+ require 'mongoid'
3
+ require "stormpath-rails"
4
+
5
+ describe "Mongoid document" do
6
+ class MongoidEntity
7
+ include Mongoid::Document
8
+ include Stormpath::Rails::Account
9
+ end
10
+
11
+ subject { MongoidEntity.new }
12
+
13
+ before(:each) do
14
+ Mongoid::Config.connect_to("stormpath_rails_test")
15
+ end
16
+
17
+ it_should_behave_like "stormpath account"
18
+
19
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,17 +1,5 @@
1
- # This file was generated by the `rspec --init` command. Conventionally, all
2
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
- # Require this file using `require "spec_helper"` to ensure that it is only
4
- # loaded once.
5
- #
6
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
- RSpec.configure do |config|
8
- config.treat_symbols_as_metadata_keys_with_true_values = true
9
- config.run_all_when_everything_filtered = true
10
- config.filter_run :focus
1
+ Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
11
2
 
12
- # Run specs in random order to surface order dependencies. If you find an
13
- # order dependency and want to debug it, you can fix the order by providing
14
- # the seed, which is printed after each run.
15
- # --seed 1234
3
+ RSpec.configure do |config|
16
4
  config.order = 'random'
17
5
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'stormpath/rails/config'
3
+
4
+ describe Stormpath::Rails::Config do
5
+ before(:each) do
6
+ Rails.stub!(:root).and_return(File.join File.dirname(__FILE__), "..", "..", "fixtures")
7
+ Rails.stub!(:env).and_return("test")
8
+ end
9
+
10
+ context "file IO" do
11
+ let(:path) { "#{Rails.root}/config/stormpath.yml" }
12
+
13
+ before(:each) do
14
+ described_class.vars = nil
15
+ end
16
+
17
+ it "should read yaml from rails app config/stormpath.yml" do
18
+ File.should_receive(:read).with(path).and_call_original
19
+ described_class[:href].should == 'stormpath_url'
20
+ end
21
+
22
+ it "should not re-red yaml once loaded" do
23
+ File.should_receive(:read).with(path).and_call_original
24
+ described_class[:href].should == 'stormpath_url'
25
+ described_class[:href].should == 'stormpath_url'
26
+ end
27
+ end
28
+
29
+ %w{test development production}.each do |environment|
30
+ it "should read root directory from #{environment} yaml section" do
31
+ Rails.stub!(:env).and_return(environment)
32
+ described_class[:root].should == "#{environment}_root"
33
+ end
34
+
35
+ it "should mix shared vars to #{environment} env" do
36
+ Rails.stub!(:env).and_return(environment)
37
+ described_class[:href].should == 'stormpath_url'
38
+ described_class[:application].should == 'application_url'
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,6 @@
1
+ module Rails
2
+ module Generators
3
+ class Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,120 @@
1
+ shared_examples "stormpath account" do
2
+ context "after initialize" do
3
+ let(:account) { mock("account", get_href: "account_href") }
4
+
5
+ let!(:logger) { Logger.new(STDERR) }
6
+
7
+ before(:each) do
8
+ subject.class::STORMPATH_FIELDS.each do |field_name|
9
+ account.stub!("get_#{field_name}").and_return(field_name.to_s)
10
+ end
11
+ Logger.stub!(:new).and_return(logger)
12
+ Stormpath::Rails::Client.stub!(:find_account).and_return(account)
13
+ end
14
+
15
+ it "should silently skip stormpath load if no stormpath_url set" do
16
+ Stormpath::Rails::Client.should_not_receive(:find_account)
17
+ expect { subject }.to_not raise_error
18
+ end
19
+
20
+ context "on find" do
21
+ before(:each) do
22
+ Stormpath::Rails::Client.stub!(:create_account!).and_return(account)
23
+ subject.save!
24
+ end
25
+
26
+ it "should find account at stormpath" do
27
+ Stormpath::Rails::Client.should_receive(:find_account).with(subject.stormpath_url)
28
+ subject.class.all.first.stormpath_url.should == subject.stormpath_url
29
+ end
30
+
31
+ it "should setup object with data from stormpath" do
32
+ Stormpath::Rails::Client.should_receive(:find_account).with(subject.stormpath_url).and_return(account)
33
+ found = subject.class.where(stormpath_url: 'account_href').first
34
+ (subject.class::STORMPATH_FIELDS - [:password]).each do |field_name|
35
+ found.send(field_name).should == account.send("get_#{field_name}")
36
+ end
37
+ end
38
+
39
+ it "should log warning if stormpath account update failed" do
40
+ Stormpath::Rails::Client.stub!(:find_account).and_raise(ResourceError.new(mock("error", get_message: "Find failed")))
41
+ logger.should_receive(:warn).with("Error loading Stormpath account (Find failed)")
42
+ found = subject.class.where(stormpath_url: 'account_href').first
43
+ subject.class::STORMPATH_FIELDS.each do |field_name|
44
+ found.send(field_name).should be_nil
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+
52
+ context "before create" do
53
+ it "should create account at stormpath and assign stormpath_url" do
54
+ Stormpath::Rails::Client.should_receive(:create_account!).and_return(mock("account", get_href: "account_href"))
55
+ subject.save!
56
+ subject.stormpath_url.should == "account_href"
57
+ end
58
+
59
+ it "should add error if stormpath account creation failed" do
60
+ Stormpath::Rails::Client.stub!(:create_account!).and_raise(ResourceError.new(mock("error", get_message: "Create failed")))
61
+ subject.save
62
+ subject.errors[:base].should == ["Create failed"]
63
+ end
64
+ end
65
+
66
+ context "before update" do
67
+ let(:account) { mock("account", get_href: "account_href") }
68
+
69
+ before(:each) do
70
+ Stormpath::Rails::Client.stub!(:create_account!).and_return(account)
71
+ subject.save!
72
+ end
73
+
74
+ it "should silently skip stormpath update if no stormpath_url set" do
75
+ subject.stormpath_url = nil
76
+ Stormpath::Rails::Client.should_not_receive(:update_account!)
77
+ expect { subject.save! }.to_not raise_error
78
+ end
79
+
80
+ it "should update account at stormpath" do
81
+ Stormpath::Rails::Client.should_receive(:update_account!).with(subject.stormpath_url, anything())
82
+ subject.save!
83
+ end
84
+
85
+ it "should add error if stormpath account update failed" do
86
+ Stormpath::Rails::Client.stub!(:update_account!).and_raise(ResourceError.new(mock("error", get_message: "Update failed")))
87
+ subject.save.should be_false
88
+ subject.errors[:base].should == ["Update failed"]
89
+ end
90
+ end
91
+
92
+ context "after destroy" do
93
+ let(:account) { mock("account", get_href: "account_href") }
94
+
95
+ let!(:logger) { Logger.new(STDERR) }
96
+
97
+ before(:each) do
98
+ Logger.stub!(:new).and_return(logger)
99
+ Stormpath::Rails::Client.stub!(:create_account!).and_return(account)
100
+ subject.save!
101
+ end
102
+
103
+ it "should silently skip stormpath delete if no stormpath_url set" do
104
+ subject.stormpath_url = nil
105
+ Stormpath::Rails::Client.should_not_receive(:delete_account!)
106
+ expect { subject.destroy }.to_not raise_error
107
+ end
108
+
109
+ it "should destroy account at stormpath" do
110
+ Stormpath::Rails::Client.should_receive(:delete_account!).with(subject.stormpath_url)
111
+ subject.destroy
112
+ end
113
+
114
+ it "should log warning if stormpath account update failed" do
115
+ Stormpath::Rails::Client.stub!(:delete_account!).and_raise(ResourceError.new(mock("error", get_message: "Delete failed")))
116
+ logger.should_receive(:warn).with("Error destroying Stormpath account (Delete failed)")
117
+ subject.destroy.should be_true
118
+ end
119
+ end
120
+ end
@@ -20,6 +20,10 @@ Gem::Specification.new do |gem|
20
20
 
21
21
  gem.add_development_dependency('rake', '~> 10.0.2')
22
22
  gem.add_development_dependency('rspec', '~> 2.12.0')
23
+ gem.add_development_dependency('activerecord')
24
+ gem.add_development_dependency('sqlite3')
25
+ gem.add_development_dependency('mongoid')
26
+ gem.add_development_dependency('railties')
23
27
  gem.add_development_dependency('guard-rspec', '~> 2.2.1')
24
28
  gem.add_development_dependency('rb-inotify', '~> 0.8.8')
25
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stormpath-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-14 00:00:00.000000000 Z
12
+ date: 2013-01-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -75,6 +75,70 @@ dependencies:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
77
  version: 2.12.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: mongoid
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: railties
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
78
142
  - !ruby/object:Gem::Dependency
79
143
  name: guard-rspec
80
144
  requirement: !ruby/object:Gem::Requirement
@@ -118,6 +182,7 @@ files:
118
182
  - .rspec
119
183
  - .travis.yml
120
184
  - Gemfile
185
+ - Guardfile
121
186
  - LICENSE.txt
122
187
  - README.md
123
188
  - Rakefile
@@ -125,9 +190,16 @@ files:
125
190
  - lib/stormpath-rails.rb
126
191
  - lib/stormpath/rails/account.rb
127
192
  - lib/stormpath/rails/client.rb
193
+ - lib/stormpath/rails/config.rb
128
194
  - lib/stormpath/rails/version.rb
129
- - spec/lib/generators/stormpath/rails/install/install_generator_spec.rb
195
+ - spec/fixtures/config/stormpath.yml
196
+ - spec/generators/stormpath/rails/install/install_generator_spec.rb
197
+ - spec/integration/active_record_spec.rb
198
+ - spec/integration/mongoid_spec.rb
130
199
  - spec/spec_helper.rb
200
+ - spec/stormpath/rails/config_spec.rb
201
+ - spec/support/rails.rb
202
+ - spec/support/stormpath_account_shared_examples.rb
131
203
  - stormpath-rails.gemspec
132
204
  homepage: http://stormpath.com
133
205
  licenses: []
@@ -154,5 +226,11 @@ signing_key:
154
226
  specification_version: 3
155
227
  summary: Stormpath SDK API Wrapper
156
228
  test_files:
157
- - spec/lib/generators/stormpath/rails/install/install_generator_spec.rb
229
+ - spec/fixtures/config/stormpath.yml
230
+ - spec/generators/stormpath/rails/install/install_generator_spec.rb
231
+ - spec/integration/active_record_spec.rb
232
+ - spec/integration/mongoid_spec.rb
158
233
  - spec/spec_helper.rb
234
+ - spec/stormpath/rails/config_spec.rb
235
+ - spec/support/rails.rb
236
+ - spec/support/stormpath_account_shared_examples.rb