Skip to content

Commit 9c2d9a5

Browse files
author
Scotty Hagan
authored
Merge pull request tractionguest#25 from tractionguest/unscope-people
WORKAROUND: ignore default scopes on user model
2 parents a51b496 + f74a16f commit 9c2d9a5

File tree

10 files changed

+111
-80
lines changed

10 files changed

+111
-80
lines changed

Gemfile.lock

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,130 @@
11
PATH
22
remote: .
33
specs:
4-
scim_rails (0.3.0)
4+
scim_rails (0.3.1)
55
jwt (>= 1.5, < 3.0)
66
rails (~> 5.0)
77

88
GEM
99
remote: https://rubygems.org/
1010
specs:
11-
actioncable (5.2.4.3)
12-
actionpack (= 5.2.4.3)
11+
actioncable (5.2.5)
12+
actionpack (= 5.2.5)
1313
nio4r (~> 2.0)
1414
websocket-driver (>= 0.6.1)
15-
actionmailer (5.2.4.3)
16-
actionpack (= 5.2.4.3)
17-
actionview (= 5.2.4.3)
18-
activejob (= 5.2.4.3)
15+
actionmailer (5.2.5)
16+
actionpack (= 5.2.5)
17+
actionview (= 5.2.5)
18+
activejob (= 5.2.5)
1919
mail (~> 2.5, >= 2.5.4)
2020
rails-dom-testing (~> 2.0)
21-
actionpack (5.2.4.3)
22-
actionview (= 5.2.4.3)
23-
activesupport (= 5.2.4.3)
21+
actionpack (5.2.5)
22+
actionview (= 5.2.5)
23+
activesupport (= 5.2.5)
2424
rack (~> 2.0, >= 2.0.8)
2525
rack-test (>= 0.6.3)
2626
rails-dom-testing (~> 2.0)
2727
rails-html-sanitizer (~> 1.0, >= 1.0.2)
28-
actionview (5.2.4.3)
29-
activesupport (= 5.2.4.3)
28+
actionview (5.2.5)
29+
activesupport (= 5.2.5)
3030
builder (~> 3.1)
3131
erubi (~> 1.4)
3232
rails-dom-testing (~> 2.0)
3333
rails-html-sanitizer (~> 1.0, >= 1.0.3)
34-
activejob (5.2.4.3)
35-
activesupport (= 5.2.4.3)
34+
activejob (5.2.5)
35+
activesupport (= 5.2.5)
3636
globalid (>= 0.3.6)
37-
activemodel (5.2.4.3)
38-
activesupport (= 5.2.4.3)
39-
activerecord (5.2.4.3)
40-
activemodel (= 5.2.4.3)
41-
activesupport (= 5.2.4.3)
37+
activemodel (5.2.5)
38+
activesupport (= 5.2.5)
39+
activerecord (5.2.5)
40+
activemodel (= 5.2.5)
41+
activesupport (= 5.2.5)
4242
arel (>= 9.0)
43-
activestorage (5.2.4.3)
44-
actionpack (= 5.2.4.3)
45-
activerecord (= 5.2.4.3)
46-
marcel (~> 0.3.1)
47-
activesupport (5.2.4.3)
43+
activestorage (5.2.5)
44+
actionpack (= 5.2.5)
45+
activerecord (= 5.2.5)
46+
marcel (~> 1.0.0)
47+
activesupport (5.2.5)
4848
concurrent-ruby (~> 1.0, >= 1.0.2)
4949
i18n (>= 0.7, < 2)
5050
minitest (~> 5.1)
5151
tzinfo (~> 1.1)
5252
arel (9.0.0)
53-
awesome_print (1.8.0)
53+
awesome_print (1.9.2)
5454
builder (3.2.4)
5555
byebug (11.1.3)
5656
coderay (1.1.3)
57-
concurrent-ruby (1.1.6)
57+
concurrent-ruby (1.1.8)
5858
crass (1.0.6)
59-
diff-lcs (1.4.1)
60-
docile (1.3.2)
61-
erubi (1.9.0)
62-
factory_bot (6.0.2)
59+
diff-lcs (1.4.4)
60+
docile (1.3.5)
61+
erubi (1.10.0)
62+
factory_bot (6.1.0)
6363
activesupport (>= 5.0.0)
64-
factory_bot_rails (6.0.0)
65-
factory_bot (~> 6.0.0)
64+
factory_bot_rails (6.1.0)
65+
factory_bot (~> 6.1.0)
6666
railties (>= 5.0.0)
67-
faker (2.12.0)
67+
faker (2.17.0)
6868
i18n (>= 1.6, < 2)
6969
globalid (0.4.2)
7070
activesupport (>= 4.2.0)
71-
i18n (1.8.4)
71+
i18n (1.8.10)
7272
concurrent-ruby (~> 1.0)
7373
interception (0.5)
74-
json (2.4.0)
75-
jwt (2.2.1)
76-
loofah (2.6.0)
74+
json (2.5.1)
75+
jwt (2.2.2)
76+
loofah (2.9.0)
7777
crass (~> 1.0.2)
7878
nokogiri (>= 1.5.9)
7979
mail (2.7.1)
8080
mini_mime (>= 0.1.1)
81-
marcel (0.3.3)
82-
mimemagic (~> 0.3.2)
81+
marcel (1.0.1)
8382
method_source (1.0.0)
84-
mimemagic (0.3.5)
85-
mini_mime (1.0.2)
86-
mini_portile2 (2.4.0)
87-
minitest (5.14.1)
88-
nio4r (2.5.2)
89-
nokogiri (1.10.10)
90-
mini_portile2 (~> 2.4.0)
91-
pry (0.13.1)
83+
mini_mime (1.1.0)
84+
mini_portile2 (2.5.0)
85+
minitest (5.14.4)
86+
nio4r (2.5.7)
87+
nokogiri (1.11.2)
88+
mini_portile2 (~> 2.5.0)
89+
racc (~> 1.4)
90+
pry (0.14.0)
9291
coderay (~> 1.1)
9392
method_source (~> 1.0)
94-
pry-rescue (1.5.1)
93+
pry-rescue (1.5.2)
9594
interception (>= 0.5)
9695
pry (>= 0.12.0)
96+
racc (1.5.2)
9797
rack (2.2.3)
9898
rack-test (1.1.0)
9999
rack (>= 1.0, < 3)
100-
rails (5.2.4.3)
101-
actioncable (= 5.2.4.3)
102-
actionmailer (= 5.2.4.3)
103-
actionpack (= 5.2.4.3)
104-
actionview (= 5.2.4.3)
105-
activejob (= 5.2.4.3)
106-
activemodel (= 5.2.4.3)
107-
activerecord (= 5.2.4.3)
108-
activestorage (= 5.2.4.3)
109-
activesupport (= 5.2.4.3)
100+
rails (5.2.5)
101+
actioncable (= 5.2.5)
102+
actionmailer (= 5.2.5)
103+
actionpack (= 5.2.5)
104+
actionview (= 5.2.5)
105+
activejob (= 5.2.5)
106+
activemodel (= 5.2.5)
107+
activerecord (= 5.2.5)
108+
activestorage (= 5.2.5)
109+
activesupport (= 5.2.5)
110110
bundler (>= 1.3.0)
111-
railties (= 5.2.4.3)
111+
railties (= 5.2.5)
112112
sprockets-rails (>= 2.0.0)
113113
rails-dom-testing (2.0.3)
114114
activesupport (>= 4.2.0)
115115
nokogiri (>= 1.6)
116116
rails-html-sanitizer (1.3.0)
117117
loofah (~> 2.3)
118-
railties (5.2.4.3)
119-
actionpack (= 5.2.4.3)
120-
activesupport (= 5.2.4.3)
118+
railties (5.2.5)
119+
actionpack (= 5.2.5)
120+
activesupport (= 5.2.5)
121121
method_source
122122
rake (>= 0.8.7)
123123
thor (>= 0.19.0, < 2.0)
124-
rake (13.0.1)
125-
rspec-core (3.9.2)
124+
rake (13.0.3)
125+
rspec-core (3.9.3)
126126
rspec-support (~> 3.9.3)
127-
rspec-expectations (3.9.2)
127+
rspec-expectations (3.9.4)
128128
diff-lcs (>= 1.2.0, < 2.0)
129129
rspec-support (~> 3.9.0)
130130
rspec-mocks (3.9.1)
@@ -138,7 +138,7 @@ GEM
138138
rspec-expectations (~> 3.9.0)
139139
rspec-mocks (~> 3.9.0)
140140
rspec-support (~> 3.9.0)
141-
rspec-support (3.9.3)
141+
rspec-support (3.9.4)
142142
simplecov (0.17.1)
143143
docile (~> 1.1)
144144
json (>= 1.8, < 3)
@@ -148,14 +148,14 @@ GEM
148148
sprockets (4.0.2)
149149
concurrent-ruby (~> 1.0)
150150
rack (> 1, < 3)
151-
sprockets-rails (3.2.1)
151+
sprockets-rails (3.2.2)
152152
actionpack (>= 4.0)
153153
activesupport (>= 4.0)
154154
sprockets (>= 3.0.0)
155155
sqlite3 (1.3.13)
156-
thor (1.0.1)
156+
thor (1.1.0)
157157
thread_safe (0.3.6)
158-
tzinfo (1.2.7)
158+
tzinfo (1.2.9)
159159
thread_safe (~> 0.1)
160160
websocket-driver (0.7.3)
161161
websocket-extensions (>= 0.1.0)

app/controllers/scim_rails/scim_groups_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def create
3333
if ScimRails.config.scim_group_prevent_update_on_create
3434
group = @company.public_send(ScimRails.config.scim_groups_scope).create!(group_attributes)
3535
else
36-
username_key = ScimRails.config.queryable_group_attributes[:userName]
37-
find_by_username = {}
38-
find_by_username[username_key] = group_attributes[username_key]
36+
display_name_key = ScimRails.config.queryable_group_attributes[:displayName]
37+
find_by_display_name = {}
38+
find_by_display_name[display_name_key] = group_attributes[display_name_key]
3939
group = @company
4040
.public_send(ScimRails.config.scim_groups_scope)
41-
.find_or_create_by(find_by_username)
41+
.find_or_create_by(find_by_display_name)
4242
group.update!(group_attributes)
4343
end
4444

app/controllers/scim_rails/scim_users_controller.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ def create
4040
else
4141
username_key = ScimRails.config.queryable_user_attributes[:userName]
4242

43-
find_by_username = Hash.new
43+
find_by_params = Hash.new
4444

45-
find_by_username[username_key] = user_params[username_key]
46-
user = @company
47-
.public_send(ScimRails.config.scim_users_scope)
48-
.find_or_create_by(find_by_username)
45+
find_by_params[username_key] = user_params[username_key]
46+
find_by_params[ScimRails.config.basic_auth_model.to_s.underscore] = @company
47+
48+
user = ScimRails.config.scim_users_model
49+
.unscoped # Specific to our use case and should be changed back after we remove the default_scope from our "User" model
50+
.find_or_initialize_by(find_by_params)
4951
user.update!(user_params)
5052
end
5153
update_status(user) unless params[:active].nil?

lib/scim_rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ScimRails
2-
VERSION = '0.3.0'
2+
VERSION = '0.3.1'
33
end

spec/controllers/scim_rails/scim_users_controller_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,24 @@
347347
expect(company.users.count).to eq 1
348348
end
349349

350+
it 'returns 201 if the user has a default_scope attribute' do
351+
create(:user, email: "new@example.com", company: company, scoped_attribute: false)
352+
post :create, params: {
353+
name: {
354+
givenName: "Not New",
355+
familyName: "User"
356+
},
357+
emails: [
358+
{
359+
value: "new@example.com"
360+
}
361+
]
362+
}
363+
expect(response.status).to eq 201
364+
expect(User.unscoped.where(company: company).count).to eq 1
365+
expect(User.unscoped.where(company: company).first.first_name).to eq "Not New"
366+
end
367+
350368
it "creates and archives inactive user" do
351369
post :create, params: {
352370
id: 1,

spec/dummy/app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class User < ApplicationRecord
44
has_many :groups_users
55
has_many :groups, through: :groups_users
66

7+
default_scope { where(scoped_attribute: true) }
8+
79
validates \
810
:first_name,
911
:last_name,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddScopedAttribute < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :users, :scoped_attribute, :boolean, default: true
4+
end
5+
end

spec/dummy/db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_04_08_164220) do
13+
ActiveRecord::Schema.define(version: 2021_03_31_202326) do
1414

1515
create_table "companies", force: :cascade do |t|
1616
t.string "name", null: false
@@ -50,6 +50,7 @@
5050
t.datetime "archived_at"
5151
t.datetime "created_at", null: false
5252
t.datetime "updated_at", null: false
53+
t.boolean "scoped_attribute", default: true
5354
end
5455

5556
end

spec/factories/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
sequence(:email) { |n| "#{n}@example.com" }
88

99
test_attribute { Faker::Games::Pokemon.name }
10+
scoped_attribute { true }
1011
alternate_email { Faker::Internet.email }
1112
end
1213
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
config.use_transactional_fixtures = true
2222
config.infer_base_class_for_anonymous_controllers = false
2323
config.order = "random"
24+
config.filter_run :focus
25+
config.run_all_when_everything_filtered = true
2426
end

0 commit comments

Comments
 (0)