openstax_salesforce 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ FactoryGirl.define do
2
+ factory :user, class: 'OpenStax::Salesforce::User' do
3
+ uid { SecureRandom.hex(10) }
4
+ name { Faker::Name.name }
5
+ oauth_token { SecureRandom.hex }
6
+ refresh_token { SecureRandom.hex }
7
+ instance_url { Faker::Internet.url }
8
+
9
+ transient do
10
+ do_not_destroy_others false
11
+ end
12
+
13
+ after(:build) do |user, evaluator|
14
+ user.define_singleton_method(:ensure_only_one_record) {} if evaluator.do_not_destroy_others
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe OpenStax::Salesforce::ApplicationController, type: :controller do
4
+
5
+ controller do
6
+ def index
7
+ render :text => "Yo"
8
+ end
9
+ end
10
+
11
+ it 'has forbidden status by default' do
12
+ get :index
13
+ expect(response).to have_http_status(:forbidden)
14
+ end
15
+
16
+ it 'can have its admin check overridden' do
17
+ mock_admin_user
18
+ get :index
19
+ expect(response).to have_http_status(:success)
20
+ end
21
+
22
+ end
@@ -0,0 +1,86 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe OpenStax::Salesforce::SettingsController, type: :controller do
4
+
5
+ context 'as admin' do
6
+ before(:each) { mock_admin_user }
7
+
8
+ context '#callback' do
9
+ context 'when there is not yet a SF user' do
10
+ it 'adds a user' do
11
+ allow_any_instance_of(described_class)
12
+ .to receive(:env)
13
+ .and_return(omniauth_env_hash)
14
+
15
+ expect{post :callback}.to change {OpenStax::Salesforce::User.count}.by(1)
16
+ end
17
+ end
18
+
19
+ context 'when there are other SF users' do
20
+ it 'adds a user and removes existing users' do
21
+ other_user_1 = FactoryGirl.create(:user, do_not_destroy_others: true)
22
+ other_user_2 = FactoryGirl.create(:user, do_not_destroy_others: true)
23
+
24
+ allow_any_instance_of(described_class)
25
+ .to receive(:env)
26
+ .and_return(omniauth_env_hash)
27
+
28
+ expect{post :callback}.to change {OpenStax::Salesforce::User.count}.by(-1)
29
+
30
+ expect(OpenStax::Salesforce::User.all.map(&:id)).not_to contain_exactly(other_user_1.id, other_user_2.id)
31
+ end
32
+ end
33
+
34
+ context 'when a user with the same uid already exists' do
35
+ it 'reuses that user' do
36
+ a_user_1 = FactoryGirl.create(:user, uid: 'someuid')
37
+
38
+ allow_any_instance_of(described_class)
39
+ .to receive(:env)
40
+ .and_return(omniauth_env_hash)
41
+
42
+ post :callback
43
+
44
+ expect(a_user_1.reload.name).to eq "Bobby Thomas"
45
+ end
46
+ end
47
+ end
48
+
49
+ context '#destroy_user' do
50
+ # https://content.pivotal.io/blog/writing-rails-engine-rspec-controller-tests
51
+ # not needed elsewhere since we do other things to make them top-level routes
52
+ routes { OpenStax::Salesforce::Engine.routes }
53
+
54
+ it 'destroys users' do
55
+ FactoryGirl.create(:user)
56
+ expect{delete :destroy_user}.to change {OpenStax::Salesforce::User.count}.by(-1)
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ context 'as non-admin' do
63
+ routes { OpenStax::Salesforce::Engine.routes }
64
+
65
+ it 'has forbidden status' do
66
+ get :show
67
+ expect(response).to have_http_status(:forbidden)
68
+ end
69
+ end
70
+
71
+ def omniauth_env_hash
72
+ {
73
+ "omniauth.auth" => Hashie::Mash.new({
74
+ uid: 'someuid',
75
+ info: {
76
+ name: 'Bobby Thomas',
77
+ },
78
+ credentials: {
79
+ token: 'oauth_token',
80
+ refresh_token: 'refresh_token',
81
+ instance_url: 'http://blah.com/'
82
+ }
83
+ })
84
+ }
85
+ end
86
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe OpenStax::Salesforce::Remote::Opportunity do
4
+
5
+ it 'returns a TermYear object' do
6
+ opportunity = described_class.new(term_year: '2016 - 17 Fall')
7
+ expect(opportunity.term_year_object).to be_a OpenStax::Salesforce::Remote::TermYear
8
+ end
9
+
10
+ end
@@ -0,0 +1,35 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe OpenStax::Salesforce::Remote::OsAncillary do
4
+
5
+ context 'when product is Concept Coach' do
6
+ subject { described_class.new(product: "Concept Coach") }
7
+
8
+ it { is_expected.to be_is_concept_coach }
9
+ it { is_expected.not_to be_is_tutor }
10
+ it { is_expected.to be_valid_product }
11
+ end
12
+
13
+ context 'when product is Tutor' do
14
+ subject { described_class.new(product: "Tutor") }
15
+
16
+ it { is_expected.not_to be_is_concept_coach }
17
+ it { is_expected.to be_is_tutor }
18
+ it { is_expected.to be_valid_product }
19
+ end
20
+
21
+ context 'when product is nil' do
22
+ subject { described_class.new }
23
+
24
+ it { is_expected.not_to be_is_concept_coach }
25
+ it { is_expected.not_to be_is_tutor }
26
+ it { is_expected.not_to be_valid_product }
27
+ end
28
+
29
+ it 'knows which account types are for college' do
30
+ described_class::COLLEGE_ACCOUNT_TYPES.each do |college_type|
31
+ expect(described_class.new(account_type: college_type)).to be_is_college
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,81 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe OpenStax::Salesforce::Remote::TermYear do
4
+
5
+ context "#from_string" do
6
+ it "works for Fall" do
7
+ term_year = described_class.from_string("2015 - 16 Fall")
8
+
9
+ expect(term_year.start_year).to eq 2015
10
+ expect(term_year.end_year).to eq 2016
11
+ expect(term_year).to be_fall
12
+ expect(term_year).not_to be_spring
13
+ end
14
+
15
+ it "works for Spring" do
16
+ term_year = described_class.from_string("2015 - 16 Spring")
17
+
18
+ expect(term_year.start_year).to eq 2015
19
+ expect(term_year.end_year).to eq 2016
20
+ expect(term_year).not_to be_fall
21
+ expect(term_year).to be_spring
22
+ end
23
+
24
+ it "works for nil" do
25
+ term_year = described_class.from_string(nil)
26
+
27
+ expect(term_year).to be_nil
28
+ end
29
+
30
+ it "freaks out for bad years" do
31
+ expect{
32
+ described_class.from_string("2015 - 17 Fall")
33
+ }.to raise_error(StandardError)
34
+ end
35
+
36
+ it "freaks out for bad formats" do
37
+ expect{
38
+ described_class.from_string("fix formula 2016")
39
+ }.to raise_error(described_class::ParseError)
40
+ end
41
+ end
42
+
43
+ context "#initialize" do
44
+ it "freaks out for bad terms" do
45
+ expect{
46
+ described_class.new(start_year: 2015, term: :blah)
47
+ }.to raise_error(StandardError)
48
+ end
49
+ end
50
+
51
+ context "#next" do
52
+ it "moves from fall to spring" do
53
+ expect(described_class.from_string("2015 - 16 Fall").next.to_s).to eq "2015 - 16 Spring"
54
+ end
55
+
56
+ it "moves from spring to fall" do
57
+ expect(described_class.from_string("2015 - 16 Spring").next.to_s).to eq "2016 - 17 Fall"
58
+ end
59
+ end
60
+
61
+ context "#guess_from_created_at" do
62
+ it "guess early part of year into spring" do
63
+ expect(
64
+ described_class.guess_from_created_at(Time.zone.local(2016,2)).to_s
65
+ ).to eq "2015 - 16 Spring"
66
+ end
67
+
68
+ it "guesses summerish into fall" do
69
+ expect(
70
+ described_class.guess_from_created_at(Time.zone.local(2016,5)).to_s
71
+ ).to eq "2016 - 17 Fall"
72
+ end
73
+
74
+ it "guesses late fall into next spring" do
75
+ expect(
76
+ described_class.guess_from_created_at(Time.zone.local(2016,12)).to_s
77
+ ).to eq "2016 - 17 Spring"
78
+ end
79
+ end
80
+
81
+ end
data/spec/rails_helper.rb CHANGED
@@ -7,6 +7,7 @@ require 'factory_girl_rails'
7
7
  require 'faker'
8
8
  require 'squeel'
9
9
  require 'byebug'
10
+ require 'database_cleaner'
10
11
 
11
12
  # Add additional requires below this line. Rails is not loaded until this point!
12
13
 
@@ -36,7 +37,7 @@ RSpec.configure do |config|
36
37
  # If you're not using ActiveRecord, or you'd prefer not to run each of your
37
38
  # examples within a transaction, remove the following line or assign false
38
39
  # instead of true.
39
- # config.use_transactional_fixtures = true
40
+ config.use_transactional_fixtures = false
40
41
 
41
42
  # RSpec Rails can automatically mix in different behaviours to your tests
42
43
  # based on their file location, for example enabling you to call `get` and
@@ -52,4 +53,48 @@ RSpec.configure do |config|
52
53
  # The different available types are documented in the features, such as in
53
54
  # https://relishapp.com/rspec/rspec-rails/docs
54
55
  config.infer_spec_type_from_file_location!
56
+
57
+ # Use DatabaseCleaner instead of rspec transaction rollbacks
58
+ # http://tomdallimore.com/blog/taking-the-test-trash-out-with-databasecleaner-and-rspec/
59
+
60
+ config.prepend_before(:suite) do
61
+ DatabaseCleaner.clean_with(:truncation)
62
+ end
63
+
64
+ config.prepend_before(:all) do
65
+ DatabaseCleaner.start
66
+ end
67
+
68
+ config.prepend_before(:all, js: true) do
69
+ DatabaseCleaner.strategy = :truncation
70
+ end
71
+
72
+ config.prepend_before(:all, truncation: true) do
73
+ DatabaseCleaner.strategy = :truncation
74
+ end
75
+
76
+ config.prepend_before(:all) do
77
+ DatabaseCleaner.strategy = :transaction
78
+ end
79
+
80
+ config.prepend_before(:each) do
81
+ DatabaseCleaner.start
82
+ end
83
+
84
+ # https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example says:
85
+ # "It's also recommended to use append_after to ensure DatabaseCleaner.clean
86
+ # runs after the after-test cleanup capybara/rspec installs."
87
+ config.append_after(:each) do
88
+ DatabaseCleaner.clean
89
+ end
90
+
91
+ config.append_after(:all) do
92
+ DatabaseCleaner.clean
93
+ end
94
+ end
95
+
96
+ def mock_admin_user
97
+ allow(OpenStax::Salesforce.configuration).to receive(:authenticate_admin_proc) {
98
+ ->(controller) { return true }
99
+ }
55
100
  end
@@ -0,0 +1,9 @@
1
+ require "rails_helper"
2
+
3
+ RSpec.describe 'Engine routes', type: :routing do
4
+ describe "/auth/salesforce/callback" do
5
+ it "routes to #callback" do
6
+ expect(get '/auth/salesforce/callback').to route_to('openstax/salesforce/settings#callback')
7
+ end
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openstax_salesforce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Slavinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-22 00:00:00.000000000 Z
11
+ date: 2017-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: database_cleaner
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  description: Interface gem for accessing OpenStax's Salesforce instance
154
168
  email:
155
169
  - jps@kindlinglabs.com
@@ -164,7 +178,6 @@ files:
164
178
  - app/controllers/openstax/salesforce/settings_controller.rb
165
179
  - app/helpers/openstax/salesforce/application_helper.rb
166
180
  - app/models/openstax/salesforce/user.rb
167
- - app/models/user.rb
168
181
  - app/views/openstax/salesforce/settings/show.html.erb
169
182
  - config/initializers/omniauth.rb
170
183
  - config/initializers/openstax_salesforce.rb
@@ -174,9 +187,14 @@ files:
174
187
  - lib/openstax/salesforce/client.rb
175
188
  - lib/openstax/salesforce/engine.rb
176
189
  - lib/openstax/salesforce/remote/book.rb
190
+ - lib/openstax/salesforce/remote/class_size.rb
177
191
  - lib/openstax/salesforce/remote/contact.rb
192
+ - lib/openstax/salesforce/remote/individual_adoption.rb
178
193
  - lib/openstax/salesforce/remote/lead.rb
194
+ - lib/openstax/salesforce/remote/opportunity.rb
195
+ - lib/openstax/salesforce/remote/os_ancillary.rb
179
196
  - lib/openstax/salesforce/remote/school.rb
197
+ - lib/openstax/salesforce/remote/term_year.rb
180
198
  - lib/openstax/salesforce/spec_helpers.rb
181
199
  - lib/openstax/salesforce/spec_helpers/salesforce_proxy.rb
182
200
  - lib/openstax/salesforce/user_missing.rb
@@ -221,8 +239,15 @@ files:
221
239
  - spec/dummy/public/422.html
222
240
  - spec/dummy/public/500.html
223
241
  - spec/dummy/public/favicon.ico
242
+ - spec/factories/user.rb
243
+ - spec/openstax/salesforce/controllers/application_controller_spec.rb
244
+ - spec/openstax/salesforce/controllers/settings_controller_spec.rb
245
+ - spec/openstax/salesforce/remote/opportunity_spec.rb
246
+ - spec/openstax/salesforce/remote/os_ancillary_spec.rb
247
+ - spec/openstax/salesforce/remote/term_year_spec.rb
224
248
  - spec/openstax/salesforce/spec_helpers_spec.rb
225
249
  - spec/rails_helper.rb
250
+ - spec/routing_spec.rb
226
251
  - spec/spec_helper.rb
227
252
  homepage: https://github.com/openstax/openstax_salesforce
228
253
  licenses:
@@ -287,6 +312,13 @@ test_files:
287
312
  - spec/dummy/public/favicon.ico
288
313
  - spec/dummy/Rakefile
289
314
  - spec/dummy/README.md
315
+ - spec/factories/user.rb
316
+ - spec/openstax/salesforce/controllers/application_controller_spec.rb
317
+ - spec/openstax/salesforce/controllers/settings_controller_spec.rb
318
+ - spec/openstax/salesforce/remote/opportunity_spec.rb
319
+ - spec/openstax/salesforce/remote/os_ancillary_spec.rb
320
+ - spec/openstax/salesforce/remote/term_year_spec.rb
290
321
  - spec/openstax/salesforce/spec_helpers_spec.rb
291
322
  - spec/rails_helper.rb
323
+ - spec/routing_spec.rb
292
324
  - spec/spec_helper.rb
data/app/models/user.rb DELETED
@@ -1,25 +0,0 @@
1
- module OpenStax::Salesforce
2
- class User < ActiveRecord::Base
3
- validates :uid, presence: true
4
- validates :oauth_token, presence: true
5
- validates :refresh_token, presence: true
6
- validates :instance_url, presence: true
7
-
8
- after_save :ensure_only_one_record
9
-
10
- def self.save_from_omniauth!(auth)
11
- where(auth.slice(:uid).permit!).first_or_initialize.tap do |user|
12
- user.uid = auth.uid
13
- user.name = auth.info.name
14
- user.oauth_token = auth.credentials.token
15
- user.refresh_token = auth.credentials.refresh_token
16
- user.instance_url = auth.credentials.instance_url
17
- user.save!
18
- end
19
- end
20
-
21
- def ensure_only_one_record
22
- self.class.where{id != self.id}.destroy_all
23
- end
24
- end
25
- end